Example #1
0
    def test_filter_druid_datasource(self):
        CLUSTER_NAME = 'new_druid'
        cluster = self.get_or_create(
            DruidCluster,
            {'cluster_name': CLUSTER_NAME},
            db.session)
        db.session.merge(cluster)

        gamma_ds = self.get_or_create(
            DruidDatasource, {'datasource_name': 'datasource_for_gamma'},
            db.session)
        gamma_ds.cluster = cluster
        db.session.merge(gamma_ds)

        no_gamma_ds = self.get_or_create(
            DruidDatasource, {'datasource_name': 'datasource_not_for_gamma'},
            db.session)
        no_gamma_ds.cluster = cluster
        db.session.merge(no_gamma_ds)
        db.session.commit()

        security.merge_perm(sm, 'datasource_access', gamma_ds.perm)
        security.merge_perm(sm, 'datasource_access', no_gamma_ds.perm)

        perm = sm.find_permission_view_menu(
            'datasource_access', gamma_ds.get_perm())
        sm.add_permission_role(sm.find_role('Gamma'), perm)
        sm.get_session.commit()

        self.login(username='******')
        url = '/druiddatasourcemodelview/list/'
        resp = self.get_resp(url)
        self.assertIn('datasource_for_gamma', resp)
        self.assertNotIn('datasource_not_for_gamma', resp)
Example #2
0
    def test_sql_json_has_access(self):
        main_db = self.get_main_database(db.session)
        utils.merge_perm(sm, 'database_access', main_db.perm)
        db.session.commit()
        main_db_permission_view = (db.session.query(
            ab_models.PermissionView).join(ab_models.ViewMenu).filter(
                ab_models.ViewMenu.name == '[main].(id:1)').first())
        astronaut = sm.add_role("Astronaut")
        sm.add_permission_role(astronaut, main_db_permission_view)
        # Astronaut role is Gamma + main db permissions
        for gamma_perm in sm.find_role('Gamma').permissions:
            sm.add_permission_role(astronaut, gamma_perm)

        gagarin = appbuilder.sm.find_user('gagarin')
        if not gagarin:
            appbuilder.sm.add_user('gagarin',
                                   'Iurii',
                                   'Gagarin',
                                   '*****@*****.**',
                                   appbuilder.sm.find_role('Astronaut'),
                                   password='******')
        data = self.run_sql('SELECT * FROM ab_user', 'gagarin', "3")
        db.session.query(models.Query).delete()
        db.session.commit()
        self.assertLess(0, len(data['data']))
Example #3
0
    def test_clean_requests_after_db_grant(self):
        session = db.session

        # Case 3. Two access requests from gamma and gamma2
        # Gamma gets database access, gamma2 access request granted
        # Check if request by gamma has been deleted

        gamma_user = sm.find_user(username='******')
        access_request1 = create_access_request(
            session, 'table', 'long_lat', TEST_ROLE_1, 'gamma')
        create_access_request(
            session, 'table', 'long_lat', TEST_ROLE_2, 'gamma2')
        ds_1_id = access_request1.datasource_id
        # gamma gets granted database access
        database = session.query(models.Database).first()

        security.merge_perm(
            sm, 'database_access', database.perm)
        ds_perm_view = sm.find_permission_view_menu(
            'database_access', database.perm)
        sm.add_permission_role(
            sm.find_role(DB_ACCESS_ROLE), ds_perm_view)
        gamma_user.roles.append(sm.find_role(DB_ACCESS_ROLE))
        session.commit()
        access_requests = self.get_access_requests('gamma', 'table', ds_1_id)
        self.assertTrue(access_requests)
        # gamma2 request gets fulfilled
        self.client.get(EXTEND_ROLE_REQUEST.format(
            'table', ds_1_id, 'gamma2', TEST_ROLE_2))
        access_requests = self.get_access_requests('gamma', 'table', ds_1_id)

        self.assertFalse(access_requests)
        gamma_user = sm.find_user(username='******')
        gamma_user.roles.remove(sm.find_role(DB_ACCESS_ROLE))
        session.commit()
Example #4
0
    def test_clean_requests_after_db_grant(self):
        session = db.session

        # Case 3. Two access requests from gamma and gamma2
        # Gamma gets database access, gamma2 access request granted
        # Check if request by gamma has been deleted

        gamma_user = sm.find_user(username='******')
        access_request1 = create_access_request(session, 'table', 'long_lat',
                                                TEST_ROLE_1, 'gamma')
        access_request2 = create_access_request(session, 'table', 'long_lat',
                                                TEST_ROLE_2, 'gamma2')
        ds_1_id = access_request1.datasource_id
        # gamma gets granted database access
        database = session.query(models.Database).first()

        security.merge_perm(sm, 'database_access', database.perm)
        ds_perm_view = sm.find_permission_view_menu('database_access',
                                                    database.perm)
        sm.add_permission_role(sm.find_role(DB_ACCESS_ROLE), ds_perm_view)
        gamma_user.roles.append(sm.find_role(DB_ACCESS_ROLE))
        session.commit()
        access_requests = self.get_access_requests('gamma', 'table', ds_1_id)
        self.assertTrue(access_requests)
        # gamma2 request gets fulfilled
        self.client.get(
            EXTEND_ROLE_REQUEST.format('table', ds_1_id, 'gamma2',
                                       TEST_ROLE_2))
        access_requests = self.get_access_requests('gamma', 'table', ds_1_id)

        self.assertFalse(access_requests)
        gamma_user = sm.find_user(username='******')
        gamma_user.roles.remove(sm.find_role(DB_ACCESS_ROLE))
        session.commit()
Example #5
0
def create_access_request(session, ds_type, ds_name, role_name, user_name):
    ds_class = SourceRegistry.sources[ds_type]
    # TODO: generalize datasource names
    if ds_type == 'table':
        ds = session.query(ds_class).filter(
            ds_class.table_name == ds_name).first()
    else:
        ds = session.query(ds_class).filter(
            ds_class.datasource_name == ds_name).first()
    ds_perm_view = sm.find_permission_view_menu('datasource_access', ds.perm)
    sm.add_permission_role(sm.find_role(role_name), ds_perm_view)
    access_request = models.DatasourceAccessRequest(
        datasource_id=ds.id,
        datasource_type=ds_type,
        created_by_fk=sm.find_user(username=user_name).id,
    )
    session.add(access_request)
    session.commit()
    return access_request
Example #6
0
 def create_access_request(ds_type, ds_name, role_name):
     ds_class = SourceRegistry.sources[ds_type]
     # TODO: generalize datasource names
     if ds_type == 'table':
         ds = session.query(ds_class).filter(
             ds_class.table_name == ds_name).first()
     else:
         ds = session.query(ds_class).filter(
             ds_class.datasource_name == ds_name).first()
     ds_perm_view = sm.find_permission_view_menu(
         'datasource_access', ds.perm)
     sm.add_permission_role(sm.find_role(role_name), ds_perm_view)
     access_request = models.DatasourceAccessRequest(
         datasource_id=ds.id,
         datasource_type=ds_type,
         created_by_fk=sm.find_user(username='******').id,
     )
     session.add(access_request)
     session.commit()
     return access_request
