def test_update_dataset_access_timeout_error(self, mock_post): case_id = 999 staff_id = 'cs999pb' current_access = [{ 'name': 'nps', 'licences': { 'nps': { 'agreed': True }, 'nps_sample': { 'agreed': False } } }] updated_access = ImmutableMultiDict([('nps', 'nps_sample')]) mock_post.side_effect = Timeout(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.update_dataset_access(case_id, staff_id, current_access, updated_access) mock_post.assert_called_once() self.assertEqual( context.exception.message, 'Connection to verification_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E403') self.assertEqual(context.exception.http_code, 500)
def test_update_dataset_access_no_update(self): case_id = 999 staff_id = 'cs999pb' # this is mocking the data that would come from calling verification_api.get_user_dataset_access() current_access = [{ 'name': 'nps', 'licences': { 'nps': { 'agreed': True }, 'nps_sample': { 'agreed': True } } }] # Updated access is flask's request.form which is an ImmutableMultiDict data structure with .getlist method updated_access = ImmutableMultiDict([('nps', 'nps'), ('nps', 'nps_sample')]) verification_api = VerificationAPI() verification_api._request = MagicMock() result = verification_api.update_dataset_access( case_id, staff_id, current_access, updated_access) # If nothing to update should return empty dict() and don't call verification-api self.assertEqual(result, {}) verification_api._request.assert_not_called()
def decline_worklist_item(): try: session['search_params'] = None staff_id = _get_user_name() item_id = request.form['item_id'] if not check_correct_lock_user(item_id, session['username']): flash('You are not the current locked user of Worklist item {}'. format(item_id)) return redirect(url_for('verification.get_worklist')) reason = request.form['decline_reason'] advice = request.form['decline_advice'] current_app.logger.info('User {} declining worklist item {}...'.format( staff_id, item_id)) verification_api = VerificationAPI() verification_api.decline_worklist_item(item_id, staff_id, reason, advice) except ApplicationError: raise ApplicationError( 'Something went wrong when declining the application. ' 'Please raise an incident quoting the following id: {}'.format( g.trace_id)) else: current_app.logger.info( 'Worklist item {} was declined'.format(item_id)) flash('Application was declined') return redirect(url_for('verification.get_worklist'))
def update_dataset_access(): try: current_app.logger.info('Updating dataset access...') item_id = request.form['item_id'] staff_id = _get_user_name() updated_access = request.form verification_api = VerificationAPI() current_access = verification_api.get_user_dataset_access(item_id) # Temporarily filter out ocod/ccod/nps_sample/res_cov_direct from access being updated for dataset in current_access: if dataset['name'] == 'licenced': dataset['licences'] = {} # Filter out sample and direct licence entries licences = { k: v for (k, v) in dataset['licences'].items() if '_direct' not in k and '_sample' not in k } dataset['licences'] = licences verification_api.update_dataset_access(item_id, staff_id, current_access, updated_access) flash("User's data access was updated") return redirect(url_for('verification.get_item', item_id=item_id)) except ApplicationError: raise ApplicationError( 'Something went wrong when updating users data access. ' 'Please raise an incident quoting the following id: {}'.format( g.trace_id))
def test_get_verification_api(self): with app.app_context() as ac: ac.g.trace_id = None ac.g.requests = requests.Session() with app.test_request_context(): verification_api = VerificationAPI() resp_data = verification_api.get_worklist() self.assertIsNotNone(resp_data)
def test_get_item(self, mock_get): mock_get.return_value.json.return_value = [{'case_id': self.item_id}] mock_get.return_value.status_code = 200 verification_api = VerificationAPI() response = verification_api.get_item(self.item_id) self.assertEqual(response, [{'case_id': self.item_id}])
def test_get_dataset_activity(self, mock_get): case_id = 999 mock_get.return_value.json.return_value = ['test'] mock_get.return_value.status_code = 200 verification_api = VerificationAPI() response = verification_api.get_dataset_activity(case_id) mock_get.assert_called_once() self.assertEqual(response, ['test'])
def check_correct_lock_user(item_id, username): verficiation_api = VerificationAPI() result = verficiation_api.get_item(item_id) locked_user = result['staff_id'] current_user = username if locked_user is None: return True elif locked_user != current_user: return False else: return True
def test_add_note(self, mock_post): response_message = 'Note added for user {}'.format(self.item_id) mock_post.return_value.json.return_value = { "message": response_message } mock_post.return_value.status_code = 201 verification_api = VerificationAPI() response = verification_api.add_note(self.item_id, self.staff_id, self.note_text) self.assertEqual(response, {"message": response_message})
def test_get_worklist_connection_error(self, mock_get): mock_get.side_effect = ConnectionError(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.get_worklist() self.assertEqual( context.exception.message, 'Encountered an error connecting to verification_api: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E402') self.assertEqual(context.exception.http_code, 500)
def test_get_worklist_timeout_error(self, mock_get): mock_get.side_effect = Timeout(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.get_worklist() self.assertEqual( context.exception.message, 'Connection to verification_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E403') self.assertEqual(context.exception.http_code, 500)
def test_approve_worklist_item_timeout_error(self, mock_post): mock_post.side_effect = Timeout(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.approve_worklist_item(self.item_id, self.staff_id) self.assertEqual( context.exception.message, 'Connection to verification_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E403') self.assertEqual(context.exception.http_code, 500)
def test_get_worklist_http_error(self, mock_get): mock_get.side_effect = HTTPError(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.get_worklist() self.assertEqual( context.exception.message, 'Received the following response from verification_api: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E401') self.assertEqual(context.exception.http_code, 500)
def test_add_note_connection_error(self, mock_post): mock_post.side_effect = ConnectionError(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.add_note(self.item_id, self.staff_id, self.note_text) self.assertEqual( context.exception.message, 'Encountered an error connecting to verification_api: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E402') self.assertEqual(context.exception.http_code, 500)
def test_add_note_http_error(self, mock_post): mock_post.side_effect = HTTPError(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.add_note(self.item_id, self.staff_id, self.note_text) self.assertEqual( context.exception.message, 'Received the following response from verification_api: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E401') self.assertEqual(context.exception.http_code, 500)
def test_get_dataset_activity_http_error(self, mock_get): case_id = 999 mock_get.side_effect = HTTPError(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.get_dataset_activity(case_id) mock_get.assert_called_once() self.assertEqual( context.exception.message, 'Received the following response from verification_api: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E401') self.assertEqual(context.exception.http_code, 500)
def test_get_dataset_activity_timeout_error(self, mock_get): case_id = 999 mock_get.side_effect = Timeout(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.get_dataset_activity(case_id) mock_get.assert_called_once() self.assertEqual( context.exception.message, 'Connection to verification_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E403') self.assertEqual(context.exception.http_code, 500)
def unlock_case(): try: session['search_params'] = None item_id = request.form['item_id'] current_app.logger.info('Unlocking worklist item {}'.format(item_id)) verification_api = VerificationAPI() verification_api.unlock(item_id) except ApplicationError: raise ApplicationError( 'Something went wrong while unlocking the application. ' 'Please raise an incident quoting the following id: {}'.format( g.trace_id)) else: current_app.logger.info('Worklist item successfully unlocked') return redirect(url_for('verification.get_worklist'))
def test_decline_worklist_item_connection_error(self, mock_post): mock_post.side_effect = ConnectionError(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.decline_worklist_item(self.item_id, self.staff_id, self.decline_reason, self.decline_advice) self.assertEqual( context.exception.message, 'Encountered an error connecting to verification_api: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E402') self.assertEqual(context.exception.http_code, 500)
def test_close_account_timeout_error(self, mock_post): mock_post.side_effect = Timeout(self.error_msg) with self.assertRaises(ApplicationError) as context: verification_api = VerificationAPI() verification_api.close_account(self.item_id, self.staff_id, self.close_requester, self.close_reason) self.assertEqual( context.exception.message, 'Connection to verification_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E403') self.assertEqual(context.exception.http_code, 500)
def test_perform_search(self, mock_post): mock_post.return_value.json.return_value = [{'case_id': 1}] mock_post.return_value.status_code = 200 search_entries = { "first_name": "test", "last_name": "", "organisation_name": "", "email": "", } verification_api = VerificationAPI() response = verification_api.perform_search(search_entries) self.assertEqual(response, [{'case_id': 1}]) self.assertEqual(mock_post.call_count, 1)
def test_get_decline_reasons(self, mock_get): mock_get.return_value.json.return_value = [{ 'decline_reason': 'test', 'decline_text': 'This is a test decline reason' }] mock_get.return_value.status_code = 200 verification_api = VerificationAPI() response = verification_api.get_decline_reasons() self.assertEqual(response, [{ 'decline_reason': 'test', 'decline_text': 'This is a test decline reason' }])
def get_worklist(): current_app.logger.info('User requested to view worklist...') try: verification_api = VerificationAPI() worklist = verification_api.get_worklist() _get_user_name() except ApplicationError: raise ApplicationError( 'Something went wrong when retrieving the worklist. ' 'Please raise an incident quoting the following id: {}'.format( g.trace_id)) else: current_app.logger.info('Putting worklist into viewable format...') worklist_items = [build_row(item) for item in worklist] return render_template('app/worklist.html', worklist_items=worklist_items)
def test_approve_worklist_item(self, mock_post): mock_post.return_value.json.return_value = { 'case_id': self.item_id, 'staff_id': self.staff_id, 'status_updated': True } mock_post.return_value.status_code = 201 verification_api = VerificationAPI() response = verification_api.approve_worklist_item( self.item_id, self.staff_id) self.assertEqual( response, { 'case_id': self.item_id, 'staff_id': self.staff_id, 'status_updated': True })
def lock_case(): try: session['search_params'] = None staff_id = _get_user_name() item_id = request.form['item_id'] current_app.logger.info( 'Locking worklist item {} to user {}...'.format(item_id, staff_id)) verification_api = VerificationAPI() verification_api.lock(item_id, staff_id) except ApplicationError: raise ApplicationError( 'Something went wrong while locking the application. ' 'Please raise an incident quoting the following id: {}'.format( g.trace_id)) else: current_app.logger.info( 'Worklist item lock successfully transferred to user {}'.format( staff_id)) return redirect(url_for('verification.get_item', item_id=item_id))
def test_update_dataset_access(self): case_id = 999 staff_id = 'cs999pb' # this is mocking the data that would come from calling verification_api.get_user_dataset_access() current_access = [{ 'name': 'nps', 'licences': { 'nps': { 'agreed': True }, 'nps_sample': { 'agreed': False } } }] # Updated access is flask's request.form which is an ImmutableMultiDict data structure with .getlist method updated_access = ImmutableMultiDict([('nps', 'nps_sample')]) verification_api = VerificationAPI() verification_api._request = MagicMock() verification_api.update_dataset_access(case_id, staff_id, current_access, updated_access) expected_data = { 'staff_id': staff_id, 'licences': [{ 'licence_id': 'nps', 'agreed': False }, { 'licence_id': 'nps_sample', 'agreed': True }] } # There is logic in update_dataset_access to create an array of licences with true/false agreed states # Ensure that the HTTP _request method is being called with correct data based on this logic verification_api._request.assert_called_with( uri='case/999/update_dataset_access'.format(case_id), data=json.dumps(expected_data))
def test_update_user_details(self, mock_post): response_message = 'Updated user details for ID: {}'.format( self.item_id) mock_post.return_value.json.return_value = { "message": response_message } mock_post.return_value.status_code = 201 response = VerificationAPI().update_user_details(self.item_id, {}) self.assertEqual(response, {"message": response_message})
def add_note(): try: session['search_items'] = None staff_id = _get_user_name() item_id = request.form['item_id'] note_text = request.form['note_text'] current_app.logger.info( 'Adding note "{}" to notepad for worklist item {}...'.format( note_text, item_id)) verification_api = VerificationAPI() verification_api.add_note(item_id, staff_id, note_text) except ApplicationError: raise ApplicationError( 'Something went wrong when adding a note. ' 'Please raise an incident quoting the following id: {}'.format( g.trace_id)) else: current_app.logger.info('Note was added to the notepad') flash('Your note was added to the notepad') return redirect(url_for('verification.get_item', item_id=item_id))
def test_perform_search_http_error(self, mock_post): mock_post.side_effect = HTTPError(self.error_msg) with self.assertRaises(ApplicationError) as context: search_entries = { "first_name": "test", "last_name": "", "organisation_name": "", "email": "", } verification_api = VerificationAPI() verification_api.perform_search(search_entries) self.assertEqual( context.exception.message, 'Received the following response from verification_api: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E401') self.assertEqual(context.exception.http_code, 500) self.assertEqual(mock_post.call_count, 1)
def test_perform_search_timeout_error(self, mock_post): mock_post.side_effect = Timeout(self.error_msg) with self.assertRaises(ApplicationError) as context: search_entries = { "first_name": "test", "last_name": "", "organisation_name": "", "email": "", } verification_api = VerificationAPI() verification_api.perform_search(search_entries) self.assertEqual( context.exception.message, 'Connection to verification_api timed out: {}'.format( self.error_msg)) self.assertEqual(context.exception.code, 'E403') self.assertEqual(context.exception.http_code, 500) self.assertEqual(mock_post.call_count, 1)