def setUp(self): self.client = app.test_client() with app.app_context(): self.headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' } try: app.logger.info('Setting up user...') with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() self.user_details = service.create_new_user(self.user_data) self.user_details_id = self.user_data['user_details_id'] self.ldap_id = self.user_data['ldap_id'] self.api_key = self.user_data['api_key'] self.agreement_id = helpers.insert_licence_agreement( self.user_details_id) app.logger.info('Done, id: {}'.format(self.user_details_id)) except Exception: app.logger.error('Setup failed') traceback.print_exc() self.tearDown()
def setUp(self): self.client = app.test_client() with app.app_context(): self.headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' } self.dataset_name = None self.dataset_list = [] self.public_resources, self.resources = (False, ) * 2 try: app.logger.info('Getting dataset info...') with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() self.dataset = service.get_dataset_by_name('ccod') self.file_name = self.dataset['resources'][0]['file_name'] app.logger.info('Done...') except Exception: app.logger.error('Setup failed') traceback.print_exc() self.tearDown()
def setUp(self): with app.app_context(): self.app = app.test_client() self.url = current_app.config["VERIFICATION_API_URL"] self.version = current_app.config["VERIFICATION_API_VERSION"] self.timeout = current_app.config["DEFAULT_TIMEOUT"] self.error_msg = 'Test error message'
def setUp(self): self.client = app.test_client() with app.app_context(): self.headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' }
def insert_user_row(user_data): with app.app_context(): user = UserDetails(user_data) db.session.add(user) db.session.commit() user_details_id = user.user_details_id db.session.close() return user_details_id
def test_acknowledge(self, mock_post): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_post.return_value.text = 'Success' mock_post.return_value.status_code = 200 response = AccountAPI.acknowledge(self, '1234-567-890') assert response == {'message': 'acknowledged'}
def test_get(self, mock_get): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_get.return_value.json.return_value = 'Success' mock_get.return_value.status_code = 200 response = AccountAPI.get(self, '1234-567-890') self.assertEqual(response, 'Success')
def test_update_groups_for_ldap_error(self, mock_account): error = ApplicationError( *errors.get("ulapd_api", "ACCOUNT_API_HTTP_ERROR")) mock_account.return_value.update_groups.side_effect = error with app.app_context() as ac: update_groups_retry = current_app.config["UPDATE_GROUPS_RETRY"] ac.g.trace_id = None update_groups_for_ldap('112-122', {'nps': True}) self.assertEqual(mock_account.return_value.update_groups.call_count, int(update_groups_retry))
def test_update_groups(self, mock_patch): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_patch.return_value.text = 'Success' mock_patch.return_value.status_code = 200 response = AccountAPI.update_groups(self, {'nps_sample': True}, '11-22') assert response == {'message': 'groups updated'}
def test_create(self, mock_create, mock_post): data = {'email': '*****@*****.**'} mock_create.return_value = {'email': '*****@*****.**'} with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_post.return_value.json.return_value = 'Success' mock_post.return_value.status_code = 200 response = AccountAPI.create(self, data) self.assertEqual(response, 'Success')
def test_create(self, mock_create, mock_post): data = {'user_id': '1234'} mock_create.return_value = {'user_id': '1234'} with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_post.return_value.json.return_value = 'Success' mock_post.return_value.status_code = 200 response = VerificationAPI.create(self, data) self.assertEqual(response, 'Success')
def insert_licence_agreement(user_details_id): with app.app_context(): terms_data = { 'user_details_id': user_details_id, 'licence_id': 'ccod' } user_terms = UserTermsLink(terms_data) db.session.add(user_terms) db.session.commit() agreement_id = user_terms.user_terms_link_id db.session.close() return agreement_id
def test_handle_role(self, mock_post): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_post.return_value.text = 'Success' mock_post.return_value.status_code = 200 response = AccountAPI.handle_role(self, { 'groups': { 'nps_sample': True }, 'ldap_id': '11-22' }) assert response == {'message': 'success'}
def test_get_with_timeout(self, mock_get): mock_get.side_effect = Timeout(self.error_msg) with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): with self.assertRaises(ApplicationError) as context: AccountAPI.get(self, '1234-567-890') self.assertTrue(ApplicationError in str(context.exception)) self.assertEqual( context.exception.message, 'Connection to account_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E711') self.assertEqual(context.exception.http_code, 500)
def test_update_groups_with_timeout(self, mock_patch): mock_patch.side_effect = Timeout(self.error_msg) with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): with self.assertRaises(ApplicationError) as context: AccountAPI.update_groups(self, {'nps_sample': True}, '11-22') self.assertTrue(ApplicationError in str(context.exception)) self.assertEqual( context.exception.message, 'Connection to account_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E711') self.assertEqual(context.exception.http_code, 500)
def test_get_connection_error(self, mock_get): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_get.side_effect = ConnectionError(self.error_msg) with self.assertRaises(ApplicationError) as context: AccountAPI.get(self, '1234-567-890') self.assertEqual( context.exception.message, 'Encountered an error connecting to account_api: {}'. format(self.error_msg)) self.assertEqual(context.exception.code, 'E710') self.assertEqual(context.exception.http_code, 500)
def insert_user_activity(user_details_id, file, dataset_id): with app.app_context(): activity_data = { 'user_details_id': user_details_id, 'activity_type': 'download', 'ip_address': '172.10.0.10', 'api': False, 'file': file, 'dataset_id': dataset_id } activity = Activity(activity_data) db.session.add(activity) db.session.commit() activity_id = activity.activity_id db.session.close() return activity_id
def test_get_http_error(self, mock_get): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_get.side_effect = HTTPError(self.error_msg) with self.assertRaises(ApplicationError) as context: AccountAPI.get(self, '1234-567-890') self.assertEqual( context.exception.message, 'Received the following response from account_api: {}'. format(self.error_msg)) self.assertEqual(context.exception.code, 'E709') self.assertEqual(context.exception.http_code, 500)
def tearDown(self): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() if self.ldap_id is None: app.logger.info('No ldap to teardown...') else: app.logger.info('Removing user from ldap...') url = '{}/users/{}'.format(self.account_url, self.ldap_id) g.requests.delete(url, headers=self.headers) if self.user_details_id is None: app.logger.info('No ulapd to teardown...') else: app.logger.info('Removing user from ulapd...') service.delete_user(self.user_details_id)
def test_update_groups_http_error(self, mock_patch): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_patch.side_effect = HTTPError(self.error_msg) with self.assertRaises(ApplicationError) as context: AccountAPI.update_groups(self, {'nps_sample': True}, '11-22') self.assertEqual( context.exception.message, 'Received the following response from account_api: {}'. format(self.error_msg)) self.assertEqual(context.exception.code, 'E709') self.assertEqual(context.exception.http_code, 500)
def test_create_with_timeout(self, mock_create, mock_post): mock_post.side_effect = Timeout(self.error_msg) data = {'foo': 'bar'} mock_create.return_value = {'foo': 'bar'} with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): with self.assertRaises(ApplicationError) as context: VerificationAPI.create(self, data) self.assertTrue(ApplicationError in str(context.exception)) self.assertEqual( context.exception.message, 'Connection to verification_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E714') self.assertEqual(context.exception.http_code, 500)
def test_create_connection_error(self, mock_create, mock_post): data = {'foo': 'bar'} mock_create.return_value = {'foo': 'bar'} with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_post.side_effect = ConnectionError(self.error_msg) with self.assertRaises(ApplicationError) as context: VerificationAPI.create(self, data) self.assertEqual( context.exception.message, 'Encountered an error connecting to verification_api: {}'. format(self.error_msg)) self.assertEqual(context.exception.code, 'E713') self.assertEqual(context.exception.http_code, 500)
def test_create_http_error(self, mock_create, mock_post): data = {'foo': 'bar'} mock_create.return_value = {'foo': 'bar'} with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_post.side_effect = HTTPError(self.error_msg) with self.assertRaises(ApplicationError) as context: VerificationAPI.create(self, data) self.assertEqual( context.exception.message, 'Received the following response from verification_api: {}' .format(self.error_msg)) self.assertEqual(context.exception.code, 'E712') self.assertEqual(context.exception.http_code, 500)
def test_handle_role_connection_error(self, mock_post): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): mock_post.side_effect = ConnectionError(self.error_msg) with self.assertRaises(ApplicationError) as context: AccountAPI.handle_role(self, { 'groups': { 'nps_sample': True }, 'ldap_id': '11-22' }) self.assertEqual( context.exception.message, 'Encountered an error connecting to account_api: {}'. format(self.error_msg)) self.assertEqual(context.exception.code, 'E710') self.assertEqual(context.exception.http_code, 500)
def test_update_groups_for_ldap(self, mock_account): mock_account.return_value = MagicMock() with app.app_context() as ac: ac.g.trace_id = None update_groups_for_ldap('112-122', {'nps': True}) mock_account.assert_called_once()
class TestActivity(unittest.TestCase): URL_ULAPD_PREFIX = '/v1' directory = path.dirname(__file__) user_data = helpers.get_json_from_file(directory, 'data/user_data.json') with app.app_context(): account_url = '{}/{}'.format(current_app.config['ACCOUNT_API_URL'], current_app.config['ACCOUNT_API_VERSION']) def setUp(self): self.client = app.test_client() with app.app_context(): self.headers = {'Accept': 'application/json', 'Content-Type': 'application/json'} try: app.logger.info('Setting up user...') with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() self.user_details = service.create_new_user(self.user_data) self.user_details_id = self.user_data['user_details_id'] self.ldap_id = self.user_data['ldap_id'] self.api_key = self.user_data['api_key'] self.agreement_id = helpers.insert_licence_agreement(self.user_details_id) self.activity_id_1 = helpers.insert_user_activity(self.user_details_id, 'CCOD_COU_2019_09.zip', 'ccod') self.activity_id_2 = helpers.insert_user_activity(self.user_details_id, 'CCOD_COU_2019_10.zip', 'ccod') self.activity_id_list = [self.activity_id_1, self.activity_id_2] app.logger.info('Done, id: {}'.format(self.user_details_id)) except Exception: app.logger.error('Setup failed') traceback.print_exc() self.tearDown() def tearDown(self): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() if self.ldap_id is None: app.logger.info('No ldap to teardown...') else: app.logger.info('Removing user from ldap...') url = '{}/users/{}'.format(self.account_url, self.ldap_id) g.requests.delete(url, headers=self.headers) if self.user_details_id is None: app.logger.info('No ulapd to teardown...') else: app.logger.info('Removing user from ulapd...') service.delete_user(self.user_details_id) def test_get_activity(self): url = '{}/activities/{}'.format(self.URL_ULAPD_PREFIX, self.user_details_id) response = self.client.get(url, headers=self.headers) self.assertEqual(200, response.status_code) response_body = response.get_json() for results in response_body: self.assertIn(results['activity_id'], self.activity_id_list) if results['activity_id'] == self.activity_id_1: self.assertEqual(results['file'], 'CCOD_COU_2019_09.zip') else: self.assertEqual(results['file'], 'CCOD_COU_2019_10.zip') def test_add_activity(self): activity_data = { 'dataset_id': 'ccod', 'user_details_id': self.user_details_id, 'activity_type': 'download', 'ip_address': 'some.ip.address', 'api': False, 'file': 'CCOD_COU_2019_09.zip' } url = '{}/activities'.format(self.URL_ULAPD_PREFIX) response = self.client.post(url, data=json.dumps(activity_data), headers=self.headers) self.assertEqual(200, response.status_code) response_body = response.get_json() self.assertIn('activity_id', response_body)
class TestUsers(unittest.TestCase): URL_ULAPD_PREFIX = '/v1' directory = path.dirname(__file__) user_data = helpers.get_json_from_file(directory, 'data/user_data.json') with app.app_context(): account_url = '{}/{}'.format(current_app.config['ACCOUNT_API_URL'], current_app.config['ACCOUNT_API_VERSION']) def setUp(self): self.client = app.test_client() with app.app_context(): self.headers = { 'Accept': 'application/json', 'Content-Type': 'application/json' } try: app.logger.info('Setting up user...') with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() self.user_details = service.create_new_user(self.user_data) self.user_details_id = self.user_data['user_details_id'] self.ldap_id = self.user_data['ldap_id'] self.api_key = self.user_data['api_key'] self.agreement_id = helpers.insert_licence_agreement( self.user_details_id) app.logger.info('Done, id: {}'.format(self.user_details_id)) except Exception: app.logger.error('Setup failed') traceback.print_exc() self.tearDown() def tearDown(self): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() if self.ldap_id is None: app.logger.info('No ldap to teardown...') else: app.logger.info('Removing user from ldap...') url = '{}/users/{}'.format(self.account_url, self.ldap_id) g.requests.delete(url, headers=self.headers) if self.user_details_id is None: app.logger.info('No ulapd to teardown...') else: app.logger.info('Removing user from ulapd...') service.delete_user(self.user_details_id) def test_get_user(self): url = '{}/users/user_details_id/{}'.format(self.URL_ULAPD_PREFIX, self.user_details_id) response = self.client.get(url, headers=self.headers) self.assertEqual(200, response.status_code) response_body = response.get_json() self.assertEqual(response_body['user_details']['user_details_id'], self.user_details_id) self.assertIn('user_details', response_body) self.assertIn('datasets', response_body) def test_get_user_licences(self): url = '{}/users/licence/{}'.format(self.URL_ULAPD_PREFIX, self.user_details_id) response = self.client.get(url, headers=self.headers) self.assertEqual(200, response.status_code) response_body = response.get_json() self.assertEqual(response_body[0]['user_terms_link_id'], self.agreement_id) self.assertEqual(response_body[0]['user_details_id'], self.user_details_id) def test_get_user_licence(self): url = '{}/users/licence/{}/ccod'.format(self.URL_ULAPD_PREFIX, self.user_details_id) response = self.client.get(url, headers=self.headers) self.assertEqual(200, response.status_code) response_body = response.get_json() self.assertEqual(response_body['valid_licence'], True) def test_add_user_licence(self): licence_data = { 'user_details_id': self.user_details_id, 'licence_id': 'ocod' } url = '{}/users/licence'.format(self.URL_ULAPD_PREFIX) response = self.client.post(url, data=json.dumps(licence_data), headers=self.headers) self.assertEqual(201, response.status_code) response_body = response.get_json() self.assertEqual(response_body['user_details_id'], self.user_details_id) self.assertEqual(response_body['licence_id'], 'ocod') helpers.delete_user_agreement(self.user_details_id, 'ocod') def test_get_user_dataset_activity(self): helpers.insert_user_activity(self.user_details_id, 'CCOD_COU_2019_09.zip', 'ccod') helpers.insert_user_activity(self.user_details_id, 'CCOD_COU_2019_10.zip', 'ccod') url = '{}/users/dataset-activity/{}'.format(self.URL_ULAPD_PREFIX, self.user_details_id) response = self.client.get(url, headers=self.headers) self.assertEqual(200, response.status_code) response_body = response.get_json() result = list( filter(lambda dataset: dataset['name'] == 'ccod', response_body)) self.assertEqual(result[0]['download_history'][0]['file'], 'CCOD_COU_2019_10.zip') self.assertEqual(result[0]['download_history'][1]['file'], 'CCOD_COU_2019_09.zip') self.assertEqual(result[0]['licence_agreed'], True) def test_create_user(self): self.tearDown() url = '{}/users'.format(self.URL_ULAPD_PREFIX) response = self.client.post(url, data=json.dumps(self.user_data), headers=self.headers) self.assertEqual(201, response.status_code) response_body = response.get_json() self.user_details_id = response_body['user_details_id'] self.ldap_id = response_body['ldap_id'] self.assertEqual(response_body['user_details_id'], self.user_details_id) def test_update_user_contact_preference(self): contact_data = { 'user_id': self.user_details_id, 'contactable': True, 'contact_preferences': ['email', 'text'] } url = '{}/users/contact_preference'.format(self.URL_ULAPD_PREFIX) response = self.client.patch(url, data=json.dumps(contact_data), headers=self.headers) self.assertEqual(200, response.status_code) response_body = response.get_json() self.assertEqual(response_body['contactable'], True) self.assertEqual(response_body['contact_preferences'], ['email', 'text']) def test_delete_user(self): url = '{}/users/{}'.format(self.URL_ULAPD_PREFIX, self.user_details_id) response = self.client.delete(url, headers=self.headers) self.assertEqual(200, response.status_code) response_body = response.get_json() self.assertEqual(response_body['user_id'], str(self.user_details_id)) self.assertEqual(response_body['message'], 'user deleted') self.user_details_id = None def test_update_api_key(self): url = '{}/users/{}/update_api_key'.format(self.URL_ULAPD_PREFIX, self.user_details_id) response = self.client.post(url, headers=self.headers) self.assertEqual(200, response.status_code) response_body = response.get_json() self.assertNotEqual(response_body['api_key'], self.api_key)
def delete_licence(licence_name): with app.app_context(): licence = Licence.get_licence_by_licence_name(licence_name) db.session.delete(licence) db.session.commit() db.session.close()
def delete_dataset(name): with app.app_context(): dataset = Dataset.get_dataset_by_name(name) db.session.delete(dataset) db.session.commit() db.session.close()
def delete_user_agreement(user_details_id, lincence_id): with app.app_context(): UserTermsLink.delete_user_licence_agreement(user_details_id, lincence_id)