Example #7
0
    def test_clean_requests_after_schema_grant(self):
        session = db.session

        # Case 4. Two access requests from gamma and gamma2
        # Gamma gets schema access, gamma2 access request granted
        # Check if request by gamma has been deleted

        gamma_user = sm.find_user(username='******')
        access_request1 = create_access_request(session, 'table',
                                                'wb_health_population',
                                                TEST_ROLE_1, 'gamma')
        access_request2 = create_access_request(session, 'table',
                                                'wb_health_population',
                                                TEST_ROLE_2, 'gamma2')
        ds_1_id = access_request1.datasource_id
        ds = session.query(models.SqlaTable).filter_by(
            table_name='wb_health_population').first()

        ds.schema = 'temp_schema'
        security.merge_perm(sm, 'schema_access', ds.schema_perm)
        schema_perm_view = sm.find_permission_view_menu(
            'schema_access', ds.schema_perm)
        sm.add_permission_role(sm.find_role(SCHEMA_ACCESS_ROLE),
                               schema_perm_view)
        gamma_user.roles.append(sm.find_role(SCHEMA_ACCESS_ROLE))
        session.commit()
        # gamma2 request gets fulfilled
        self.client.get(
            EXTEND_ROLE_REQUEST.format('table', ds_1_id, 'gamma2',
                                       TEST_ROLE_2))
        access_requests = self.get_access_requests('gamma', 'table', ds_1_id)
        self.assertFalse(access_requests)
        gamma_user = sm.find_user(username='******')
        gamma_user.roles.remove(sm.find_role(SCHEMA_ACCESS_ROLE))

        ds = session.query(models.SqlaTable).filter_by(
            table_name='wb_health_population').first()
        ds.schema = None

        session.commit()
Example #8
0
    def test_sql_json_has_access(self):
        main_db = self.get_main_database(db.session)
        sm.add_permission_view_menu('database_access', main_db.perm)
        db.session.commit()
        main_db_permission_view = (
            db.session.query(ab_models.PermissionView)
            .join(ab_models.ViewMenu)
            .filter(ab_models.ViewMenu.name == '[main].(id:1)')
            .first()
        )
        astronaut = sm.add_role("Astronaut")
        sm.add_permission_role(astronaut, main_db_permission_view)
        # Astronaut role is Gamma + sqllab +  main db permissions
        for perm in sm.find_role('Gamma').permissions:
            sm.add_permission_role(astronaut, perm)
        for perm in sm.find_role('sql_lab').permissions:
            sm.add_permission_role(astronaut, perm)

        gagarin = appbuilder.sm.find_user('gagarin')
        if not gagarin:
            appbuilder.sm.add_user(
                'gagarin', 'Iurii', 'Gagarin', '*****@*****.**',
                astronaut,
                password='******')
        data = self.run_sql('SELECT * FROM ab_user', "3", user_name='gagarin')
        db.session.query(models.Query).delete()
        db.session.commit()
        self.assertLess(0, len(data['data']))
Example #9
0
    def test_clean_requests_after_schema_grant(self):
        session = db.session

        # Case 4. Two access requests from gamma and gamma2
        # Gamma gets schema access, gamma2 access request granted
        # Check if request by gamma has been deleted

        gamma_user = sm.find_user(username='******')
        access_request1 = create_access_request(
            session, 'table', 'wb_health_population', TEST_ROLE_1, 'gamma')
        access_request2 = create_access_request(
            session, 'table', 'wb_health_population', TEST_ROLE_2, 'gamma2')
        ds_1_id = access_request1.datasource_id
        ds = session.query(SqlaTable).filter_by(
            table_name='wb_health_population').first()


        ds.schema = 'temp_schema'
        security.merge_perm(
            sm, 'schema_access', ds.schema_perm)
        schema_perm_view = sm.find_permission_view_menu(
            'schema_access', ds.schema_perm)
        sm.add_permission_role(
            sm.find_role(SCHEMA_ACCESS_ROLE) , schema_perm_view)
        gamma_user.roles.append(sm.find_role(SCHEMA_ACCESS_ROLE))
        session.commit()
        # gamma2 request gets fulfilled
        self.client.get(EXTEND_ROLE_REQUEST.format(
            'table', ds_1_id, 'gamma2', TEST_ROLE_2))
        access_requests = self.get_access_requests('gamma', 'table', ds_1_id)
        self.assertFalse(access_requests)
        gamma_user = sm.find_user(username='******')
        gamma_user.roles.remove(sm.find_role(SCHEMA_ACCESS_ROLE))

        ds = session.query(SqlaTable).filter_by(
            table_name='wb_health_population').first()
        ds.schema = None

        session.commit()
Example #10
0
    def test_request_access(self):
        session = db.session
        self.logout()
        self.login(username='******')
        gamma_user = sm.find_user(username='******')
        sm.add_role('dummy_role')
        gamma_user.roles.append(sm.find_role('dummy_role'))
        session.commit()

        ACCESS_REQUEST = ('/superset/request_access?'
                          'datasource_type={}&'
                          'datasource_id={}&'
                          'action={}&')
        ROLE_EXTEND_LINK = (
            '<a href="/superset/approve?datasource_type={}&datasource_id={}&'
            'created_by={}&role_to_extend={}">Extend {} Role</a>')
        ROLE_GRANT_LINK = (
            '<a href="/superset/approve?datasource_type={}&datasource_id={}&'
            'created_by={}&role_to_grant={}">Grant {} Role</a>')

        # Request table access, there are no roles have this table.

        table1 = session.query(models.SqlaTable).filter_by(
            table_name='random_time_series').first()
        table_1_id = table1.id

        # request access to the table
        resp = self.get_resp(ACCESS_REQUEST.format('table', table_1_id, 'go'))
        assert "Access was requested" in resp
        access_request1 = self.get_access_requests('gamma', 'table',
                                                   table_1_id)
        assert access_request1 is not None

        # Request access, roles exist that contains the table.
        # add table to the existing roles
        table3 = session.query(
            models.SqlaTable).filter_by(table_name='energy_usage').first()
        table_3_id = table3.id
        table3_perm = table3.perm

        sm.add_role('energy_usage_role')
        alpha_role = sm.find_role('Alpha')
        sm.add_permission_role(
            alpha_role,
            sm.find_permission_view_menu('datasource_access', table3_perm))
        sm.add_permission_role(
            sm.find_role("energy_usage_role"),
            sm.find_permission_view_menu('datasource_access', table3_perm))
        session.commit()

        self.get_resp(ACCESS_REQUEST.format('table', table_3_id, 'go'))
        access_request3 = self.get_access_requests('gamma', 'table',
                                                   table_3_id)
        approve_link_3 = ROLE_GRANT_LINK.format('table', table_3_id, 'gamma',
                                                'energy_usage_role',
                                                'energy_usage_role')
        self.assertEqual(access_request3.roles_with_datasource,
                         '<ul><li>{}</li></ul>'.format(approve_link_3))

        # Request druid access, there are no roles have this table.
        druid_ds_4 = session.query(models.DruidDatasource).filter_by(
            datasource_name='druid_ds_1').first()
        druid_ds_4_id = druid_ds_4.id

        # request access to the table
        self.get_resp(ACCESS_REQUEST.format('druid', druid_ds_4_id, 'go'))
        access_request4 = self.get_access_requests('gamma', 'druid',
                                                   druid_ds_4_id)

        self.assertEqual(access_request4.roles_with_datasource,
                         '<ul></ul>'.format(access_request4.id))

        # Case 5. Roles exist that contains the druid datasource.
        # add druid ds to the existing roles
        druid_ds_5 = session.query(models.DruidDatasource).filter_by(
            datasource_name='druid_ds_2').first()
        druid_ds_5_id = druid_ds_5.id
        druid_ds_5_perm = druid_ds_5.perm

        druid_ds_2_role = sm.add_role('druid_ds_2_role')
        admin_role = sm.find_role('Admin')
        sm.add_permission_role(
            admin_role,
            sm.find_permission_view_menu('datasource_access', druid_ds_5_perm))
        sm.add_permission_role(
            druid_ds_2_role,
            sm.find_permission_view_menu('datasource_access', druid_ds_5_perm))
        session.commit()

        self.get_resp(ACCESS_REQUEST.format('druid', druid_ds_5_id, 'go'))
        access_request5 = self.get_access_requests('gamma', 'druid',
                                                   druid_ds_5_id)
        approve_link_5 = ROLE_GRANT_LINK.format('druid', druid_ds_5_id,
                                                'gamma', 'druid_ds_2_role',
                                                'druid_ds_2_role')
        self.assertEqual(access_request5.roles_with_datasource,
                         '<ul><li>{}</li></ul>'.format(approve_link_5))

        # cleanup
        gamma_user = sm.find_user(username='******')
        gamma_user.roles.remove(sm.find_role('dummy_role'))
        session.commit()
