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 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 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(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_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)
def test_get_dataset_list_details_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: UlapdAPI.get_dataset_list_details(self) expected_err = ('verification_api', 'ULAPD_API_HTTP_ERROR') 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)
def get_user_dataset_access(case_id): log.info('Getting dataset access for {}'.format(case_id)) case = Case.get_case_by_id(case_id) if case is None: raise ApplicationError(*errors.get('verification_api', 'CASE_NOT_FOUND', filler=case_id), http_code=404) ulapd = UlapdAPI() dataset_access_list = ulapd.get_user_dataset_access(case.user_id) return dataset_access_list
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 get_dataset_list_details(): ulapd = UlapdAPI() dataset_list = ulapd.get_dataset_list_details() return dataset_list
email = search_params.get('email', '') return _extract_rows( Case.search(first_name, last_name, organisation_name, email)) @handle_errors(is_get=False) def update_user_details(case_id, data): case = Case.get_case_by_id(case_id) if case is None: raise ApplicationError(*errors.get('verification_api', 'CASE_NOT_FOUND', filler=case_id), http_code=404) # update preference in ulapd ulapd = UlapdAPI() ulapd_data = {'user_id': case.user_id} for key, value in data['updated_data'].items(): ulapd_data[key] = value ulapd.update(ulapd_data) case.registration_data = _update_registration_data( dict(case.registration_data), data['updated_data']) if 'contactable' in data['updated_data']: _add_update_note(case_id, data) db.session.commit() return {'updated': True}