def insert_dst_case(case_data): with app.app_context(): case = Case(case_data) db.session.add(case) db.session.commit() case_id = case.verification_id db.session.close()
def test_insert_metric_event_success(self, mock_create_payload, mock_metric_api, *_): mock_metric_api.add_event.return_value = {'foo': 'bar'} with app.app_context() as ac: ac.g.trace_id = None insert_metric_event('dst_action_approved', self.payload) mock_create_payload.assert_called_once()
def test_get_user_dataset_access(self, mock_get): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): dataset_access = [{ 'name': 'res_cov', 'title': 'Restrictive Covenants', 'licences': { 'res_cov_direct': { 'title': 'Direct Use', 'agreed': True } } }] mock_get.return_value.json.return_value = dataset_access mock_get.return_value.status_code = 200 response = UlapdAPI.get_user_dataset_access(self, 1) mock_get.assert_called_once() mock_get.assert_called_with( '{0}/users/dataset-access/{1}'.format(self.url, 1), headers=self.headers, timeout=self.timeout) self.assertEqual(response, dataset_access)
def test_get_dataset_activity(self, mock_get): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): dataset_activity = [{ "download_history": [], "id": 356, "licence_agreed": False, "name": "ccod", "private": False, "title": "UK companies that own property in England and Wales" }] mock_get.return_value.json.return_value = dataset_activity mock_get.return_value.status_code = 200 response = UlapdAPI.get_dataset_activity(self, 1) mock_get.assert_called_once() mock_get.assert_called_with( '{0}/users/dataset-activity/{1}'.format(self.url, 1), headers=self.headers, timeout=self.timeout) self.assertEqual(response, dataset_activity)
def test_update_dataset_access(self, mock_post): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): input_data = { 'user_details_id': 1, 'licences': [{ 'licence_id': 'nps', 'agreed': True }] } mock_post.return_value.json.return_value = {'nps': True} mock_post.return_value.status_code = 200 response = UlapdAPI.update_dataset_access(self, input_data) mock_post.assert_called_once() mock_post.assert_called_with('{0}/users/licence'.format( self.url), json=input_data, headers=self.headers, timeout=self.timeout) self.assertEqual(response, {'nps': True})
def setUp(self): with app.app_context(): self.app = app.test_client() self.url = current_app.config["ACCOUNT_API_URL"] self.version = current_app.config["ACCOUNT_API_VERSION"] self.timeout = current_app.config["DEFAULT_TIMEOUT"] self.error_msg = 'Test error message' self.key = current_app.config['MASTER_API_KEY']
def teardown_dst_case(case_id): if case_id is None: return with app.app_context(): case = Case.query.filter_by(verification_id=case_id).first() db.session.delete(case) db.session.commit() db.session.close()
def test_add_event_success(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 = MetricAPI.add_event(self, self.payload) self.assertEqual(response, {'message': 'event added'})
def test_update(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 = UlapdAPI.update(self, self.updated_data) assert response == {'message': 'user updated'}
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, '1234-567-890', {"nps": True}) self.assertEqual(response, {'message': 'groups updated'})
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_close(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.close(self, '1234-567-890', '0987-654-321', 'customer') assert response == {'message': 'closed'}
def test_decline(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.decline(self, '1234-567-890', 'Failed company checks', 'Reapply', '1') assert response == {'message': 'declined'}
def test_approve(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.approve(self, '1234-567-890') assert response == {'message': 'approved'}
def setUp(self): with app.app_context(): self.app = app.test_client() self.url = current_app.config["ULAPD_API_URL"] self.timeout = current_app.config["DEFAULT_TIMEOUT"] self.error_msg = 'Test error message' self.updated_data = {'user_id': '123', 'contactable': True} self.headers = { "Content-Type": "application/json", "Accept": "application/json" }
def test_insert_metric_event_error(self, mock_create_payload, mock_metric_api, *_): mock_create_payload.return_value = {'foo': 'bar'} error = ApplicationError( *errors.get("verification_api", "METRIC_API_HTTP_ERROR")) mock_metric_api.return_value.add_event.side_effect = error with app.app_context() as ac: metric_retry = current_app.config["METRIC_RETRY"] ac.g.trace_id = None insert_metric_event('dst_action_approved', self.payload) self.assertEqual(mock_metric_api.return_value.add_event.call_count, int(metric_retry))
def _setup_service_user(self): with app.app_context() as ac: ac.g.requests = requests.Session() with app.test_request_context(): url = self.ulapd_url + '/users' data = json.dumps(self.ulapd_user) ulapd_response = g.requests.post(url, data=data, headers=self.headers) self.assertEqual(201, ulapd_response.status_code) ulapd_body = json.loads(ulapd_response.text) self.ldap_id = ulapd_body['ldap_id'] self.ulapd_id = str(ulapd_body['user_details_id']) self.case_data['user_id'] = self.ulapd_id
def test_approve_with_timeout(self, mock_post): mock_post.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.approve(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, 'E503') self.assertEqual(context.exception.http_code, 500)
def setUp(self): with app.app_context(): self.app = app.test_client() self.url = current_app.config["METRIC_API_URL"] self.timeout = current_app.config["DEFAULT_TIMEOUT"] self.error_msg = "Test error" self.payload = { 'registration_data': { 'user_id': '123-456-abc', 'status': 'Pending', 'user_type': 'organisation-uk' } }
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, 'E501') 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, 'E502') self.assertEqual(context.exception.http_code, 500)
def test_add_event_http_error(self, mock_post): mock_post.side_effect = HTTPError(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: MetricAPI.add_event(self, self.payload) self.assertEqual( context.exception.message, 'Received the following response from dps_metric_api: {}'. format(self.error_msg)) self.assertEqual(context.exception.code, 'E514') self.assertEqual(context.exception.http_code, 500)
def test_add_event_connection_error(self, mock_post): mock_post.side_effect = ConnectionError(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: MetricAPI.add_event(self, self.payload) self.assertEqual( context.exception.message, 'Encountered an error connecting to dps_metric_api: {}'. format(self.error_msg)) self.assertEqual(context.exception.code, 'E515') self.assertEqual(context.exception.http_code, 500)
def test_update_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: UlapdAPI.update(self, self.updated_data) self.assertTrue(ApplicationError in str(context.exception)) self.assertEqual( context.exception.message, 'Connection to ulapd_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E519') self.assertEqual(context.exception.http_code, 500)
def test_update_connection_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 = ConnectionError(self.error_msg) with self.assertRaises(ApplicationError) as context: UlapdAPI.update(self, self.updated_data) self.assertEqual( context.exception.message, 'Encountered an error connecting to ulapd_api: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E518') self.assertEqual(context.exception.http_code, 500)
def test_update_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: UlapdAPI.update(self, self.updated_data) self.assertEqual( context.exception.message, 'Received the following response from ulapd_api: {}'. format(self.error_msg)) self.assertEqual(context.exception.code, 'E517') self.assertEqual(context.exception.http_code, 500)
def _teardown_service_user(self): if self.ldap_id is None: return with app.app_context() as ac: ac.g.requests = requests.Session() with app.test_request_context(): url = '{}/users/{}'.format(self.account_url, self.ldap_id) g.requests.delete(url, headers=self.headers) if self.ulapd_id is None: return url = self.ulapd_url + '/users/' + self.ulapd_id g.requests.delete(url, headers=self.headers)
def test_get_dataset_list_details(self, mock_get): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): dataset_list = [{ 'title': 'Test Dataset', 'id': '1', 'private': False, 'name': 'test' }] mock_get.return_value.json.return_value = dataset_list mock_get.return_value.status_code = 200 response = UlapdAPI.get_dataset_list_details(self) mock_get.assert_called_once() self.assertEqual(response, dataset_list)
def test_update_dataset_access_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: UlapdAPI.update_dataset_access(self, {}) expected_err = ('verification_api', 'ULAPD_API_CONN_ERROR') expected_err_message = errors.get_message( *expected_err, filler=self.error_msg) expected_err_code = errors.get_code(*expected_err) mock_post.assert_called_once() self.assertEqual(context.exception.message, expected_err_message) self.assertEqual(context.exception.code, expected_err_code)
def test_get_user_dataset_access_timeout(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 = Timeout(self.error_msg) with self.assertRaises(ApplicationError) as context: UlapdAPI.get_user_dataset_access(self, 1) expected_err = ('verification_api', 'ULAPD_API_TIMEOUT') expected_err_message = errors.get_message( *expected_err, filler=self.error_msg) expected_err_code = errors.get_code(*expected_err) mock_get.assert_called_once() self.assertEqual(context.exception.message, expected_err_message) self.assertEqual(context.exception.code, expected_err_code)