Example #11
0
    def test_request_access(self):
        session = db.session
        self.logout()
        self.login(username='******')
        gamma_user = sm.find_user(username='******')
        sm.add_role('dummy_role')
        gamma_user.roles.append(sm.find_role('dummy_role'))
        session.commit()

        ACCESS_REQUEST = (
            '/superset/request_access?'
            'datasource_type={}&'
            'datasource_id={}&'
            'action={}&')
        ROLE_EXTEND_LINK = (
            '<a href="/superset/approve?datasource_type={}&datasource_id={}&'
            'created_by={}&role_to_extend={}">Extend {} Role</a>')
        ROLE_GRANT_LINK = (
            '<a href="/superset/approve?datasource_type={}&datasource_id={}&'
            'created_by={}&role_to_grant={}">Grant {} Role</a>')

        # Request table access, there are no roles have this table.

        table1 = session.query(models.SqlaTable).filter_by(
            table_name='random_time_series').first()
        table_1_id = table1.id

        # request access to the table
        resp = self.get_resp(
            ACCESS_REQUEST.format('table', table_1_id, 'go'))
        assert "Access was requested" in resp
        access_request1 = self.get_access_requests('gamma', 'table', table_1_id)
        assert access_request1 is not None

        # Request access, roles exist that contains the table.
        # add table to the existing roles
        table3 = session.query(models.SqlaTable).filter_by(
            table_name='energy_usage').first()
        table_3_id = table3.id
        table3_perm = table3.perm

        sm.add_role('energy_usage_role')
        alpha_role = sm.find_role('Alpha')
        sm.add_permission_role(
            alpha_role,
            sm.find_permission_view_menu('datasource_access', table3_perm))
        sm.add_permission_role(
            sm.find_role("energy_usage_role"),
            sm.find_permission_view_menu('datasource_access', table3_perm))
        session.commit()

        self.get_resp(
            ACCESS_REQUEST.format('table', table_3_id, 'go'))
        access_request3 = self.get_access_requests('gamma', 'table', table_3_id)
        approve_link_3 = ROLE_GRANT_LINK.format(
            'table', table_3_id, 'gamma', 'energy_usage_role',
            'energy_usage_role')
        self.assertEqual(access_request3.roles_with_datasource,
                         '<ul><li>{}</li></ul>'.format(approve_link_3))

        # Request druid access, there are no roles have this table.
        druid_ds_4 = session.query(models.DruidDatasource).filter_by(
            datasource_name='druid_ds_1').first()
        druid_ds_4_id = druid_ds_4.id

        # request access to the table
        self.get_resp(ACCESS_REQUEST.format('druid', druid_ds_4_id, 'go'))
        access_request4 = self.get_access_requests('gamma', 'druid', druid_ds_4_id)

        self.assertEqual(
            access_request4.roles_with_datasource,
            '<ul></ul>'.format(access_request4.id))

        # Case 5. Roles exist that contains the druid datasource.
        # add druid ds to the existing roles
        druid_ds_5 = session.query(models.DruidDatasource).filter_by(
            datasource_name='druid_ds_2').first()
        druid_ds_5_id = druid_ds_5.id
        druid_ds_5_perm = druid_ds_5.perm

        druid_ds_2_role = sm.add_role('druid_ds_2_role')
        admin_role = sm.find_role('Admin')
        sm.add_permission_role(
            admin_role,
            sm.find_permission_view_menu('datasource_access', druid_ds_5_perm))
        sm.add_permission_role(
            druid_ds_2_role,
            sm.find_permission_view_menu('datasource_access', druid_ds_5_perm))
        session.commit()

        self.get_resp(ACCESS_REQUEST.format('druid', druid_ds_5_id, 'go'))
        access_request5 = self.get_access_requests(
            'gamma', 'druid', druid_ds_5_id)
        approve_link_5 = ROLE_GRANT_LINK.format(
            'druid', druid_ds_5_id, 'gamma', 'druid_ds_2_role',
            'druid_ds_2_role')
        self.assertEqual(access_request5.roles_with_datasource,
                         '<ul><li>{}</li></ul>'.format(approve_link_5))

        # cleanup
        gamma_user = sm.find_user(username='******')
        gamma_user.roles.remove(sm.find_role('dummy_role'))
        session.commit()
