Example #1
0
    def setUp(self):
        """Set up the configuration to access the triplestore

        Use the config file test.virtuoso.ini to not interfere with
        production data
        """

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.settings['askomics.upload_user_data_method'] = 'insert'

        self.request = testing.DummyRequest()

        self.config = testing.setUp(request=self.request)
        self.config.add_route('load_data_into_graph', '/load_data_into_graph')
        self.config.scan()

        self.request.session['username'] = '******'
        self.request.session['blocked'] = False
        self.request.session['group'] = 'base'
        self.request.session['graph'] = 'urn:sparql:test_askomics:jdoe'

        self.request.host_url = 'http://localhost:6543'

        self.request.json_body = {}

        SetupTests(self.settings, self.request.session)

        self.tps = InterfaceTpsDb(self.settings, self.request)

        self.askview = AskView(self.request)
        self.askview.settings = self.settings
Example #2
0
    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = False
        self.request.session['graph'] = "test/nosetest/jdoe"

        SetupTests(self.settings, self.request.session)
        self.tps = InterfaceTpsDb(self.settings, self.request)
Example #3
0
    def setUp(self):
        """Set up the configuration to access the triplestore

        Use the config file test.virtuoso.ini to not interfere with
        production data
        """

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.settings['askomics.upload_user_data_method'] = 'insert'

        self.request = testing.DummyRequest()

        self.config = testing.setUp(request=self.request)
        self.config.add_route('load_data_into_graph', '/load_data_into_graph')
        self.config.scan()

        self.request.session['user_id'] = 1
        self.request.session['username'] = '******'
        self.request.session['email'] = '*****@*****.**'
        self.request.session['admin'] = True
        self.request.session['blocked'] = False
        self.request.session['graph'] = 'urn:sparql:test_askomics:jdoe'

        self.request.host_url = 'http://localhost:6543'

        self.request.json_body = {}

        SetupTests(self.settings, self.request.session)

        self.tps = InterfaceTpsDb(self.settings, self.request)

        self.askview = AskView(self.request)
        self.askview.settings = self.settings
Example #4
0
    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = True

        SetupTests(self.settings, self.request.session)

        self.tps = InterfaceTpsDb(self.settings, self.request)
Example #5
0
    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.settings['askomics.upload_user_data_method'] = 'insert'

        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = False

        self.request.session['graph'] = "test/nosetest/jdoe"

        self.tps = InterfaceTpsDb(self.settings, self.request)
