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_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 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))