Example #12
0
    def __init__(self, *args, **kwargs):
        if (self.requires_examples and not os.environ.get('SOLO_TEST')
                and not os.environ.get('examples_loaded')):
            logging.info("Loading examples")
            cli.load_examples(load_test_data=True)
            logging.info("Done loading examples")
            sync_role_definitions()
            os.environ['examples_loaded'] = '1'
        else:
            sync_role_definitions()
        super(SupersetTestCase, self).__init__(*args, **kwargs)
        self.client = app.test_client()
        self.maxDiff = None

        gamma_sqllab = sm.add_role("gamma_sqllab")
        for perm in sm.find_role('Gamma').permissions:
            sm.add_permission_role(gamma_sqllab, perm)
        for perm in sm.find_role('sql_lab').permissions:
            sm.add_permission_role(gamma_sqllab, perm)

        admin = appbuilder.sm.find_user('admin')
        if not admin:
            appbuilder.sm.add_user('admin',
                                   'admin',
                                   ' user',
                                   '*****@*****.**',
                                   appbuilder.sm.find_role('Admin'),
                                   password='******')

        gamma = appbuilder.sm.find_user('gamma')
        if not gamma:
            appbuilder.sm.add_user('gamma',
                                   'gamma',
                                   'user',
                                   '*****@*****.**',
                                   appbuilder.sm.find_role('Gamma'),
                                   password='******')

        gamma_sqllab = appbuilder.sm.find_user('gamma_sqllab')
        if not gamma_sqllab:
            gamma_sqllab = appbuilder.sm.add_user(
                'gamma_sqllab',
                'gamma_sqllab',
                'user',
                '*****@*****.**',
                appbuilder.sm.find_role('gamma_sqllab'),
                password='******')

        alpha = appbuilder.sm.find_user('alpha')
        if not alpha:
            appbuilder.sm.add_user('alpha',
                                   'alpha',
                                   'user',
                                   '*****@*****.**',
                                   appbuilder.sm.find_role('Alpha'),
                                   password='******')

        # create druid cluster and druid datasources
        session = db.session
        cluster = session.query(
            models.DruidCluster).filter_by(cluster_name="druid_test").first()
        if not cluster:
            cluster = models.DruidCluster(cluster_name="druid_test")
            session.add(cluster)
            session.commit()

            druid_datasource1 = models.DruidDatasource(
                datasource_name='druid_ds_1', cluster_name='druid_test')
            session.add(druid_datasource1)
            druid_datasource2 = models.DruidDatasource(
                datasource_name='druid_ds_2', cluster_name='druid_test')
            session.add(druid_datasource2)
            session.commit()
Example #13
0
    def __init__(self, *args, **kwargs):
        if (
                self.requires_examples and
                not os.environ.get('SOLO_TEST') and
                not os.environ.get('examples_loaded')
            ):
            logging.info("Loading examples")
            cli.load_examples(load_test_data=True)
            logging.info("Done loading examples")
            sync_role_definitions()
            os.environ['examples_loaded'] = '1'
        else:
            sync_role_definitions()
        super(SupersetTestCase, self).__init__(*args, **kwargs)
        self.client = app.test_client()
        self.maxDiff = None

        gamma_sqllab = sm.add_role("gamma_sqllab")
        for perm in sm.find_role('Gamma').permissions:
            sm.add_permission_role(gamma_sqllab, perm)
        for perm in sm.find_role('sql_lab').permissions:
            sm.add_permission_role(gamma_sqllab, perm)

        admin = appbuilder.sm.find_user('admin')
        if not admin:
            appbuilder.sm.add_user(
                'admin', 'admin', ' user', '*****@*****.**',
                appbuilder.sm.find_role('Admin'),
                password='******')

        gamma = appbuilder.sm.find_user('gamma')
        if not gamma:
            appbuilder.sm.add_user(
                'gamma', 'gamma', 'user', '*****@*****.**',
                appbuilder.sm.find_role('Gamma'),
                password='******')

        gamma_sqllab = appbuilder.sm.find_user('gamma_sqllab')
        if not gamma_sqllab:
            gamma_sqllab = appbuilder.sm.add_user(
                'gamma_sqllab', 'gamma_sqllab', 'user', '*****@*****.**',
                appbuilder.sm.find_role('gamma_sqllab'),
                password='******')

        alpha = appbuilder.sm.find_user('alpha')
        if not alpha:
            appbuilder.sm.add_user(
                'alpha', 'alpha', 'user', '*****@*****.**',
                appbuilder.sm.find_role('Alpha'),
                password='******')

        # create druid cluster and druid datasources
        session = db.session
        cluster = session.query(models.DruidCluster).filter_by(
            cluster_name="druid_test").first()
        if not cluster:
            cluster = models.DruidCluster(cluster_name="druid_test")
            session.add(cluster)
            session.commit()

            druid_datasource1 = models.DruidDatasource(
                datasource_name='druid_ds_1',
                cluster_name='druid_test'
            )
            session.add(druid_datasource1)
            druid_datasource2 = models.DruidDatasource(
                datasource_name='druid_ds_2',
                cluster_name='druid_test'
            )
            session.add(druid_datasource2)
            session.commit()
Example #14
0
    def __init__(self, *args, **kwargs):
        if (self.requires_examples and not os.environ.get('SOLO_TEST')
                and not os.environ.get('examples_loaded')):
            logging.info('Loading examples')
            cli.load_examples(load_test_data=True)
            logging.info('Done loading examples')
            sync_role_definitions()
            os.environ['examples_loaded'] = '1'
        else:
            sync_role_definitions()
        super(SupersetTestCase, self).__init__(*args, **kwargs)
        self.client = app.test_client()
        self.maxDiff = None

        gamma_sqllab_role = sm.add_role('gamma_sqllab')
        for perm in sm.find_role('Gamma').permissions:
            sm.add_permission_role(gamma_sqllab_role, perm)
        db_perm = self.get_main_database(sm.get_session).perm
        security.merge_perm(sm, 'database_access', db_perm)
        db_pvm = sm.find_permission_view_menu(
            view_menu_name=db_perm, permission_name='database_access')
        gamma_sqllab_role.permissions.append(db_pvm)
        for perm in sm.find_role('sql_lab').permissions:
            sm.add_permission_role(gamma_sqllab_role, perm)

        admin = appbuilder.sm.find_user('admin')
        if not admin:
            appbuilder.sm.add_user('admin',
                                   'admin',
                                   ' user',
                                   '*****@*****.**',
                                   appbuilder.sm.find_role('Admin'),
                                   password='******')

        gamma = appbuilder.sm.find_user('gamma')
        if not gamma:
            appbuilder.sm.add_user('gamma',
                                   'gamma',
                                   'user',
                                   '*****@*****.**',
                                   appbuilder.sm.find_role('Gamma'),
                                   password='******')

        gamma2 = appbuilder.sm.find_user('gamma2')
        if not gamma2:
            appbuilder.sm.add_user('gamma2',
                                   'gamma2',
                                   'user',
                                   '*****@*****.**',
                                   appbuilder.sm.find_role('Gamma'),
                                   password='******')

        gamma_sqllab_user = appbuilder.sm.find_user('gamma_sqllab')
        if not gamma_sqllab_user:
            appbuilder.sm.add_user('gamma_sqllab',
                                   'gamma_sqllab',
                                   'user',
                                   '*****@*****.**',
                                   gamma_sqllab_role,
                                   password='******')

        alpha = appbuilder.sm.find_user('alpha')
        if not alpha:
            appbuilder.sm.add_user('alpha',
                                   'alpha',
                                   'user',
                                   '*****@*****.**',
                                   appbuilder.sm.find_role('Alpha'),
                                   password='******')
        sm.get_session.commit()
        # create druid cluster and druid datasources
        session = db.session
        cluster = (session.query(DruidCluster).filter_by(
            cluster_name='druid_test').first())
        if not cluster:
            cluster = DruidCluster(cluster_name='druid_test')
            session.add(cluster)
            session.commit()

            druid_datasource1 = DruidDatasource(
                datasource_name='druid_ds_1',
                cluster_name='druid_test',
            )
            session.add(druid_datasource1)
            druid_datasource2 = DruidDatasource(
                datasource_name='druid_ds_2',
                cluster_name='druid_test',
            )
            session.add(druid_datasource2)
            session.commit()