Example #6
0
class JobManagerTests(unittest.TestCase):
    """Test for the ModuleManager class"""
    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.settings['askomics.upload_user_data_method'] = 'insert'

        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = False

        self.request.session['graph'] = "test/nosetest/jdoe"

        self.tps = InterfaceTpsDb(self.settings, self.request)

    def test_save_integration_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('file.tsv')

        assert self.tps.test_row_presence(
            'integration', 'user_id, filename, state, end, error',
            (1, 'file.tsv', 'wait', None, None))

    def test_query_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_query_job(
            'graph_string',
            '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D'
        )

        assert self.tps.test_row_presence(
            'query',
            'user_id, state, end, data, file, preview, graph, variates, nrows, error',
            (1, 'wait', None, None, None, None, 'graph_string',
             '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D',
             None, None))

    def test_done_integration_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('file.tsv')
        job_manager.done_integration_job(1)

        assert self.tps.test_row_presence('integration',
                                          'user_id, filename, state, error',
                                          (1, 'file.tsv', 'done', None))

    def test_done_query_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_query_job(
            'graph_string',
            '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D'
        )
        job_manager.done_query_job(1, 15, 'data_string', 'file_string')

        assert self.tps.test_row_presence(
            'query',
            'user_id, state, data, file, preview, graph, variates, nrows, error',
            (1, 'done', '_s3_22data_string_s3_22', 'file_string', None,
             'graph_string',
             '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D',
             15, None))

    def test_set_error_message(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('error_file.tsv')
        job_manager.set_error_message('integration', 'error_string', 1)

        assert self.tps.test_row_presence(
            'integration', 'user_id, filename, state, end, error',
            (1, 'error_file.tsv', 'error', None, 'error_string'))

    def test_list_integration_jobs(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('file.tsv')
        job_manager.save_integration_job('file2.tsv')
        job_manager.done_integration_job(2)

        result = job_manager.list_integration_jobs()

        assert len(result) == 2

    def test_list_query_jobs(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_query_job(
            'graph_string',
            '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D'
        )
        job_manager.save_query_job(
            'graph_string2',
            '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D'
        )
        job_manager.done_query_job(2, 15, 'data_string2', 'file_string2')

        result = job_manager.list_query_jobs()

        assert len(result) == 2

    def test_remove_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('file.tsv')
        assert self.tps.test_row_presence(
            'integration', 'user_id, filename, state, end, error',
            (1, 'file.tsv', 'wait', None, None))
        job_manager.remove_job('integration', 1)
        assert not self.tps.test_row_presence(
            'integration', 'user_id, filename, state, end, error',
            (1, 'file.tsv', 'wait', None, None))
Example #7
0
class JobManagerTests(unittest.TestCase):
    """Test for the ModuleManager class"""

    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.settings['askomics.upload_user_data_method'] = 'insert'

        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = False

        self.request.session['graph'] = "test/nosetest/jdoe"

        self.tps = InterfaceTpsDb(self.settings, self.request)

        
    def test_save_integration_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('file.tsv', 1)

        assert self.tps.test_row_presence('integration', 'user_id, filename, state, end, error', (1, 'file.tsv', 'wait', None, None))


    def test_query_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_query_job('graph_string', '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D', 1)

        assert self.tps.test_row_presence('query', 'user_id, state, end, data, file, preview, graph, variates, nrows, error', (1, 'wait', None, None, None, None, 'graph_string', '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D', None, None))

    def test_done_integration_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('file.tsv', 1)
        job_manager.done_integration_job(1)

        assert self.tps.test_row_presence('integration', 'user_id, filename, state, error', (1, 'file.tsv', 'done', None))


    def test_done_query_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_query_job('graph_string', '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D', 1)
        job_manager.done_query_job(1, 15, 'data_string', 'file_string')

        assert self.tps.test_row_presence('query', 'user_id, state, data, file, preview, graph, variates, nrows, error', (1, 'done', '_s3_22data_string_s3_22', 'file_string', None, 'graph_string', '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D', 15, None))

    def test_set_error_message(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('error_file.tsv', 1)
        job_manager.set_error_message('integration', 'error_string', 1)

        assert self.tps.test_row_presence('integration', 'user_id, filename, state, end, error', (1, 'error_file.tsv', 'error', None, 'error_string'))

    def test_list_integration_jobs(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('file.tsv', 1)
        job_manager.save_integration_job('file2.tsv', 1)
        job_manager.done_integration_job(2)

        result = job_manager.list_integration_jobs(1)

        assert len(result) == 2

    def test_list_query_jobs(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_query_job('graph_string', '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D', 1)
        job_manager.save_query_job('graph_string2', '_s3_7B_s3_27variate_s3_27_s3_3A_s3_20_s3_5B_s3_27_s3_3Fvariate_s3_27_s3_5D_s3_7D', 1)
        job_manager.done_query_job(2, 15, 'data_string2', 'file_string2')

        result = job_manager.list_query_jobs(1)

        assert len(result) == 2

    def test_remove_job(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        job_manager = JobManager(self.settings, self.request.session)
        job_manager.save_integration_job('file.tsv', 1)
        assert self.tps.test_row_presence('integration', 'user_id, filename, state, end, error', (1, 'file.tsv', 'wait', None, None))
        job_manager.remove_job('integration', 1)
        assert not self.tps.test_row_presence('integration', 'user_id, filename, state, end, error', (1, 'file.tsv', 'wait', None, None))
Example #8
0
class LocalAuthTests(unittest.TestCase):

    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = True

        SetupTests(self.settings, self.request.session)

        self.tps = InterfaceTpsDb(self.settings, self.request)

    def test_set_auth_type(self):

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.auth_type == "local"

        local_auth.set_auth_type('ldap')

        assert local_auth.auth_type == "ldap"

    def test_get_galaxy_infos(self):

        self.tps.clean_up()
        self.tps.add_galaxy_account()

        local_auth = LocalAuth(self.settings, self.request.session)
        galaxy_infos = local_auth.get_galaxy_infos(1)

        assert galaxy_infos == {'url': 'http://locahost/galaxy', 'key': 'fake_api_key'}

    def test_get_user_infos(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_jsmith_in_users()
        self.tps.add_galaxy_account()

        local_auth = LocalAuth(self.settings, self.request.session)

        user_infos_jdoe_username = local_auth.get_user_infos('jdoe')
        user_infos_jdoe_email = local_auth.get_user_infos('*****@*****.**')
        user_infos_jsmith_username = local_auth.get_user_infos('jsmith')
        assert user_infos_jdoe_username == user_infos_jdoe_email
        assert user_infos_jdoe_username == {'id': 1, 'username': '******', 'email': '*****@*****.**', 'password': '******', 'salt': '00000000000000000000', 'admin': False, 'blocked': False, 'apikey': 'jdoe_apikey', 'galaxy': {'url': 'http://locahost/galaxy', 'key': 'fake_api_key'}, 'auth_type': 'local'}
        assert user_infos_jsmith_username == {'id': 2, 'username': '******', 'email': '*****@*****.**', 'password': '******', 'salt': '00000000000000000000', 'admin': False, 'blocked': False, 'apikey': 'jsmith_apikey', 'galaxy': None, 'auth_type': 'local'}

    def test_get_user_infos_api_key(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_jsmith_in_users()
        self.tps.add_galaxy_account()

        local_auth = LocalAuth(self.settings, self.request.session)

        user_infos_jdoe = local_auth.get_user_infos_api_key('jdoe_apikey')
        user_infos_jsmith = local_auth.get_user_infos_api_key('jsmith_apikey')
        assert user_infos_jdoe == {'id': 1, 'username': '******', 'email': '*****@*****.**', 'password': '******', 'salt': '00000000000000000000', 'admin': False, 'blocked': False, 'apikey': 'jdoe_apikey', 'galaxy': {'url': 'http://locahost/galaxy', 'key': 'fake_api_key'}, 'auth_type': 'local'}
        assert user_infos_jsmith == {'id': 2, 'username': '******', 'email': '*****@*****.**', 'password': '******', 'salt': '00000000000000000000', 'admin': False, 'blocked': False, 'apikey': 'jsmith_apikey', 'galaxy': None, 'auth_type': 'local'}

    def test_is_username_in_db(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.is_username_in_db('jdoe')
        assert not local_auth.is_username_in_db('jsmith')

    def test_is_email_in_db(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.is_email_in_db('*****@*****.**')
        assert not local_auth.is_email_in_db('*****@*****.**')

    def test_is_user_in_db(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.is_user_in_db('jdoe')
        assert local_auth.is_user_in_db('*****@*****.**')
        assert not local_auth.is_user_in_db('jsmith')
        assert not local_auth.is_user_in_db('*****@*****.**')

    def test_get_auth_type(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_ldap_jdoe_user_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.get_auth_type('jdoe') == 'local'
        assert local_auth.get_auth_type('ldap_jdoe') == 'ldap'

    def test_persist_user(self):

        self.tps.clean_up()

        local_auth = LocalAuth(self.settings, self.request.session)
        local_auth.persist_user('jdoe', '*****@*****.**', 'jdoe')
        local_auth.persist_user('jsmith', '*****@*****.**', 'jsmith')
        local_auth.set_auth_type('ldap')
        local_auth.persist_user('ldap_jdoe', '*****@*****.**')

        assert self.tps.test_row_presence('users', 'username, email, password', ('ldap_jdoe', '*****@*****.**', None))
        assert self.tps.test_row_presence('users', 'username, email', ('jdoe', '*****@*****.**'))
        assert self.tps.test_row_presence('users', 'username, email', ('jsmith', '*****@*****.**'))

    def test_get_number_of_users(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_jsmith_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.get_number_of_users() == 2

    def test_authenticate_user(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.authenticate_user('jdoe', 'iamjohndoe')
        assert local_auth.authenticate_user('*****@*****.**', 'iamjohndoe')
        assert not local_auth.authenticate_user('jsmith', 'iamjanesmith')
        assert not local_auth.authenticate_user('*****@*****.**', 'iamjanesmith')

    def test_authenticate_user_with_username(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.authenticate_user_with_username('jdoe', 'iamjohndoe')
        assert not local_auth.authenticate_user_with_username('jsmith', 'iamjanesmith')

    def test_authenticate_user_with_email(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.authenticate_user_with_email('*****@*****.**', 'iamjohndoe')
        assert not local_auth.authenticate_user_with_email('*****@*****.**', 'iamjanesmith')

    def test_create_user_graph(self):

        local_auth = LocalAuth(self.settings, self.request.session)
        local_auth.create_user_graph('jdoe')

        assert self.tps.test_triple_presence('urn:sparql:test_askomics', '<urn:sparql:test_askomics:jdoe> rdfg:subGraphOf <urn:sparql:test_askomics>')
        assert not self.tps.test_triple_presence('urn:sparql:test_askomics', '<urn:sparql:test_askomics:jsmith> rdfg:subGraphOf <urn:sparql:test_askomics>')


    def test_renew_apikey(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username, email, apikey', ('jdoe', '*****@*****.**', 'jdoe_apikey'))

        local_auth.renew_apikey('jdoe')

        assert not self.tps.test_row_presence('users', 'username, email, apikey', ('jdoe', '*****@*****.**', 'jdoe_apikey'))
        assert self.tps.test_row_presence('users', 'username, email', ('jdoe', '*****@*****.**'))

    def test_add_galaxy(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        galaxy_url = self.settings['askomics.testing_galaxy_url']
        galaxy_key = self.settings['askomics.testing_galaxy_key']

        local_auth = LocalAuth(self.settings, self.request.session)

        local_auth.add_galaxy(galaxy_url, galaxy_key, 1)

        assert self.tps.test_row_presence('galaxy_accounts', '*', (1, 1, galaxy_url, galaxy_key))

    def test_get_all_users_infos(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_ldap_jdoe_user_in_users()
        self.tps.add_jsmith_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)
        all_users = local_auth.get_all_users_infos()

        # Get dir size
        path = self.settings['askomics.files_dir'] + '/jdoe'
        size = 0
        for dirpath, dirnames, filenames in os.walk(path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                size += os.path.getsize(fp)
        hsize = humanize.naturalsize(size)

        assert all_users == [{'ldap': False, 'username': '******', 'email': '*****@*****.**', 'admin': False, 'blocked': False, 'gurl': None, 'nquery': 0, 'nintegration': 0, 'dirsize': size, 'hdirsize': hsize}, {'ldap': False, 'username': '******', 'email': '*****@*****.**', 'admin': False, 'blocked': False, 'gurl': None, 'nquery': 0, 'nintegration': 0, 'dirsize': 0, 'hdirsize': '0 Bytes'}, {'ldap': True, 'username': '******', 'email': '*****@*****.**', 'admin': False, 'blocked': False, 'gurl': None, 'nquery': 0, 'nintegration': 0, 'dirsize': 0, 'hdirsize': '0 Bytes'}]

    def test_lock_user(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username, admin, blocked', ('jdoe', 'false', 'false'))

        local_auth.lock_user('true', 'jdoe')

        assert self.tps.test_row_presence('users', 'username, admin, blocked', ('jdoe', 'false', 'true'))

    def test_admin_user(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username, admin, blocked', ('jdoe', 'false', 'false'))

        local_auth.admin_user('true', 'jdoe')

        assert self.tps.test_row_presence('users', 'username, admin, blocked', ('jdoe', 'true', 'false'))

    def test_delete_user(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username', ('jdoe', ))

        local_auth.delete_user('jdoe')

        assert not self.tps.test_row_presence('users', 'username', ('jdoe', ))

    def test_update_email(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username, email', ('jdoe', '*****@*****.**'))

        local_auth.update_email('jdoe', '*****@*****.**')

        assert self.tps.test_row_presence('users', 'username, email', ('jdoe', '*****@*****.**'))

    def test_update_password(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username, password, salt', ('jdoe', 'f49d76161eb1617568fedf0a0adc92532cc81c1a2626ec0e2d5fa36bd600f55b17f599a4a343a5ccdc907a2831db70c7a390a9f96afbf346190e6fe3d6ed836f', '00000000000000000000'))

        local_auth.update_password('jdoe', 'newpass')

        assert not self.tps.test_row_presence('users', 'username, password, salt', ('jdoe', 'f49d76161eb1617568fedf0a0adc92532cc81c1a2626ec0e2d5fa36bd600f55b17f599a4a343a5ccdc907a2831db70c7a390a9f96afbf346190e6fe3d6ed836f', '00000000000000000000'))

    def test_get_random_string(self):

        local_auth = LocalAuth(self.settings, self.request.session)

        assert len(local_auth.get_random_string(20)) == 20
Example #9
0
class AskViewTests(unittest.TestCase):
    """Test for Askview

    Contain all the tests for the askView class
    """

    def setUp(self):
        """Set up the configuration to access the triplestore

        Use the config file test.virtuoso.ini to not interfere with
        production data
        """

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.settings['askomics.upload_user_data_method'] = 'insert'

        self.request = testing.DummyRequest()

        self.config = testing.setUp(request=self.request)
        self.config.add_route('load_data_into_graph', '/load_data_into_graph')
        self.config.scan()

        self.request.session['user_id'] = 1
        self.request.session['username'] = '******'
        self.request.session['email'] = '*****@*****.**'
        self.request.session['admin'] = True
        self.request.session['blocked'] = False
        self.request.session['graph'] = 'urn:sparql:test_askomics:jdoe'

        self.request.host_url = 'http://*****:*****@example.com',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        data = self.askview.signup()

        assert data == {'error': [], 'user_id': 1, 'username': '******', 'email': '*****@*****.**', 'admin': True, 'blocked': False, 'galaxy': None}

    def test_checkuser(self):
        """Test checkuser method"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        data = self.askview.checkuser()

        assert data == {'user_id': 1, 'username': '******', 'email': '*****@*****.**', 'admin': False, 'blocked': False, 'galaxy': None}


    def test_logout(self):
        """Test logout method"""

        self.tps.clean_up()

        self.askview.logout()

        assert self.request.session == {}


    def test_login(self):
        """Test login method"""

        self.tps.clean_up()

        #first, create a user
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        self.askview.signup()

        # then, logout this user
        self.askview.logout()

        # and then, test login
        self.request.json_body = {
            'username_email': 'jdoe',
            'password': '******'
        }

        data = self.askview.login()

        assert data == {'error': [], 'user_id': 1, 'username': '******', 'email': '*****@*****.**', 'admin': True, 'blocked': False, 'galaxy': None}

    def test_login_api(self):
        """Test login_api method"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        self.request.GET['key'] = 'jdoe_apikey'

        data = self.askview.login_api()

        assert data == {'error': '', 'user_id': 1, 'username': '******', 'email': '*****@*****.**', 'admin': False, 'blocked': False, 'galaxy': None}

    def test_login_api_gie(self):
        """Test login_api_gie method"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        self.request.GET['key'] = 'jdoe_apikey'

        self.askview.login_api()

    def test_get_users_infos(self):
        """Test get_users_infos"""

        self.tps.clean_up()
        # first test with non admin
        try :
            data = self.askview.get_users_infos()
            assert False
        except Exception as e:
            assert True

        # then, is user is admin
        self.request.session['admin'] = True

        data = self.askview.get_users_infos()

        assert data == {'result': [], 'me': 'jdoe'} #result is empty cause there is no user

        #test with user
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        # get dir size
        pm = ParamManager(self.settings, self.request.session)
        dir_size = pm.get_size(pm.get_user_dir_path())
        human_dir_size = humanize.naturalsize(dir_size)

        self.askview.signup()

        data = self.askview.get_users_infos()

        assert data == {'result': [{'ldap': False, 'username': '******', 'email': '*****@*****.**', 'admin': True, 'blocked': False, 'gurl': None, 'nquery': 0, 'nintegration': 0, 'dirsize': dir_size, 'hdirsize': human_dir_size}], 'me': 'jdoe', 'error': [], 'user_id': 1, 'username': '******', 'email': '*****@*****.**', 'admin': True, 'blocked': False, 'galaxy': None}

    def test_lock_user(self):
        """Test lock_user method"""

        self.tps.clean_up()

        self.request.json_body = {
            'username': '******',
            'lock': True
        }

        # first test with non admin

        try:
            data = self.askview.lock_user()
            assert False
        except Exception as e:
            assert True

        # then, is user is admin
        self.request.session['admin'] = True

        data = self.askview.lock_user()

        assert data == 'success'


    def test_set_admin(self):
        """Test set_admin_method"""

        self.tps.clean_up()

        self.request.json_body = {
            'username': '******',
            'admin': True
        }

        try:
            data = self.askview.set_admin()
        except Exception as e:
            assert True

        # then, is user is admin
        self.request.session['admin'] = True

        data = self.askview.set_admin()

        assert data == 'success'

    def test_delete_user(self):
        """Test delete_user method"""

        self.tps.clean_up()

        # Insert a user
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        self.askview.signup()

        # test the deletion
        self.request.json_body = {
            'username': '******',
            'passwd': 'iamjohndoe',
            'passwd_conf': 'iamjohndoe'
        }

        self.request.session['blocked'] = False
        self.request.session['admin'] = False
        self.request.session['username'] = '******'

        data = self.askview.delete_user()

        assert data == 'success'


    def test_get_my_infos(self):
        """Test get_my_infos"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        # get my infos
        data = self.askview.get_my_infos()

        assert data == {'user_id': 1, 'username': '******', 'email': '*****@*****.**', 'admin': False, 'blocked': False, 'apikey': 'jdoe_apikey', 'galaxy': None, 'ldap': False}

    def test_update_mail(self):
        """Test update_mail"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        # And change my email
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**'
        }

        data = self.askview.update_mail()

        assert data == {'success': 'success'}

    def test_update_passwd(self):
        """Test update_passwd method"""

        self.tps.clean_up()

        # First, insert me
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        self.askview.signup()

        # And update my password
        self.request.json_body = {
            'username': '******',
            'current_passwd': 'iamjohndoe',
            'passwd': 'mynewpassword',
            'passwd2': 'mynewpassword',
        }

        data = self.askview.update_passwd()

        assert data == {'error': [], 'user_id': 1, 'username': '******', 'email': '*****@*****.**', 'admin': True, 'blocked': False, 'galaxy': None, 'success': 'success'}
Example #10
0
class EndpointManagerTests(unittest.TestCase):
    """Test for the ModuleManager class"""
    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = False
        self.request.session['graph'] = "test/nosetest/jdoe"

        SetupTests(self.settings, self.request.session)
        self.tps = InterfaceTpsDb(self.settings, self.request)

    def test_save_endpoint(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        assert endpoint_manager.save_endpoint('endpoint1',
                                              'http://endpoint./sparql') == 1

    def test_enable(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint_manager.enable(
            endpoint_manager.save_endpoint('endpoint1',
                                           'http://endpoint/sparql'))

        assert self.tps.test_row_presence(
            'endpoints', 'id, name, url, auth, enable, message',
            (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 1, None))

    def test_disable(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint_id = endpoint_manager.save_endpoint('endpoint1',
                                                     'http://endpoint/sparql',
                                                     isenable=True)

        assert self.tps.test_row_presence(
            'endpoints', 'id, name, url, auth, enable, message',
            (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 1, None))

        endpoint_manager.disable(endpoint_id, 'message')

        assert self.tps.test_row_presence(
            'endpoints', 'id, name, url, auth, enable, message',
            (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 0, 'message'))

    def test_disable_by_url(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint_manager.save_endpoint('endpoint1',
                                       'http://endpoint/sparql',
                                       isenable=True)

        assert self.tps.test_row_presence(
            'endpoints', 'id, name, url, auth, enable, message',
            (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 1, None))

        endpoint_manager.disable_by_url('http://endpoint/sparql', 'message')

        assert self.tps.test_row_presence(
            'endpoints', 'id, name, url, auth, enable, message',
            (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 0, 'message'))

    def test_list_endpoints(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint1 = endpoint_manager.save_endpoint('endpoint1',
                                                   'http://endpoint/sparql',
                                                   isenable=True)
        endpoint2 = endpoint_manager.save_endpoint(
            'endpoint2', 'http://other_endpoint/sparql', isenable=True)

        list_endpoints = endpoint_manager.list_endpoints()

        assert list_endpoints == [{
            'id': 1,
            'name': 'endpoint1',
            'endpoint': 'http://endpoint/sparql',
            'auth': 'BASIC',
            'enable': True,
            'message': None
        }, {
            'id': 2,
            'name': 'endpoint2',
            'endpoint': 'http://other_endpoint/sparql',
            'auth': 'BASIC',
            'enable': True,
            'message': None
        }]

    def test_list_active_endpoints(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint1 = endpoint_manager.save_endpoint('endpoint1',
                                                   'http://endpoint/sparql',
                                                   isenable=True)
        endpoint2 = endpoint_manager.save_endpoint(
            'endpoint2', 'http://other_endpoint/sparql', isenable=False)

        list_endpoints = endpoint_manager.list_active_endpoints()

        assert list_endpoints == [{
            'id': 1,
            'name': 'endpoint1',
            'endpoint': 'http://endpoint/sparql',
            'auth': 'BASIC',
            'enable': True,
            'message': None
        }]

    def test_remove_endpoint(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint = endpoint_manager.save_endpoint('endpoint1',
                                                  'http://endpoint/sparql')

        assert self.tps.test_row_presence(
            'endpoints', 'id, name, url, auth, enable, message',
            (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 0, None))

        endpoint_manager.remove_endpoint(endpoint)

        assert not self.tps.test_row_presence(
            'endpoints', 'id, name, url, auth, enable, message',
            (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 0, None))
Example #11
0
class SecurityTests(unittest.TestCase):
    """Test for the Security class"""
    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = True

        SetupTests(self.settings, self.request.session)

        self.tps = InterfaceTpsDb(self.settings, self.request)

    def test_get_sha256_pw(self):
        """Test get_sha256_pw"""

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        assert len(security.get_sha256_pw(
        )) == 128  # We cant predict the string cause there is random for salt

    def test_check_email(self):
        """Test for check_email"""

        # Test if good mail return True
        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')
        assert security.check_email()

        # Test if wrong email return False
        security = Security(self.settings, self.request.session, 'jdoe',
                            'wrong_email', 'iamjohndoe', 'iamjohndoe')
        assert not security.check_email()

    def test_check_passwords(self):
        """Test for check_passwords"""

        # Test if identical passwords return True
        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')
        assert security.check_passwords()

        # Test if different passwords return False
        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe',
                            'second_wrong_password')
        assert not security.check_passwords()

    def test_check_password_length(self):
        """Test for check_password_length"""

        # Test if long passwords return True
        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')
        assert security.check_password_length()

        # Test if short passwords return False
        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'a', 'a')
        # assert not security.check_password_length() #FIXME: not working because max lenght is 1

    def test_check_username_in_database(self):
        """test check_username_in_database

        Insert jdoe and test if jdoe is in database, then test
        jsmith is not in database
        """

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        # test with same username
        assert security.check_username_in_database()

        security = Security(self.settings, self.request.session, 'jsmith',
                            '*****@*****.**', 'iamjohnsmith',
                            'iamjohnsmith')

        # test with a different username
        assert not security.check_username_in_database()

    def test_check_email_in_database(self):
        """test check_username_in_database

        Insert jdoe and test if jdoe is in database, then test
        jsmith is not in database
        """

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        # test with same username
        assert security.check_email_in_database()

        security = Security(self.settings, self.request.session, 'jsmith',
                            '*****@*****.**', 'iamjohnsmith',
                            'iamjohnsmith')

        # test with a different username
        assert not security.check_email_in_database()

    def test_set_username_by_email(self):
        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')
        security.set_username_by_email()

    def test_get_owner_of_apikey(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')
        security.get_owner_of_apikey("test")

    def test_ckeck_key_belong_user(self):
        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')
        security.ckeck_key_belong_user("test")

    def test_check_email_password(self):
        """Test check_email_password

        Insert jdoe and check the password
        """

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        # try with the good password
        assert security.check_email_password()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'wrong_password',
                            'wrong_password')

        # Try with a wrong password
        assert not security.check_email_password()

    def test_check_username_password(self):
        """Test check_username_password

        Insert jdoe and check the password
        """

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        # try with the good password
        assert security.check_username_password()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'wrong_password',
                            'wrong_password')

        # Try with a wrong password
        assert not security.check_username_password()

    def test_get_number_of_users(self):
        """Test get_number_of_users

        test the method with 0, 1 and 2 users
        """

        self.tps.clean_up()

        security = Security(self.settings, self.request.session, '', '', '',
                            '')

        assert security.get_number_of_users() == 0

        self.tps.add_jdoe_in_users()

        assert security.get_number_of_users() == 1

        self.tps.add_jsmith_in_users()

        assert security.get_number_of_users() == 2

    def test_persist_user(self):
        """Test persist_user

        Insert a user and test if get_number_of_user return 1
        """

        self.tps.clean_up()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')
        assert security.get_number_of_users() == 0
        security.persist_user('http://localhost')

        assert security.get_number_of_users() == 1
        #assert security.
        security = Security(self.settings, self.request.session, 'jdoe2',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        security.persist_user('http://localhost')

        assert security.get_number_of_users() == 2

    def test_send_mails(self):
        """Test send_mails"""

        #TODO: how to test mail ?
        pass

    def test_create_user_graph(self):
        """Test create_user_graph

        create the graph, and then test the presence of the triple
        """

        self.tps.clean_up()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        security.create_user_graph()

        assert self.tps.test_triple_presence(
            'urn:sparql:test_askomics',
            '<urn:sparql:test_askomics:jdoe> rdfg:subGraphOf <urn:sparql:test_askomics>'
        )
        assert not self.tps.test_triple_presence(
            'urn:sparql:test_askomics',
            '<urn:sparql:test_askomics:jsmith> rdfg:subGraphOf <urn:sparql:test_askomics>'
        )

    def test_get_admins_emails(self):
        """Test get_admins_emails

        Test the finction with nobody, 1, 2 non admin users, and with 1 and 2 admins users
        """

        self.tps.clean_up()

        security = Security(self.settings, self.request.session, '', '', '',
                            '')

        assert security.get_admins_emails() == []

        self.tps.add_jdoe_in_users()

        assert security.get_admins_emails() == []

        self.tps.add_jsmith_in_users()

        assert security.get_admins_emails() == []

        self.tps.add_admin_in_users()

        assert security.get_admins_emails() == ['*****@*****.**']

        self.tps.add_another_admin_in_users()

        res_emails = security.get_admins_emails()

        assert len(res_emails) == 2
        assert '*****@*****.**' in res_emails
        assert '*****@*****.**' in res_emails

    def test_set_admin(self):
        """Test set_admin"""

        self.tps.clean_up()

        security = Security(self.settings, self.request.session, 'jdoe', '',
                            '', '')

        assert not self.request.session['admin']

        security.set_admin(True)

        assert self.request.session['admin']

    def test_set_galaxy(self):
        """Test set_admin"""

        self.tps.clean_up()

        security = Security(self.settings, self.request.session, 'jdoe', '',
                            '', '')

        security.set_galaxy(True)

        assert self.request.session['galaxy']

    def test_set_blocked(self):
        """Test set_blocked"""

        self.tps.clean_up()

        security = Security(self.settings, self.request.session, 'jdoe', '',
                            '', '')

        assert self.request.session['blocked']

        security.set_blocked(False)

        assert not self.request.session['blocked']

    def test_get_admin_blocked_by_usrnm(self):
        """Test get_admin_blocked_by_username"""

        self.tps.clean_up()

        self.tps.add_jdoe_in_users()
        self.tps.add_admin_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        assert security.get_admin_blocked_by_username() == {
            'blocked': False,
            'admin': False
        }

        security = Security(self.settings, self.request.session, 'king',
                            '*****@*****.**', 'iamadmin', 'iamadmin')

        assert security.get_admin_blocked_by_username() == {
            'blocked': False,
            'admin': True
        }

    def test_get_admin_blocked_by_email(self):
        """Test get_admin_blocked_by_email"""

        self.tps.clean_up()

        self.tps.add_jdoe_in_users()
        self.tps.add_admin_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        assert security.get_admin_blocked_by_email() == {
            'blocked': False,
            'admin': False
        }

        security = Security(self.settings, self.request.session, 'king',
                            '*****@*****.**', 'iamadmin', 'iamadmin')

        assert security.get_admin_blocked_by_email() == {
            'blocked': False,
            'admin': True
        }

    def test_log_user(self):
        """Test log_user"""

        self.tps.clean_up()

        security = Security(self.settings, self.request.session, 'testlog',
                            '*****@*****.**', 'testlog', 'testlog')

        security.log_user(self.request)

        assert self.request.session['username'] == 'testlog'
        assert not self.request.session['admin']
        assert self.request.session['blocked']
        assert self.request.session[
            'graph'] == 'urn:sparql:test_askomics:testlog'

    def test_update_email(self):
        """Test update_mail"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'iamjohndoe', 'iamjohndoe')

        security.update_email()

        assert self.tps.test_row_presence('users', 'email',
                                          ('*****@*****.**', ))
        assert not self.tps.test_row_presence('users', 'email',
                                              ('*****@*****.**', ))

    def test_update_passwd(self):
        """Test update_mail"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        security = Security(self.settings, self.request.session, 'jdoe',
                            '*****@*****.**', 'newpasswd', 'newpasswd')

        security.update_passwd()

        assert self.tps.test_row_presence('users', 'password',
                                          (security.get_sha256_pw(), ))
        assert not self.tps.test_row_presence('users', 'password', (
            '23df582b51c3482b677c8eac54872b8bd0a49bfadc853628b8b8bd4806147b54',
        ))
Example #12
0
class EndpointManagerTests(unittest.TestCase):
    """Test for the ModuleManager class"""

    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = False
        self.request.session['graph'] = "test/nosetest/jdoe"

        SetupTests(self.settings, self.request.session)
        self.tps = InterfaceTpsDb(self.settings, self.request)

    def test_save_endpoint(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        assert endpoint_manager.save_endpoint('endpoint1', 'http://endpoint./sparql') == 1

    def test_enable(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint_manager.enable(endpoint_manager.save_endpoint('endpoint1', 'http://endpoint/sparql'))

        assert self.tps.test_row_presence('endpoints', 'id, name, url, auth, enable, message', (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 1, None))


    def test_disable(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint_id = endpoint_manager.save_endpoint('endpoint1', 'http://endpoint/sparql', isenable=True)

        assert self.tps.test_row_presence('endpoints', 'id, name, url, auth, enable, message', (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 1, None))

        endpoint_manager.disable(endpoint_id, 'message')

        assert self.tps.test_row_presence('endpoints', 'id, name, url, auth, enable, message', (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 0, 'message'))

    def test_disable_by_url(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint_manager.save_endpoint('endpoint1', 'http://endpoint/sparql', isenable=True)

        assert self.tps.test_row_presence('endpoints', 'id, name, url, auth, enable, message', (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 1, None))

        endpoint_manager.disable_by_url('http://endpoint/sparql', 'message')

        assert self.tps.test_row_presence('endpoints', 'id, name, url, auth, enable, message', (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 0, 'message'))

    def test_list_endpoints(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint1 = endpoint_manager.save_endpoint('endpoint1', 'http://endpoint/sparql', isenable=True)
        endpoint2 = endpoint_manager.save_endpoint('endpoint2', 'http://other_endpoint/sparql', isenable=True)

        list_endpoints = endpoint_manager.list_endpoints()

        assert list_endpoints == [{
            'id': 1,
            'name': 'endpoint1',
            'endpoint': 'http://endpoint/sparql',
            'auth': 'BASIC',
            'enable': True,
            'message': None
        },
        {
            'id': 2,
            'name': 'endpoint2',
            'endpoint': 'http://other_endpoint/sparql',
            'auth': 'BASIC',
            'enable': True,
            'message': None
        }]


    def test_list_active_endpoints(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint1 = endpoint_manager.save_endpoint('endpoint1', 'http://endpoint/sparql', isenable=True)
        endpoint2 = endpoint_manager.save_endpoint('endpoint2', 'http://other_endpoint/sparql', isenable=False)

        list_endpoints = endpoint_manager.list_active_endpoints()

        assert list_endpoints == [{
            'id': 1,
            'name': 'endpoint1',
            'endpoint': 'http://endpoint/sparql',
            'auth': 'BASIC',
            'enable': True,
            'message': None
        }]



    def test_remove_endpoint(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        endpoint_manager = EndpointManager(self.settings, self.request.session)
        endpoint = endpoint_manager.save_endpoint('endpoint1', 'http://endpoint/sparql')
        
        assert self.tps.test_row_presence('endpoints', 'id, name, url, auth, enable, message', (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 0, None))

        endpoint_manager.remove_endpoint(endpoint)

        assert not self.tps.test_row_presence('endpoints', 'id, name, url, auth, enable, message', (1, 'endpoint1', 'http://endpoint/sparql', 'BASIC', 0, None))
Example #13
0
class AskViewTests(unittest.TestCase):
    """Test for Askview

    Contain all the tests for the askView class
    """
    def setUp(self):
        """Set up the configuration to access the triplestore

        Use the config file test.virtuoso.ini to not interfere with
        production data
        """

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.settings['askomics.upload_user_data_method'] = 'insert'

        self.request = testing.DummyRequest()

        self.config = testing.setUp(request=self.request)
        self.config.add_route('load_data_into_graph', '/load_data_into_graph')
        self.config.scan()

        self.request.session['username'] = '******'
        self.request.session['blocked'] = False
        self.request.session['group'] = 'base'
        self.request.session['graph'] = 'urn:sparql:test_askomics:jdoe'

        self.request.host_url = 'http://*****:*****@example.com',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        data = self.askview.signup()

        assert data == {
            'error': [],
            'blocked': False,
            'admin': True,
            'username': '******',
            'galaxy': False
        }

    def test_checkuser(self):
        """Test checkuser method"""

        self.tps.clean_up()

        data = self.askview.checkuser()

        assert data == {
            'admin': False,
            'username': '******',
            'blocked': False,
            'galaxy': False
        }

    def test_logout(self):
        """Test logout method"""

        self.tps.clean_up()

        self.askview.logout()

        assert self.request.session == {}

    def test_login(self):
        """Test login method"""

        self.tps.clean_up()

        #first, create a user
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        self.askview.signup()

        # then, logout this user
        self.askview.logout()

        # and then, test login
        self.request.json_body = {
            'username_email': 'jdoe',
            'password': '******'
        }

        data = self.askview.login()

        assert data == {
            'blocked': False,
            'admin': True,
            'error': [],
            'username': '******',
            'galaxy': False
        }

    def test_login_api(self):
        """Test login_api method"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        # then, try to log with API key
        self.request.json_body = {'apikey': 'jdoe_apikey'}

        data = self.askview.login_api()

        assert data == {
            'admin': False,
            'blocked': False,
            'username': '******',
            'error': ''
        }

    def test_login_api_gie(self):
        """Test login_api_gie method"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        # then, try to log with API key
        self.request.json_body = {'apikey': 'jdoe_apikey'}

        self.askview.login_api()

    def test_get_users_infos(self):
        """Test get_users_infos"""

        self.tps.clean_up()
        # first test with non admin
        try:
            data = self.askview.get_users_infos()
            assert False
        except Exception as e:
            assert True

        # then, is user is admin
        self.request.session['admin'] = True

        data = self.askview.get_users_infos()

        assert data == {'result': []}  #result is empty cause there is no user

        #test with user
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        self.askview.signup()

        data = self.askview.get_users_infos()

        assert data == {
            'result': [{
                'username': '******',
                'email': '*****@*****.**',
                'admin': True,
                'blocked': False
            }],
            'error': [],
            'username':
            '******',
            'admin':
            True,
            'blocked':
            False,
            'galaxy':
            False
        }

    def test_lock_user(self):
        """Test lock_user method"""

        self.tps.clean_up()

        self.request.json_body = {'username': '******', 'lock': True}

        # first test with non admin

        try:
            data = self.askview.lock_user()
            assert False
        except Exception as e:
            assert True

        # then, is user is admin
        self.request.session['admin'] = True

        data = self.askview.lock_user()

        assert data == 'success'

    def test_set_admin(self):
        """Test set_admin_method"""

        self.tps.clean_up()

        self.request.json_body = {'username': '******', 'admin': True}

        try:
            data = self.askview.set_admin()
        except Exception as e:
            assert True

        # then, is user is admin
        self.request.session['admin'] = True

        data = self.askview.set_admin()

        assert data == 'success'

    def test_delete_user(self):
        """Test delete_user method"""

        self.tps.clean_up()

        # Insert a user
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        self.askview.signup()

        # test the deletion
        self.request.json_body = {
            'username': '******',
            'passwd': 'iamjohndoe',
            'passwd_conf': 'iamjohndoe'
        }

        self.request.session['blocked'] = False
        self.request.session['admin'] = False
        self.request.session['username'] = '******'

        data = self.askview.delete_user()

        assert data == 'success'

    def test_get_my_infos(self):
        """Test get_my_infos"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        # get my infos
        data = self.askview.get_my_infos()

        assert data == {
            'email': '*****@*****.**',
            'username': '******',
            'apikey': 'jdoe_apikey',
            'blocked': False,
            'admin': False
        }

    def test_update_mail(self):
        """Test update_mail"""

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        # And change my email
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**'
        }

        data = self.askview.update_mail()

        assert data == {'success': 'success'}

    def test_update_passwd(self):
        """Test update_passwd method"""

        self.tps.clean_up()

        # First, insert me
        self.request.json_body = {
            'username': '******',
            'email': '*****@*****.**',
            'password': '******',
            'password2': 'iamjohndoe'
        }

        self.askview.signup()

        # And update my password
        self.request.json_body = {
            'username': '******',
            'current_passwd': 'iamjohndoe',
            'passwd': 'mynewpassword',
            'passwd2': 'mynewpassword',
        }

        data = self.askview.update_passwd()

        assert data == {
            'error': [],
            'admin': True,
            'blocked': False,
            'username': '******',
            'success': 'success',
            'galaxy': False
        }
Example #14
0
class LocalAuthTests(unittest.TestCase):
    def setUp(self):
        """Set up the settings and session"""

        self.settings = get_appsettings('configs/tests.ini', name='main')
        self.request = testing.DummyRequest()
        self.request.session['username'] = '******'
        self.request.session['group'] = 'base'
        self.request.session['admin'] = False
        self.request.session['blocked'] = True

        SetupTests(self.settings, self.request.session)

        self.tps = InterfaceTpsDb(self.settings, self.request)

    def test_set_auth_type(self):

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.auth_type == "local"

        local_auth.set_auth_type('ldap')

        assert local_auth.auth_type == "ldap"

    def test_get_galaxy_infos(self):

        self.tps.clean_up()
        self.tps.add_galaxy_account()

        local_auth = LocalAuth(self.settings, self.request.session)
        galaxy_infos = local_auth.get_galaxy_infos(1)

        assert galaxy_infos == {
            'url': 'http://locahost/galaxy',
            'key': 'fake_api_key'
        }

    def test_get_user_infos(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_jsmith_in_users()
        self.tps.add_galaxy_account()

        local_auth = LocalAuth(self.settings, self.request.session)

        user_infos_jdoe_username = local_auth.get_user_infos('jdoe')
        user_infos_jdoe_email = local_auth.get_user_infos('*****@*****.**')
        user_infos_jsmith_username = local_auth.get_user_infos('jsmith')
        assert user_infos_jdoe_username == user_infos_jdoe_email
        assert user_infos_jdoe_username == {
            'id': 1,
            'username': '******',
            'email': '*****@*****.**',
            'password':
            '******',
            'salt': '00000000000000000000',
            'admin': False,
            'blocked': False,
            'apikey': 'jdoe_apikey',
            'galaxy': {
                'url': 'http://locahost/galaxy',
                'key': 'fake_api_key'
            },
            'auth_type': 'local'
        }
        assert user_infos_jsmith_username == {
            'id': 2,
            'username': '******',
            'email': '*****@*****.**',
            'password':
            '******',
            'salt': '00000000000000000000',
            'admin': False,
            'blocked': False,
            'apikey': 'jsmith_apikey',
            'galaxy': None,
            'auth_type': 'local'
        }

    def test_get_user_infos_api_key(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_jsmith_in_users()
        self.tps.add_galaxy_account()

        local_auth = LocalAuth(self.settings, self.request.session)

        user_infos_jdoe = local_auth.get_user_infos_api_key('jdoe_apikey')
        user_infos_jsmith = local_auth.get_user_infos_api_key('jsmith_apikey')
        assert user_infos_jdoe == {
            'id': 1,
            'username': '******',
            'email': '*****@*****.**',
            'password':
            '******',
            'salt': '00000000000000000000',
            'admin': False,
            'blocked': False,
            'apikey': 'jdoe_apikey',
            'galaxy': {
                'url': 'http://locahost/galaxy',
                'key': 'fake_api_key'
            },
            'auth_type': 'local'
        }
        assert user_infos_jsmith == {
            'id': 2,
            'username': '******',
            'email': '*****@*****.**',
            'password':
            '******',
            'salt': '00000000000000000000',
            'admin': False,
            'blocked': False,
            'apikey': 'jsmith_apikey',
            'galaxy': None,
            'auth_type': 'local'
        }

    def test_is_username_in_db(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.is_username_in_db('jdoe')
        assert not local_auth.is_username_in_db('jsmith')

    def test_is_email_in_db(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.is_email_in_db('*****@*****.**')
        assert not local_auth.is_email_in_db('*****@*****.**')

    def test_is_user_in_db(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.is_user_in_db('jdoe')
        assert local_auth.is_user_in_db('*****@*****.**')
        assert not local_auth.is_user_in_db('jsmith')
        assert not local_auth.is_user_in_db('*****@*****.**')

    def test_get_auth_type(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_ldap_jdoe_user_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.get_auth_type('jdoe') == 'local'
        assert local_auth.get_auth_type('ldap_jdoe') == 'ldap'

    def test_persist_user(self):

        self.tps.clean_up()

        local_auth = LocalAuth(self.settings, self.request.session)
        local_auth.persist_user('jdoe', '*****@*****.**', 'jdoe')
        local_auth.persist_user('jsmith', '*****@*****.**', 'jsmith')
        local_auth.set_auth_type('ldap')
        local_auth.persist_user('ldap_jdoe', '*****@*****.**')

        assert self.tps.test_row_presence(
            'users', 'username, email, password',
            ('ldap_jdoe', '*****@*****.**', None))
        assert self.tps.test_row_presence('users', 'username, email',
                                          ('jdoe', '*****@*****.**'))
        assert self.tps.test_row_presence('users', 'username, email',
                                          ('jsmith', '*****@*****.**'))

    def test_get_number_of_users(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_jsmith_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.get_number_of_users() == 2

    def test_authenticate_user(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.authenticate_user('jdoe', 'iamjohndoe')
        assert local_auth.authenticate_user('*****@*****.**', 'iamjohndoe')
        assert not local_auth.authenticate_user('jsmith', 'iamjanesmith')
        assert not local_auth.authenticate_user('*****@*****.**',
                                                'iamjanesmith')

    def test_authenticate_user_with_username(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.authenticate_user_with_username('jdoe', 'iamjohndoe')
        assert not local_auth.authenticate_user_with_username(
            'jsmith', 'iamjanesmith')

    def test_authenticate_user_with_email(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert local_auth.authenticate_user_with_email('*****@*****.**',
                                                       'iamjohndoe')
        assert not local_auth.authenticate_user_with_email(
            '*****@*****.**', 'iamjanesmith')

    def test_create_user_graph(self):

        local_auth = LocalAuth(self.settings, self.request.session)
        local_auth.create_user_graph('jdoe')

        assert self.tps.test_triple_presence(
            'urn:sparql:test_askomics',
            '<urn:sparql:test_askomics:jdoe> rdfg:subGraphOf <urn:sparql:test_askomics>'
        )
        assert not self.tps.test_triple_presence(
            'urn:sparql:test_askomics',
            '<urn:sparql:test_askomics:jsmith> rdfg:subGraphOf <urn:sparql:test_askomics>'
        )

    def test_renew_apikey(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence(
            'users', 'username, email, apikey',
            ('jdoe', '*****@*****.**', 'jdoe_apikey'))

        local_auth.renew_apikey('jdoe')

        assert not self.tps.test_row_presence(
            'users', 'username, email, apikey',
            ('jdoe', '*****@*****.**', 'jdoe_apikey'))
        assert self.tps.test_row_presence('users', 'username, email',
                                          ('jdoe', '*****@*****.**'))

    def test_add_galaxy(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        galaxy_url = self.settings['askomics.testing_galaxy_url']
        galaxy_key = self.settings['askomics.testing_galaxy_key']

        local_auth = LocalAuth(self.settings, self.request.session)

        local_auth.add_galaxy(galaxy_url, galaxy_key, 1)

        assert self.tps.test_row_presence('galaxy_accounts', '*',
                                          (1, 1, galaxy_url, galaxy_key))

    def test_get_all_users_infos(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()
        self.tps.add_ldap_jdoe_user_in_users()
        self.tps.add_jsmith_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)
        all_users = local_auth.get_all_users_infos()

        # Get dir size
        path = self.settings['askomics.files_dir'] + '/jdoe'
        size = 0
        for dirpath, dirnames, filenames in os.walk(path):
            for f in filenames:
                fp = os.path.join(dirpath, f)
                size += os.path.getsize(fp)
        hsize = humanize.naturalsize(size)

        assert all_users == [{
            'ldap': False,
            'username': '******',
            'email': '*****@*****.**',
            'admin': False,
            'blocked': False,
            'gurl': None,
            'nquery': 0,
            'nintegration': 0,
            'dirsize': size,
            'hdirsize': hsize
        }, {
            'ldap': False,
            'username': '******',
            'email': '*****@*****.**',
            'admin': False,
            'blocked': False,
            'gurl': None,
            'nquery': 0,
            'nintegration': 0,
            'dirsize': 0,
            'hdirsize': '0 Bytes'
        }, {
            'ldap': True,
            'username': '******',
            'email': '*****@*****.**',
            'admin': False,
            'blocked': False,
            'gurl': None,
            'nquery': 0,
            'nintegration': 0,
            'dirsize': 0,
            'hdirsize': '0 Bytes'
        }]

    def test_lock_user(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username, admin, blocked',
                                          ('jdoe', 'false', 'false'))

        local_auth.lock_user('true', 'jdoe')

        assert self.tps.test_row_presence('users', 'username, admin, blocked',
                                          ('jdoe', 'false', 'true'))

    def test_admin_user(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username, admin, blocked',
                                          ('jdoe', 'false', 'false'))

        local_auth.admin_user('true', 'jdoe')

        assert self.tps.test_row_presence('users', 'username, admin, blocked',
                                          ('jdoe', 'true', 'false'))

    def test_delete_user(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username', ('jdoe', ))

        local_auth.delete_user('jdoe')

        assert not self.tps.test_row_presence('users', 'username', ('jdoe', ))

    def test_update_email(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username, email',
                                          ('jdoe', '*****@*****.**'))

        local_auth.update_email('jdoe', '*****@*****.**')

        assert self.tps.test_row_presence('users', 'username, email',
                                          ('jdoe', '*****@*****.**'))

    def test_update_password(self):

        self.tps.clean_up()
        self.tps.add_jdoe_in_users()

        local_auth = LocalAuth(self.settings, self.request.session)

        assert self.tps.test_row_presence('users', 'username, password, salt', (
            'jdoe',
            'f49d76161eb1617568fedf0a0adc92532cc81c1a2626ec0e2d5fa36bd600f55b17f599a4a343a5ccdc907a2831db70c7a390a9f96afbf346190e6fe3d6ed836f',
            '00000000000000000000'))

        local_auth.update_password('jdoe', 'newpass')

        assert not self.tps.test_row_presence(
            'users', 'username, password, salt',
            ('jdoe',
             'f49d76161eb1617568fedf0a0adc92532cc81c1a2626ec0e2d5fa36bd600f55b17f599a4a343a5ccdc907a2831db70c7a390a9f96afbf346190e6fe3d6ed836f',
             '00000000000000000000'))

    def test_get_random_string(self):

        local_auth = LocalAuth(self.settings, self.request.session)

        assert len(local_auth.get_random_string(20)) == 20