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_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_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 test_redirects_when_no_flag(self): mock_prison_response() self.login( responses, user_data=self.get_user_data(flags=[ hmpps_employee_flag, ], ), ) for view in self.protected_views: response = self.client.get(reverse(view), follow=True) self.assertContains(response, '<!-- confirm_prisons -->')
def test_not_all_prisons(self): """ Test that clicking on 'Remove all prisons' redirects to the default version of the page (not the 'All Prison' one). Note: the test does not save the form but it only tests its initialisation. """ current_prison = SAMPLE_PRISONS[0] mock_prison_response() self.login(responses, user_data=self.get_user_data( prisons=[current_prison], flags=[hmpps_employee_flag, confirmed_prisons_flag])) response = self.client.post(reverse('change_prisons'), data={ 'all_prisons': True, 'submit_notall': True }, follow=True) self.assertContains(response, 'name="prison_1"') self.assertNotContains(response, 'All prisons')
def test_remove_prison(self): """ Test that clicking on 'Remove' redirects to the same page with that textfield removed. Note: the test does not save the form but it only tests its initialisation. """ 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])) response = self.client.post(reverse('change_prisons'), data={ 'prison_0': current_prison['nomis_id'], 'prison_1': new_prison['nomis_id'], 'submit_remove_prison_0': True }, follow=True) self.assertNotContains(response, 'name="prison_0"') self.assertContains(response, 'name="prison_1"')
def test_next_url(self): """ Test that if the next param is passed in, the view redirects to it after saving the changes. """ 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]) next_page = reverse('root') response = self.client.post( f"{reverse('change_prisons')}?{REDIRECT_FIELD_NAME}={next_page}", data={ 'prison_0': new_prison['nomis_id'], 'submit_save': True, }, ) self.assertRedirects(response, next_page, target_status_code=302)