Example #15
0
def sync_role_definitions():
    """Inits the Superset application with security roles and such"""
    logging.info("Syncing role definition")

    # Creating default roles
    alpha = sm.add_role("Alpha")
    admin = sm.add_role("Admin")
    gamma = sm.add_role("Gamma")
    public = sm.add_role("Public")
    sql_lab = sm.add_role("sql_lab")
    granter = sm.add_role("granter")

    get_or_create_main_db()

    # Global perms
    merge_perm(sm, 'all_datasource_access', 'all_datasource_access')
    merge_perm(sm, 'all_database_access', 'all_database_access')

    perms = db.session.query(ab_models.PermissionView).all()
    perms = [p for p in perms if p.permission and p.view_menu]

    logging.info("Syncing admin perms")
    for p in perms:
        # admin has all_database_access and all_datasource_access
        if is_user_defined_permission(p):
            sm.del_permission_role(admin, p)
        else:
            sm.add_permission_role(admin, p)

    logging.info("Syncing alpha perms")
    for p in perms:
        # alpha has all_database_access and all_datasource_access
        if is_user_defined_permission(p):
            sm.del_permission_role(alpha, p)
        elif ((p.view_menu.name not in ADMIN_ONLY_VIEW_MENUES
               and p.permission.name not in ADMIN_ONLY_PERMISSIONS)
              or (p.permission.name, p.view_menu.name) in READ_ONLY_PRODUCT):
            sm.add_permission_role(alpha, p)
        else:
            sm.del_permission_role(alpha, p)

    logging.info("Syncing gamma perms and public if specified")
    PUBLIC_ROLE_LIKE_GAMMA = conf.get('PUBLIC_ROLE_LIKE_GAMMA', False)
    for p in perms:
        if ((p.view_menu.name not in ADMIN_ONLY_VIEW_MENUES
             and p.view_menu.name not in GAMMA_READ_ONLY_MODELVIEWS
             and p.permission.name not in ADMIN_ONLY_PERMISSIONS
             and p.permission.name not in ALPHA_ONLY_PERMISSIONS) or
            (p.permission.name, p.view_menu.name) in GAMMA_READ_ONLY_PRODUCT):
            sm.add_permission_role(gamma, p)
            if PUBLIC_ROLE_LIKE_GAMMA:
                sm.add_permission_role(public, p)
        else:
            sm.del_permission_role(gamma, p)
            sm.del_permission_role(public, p)

    logging.info("Syncing sql_lab perms")
    for p in perms:
        if (p.view_menu.name in {'SQL Lab'} or p.permission.name
                in {'can_sql_json', 'can_csv', 'can_search_queries'}):
            sm.add_permission_role(sql_lab, p)
        else:
            sm.del_permission_role(sql_lab, p)

    logging.info("Syncing granter perms")
    for p in perms:
        if (p.permission.name
                in {'can_override_role_permissions', 'can_aprove'}):
            sm.add_permission_role(granter, p)
        else:
            sm.del_permission_role(granter, p)

    logging.info("Making sure all data source perms have been created")
    session = db.session()
    datasources = [o for o in session.query(models.SqlaTable).all()]
    datasources += [o for o in session.query(models.DruidDatasource).all()]
    for datasource in datasources:
        perm = datasource.get_perm()
        merge_perm(sm, 'datasource_access', perm)
        if datasource.schema:
            merge_perm(sm, 'schema_access', datasource.schema_perm)
        if perm != datasource.perm:
            datasource.perm = perm

    logging.info("Making sure all database perms have been created")
    databases = [o for o in session.query(models.Database).all()]
    for database in databases:
        perm = database.get_perm()
        if perm != database.perm:
            database.perm = perm
        merge_perm(sm, 'database_access', perm)
    session.commit()

    logging.info("Making sure all metrics perms exist")
    models.init_metrics_perm()
Example #16
0
def sync_role_definitions():
    """Inits the Superset application with security roles and such"""
    logging.info("Syncing role definition")

    # Creating default roles
    alpha = sm.add_role("Alpha")
    admin = sm.add_role("Admin")
    gamma = sm.add_role("Gamma")
    public = sm.add_role("Public")
    sql_lab = sm.add_role("sql_lab")
    granter = sm.add_role("granter")

    get_or_create_main_db()

    # Global perms
    sm.add_permission_view_menu(
        'all_datasource_access', 'all_datasource_access')
    sm.add_permission_view_menu('all_database_access', 'all_database_access')

    perms = db.session.query(ab_models.PermissionView).all()
    perms = [p for p in perms if p.permission and p.view_menu]

    logging.info("Syncing admin perms")
    for p in perms:
        # admin has all_database_access and all_datasource_access
        if is_user_defined_permission(p):
            sm.del_permission_role(admin, p)
        else:
            sm.add_permission_role(admin, p)

    logging.info("Syncing alpha perms")
    for p in perms:
        # alpha has all_database_access and all_datasource_access
        if is_user_defined_permission(p):
            sm.del_permission_role(alpha, p)
        elif (
                (
                    p.view_menu.name not in ADMIN_ONLY_VIEW_MENUES and
                    p.permission.name not in ADMIN_ONLY_PERMISSIONS
                ) or
                (p.permission.name, p.view_menu.name) in READ_ONLY_PRODUCT
        ):
            sm.add_permission_role(alpha, p)
        else:
            sm.del_permission_role(alpha, p)

    logging.info("Syncing gamma perms and public if specified")
    PUBLIC_ROLE_LIKE_GAMMA = conf.get('PUBLIC_ROLE_LIKE_GAMMA', False)
    for p in perms:
        if (
                (
                    p.view_menu.name not in ADMIN_ONLY_VIEW_MENUES and
                    p.permission.name not in ADMIN_ONLY_PERMISSIONS and
                    p.permission.name not in ALPHA_ONLY_PERMISSIONS
                ) or
                (p.permission.name, p.view_menu.name) in READ_ONLY_PRODUCT
        ):
            sm.add_permission_role(gamma, p)
            if PUBLIC_ROLE_LIKE_GAMMA:
                sm.add_permission_role(public, p)
        else:
            sm.del_permission_role(gamma, p)
            sm.del_permission_role(public, p)

    logging.info("Syncing sql_lab perms")
    for p in perms:
        if (
                p.view_menu.name in {'SQL Lab'} or
                p.permission.name in {
                    'can_sql_json', 'can_csv', 'can_search_queries'}
        ):
            sm.add_permission_role(sql_lab, p)
        else:
            sm.del_permission_role(sql_lab, p)

    logging.info("Syncing granter perms")
    for p in perms:
        if (
                p.permission.name in {
                    'can_override_role_permissions', 'can_aprove'}
        ):
            sm.add_permission_role(granter, p)
        else:
            sm.del_permission_role(granter, p)

    logging.info("Making sure all data source perms have been created")
    session = db.session()
    datasources = [
        o for o in session.query(models.SqlaTable).all()]
    datasources += [
        o for o in session.query(models.DruidDatasource).all()]
    for datasource in datasources:
        perm = datasource.get_perm()
        sm.add_permission_view_menu('datasource_access', perm)
        if perm != datasource.perm:
            datasource.perm = perm

    logging.info("Making sure all database perms have been created")
    databases = [o for o in session.query(models.Database).all()]
    for database in databases:
        perm = database.get_perm()
        if perm != database.perm:
            database.perm = perm
        sm.add_permission_view_menu('database_access', perm)
    session.commit()

    logging.info("Making sure all metrics perms exist")
    models.init_metrics_perm()
