Esempio n. 1
0
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)
Esempio n. 2
0
class TestHistoryForFeedback(SignalsBaseApiTestCase, SIAReadUserMixin):
    def setUp(self):
        self.signal = SignalFactoryValidLocation()
        self.feedback = FeedbackFactory(
            _signal=self.signal,
            is_satisfied=None,
        )

        self.feedback_endpoint = '/signals/v1/public/feedback/forms/{token}'
        self.history_endpoint = '/signals/v1/private/signals/{id}/history'

    def test_setup(self):
        self.assertEqual(Signal.objects.count(), 1)
        self.assertEqual(Feedback.objects.count(), 1)

        self.assertEqual(Feedback.objects.count(), 1)
        self.assertEqual(self.feedback.is_satisfied, None)
        self.assertEqual(self.feedback.submitted_at, None)

    def test_submit_feedback_check_history(self):
        # get a user privileged to read from API
        read_user = self.sia_read_user
        read_user.user_permissions.add(
            Permission.objects.get(codename='sia_can_view_all_categories'))
        self.client.force_authenticate(user=read_user)
        history_url = self.history_endpoint.format(id=self.signal.id)

        # check history before submitting feedback
        response = self.client.get(history_url)
        self.assertEqual(response.status_code, 200)

        response_data = response.json()
        self.assertEqual(len(response_data), 6)

        # Note the unhappy flow regarding feedback is tested in the feedback
        # app. Here we only check that it shows up in the history.
        url = self.feedback_endpoint.format(token=self.feedback.token)
        payload = {
            'is_satisfied': True,
            'allows_contact': False,
            'text': 'De zon schijnt.',
            'text_extra': 'maar niet heus',
        }

        response = self.client.put(url, data=payload, format='json')
        self.assertEqual(response.status_code, 200)

        # check that feedback object in db is updated
        self.feedback.refresh_from_db()
        self.assertEqual(self.feedback.is_satisfied, True)
        self.assertNotEqual(self.feedback.submitted_at, None)

        # check have an entry in the history for the feedback
        response = self.client.get(history_url)
        self.assertEqual(response.status_code, 200)

        response_data = response.json()
        self.assertEqual(len(response_data), 7)

        # check that filtering by RECEIVE_FEEDBACK works
        response = self.client.get(history_url + '?what=RECEIVE_FEEDBACK')
        self.assertEqual(response.status_code, 200)

        response_data = response.json()
        self.assertEqual(len(response_data), 1)

    def test_history_entry_description_property(self):
        # equivalent to submitting feedback:
        text = 'TEXT'
        text_extra = 'TEXT_EXTRA'

        self.feedback.is_satisfied = True
        self.feedback.allows_contact = False
        self.feedback.text = text
        self.feedback.text_extra = text_extra
        self.feedback.submitted_at = self.feedback.created_at + timedelta(
            days=1)
        self.feedback.save()

        # check the rendering
        read_user = self.sia_read_user
        read_user.user_permissions.add(
            Permission.objects.get(codename='sia_can_view_all_categories'))
        self.client.force_authenticate(user=read_user)
        history_url = self.history_endpoint.format(id=self.signal.id)

        response = self.client.get(history_url + '?what=RECEIVE_FEEDBACK')
        self.assertEqual(response.status_code, 200)

        response_data = response.json()
        self.assertEqual(len(response_data), 1)
        history_entry = response_data[0]

        self.assertIn('Ja, de melder is tevreden',
                      history_entry['description'])
        self.assertIn(f'Waarom: {text}', history_entry['description'])
        self.assertIn(f'Toelichting: {text_extra}',
                      history_entry['description'])
        self.assertIn('Toestemming contact opnemen: Nee',
                      history_entry['description'])

    def test_history_no_permissions(self):
        """
        The sia_read_user does not have a link with any department and also is not configured with the permission
        "sia_can_view_all_categories". Therefore it should not be able to see a Signal and it's history.
        """
        self.client.force_authenticate(user=self.sia_read_user)
        response = self.client.get(
            self.history_endpoint.format(id=self.signal.id) +
            '?what=RECEIVE_FEEDBACK')
        self.assertEqual(response.status_code, 403)