def test_prison_confirmation(self): current_prison = SAMPLE_PRISONS[0] new_prison = SAMPLE_PRISONS[1] mock_prison_response() self.login(responses, user_data=self.get_user_data(prisons=[current_prison], flags=[hmpps_employee_flag])) responses.add(responses.PATCH, api_url('/users/shall/'), json={}) responses.replace(responses.GET, api_url('/users/shall/'), json=self.get_user_data(prisons=[new_prison])) responses.add(responses.PUT, api_url('/users/shall/flags/%s/' % confirmed_prisons_flag), json={}) response = self.client.post(reverse('confirm_prisons'), data={'prisons': [new_prison['nomis_id']]}, follow=True) self.assertEqual( set(p['nomis_id'] for p in json.loads( responses.calls[-3].request.body.decode())['prisons']), set([new_prison['nomis_id']])) self.assertContains(response, '<!-- confirm_prisons_confirmation -->') self.assertIn(confirmed_prisons_flag, self.client.session[USER_DATA_SESSION_KEY]['flags']) self.assertIn(confirmed_prisons_flag, response.context['user'].user_data['flags']) self.assertEqual([new_prison], self.client.session[USER_DATA_SESSION_KEY]['prisons']) self.assertEqual([new_prison], response.context['user'].user_data['prisons'])
def test_location_file_upload(self, mock_api_client): self.login() self.setup_mock_get_authenticated_api_session(mock_api_client) file_data, expected_data = generate_testable_location_data() expected_calls = [expected_data] with responses.RequestsMock() as rsps, silence_logger( level=logging.WARNING): mock_prison_response(rsps) rsps.add(rsps.POST, api_url('/prisoner_locations/actions/delete_inactive/')) rsps.add(rsps.POST, api_url('/prisoner_locations/')) rsps.add(rsps.POST, api_url('/prisoner_locations/actions/delete_old/')) response = self.client.post( reverse('location_file_upload'), {'location_file': get_csv_data_as_file(file_data)}) for call in rsps.calls: if call.request.url == api_url('/prisoner_locations/'): self.assertEqual(json.loads(call.request.body.decode()), expected_calls.pop()) if expected_calls: self.fail('Not all location data was uploaded') self.assertRedirects(response, reverse('location_file_upload'))
def test_invalid_next_url(self): """ Test that if the passed in next param is not in the allowed hosts list, the view redirects to the default view after saving the changes instead. """ current_prison = SAMPLE_PRISONS[0] new_prison = SAMPLE_PRISONS[1] mock_prison_response() self.login( responses, user_data=self.get_user_data(prisons=[current_prison], ), ) self._mock_save_prisons_responses([new_prison]) responses.add( responses.GET, api_url('/emailpreferences/'), json={'frequency': EmailNotifications.never}, ) response = self.client.post( f"{reverse('change_prisons')}?{REDIRECT_FIELD_NAME}=http://google.co.uk", data={ 'prison_0': new_prison['nomis_id'], 'submit_save': True, }, ) self.assertRedirects(response, reverse('settings'))
def test_change_prisons(self): """ Test changing my prisons' data by replacing an existing previously selected prison with a new one. """ current_prison = SAMPLE_PRISONS[0] new_prison = SAMPLE_PRISONS[1] mock_prison_response() self.login(responses, user_data=self.get_user_data( prisons=[current_prison], flags=[hmpps_employee_flag, confirmed_prisons_flag])) responses.add( responses.GET, api_url('/emailpreferences/'), json={'frequency': EmailNotifications.never}, ) response = self.client.get(reverse('settings'), follow=True) self.assertContains(response, escape(current_prison['name'])) self._mock_save_prisons_responses([new_prison]) response = self.client.post(reverse('change_prisons'), data={ 'prison_0': new_prison['nomis_id'], 'submit_save': True }, follow=True) self.assertContains(response, escape(new_prison['name']))
def _mock_save_prisons_responses(self, new_prisons): """ Mocks all responses related to saving the form successfully. """ responses.add( responses.PATCH, api_url('/users/shall/'), json={}, ) responses.replace( responses.GET, api_url('/users/shall/'), json=self.get_user_data(prisons=new_prisons, ), ) responses.add( responses.PUT, api_url(f'/users/shall/flags/{confirmed_prisons_flag}/'), json={}, )
def test_location_file_batch_upload(self, mock_api_client): self.setup_mock_get_authenticated_api_session(mock_api_client) file_data, expected_data = generate_testable_location_data(length=50) expected_calls = [ expected_data[40:50], expected_data[30:40], expected_data[20:30], expected_data[10:20], expected_data[0:10] ] request = self.make_request(get_csv_data_as_file(file_data)) with responses.RequestsMock() as rsps, silence_logger(level=logging.WARNING): mock_prison_response(rsps) form = LocationFileUploadForm(request.POST, request.FILES, request=request) self.assertTrue(form.is_valid()) rsps.add( rsps.POST, api_url('/prisoner_locations/actions/delete_inactive/') ) rsps.add( rsps.POST, api_url('/prisoner_locations/') ) rsps.add( rsps.POST, api_url('/prisoner_locations/actions/delete_old/') ) form.update_locations() for call in rsps.calls: if call.request.url == api_url('/prisoner_locations/'): self.assertEqual( json.loads(call.request.body.decode()), expected_calls.pop() ) if expected_calls: self.fail('Not all location data was uploaded')
def test_location_file_with_ignored_prisons(self, mock_api_client): self.setup_mock_get_authenticated_api_session(mock_api_client) file_data, expected_data = generate_testable_location_data(length=20, extra_rows=[ 'A1234ZZ,Smith,John,2/9/1997,TRN', 'A1235ZZ,Smith,Fred,2/9/1997,ZCH', ]) expected_calls = [expected_data] request = self.make_request(get_csv_data_as_file(file_data)) with responses.RequestsMock() as rsps, silence_logger(level=logging.WARNING): mock_prison_response(rsps) form = LocationFileUploadForm(request.POST, request.FILES, request=request) self.assertTrue(form.is_valid()) rsps.add( rsps.POST, api_url('/prisoner_locations/actions/delete_inactive/') ) rsps.add( rsps.POST, api_url('/prisoner_locations/') ) rsps.add( rsps.POST, api_url('/prisoner_locations/actions/delete_old/') ) form.update_locations() for call in rsps.calls: if call.request.url == api_url('/prisoner_locations/'): self.assertEqual( json.loads(call.request.body.decode()), expected_calls.pop() ) if expected_calls: self.fail('Not all location data was uploaded')
def test_location_file_with_ignored_prisons(self, mock_api_client): self.login() self.setup_mock_get_authenticated_api_session(mock_api_client) file_data, expected_data = generate_testable_location_data( length=20, extra_rows=[ 'A1234ZZ,Smith,John,2/9/1997,TRN', 'A1235ZZ,Smith,Fred,2/9/1997,ZCH', ]) expected_calls = [expected_data] with responses.RequestsMock() as rsps, silence_logger( level=logging.WARNING): mock_prison_response(rsps) rsps.add(rsps.POST, api_url('/prisoner_locations/actions/delete_inactive/')) rsps.add(rsps.POST, api_url('/prisoner_locations/')) rsps.add(rsps.POST, api_url('/prisoner_locations/actions/delete_old/')) response = self.client.post( reverse('location_file_upload'), {'location_file': get_csv_data_as_file(file_data)}, follow=True) for call in rsps.calls: if call.request.url == api_url('/prisoner_locations/'): self.assertEqual(json.loads(call.request.body.decode()), expected_calls.pop()) if expected_calls: self.fail('Not all location data was uploaded') self.assertRedirects(response, reverse('location_file_upload')) response_content = response.content.decode() self.assertIn('Ignored 1 prisoner in transfer', response_content) self.assertIn('Ignored 1 prisoner in unsupported prison', response_content) self.assertIn('NOMIS code: ZCH', response_content)
def test_location_file_upload_api_error_displays_message( self, mock_api_client): self.login() self.setup_mock_get_authenticated_api_session(mock_api_client) api_error_message = 'prison not found' response_content = ('[{"prison": ["%s"]}]' % api_error_message).encode() file_data, _ = generate_testable_location_data() with responses.RequestsMock() as rsps, silence_logger(): mock_prison_response(rsps) rsps.add(rsps.POST, api_url('/prisoner_locations/actions/delete_inactive/')) rsps.add(rsps.POST, api_url('/prisoner_locations/'), status=400, body=response_content) response = self.client.post( reverse('location_file_upload'), {'location_file': get_csv_data_as_file(file_data)}) self.assertContains(response, api_error_message)
def test_does_not_redirect_for_user_admin(self): responses.add( responses.GET, api_url('/requests') + '?page_size=1', json={'count': 0}, status=200, ) self.login( responses, user_data=self.get_user_data( flags=[hmpps_employee_flag, provided_job_info_flag], permissions=required_permissions + ['auth.change_user'])) response = self.client.get(reverse('security:dashboard'), follow=True) self.assertContains(response, '<!-- security:dashboard -->')