Example #17
0
def sync_role_definitions():
    """Inits the Superset application with security roles and such"""
    logging.info("Syncing role definition")

    # Creating default roles
    alpha = sm.add_role("Alpha")
    admin = sm.add_role("Admin")
    gamma = sm.add_role("Gamma")
    public = sm.add_role("Public")
    sql_lab = sm.add_role("sql_lab")
    granter = sm.add_role("granter")
    dashboard_access = sm.add_role("dashboard_access")
    dashboard_edit = sm.add_role("dashboard_edit")
    slice_access = sm.add_role("slice_access")
    slice_edit = sm.add_role("slice_edit")
    datasource_access = sm.add_role("datasource_access")
    datasource_edit = sm.add_role("datasource_edit")
    manage_edit = sm.add_role("manage_edit")
    user_role_edit = sm.add_role("user_role_edit")

    get_or_create_main_db()

    # Global perms
    merge_perm(sm, 'all_datasource_access', 'all_datasource_access')
    merge_perm(sm, 'all_database_access', 'all_database_access')

    perms = db.session.query(ab_models.PermissionView).all()
    perms = [p for p in perms if p.permission and p.view_menu]

    logging.info("Syncing admin perms")
    for p in perms:
        # admin has all_database_access and all_datasource_access
        if is_user_defined_permission(p):
            sm.del_permission_role(admin, p)
        else:
            sm.add_permission_role(admin, p)

    logging.info("Syncing alpha perms")
    for p in perms:
        # alpha has all_database_access and all_datasource_access
        if is_user_defined_permission(p):
            sm.del_permission_role(alpha, p)
        elif ((p.view_menu.name not in ADMIN_ONLY_VIEW_MENUES
               and p.permission.name not in ADMIN_ONLY_PERMISSIONS)
              or (p.permission.name, p.view_menu.name) in READ_ONLY_PRODUCT):
            sm.add_permission_role(alpha, p)
        else:
            sm.del_permission_role(alpha, p)

    logging.info("Syncing gamma perms and public if specified")
    PUBLIC_ROLE_LIKE_GAMMA = conf.get('PUBLIC_ROLE_LIKE_GAMMA', False)
    for p in perms:
        if ((p.view_menu.name not in ADMIN_ONLY_VIEW_MENUES
             and p.view_menu.name not in GAMMA_READ_ONLY_MODELVIEWS
             and p.permission.name not in ADMIN_ONLY_PERMISSIONS
             and p.permission.name not in ALPHA_ONLY_PERMISSIONS) or
            (p.permission.name, p.view_menu.name) in GAMMA_READ_ONLY_PRODUCT):
            sm.add_permission_role(gamma, p)
            if PUBLIC_ROLE_LIKE_GAMMA:
                sm.add_permission_role(public, p)
        else:
            sm.del_permission_role(gamma, p)
            sm.del_permission_role(public, p)

    logging.info("Syncing sql_lab perms")
    for p in perms:
        if (p.view_menu.name in {'SQL Lab'} or p.permission.name
                in {'can_sql_json', 'can_csv', 'can_search_queries'}):
            sm.add_permission_role(sql_lab, p)
        else:
            sm.del_permission_role(sql_lab, p)

    logging.info("Syncing granter perms")
    for p in perms:
        if (p.permission.name
                in {'can_override_role_permissions', 'can_aprove'}):
            sm.add_permission_role(granter, p)
        else:
            sm.del_permission_role(granter, p)

    logging.info("Syncing dashboard_access perms")
    for p in perms:
        if (p.view_menu.name in {'Dashboards'} or p.permission.name in {
                'can_explore', 'can_explore_json', 'can_slice',
                'can_created_dashboards', 'can_fave_dashboards',
                'all_datasource_access', 'all_database_access', 'can_profile'
        } or (p.permission.name in {'can_list', 'can_show', 'can_download'}
              and p.view_menu.name in {'DashboardModelView'}) or
            (p.permission.name in {'can_list', 'can_show', 'can_download'}
             and p.view_menu.name in {'DashboardModelViewAsync'})
                or (p.permission.name in {
                    'can_show', 'can_edit', 'can_download', 'can_userinfo',
                    'resetmypassword', 'userinfoedit'
                } and p.view_menu.name in {'UserDBModelView'})):
            sm.add_permission_role(dashboard_access, p)
        else:
            sm.del_permission_role(dashboard_access, p)

    logging.info("Syncing dashboard_edit perms")
    for p in perms:
        if (p.view_menu.name in {'Dashboards'} or p.permission.name in {
                'can_explore', 'can_explore_json', 'can_slice',
                'can_created_dashboards', 'can_fave_dashboards',
                'all_datasource_access', 'all_database_access', 'can_profile'
        } or (p.permission.name in {
                'can_list', 'can_show', 'can_add', 'can_delete', 'muldelete',
                'can_edit', 'can_download', 'mulexport'
        } and p.view_menu.name in {'DashboardModelView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'muldelete', 'can_edit', 'can_download', 'mulexport'
                } and p.view_menu.name in {'DashboardModelViewAsync'})
                or (p.permission.name in {
                    'can_show', 'can_edit', 'can_download', 'can_userinfo',
                    'resetmypassword', 'userinfoedit'
                } and p.view_menu.name in {'UserDBModelView'})):
            sm.add_permission_role(dashboard_edit, p)
        else:
            sm.del_permission_role(dashboard_edit, p)

    logging.info("Syncing slice_access perms")
    for p in perms:
        if (p.view_menu.name in {'Slices'} or p.permission.name in {
                'can_explore', 'can_explore_json', 'can_slice',
                'can_created_slices', 'can_fave_slices',
                'all_datasource_access', 'all_database_access', 'can_profile'
        } or (p.permission.name in {'can_list', 'can_show', 'can_download'}
              and p.view_menu.name in {'SliceModelView'}) or
            (p.permission.name in {'can_list', 'can_show', 'can_download'}
             and p.view_menu.name in {'SliceAsync'}) or (p.permission.name in {
                 'can_show', 'can_edit', 'can_userinfo', 'resetmypassword',
                 'userinfoedit'
             } and p.view_menu.name in {'UserDBModelView'})):
            sm.add_permission_role(slice_access, p)
        else:
            sm.del_permission_role(slice_access, p)

    logging.info("Syncing slice_edit perms")
    for p in perms:
        if (p.view_menu.name in {'Slices'} or p.permission.name in {
                'can_explore', 'can_explore_json', 'can_slice',
                'can_created_slices', 'can_fave_slices', 'can_add_slices',
                'all_datasource_access', 'all_database_access', 'can_profile'
        } or (p.permission.name in {
                'can_list', 'can_show', 'can_add', 'can_delete', 'muldelete',
                'can_edit', 'can_download'
        } and p.view_menu.name in {'SliceModelView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'muldelete', 'can_edit', 'can_download'
                } and p.view_menu.name in {'SliceAsync'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'muldelete', 'can_edit', 'can_download'
                } and p.view_menu.name in {'SliceAddView'})
                or (p.permission.name in {
                    'can_show', 'can_edit', 'can_userinfo', 'resetmypassword',
                    'userinfoedit'
                } and p.view_menu.name in {'UserDBModelView'})):
            sm.add_permission_role(slice_edit, p)
        else:
            sm.del_permission_role(slice_edit, p)

    logging.info("Syncing datasource_access perms")
    for p in perms:
        if (p.view_menu.name in {
                'Sources', 'Databases', 'Tables', 'Druid Clusters',
                'Druid Datasources'
        } or p.permission.name in {
                'can_explore', 'can_explore_json', 'all_datasource_access',
                'all_database_access', 'can_profile'
        } or (p.permission.name in {'can_list', 'can_show'}
              and p.view_menu.name in {'DatabaseView'})
                or (p.permission.name in {'can_list', 'can_show'}
                    and p.view_menu.name in {'DatabaseAsync'})
                or (p.permission.name in {'can_list', 'can_show'}
                    and p.view_menu.name in {'TableModelView'})
                or (p.permission.name in {'can_list', 'can_show'}
                    and p.view_menu.name in {'DatabaseTableAsync'})
                or (p.permission.name in {'can_list', 'can_show'}
                    and p.view_menu.name in {'DruidDatasourceModelView'})
                or (p.permission.name in {'can_list', 'can_show'}
                    and p.view_menu.name in {'DruidClusterModelView'})
                or (p.permission.name in {
                    'can_show', 'can_edit', 'can_userinfo', 'resetmypassword',
                    'userinfoedit'
                } and p.view_menu.name in {'UserDBModelView'})):
            sm.add_permission_role(datasource_access, p)
        else:
            sm.del_permission_role(datasource_access, p)

    logging.info("Syncing datasource_edit perms")
    for p in perms:
        if (p.view_menu.name in {
                'Sources', 'Databases', 'Tables', 'Druid Clusters',
                'Druid Datasources', 'Refresh Druid Metadata',
                'TableColumnInlineView', 'SqlMetricInlineView'
        } or p.permission.name in {
                'can_explore', 'can_explore_json', 'can_testconn',
                'can_checkbox', 'can_refresh_datasources',
                'all_datasource_access', 'all_database_access', 'can_profile'
        } or (p.permission.name in {
                'can_list', 'can_show', 'can_add', 'can_delete', 'muldelete',
                'can_edit', 'can_download'
        } and p.view_menu.name in {'DatabaseView'}) or (p.permission.name in {
                'can_list', 'can_show', 'can_add', 'can_delete', 'muldelete',
                'can_edit', 'can_download'
        } and p.view_menu.name in {'DatabaseAsync'}) or (p.permission.name in {
                'can_list', 'can_show', 'can_add', 'can_delete', 'muldelete',
                'can_edit', 'can_download'
        } and p.view_menu.name in {'TableModelView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'muldelete', 'can_edit', 'can_download'
                } and p.view_menu.name in {'DatabaseTablesAsync'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'muldelete', 'can_edit', 'can_download'
                } and p.view_menu.name in {'DruidDatasourceModelView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'muldelete', 'can_edit', 'can_download'
                } and p.view_menu.name in {'DruidClusterModelView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'can_edit', 'can_download'
                } and p.view_menu.name in {'TableColumnInlineView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'can_edit', 'can_download'
                } and p.view_menu.name in {'SqlMetricInlineView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'can_edit', 'can_download'
                } and p.view_menu.name in {'DruidColumnInlineView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'can_edit', 'can_download'
                } and p.view_menu.name in {'DruidMetricInlineView'})
                or (p.permission.name in {
                    'can_show', 'can_edit', 'can_userinfo', 'resetmypassword',
                    'userinfoedit'
                } and p.view_menu.name in {'UserDBModelView'})):
            sm.add_permission_role(datasource_edit, p)
        else:
            sm.del_permission_role(datasource_edit, p)

    logging.info("Syncing manage_edit perms")
    for p in perms:
        if (p.view_menu.name
                in {'Manage', 'Import Dashboards', 'Queries', 'CSS Templates'}
                or p.permission.name in {'can_profile'}
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'can_edit', 'can_download'
                } and p.view_menu.name in {'QueryView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'can_edit', 'can_download'
                } and p.view_menu.name in {'CssTemplateModelView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'can_edit', 'can_download'
                } and p.view_menu.name in {'CssTemplateAsyncModelView'})
                or (p.permission.name in {'can_add'}
                    and p.view_menu.name in {'DashboardModelView'})
                or (p.permission.name in {'can_add'}
                    and p.view_menu.name in {'SliceAddView'})
                or (p.permission.name in {
                    'can_show', 'can_edit', 'can_userinfo', 'resetmypassword',
                    'userinfoedit'
                } and p.view_menu.name in {'UserDBModelView'})):
            sm.add_permission_role(manage_edit, p)
        else:
            sm.del_permission_role(manage_edit, p)

    logging.info("Syncing user_role_edit perms")
    for p in perms:
        if (p.view_menu.name in {
                'Security', 'List Users', 'List Roles', "User's Statistics",
                'Base Permissions', 'Views/Menus', 'Permission on Views/Menus',
                'Access requests', 'Action Log'
        } or p.permission.name in {'can_recent_activity', 'can_profile'}
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'muldelete', 'can_edit', 'can_download', 'can_userinfo',
                    'resetmypassword', 'resetpasswords', 'userinfoedit'
                } and p.view_menu.name in {'UserDBModelView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'muldelete', 'can_edit', 'can_download', 'Copy Role',
                    'can_update_role', 'can_override_role_permissions'
                } and p.view_menu.name in {'RoleModelView'})
                or (p.permission.name in {'can_chart'}
                    and p.view_menu.name in {'UserStatsChartView'})
                or (p.permission.name in {'can_list'}
                    and p.view_menu.name in {'PermissionModelView'})
                or (p.permission.name in {'can_list'}
                    and p.view_menu.name in {'ViewMenuModelView'})
                or (p.permission.name in {'can_list'}
                    and p.view_menu.name in {'PermissionViewModelView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'can_edit', 'can_download', 'muldelete'
                } and p.view_menu.name in {'AccessRequestsModelView'})
                or (p.permission.name in {
                    'can_list', 'can_show', 'can_add', 'can_delete',
                    'can_edit', 'can_download'
                } and p.view_menu.name in {'LogModelView'}) or
            (p.permission.name in {'can_this_form_post', 'can_this_form_get'}
             and p.view_menu.name in {'ResetMyPasswordView'}) or
            (p.permission.name in {'can_this_form_post', 'can_this_form_get'}
             and p.view_menu.name in {'ResetPasswordView'}) or
            (p.permission.name in {'can_this_form_post', 'can_this_form_get'}
             and p.view_menu.name in {'UserInfoEditView'})):
            sm.add_permission_role(user_role_edit, p)
        else:
            sm.del_permission_role(user_role_edit, p)

    logging.info("Making sure all data source perms have been created")
    session = db.session()
    datasources = [o for o in session.query(models.SqlaTable).all()]
    datasources += [o for o in session.query(models.DruidDatasource).all()]
    for datasource in datasources:
        perm = datasource.get_perm()
        merge_perm(sm, 'datasource_access', perm)
        if datasource.schema:
            merge_perm(sm, 'schema_access', datasource.schema_perm)
        if perm != datasource.perm:
            datasource.perm = perm

    logging.info("Making sure all database perms have been created")
    databases = [o for o in session.query(models.Database).all()]
    for database in databases:
        perm = database.get_perm()
        if perm != database.perm:
            database.perm = perm
        merge_perm(sm, 'database_access', perm)
    session.commit()

    logging.info("Making sure all dashboard perms have been created")
    dashboards = [o for o in session.query(models.Dashboard).all()]
    for dashboard in dashboards:
        perm = dashboard.get_dashboard_title()
        sm.add_permission_view_menu('dashboard_access', perm)
    session.commit()

    logging.info("Making sure all metrics perms exist")
    models.init_metrics_perm()
