def test_handling_message_in_history_is_constant(self): # SIG-3555 because history is not tracked for the Category model and its # handling_message. Changing the handling_message will also change the # history of every Signal that has that Category associated with. # This test demonstrates the problem. message_1 = 'MESSAGE 1' message_2 = 'MESSAGE 2' cat1 = CategoryFactory.create(name='cat1', handling_message=message_1) cat2 = CategoryFactory.create(name='cat2') signal = SignalFactoryValidLocation(category_assignment__category=cat1) self.sia_read_write_user.user_permissions.add( Permission.objects.get(codename='sia_can_view_all_categories')) self.client.force_authenticate(user=self.sia_read_write_user) # Retrieve signal history before changing the Category handling message. response = self.client.get( f'/signals/v1/private/signals/{signal.id}/history' + '?what=UPDATE_SLA') self.assertEqual(response.status_code, 200) response_json = response.json() self.assertEqual(len(response_json), 1) self.assertEqual(response_json[0]['description'], message_1) # Change handling message on category cat1.handling_message = message_2 cat1.save() cat1.refresh_from_db() # Assign different category and then the same again, check handling messages. Signal.actions.update_category_assignment({'category': cat2}, signal) signal.refresh_from_db() Signal.actions.update_category_assignment({'category': cat1}, signal) response = self.client.get( f'/signals/v1/private/signals/{signal.id}/history' + '?what=UPDATE_SLA') self.assertEqual(response.status_code, 200) response_json = response.json() self.assertEqual(len(response_json), 1) self.assertEqual(response_json[0]['description'], message_1) # check that the correct handling messages are stored: category_assignments = list( CategoryAssignment.objects.filter( _signal_id=signal.id).order_by('id')) self.assertEqual(category_assignments[0].stored_handling_message, message_1) self.assertEqual(category_assignments[2].stored_handling_message, message_2)
class TestGenerateOmschrijving(TestCase): def setUp(self): self.signal = SignalFactoryValidLocation( priority__priority=Priority.PRIORITY_HIGH) def test_generate_omschrijving_urgent(self): stadsdeel = self.signal.location.stadsdeel correct = 'SIA-{}.02 URGENT {} {}'.format( self.signal.pk, SIGMAX_STADSDEEL_MAPPING.get(stadsdeel, 'SD--'), self.signal.location.short_address_text) seq_no = '02' self.assertEqual(_generate_omschrijving(self.signal, seq_no), correct) def test_generate_omschrijving_no_stadsdeel_urgent(self): # test that we get SD-- as part of the omschrijving when stadsdeel is missing self.signal.location.stadsdeel = None self.signal.location.save() correct = 'SIA-{}.04 URGENT SD-- {}'.format( self.signal.pk, self.signal.location.short_address_text) seq_no = '04' self.assertEqual(_generate_omschrijving(self.signal, seq_no), correct) def test_stadsdeel_mapping(self): # Check that all known STADSDELEN (city districts) have a code for use # by Sigmax/CityControl. seq_no = '01' for short_code, name in STADSDELEN: self.signal.location.stadsdeel = short_code self.signal.location.save() self.signal.refresh_from_db() self.assertNotRegex(_generate_omschrijving(self.signal, seq_no), 'SD--')
class TestFeedbackFlow(SignalsBaseApiTestCase): def setUp(self): # Times for various actions (assumes a 14 day window for feedback). self.t_now = '2019-04-01 12:00:00' self.t_creation = '2019-03-01 12:00:00' self.t_expired = '2019-03-02 12:00:00' self.t_received = '2019-03-29 12:00:00' # Setup our test signal and feedback instances with freeze_time(self.t_creation): self.reporter = ReporterFactory() self.signal = SignalFactoryValidLocation( reporter=self.reporter, status__state=workflow.AFGEHANDELD, ) with freeze_time(self.t_now): self.feedback = FeedbackFactory( submitted_at=None, _signal=self.signal, ) with freeze_time(self.t_expired): self.feedback_expired = FeedbackFactory( submitted_at=None, _signal=self.signal, ) with freeze_time(self.t_received): self.feedback_received = FeedbackFactory( submitted_at=timezone.now() - timedelta(days=5), _signal=self.signal, ) # Setup a standard answer that triggers a request to reopen. self.sa_reopens = StandardAnswerFactory.create( text='Ik ben niet blij met de afhandeling, duurde te lang.', is_satisfied=False, reopens_when_unhappy=True, ) self.sa_no_sideeffect = StandardAnswerFactory.create( text='Ik ben niet blij. Blah, blah.', is_satisfied=False, reopens_when_unhappy=False, ) def test_setup(self): self.assertEqual(Feedback.objects.count(), 3) def test_404_if_no_feedback_requested(self): response = self.client.get('/forms/DIT_IS_GEEN_token/') self.assertEqual(response.status_code, 404) def test_410_gone_too_late(self): token = self.feedback_expired.token with freeze_time(self.t_now): response = self.client.get('/forms/{}/'.format(token)) self.assertEqual(response.status_code, 410) # faalt! self.assertEqual(response.json()['detail'], 'too late') response = self.client.put('/forms/{}/'.format(token), data={}) self.assertEqual(response.status_code, 410) self.assertEqual(response.json()['detail'], 'too late') def test_410_gone_filled_out(self): """Test that we receive correct HTTP 410 reply when form filled out already""" token = self.feedback_received.token with freeze_time(self.t_now): response = self.client.get('/forms/{}/'.format(token)) self.assertEqual(response.status_code, 410) self.assertEqual(response.json()['detail'], 'filled out') response = self.client.put('/forms/{}/'.format(token), data={}) self.assertEqual(response.status_code, 410) self.assertEqual(response.json()['detail'], 'filled out') def test_200_if_feedback_requested(self): """Test that we receive an empty JSON object HTTP 200 reply.""" token = self.feedback.token with freeze_time(self.t_now): response = self.client.get('/forms/{}/'.format(token)) self.assertEqual(response.status_code, 200) self.assertEqual(response.json(), {}) def test_200_on_submit_feedback(self): """Test that the feedback can be PUT once.""" token = self.feedback.token reason = 'testen is leuk' explanation = 'ook voor de lunch' data = { 'is_satisfied': True, 'allows_contact': True, 'text': reason, 'text_area': explanation, } with freeze_time(self.t_now): response = self.client.put( '/forms/{}/'.format(token), data=data, format='json', ) self.assertEqual(response.status_code, 200) self.feedback.refresh_from_db() self.assertEqual(self.feedback.is_satisfied, True) self.assertEqual(self.feedback.allows_contact, True) self.assertEqual(self.feedback.text, reason) def test_400_on_submit_feedback_without_is_satisfied(self): """Test that the feedback can be PUT once.""" token = self.feedback.token reason = 'testen is leuk' explanation = 'ook voor de lunch' data = { 'allows_contact': True, 'text': reason, 'text_area': explanation, } with freeze_time(self.t_now): response = self.client.put( '/forms/{}/'.format(token), data=data, format='json', ) self.assertEqual(response.status_code, 400) def test_reopen_requested_on_unsatisfied_standard_answer(self): """Certain standard answers (in feedback) lead to "reopen requested" state.""" token = self.feedback.token data = { 'allows_contact': False, 'text': self.sa_reopens.text, 'is_satisfied': False, } with freeze_time(self.t_now): response = self.client.put( '/forms/{}/'.format(token), data=data, format='json', ) self.assertEqual(response.status_code, 200) self.signal.refresh_from_db() self.assertEqual(self.signal.status.state, workflow.VERZOEK_TOT_HEROPENEN) def test_reopen_requested_on_unsatisfied_custom_answer(self): """All custom unsatisfied answers (in feedback) lead to "reopen requested" state.""" token = self.feedback.token data = { 'allows_contact': False, 'text': 'MEH, probleem niet opgelost.', 'is_satisfied': False, } with freeze_time(self.t_now): response = self.client.put( '/forms/{}/'.format(token), data=data, format='json', ) self.assertEqual(response.status_code, 200) self.signal.refresh_from_db() self.assertEqual(self.signal.status.state, workflow.VERZOEK_TOT_HEROPENEN) def test_no_reopen_requested_on_unsatisfied_and_known_feedback(self): """Some negative feedback is explicitly marked not to trigger reopen requested.""" token = self.feedback.token data = { 'allows_contact': False, 'text': self.sa_no_sideeffect.text, 'is_satisfied': False, } with freeze_time(self.t_now): response = self.client.put( '/forms/{}/'.format(token), data=data, format='json', ) self.assertEqual(response.status_code, 200) self.signal.refresh_from_db() self.assertEqual(self.signal.status.state, workflow.AFGEHANDELD) def test_no_reopen_requested_when_not_in_state_afgehandeld(self): """Only request reopen from AFGEHANDELD state.""" with freeze_time(self.t_now): # Reopen the test signal (so it is no longer in AFGEHANDELD). payload = { 'text': 'De melder is niet tevreden blijkt uit feedback. Zo nodig heropenen.', 'state': workflow.HEROPEND, } Signal.actions.update_status(payload, self.signal) # Send feedback that potentially reopens a signal (should not happen in this test). token = self.feedback.token data = { 'allows_contact': False, 'text': self.sa_reopens.text, 'is_satisfied': False, } response = self.client.put( '/forms/{}/'.format(token), data=data, format='json', ) self.assertEqual(response.status_code, 200) # Assert that nothing happened. self.signal.refresh_from_db() self.assertEqual(self.signal.status.state, workflow.HEROPEND) def test_no_reopen_requested_on_positive_feedback(self): """Positive feedback should never request a reopen""" # Create a positive feedback StandardAnswer that could possibly lead to # the status reopen requested. sa_positive = StandardAnswerFactory.create( text='Ik ben blij met de afhandeling.', is_satisfied=True, reopens_when_unhappy=True, ) status_id_before = self.signal.status.id with freeze_time(self.t_now): # Send feedback that potentially reopens a signal (should not happen in this test). token = self.feedback.token data = { 'allows_contact': False, 'text': sa_positive.text, 'is_satisfied': True, # should not be able to override, refactor into separate test } response = self.client.put( '/forms/{}/'.format(token), data=data, format='json', ) self.assertEqual(response.status_code, 200) # Assert that nothing happened. self.signal.refresh_from_db() self.assertEqual(self.signal.status.state, workflow.AFGEHANDELD) self.assertEqual(status_id_before, self.signal.status.id)