Example #18
0
    def __init__(self, *args, **kwargs):
        if (
                        self.requires_examples and
                        not os.environ.get('SOLO_TEST') and
                        not os.environ.get('examples_loaded')
        ):
            logging.info("Loading examples")
            cli.load_examples(load_test_data=True)
            logging.info("Done loading examples")
            sync_role_definitions()
            os.environ['examples_loaded'] = '1'
        else:
            sync_role_definitions()
        super(SupersetTestCase, self).__init__(*args, **kwargs)
        self.client = app.test_client()
        self.maxDiff = None

        gamma_sqllab_role = sm.add_role("gamma_sqllab")
        for perm in sm.find_role('Gamma').permissions:
            sm.add_permission_role(gamma_sqllab_role, perm)
        db_perm = self.get_main_database(sm.get_session).perm
        security.merge_perm(sm, 'database_access', db_perm)
        db_pvm = sm.find_permission_view_menu(
            view_menu_name=db_perm, permission_name='database_access')
        gamma_sqllab_role.permissions.append(db_pvm)
        for perm in sm.find_role('sql_lab').permissions:
            sm.add_permission_role(gamma_sqllab_role, perm)

        admin = appbuilder.sm.find_user('admin')
        if not admin:
            appbuilder.sm.add_user(
                'admin', 'admin', ' user', '*****@*****.**',
                appbuilder.sm.find_role('Admin'),
                password='******')

        gamma = appbuilder.sm.find_user('gamma')
        if not gamma:
            appbuilder.sm.add_user(
                'gamma', 'gamma', 'user', '*****@*****.**',
                appbuilder.sm.find_role('Gamma'),
                password='******')

        gamma2 = appbuilder.sm.find_user('gamma2')
        if not gamma2:
            appbuilder.sm.add_user(
                'gamma2', 'gamma2', 'user', '*****@*****.**',
                appbuilder.sm.find_role('Gamma'),
                password='******')

        gamma_sqllab_user = appbuilder.sm.find_user('gamma_sqllab')
        if not gamma_sqllab_user:
            appbuilder.sm.add_user(
                'gamma_sqllab', 'gamma_sqllab', 'user', '*****@*****.**',
                gamma_sqllab_role, password='******')

        alpha = appbuilder.sm.find_user('alpha')
        if not alpha:
            appbuilder.sm.add_user(
                'alpha', 'alpha', 'user', '*****@*****.**',
                appbuilder.sm.find_role('Alpha'),
                password='******')
        sm.get_session.commit()
        # create druid cluster and druid datasources
        session = db.session
        cluster = (
            session.query(DruidCluster)
            .filter_by(cluster_name="druid_test")
            .first()
        )
        if not cluster:
            cluster = DruidCluster(cluster_name="druid_test")
            session.add(cluster)
            session.commit()

            druid_datasource1 = DruidDatasource(
                datasource_name='druid_ds_1',
                cluster_name='druid_test'
            )
            session.add(druid_datasource1)
            druid_datasource2 = DruidDatasource(
                datasource_name='druid_ds_2',
                cluster_name='druid_test'
            )
            session.add(druid_datasource2)
            session.commit()
Example #19
0
    def __init__(self, *args, **kwargs):
        if self.requires_examples and not os.environ.get("SOLO_TEST") and not os.environ.get("examples_loaded"):
            logging.info("Loading examples")
            cli.load_examples(load_test_data=True)
            logging.info("Done loading examples")
            sync_role_definitions()
            os.environ["examples_loaded"] = "1"
        else:
            sync_role_definitions()
        super(SupersetTestCase, self).__init__(*args, **kwargs)
        self.client = app.test_client()
        self.maxDiff = None

        gamma_sqllab_role = sm.add_role("gamma_sqllab")
        for perm in sm.find_role("Gamma").permissions:
            sm.add_permission_role(gamma_sqllab_role, perm)
        db_perm = self.get_main_database(sm.get_session).perm
        security.merge_perm(sm, "database_access", db_perm)
        db_pvm = sm.find_permission_view_menu(view_menu_name=db_perm, permission_name="database_access")
        gamma_sqllab_role.permissions.append(db_pvm)
        for perm in sm.find_role("sql_lab").permissions:
            sm.add_permission_role(gamma_sqllab_role, perm)

        admin = appbuilder.sm.find_user("admin")
        if not admin:
            appbuilder.sm.add_user(
                "admin", "admin", " user", "*****@*****.**", appbuilder.sm.find_role("Admin"), password="******"
            )

        gamma = appbuilder.sm.find_user("gamma")
        if not gamma:
            appbuilder.sm.add_user(
                "gamma", "gamma", "user", "*****@*****.**", appbuilder.sm.find_role("Gamma"), password="******"
            )

        gamma_sqllab_user = appbuilder.sm.find_user("gamma_sqllab")
        if not gamma_sqllab_user:
            appbuilder.sm.add_user(
                "gamma_sqllab", "gamma_sqllab", "user", "*****@*****.**", gamma_sqllab_role, password="******"
            )

        alpha = appbuilder.sm.find_user("alpha")
        if not alpha:
            appbuilder.sm.add_user(
                "alpha", "alpha", "user", "*****@*****.**", appbuilder.sm.find_role("Alpha"), password="******"
            )
        sm.get_session.commit()

        # create druid cluster and druid datasources
        session = db.session
        cluster = session.query(models.DruidCluster).filter_by(cluster_name="druid_test").first()
        if not cluster:
            cluster = models.DruidCluster(cluster_name="druid_test")
            session.add(cluster)
            session.commit()

            druid_datasource1 = models.DruidDatasource(datasource_name="druid_ds_1", cluster_name="druid_test")
            session.add(druid_datasource1)
            druid_datasource2 = models.DruidDatasource(datasource_name="druid_ds_2", cluster_name="druid_test")
            session.add(druid_datasource2)
            session.commit()