Exemplo n.º 1
0
    def test_links_in_different_environments_frontend_url_set(self):
        """Test that generated feedback links contain the correct host."""
        # Prepare signal with status change to `AFGEHANDELD`.
        StatusFactory.create(_signal=self.signal, state=workflow.BEHANDELING)
        status = StatusFactory.create(_signal=self.signal,
                                      state=workflow.AFGEHANDELD)
        self.signal.status = status
        self.signal.save()

        test_frontend_urls = [
            'https://acc.meldingen.amsterdam.nl',
            'https://meldingen.amsterdam.nl',
            'https://random.net',
        ]
        for test_frontend_url in test_frontend_urls:
            with override_settings(FRONTEND_URL=test_frontend_url):
                mail.outbox = []
                ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
                ma.apply(signal_id=self.signal.id)

                self.assertEqual(len(mail.outbox), 1)
                message = mail.outbox[0]
                self.assertIn(test_frontend_url, message.body)
                self.assertIn(test_frontend_url, message.alternatives[0][0])

        # we want a history entry when a email was sent
        self.assertEqual(Note.objects.count(), len(test_frontend_urls))
Exemplo n.º 2
0
    def test_send_mail_reporter_created_no_note(self):
        # Is the intended rule activated?
        actions = self._get_mail_rules(['Send mail signal created'
                                        ])._get_actions(self.signal)
        self.assertEqual(len(actions), 1)

        # We do not want to add a note so we should remove the 'history_entry_text'
        COPIED_SIGNAL_MAIL_RULES = [
            copy.deepcopy(r) for r in SIGNAL_MAIL_RULES
            if r['name'] == 'Send mail signal created'
        ]
        COPIED_SIGNAL_MAIL_RULES[0]['additional_info'].pop(
            'history_entry_text')

        # Is it the only one that activates?
        ma = MailActions(mail_rules=COPIED_SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(set(actions), set(activated))

        # Check mail contents
        ma.apply(signal_id=self.signal.id)
        self.assertEqual(len(mail.outbox), 1)

        # we want a history entry when a email was sent
        self.assertEqual(Note.objects.count(), 0)
Exemplo n.º 3
0
    def test_send_mail_reporter_status_changed_afgehandeld_no_email(self):
        # Prepare signal with status change to `AFGEHANDELD`.
        StatusFactory.create(_signal=self.signal_no_email,
                             state=workflow.BEHANDELING)
        status = StatusFactory.create(_signal=self.signal_no_email,
                                      state=workflow.AFGEHANDELD)
        self.signal_no_email.status = status
        self.signal_no_email.save()

        self._apply_mail_actions(['Send mail signal handled'],
                                 self.signal_no_email)

        # no mail rule should activate
        actions = self._get_mail_rules(['Send mail signal handled'
                                        ])._get_actions(self.signal_no_email)
        self.assertEqual(len(actions), 0)

        # Check mail contents
        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        ma.apply(signal_id=self.signal_no_email.id)
        self.assertEqual(0, Feedback.objects.count())
        self.assertEqual(len(mail.outbox), 0)

        # we want no history entry when no email was sent
        self.assertEqual(Note.objects.count(), 0)
Exemplo n.º 4
0
    def test_send_mail_reporter_created_text_contains_url(self):
        """
        URL's in the signal text are removed from the context and do not end up in the email
        """
        self.signal.text = 'These links should be removed: https://www.example.com/just/a/path/ and http://test.com'
        self.signal.save()

        actions = self._get_mail_rules(['Send mail signal created'])._get_actions(self.signal)
        self.assertEqual(len(actions), 1)

        # Is it the only one that activates?
        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(set(actions), set(activated))

        # Check mail contents
        ma.apply(signal_id=self.signal.id)

        self.assertEqual(len(mail.outbox), 1)
        self.assertIn('These links should be removed:  and ', mail.outbox[0].body)
        self.assertNotIn('https://www.example.com/just/a/path/', mail.outbox[0].body)
        self.assertNotIn('http://test.com', mail.outbox[0].body)

        # we want a history entry when a email was sent
        self.assertEqual(Note.objects.count(), 1)
Exemplo n.º 5
0
    def test_send_mail_reporter_created(self):
        # Is the intended rule activated?
        actions = self._get_mail_rules(['Send mail signal created'
                                        ])._get_actions(self.signal)
        self.assertEqual(len(actions), 1)

        # Is it the only one that activates?
        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(set(actions), set(activated))

        # Check mail contents
        ma.apply(signal_id=self.signal.id)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject,
                         f'Uw melding {self.signal.id}')
        self.assertEqual(mail.outbox[0].to, [
            self.signal.reporter.email,
        ])
        self.assertEqual(mail.outbox[0].from_email,
                         settings.DEFAULT_FROM_EMAIL)
        self.assertIn('10 oktober 2018 12:00', mail.outbox[0].body)
        category = self.signal.category_assignment.category
        self.assertIn(category.handling_message, mail.outbox[0].body)
        self.assertIn(settings.ORGANIZATION_NAME, mail.outbox[0].body)

        # we want a history entry when a email was sent
        self.assertEqual(Note.objects.count(), 1)
Exemplo n.º 6
0
    def test_send_mail_reporter_status_changed_afgehandeld(self):
        # Prepare signal with status change from `BEHANDELING` to `AFGEHANDELD`.
        StatusFactory.create(_signal=self.signal, state=workflow.BEHANDELING)
        status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD)
        self.signal.status = status
        self.signal.save()

        # Is the intended rule activated?
        actions = self._get_mail_rules(['Send mail signal handled'])._get_actions(self.signal)
        self.assertEqual(len(actions), 1)

        # Is it the only one that activates?
        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(set(actions), set(activated))

        # Check mail contents
        ma.apply(signal_id=self.signal.id)
        self.assertEqual(1, Feedback.objects.count())
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject, f'Uw melding {self.signal.id}')
        self.assertEqual(mail.outbox[0].to, [self.signal.reporter.email, ])
        self.assertEqual(mail.outbox[0].from_email, settings.DEFAULT_FROM_EMAIL)
        self.assertIn(settings.ORGANIZATION_NAME, mail.outbox[0].body)

        # we want a history entry when a email was sent
        self.assertEqual(Note.objects.count(), 1)
Exemplo n.º 7
0
    def test_links_in_different_environments(self):
        """Test that generated feedback links contain the correct host."""
        # Prepare signal with status change to `AFGEHANDELD`.
        StatusFactory.create(_signal=self.signal, state=workflow.BEHANDELING)
        status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD)
        self.signal.status = status
        self.signal.save()

        # Check that generated emails contain the correct links for all
        # configured environments:
        env_fe_mapping = getattr(settings, 'FEEDBACK_ENV_FE_MAPPING', feedback_settings.FEEDBACK_ENV_FE_MAPPING)
        self.assertEqual(len(env_fe_mapping), 3)  # sanity check Amsterdam installation has three

        for environment, fe_location in env_fe_mapping.items():
            local_env = {'ENVIRONMENT': environment}

            with mock.patch.dict('os.environ', local_env):
                mail.outbox = []
                ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
                ma.apply(signal_id=self.signal.id)

                self.assertEqual(len(mail.outbox), 1)
                message = mail.outbox[0]
                self.assertIn(fe_location, message.body)
                self.assertIn(fe_location, message.alternatives[0][0])

        # we want a history entry when a email was sent
        self.assertEqual(Note.objects.count(), len(env_fe_mapping))
Exemplo n.º 8
0
    def test_links_environment_env_var_not_set(self):
        """Deals with the case where nothing is overridden and `environment` not set."""

        # Prepare signal with status change to `AFGEHANDELD`.
        status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD)
        self.signal.status = status
        self.signal.save()

        # Check that generated emails contain the correct links for all
        # configured environments:
        env_fe_mapping = feedback_settings.FEEDBACK_ENV_FE_MAPPING
        self.assertEqual(len(env_fe_mapping), 1)

        for environment, fe_location in env_fe_mapping.items():
            with mock.patch.dict('os.environ', {}, clear=True):
                mail.outbox = []
                ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
                ma.apply(signal_id=self.signal.id)

                self.assertEqual(len(mail.outbox), 1)
                message = mail.outbox[0]
                self.assertIn('http://dummy_link', message.body)
                self.assertIn('http://dummy_link', message.alternatives[0][0])

        # we want a history entry when a email was sent
        self.assertEqual(Note.objects.count(), len(env_fe_mapping))
    def test_address_formatting(self):
        EmailTemplate.objects.create(
            key=EmailTemplate.SIGNAL_CREATED,
            title='Template title {{ signal_id }}',
            body='{{ location|format_address:"O hl, P W" }}',
        )

        valid_location = copy.deepcopy(STADHUIS)
        longitude = valid_location.pop('lon')
        latitude = valid_location.pop('lat')

        signal = SignalFactory.create(
            reporter__email='*****@*****.**',
            location__geometrie=Point(longitude, latitude),
            location__buurt_code=valid_location.pop('buurt_code'),
            location__stadsdeel=valid_location.pop('stadsdeel'),
            location__address=valid_location,
        )

        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        ma.apply(signal_id=signal.id)

        self.assertEqual(f'Template title {signal.id}', mail.outbox[0].subject)

        postcode_no_spaces = signal.location.address["postcode"].replace(
            ' ', '')
        postcode = f'{postcode_no_spaces[:4]} {postcode_no_spaces[-2:]}'

        expected_address = f'{signal.location.address["openbare_ruimte"]} {signal.location.address["huisnummer"]}, ' \
                           f'{postcode} {signal.location.address["woonplaats"]}\n\n'
        self.assertEqual(expected_address, mail.outbox[0].body)
Exemplo n.º 10
0
 def _apply_mail_actions(self, action_names, signal):
     """
     Apply only specified mail rules. (allows better isolation of tests)
     """
     mail_rules = [r for r in SIGNAL_MAIL_RULES if r['name'] in set(action_names)]
     ma = MailActions(mail_rules=mail_rules)
     ma.apply(signal_id=signal.id)
Exemplo n.º 11
0
    def test_no_email_for_anonymous_reporter(self):
        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        ma.apply(self.signal_no_email.id, send_mail=True)

        self.assertEqual(len(mail.outbox), 0)

        # we want no history entry when no email was sent
        self.assertEqual(Note.objects.count(), 0)
Exemplo n.º 12
0
    def test_organization_name_contains_quote(self):
        signal = SignalFactory.create(reporter__email=self.get_email())

        with self.settings(ORGANIZATION_NAME='Gemeente \'s-Hertogenbosch'):
            ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
            ma.apply(signal_id=signal.id)

        self.assertEqual(f'Template title {signal.id}', mail.outbox[0].subject)
        self.assertEqual(
            f'Template title\n\nThanks a lot for reporting {signal.id} {signal.text}\n'
            f'Gemeente \'s-Hertogenbosch\n\n', mail.outbox[0].body)
Exemplo n.º 13
0
    def test_never_send_mail_for_child_signal(self):
        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)

        actions = ma._get_actions(self.child_signal)
        self.assertEqual(len(actions), 0)

        ma.apply(signal_id=self.child_signal.id, send_mail=True)
        self.assertEqual(len(mail.outbox), 0)

        # we want no history entry when no email was sent:
        self.assertEqual(Note.objects.count(), 0)
Exemplo n.º 14
0
    def test_send_default_mail_template(self):
        actions = self._get_mail_rules(['Send mail signal created'])._get_actions(self.signal)
        self.assertEqual(len(actions), 1)

        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(set(actions), set(activated))

        # Check mail contents
        ma.apply(signal_id=self.signal.id)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(Note.objects.count(), 1)
Exemplo n.º 15
0
    def test_evil_input(self):
        evil_signal = SignalFactory.create(
            reporter__email=self.get_email(),
            text='<script>alert("something evil");</script>')

        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        ma.apply(signal_id=evil_signal.id)

        self.assertEqual(f'Template title {evil_signal.id}',
                         mail.outbox[0].subject)
        self.assertEqual(
            f'Template title\n\nThanks a lot for reporting {evil_signal.id} '
            f'alert("something evil");\n{settings.ORGANIZATION_NAME}\n\n',
            mail.outbox[0].body)
Exemplo n.º 16
0
    def test_do_not_send_default_mail_template(self):
        new_status = StatusFactory.create(_signal=self.signal, state=workflow.GEANNULEERD, send_email=False)
        self.signal.status = new_status
        self.signal.save()

        actions = self._get_mail_rules(['Send mail optional'])._get_actions(self.signal)
        self.assertEqual(len(actions), 0)

        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(len(activated), 0)

        # Check mail contents
        ma.apply(signal_id=self.signal.id)
        self.assertEqual(len(mail.outbox), 0)
        self.assertEqual(Note.objects.count(), 0)
Exemplo n.º 17
0
    def test_send_mail_reporter_created_custom_handling_message(self):
        # Make sure a category's handling messages makes it to the reporter via
        # the mail generated on creation of a nuisance complaint.
        category_assignment = self.signal.category_assignment
        category_assignment.stored_handling_message = 'This text should end up in the mail to the reporter'
        category_assignment.save()
        self.signal.refresh_from_db()

        MailActions(mail_rules=SIGNAL_MAIL_RULES).apply(
            signal_id=self.signal.id)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject,
                         f'Uw melding {self.signal.id}')
        self.assertEqual(mail.outbox[0].to, [
            self.signal.reporter.email,
        ])
        self.assertEqual(mail.outbox[0].from_email,
                         settings.DEFAULT_FROM_EMAIL)
        self.assertIn(settings.ORGANIZATION_NAME, mail.outbox[0].body)

        self.assertIn('10 oktober 2018 12:00', mail.outbox[0].body)
        self.assertIn(category_assignment.stored_handling_message,
                      mail.outbox[0].body)

        # we want a history entry when a email was sent
        self.assertEqual(Note.objects.count(), 1)
Exemplo n.º 18
0
    def test_send_mail_reporter_created_send_mail_false(self):
        # Is the intended rule activated?
        actions = self._get_mail_rules(['Send mail signal created'])._get_actions(self.signal)
        self.assertEqual(len(actions), 1)

        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(set(actions), set(activated))

        # Check mail contents
        ma.apply(signal_id=self.signal.id, send_mail=False)

        # No mail should be sent
        self.assertEqual(len(mail.outbox), 0)

        # we want no history entry because no mail was sent
        self.assertEqual(Note.objects.count(), 0)
Exemplo n.º 19
0
    def test_send_mail_reporter_status_changed_afgehandeld_txt_and_html(self):
        # Prepare signal with status change from `BEHANDELING` to `AFGEHANDELD`.
        StatusFactory.create(_signal=self.signal, state=workflow.BEHANDELING)
        status = StatusFactory.create(_signal=self.signal,
                                      state=workflow.AFGEHANDELD)
        self.signal.status = status
        self.signal.save()

        # Is the intended rule activated?
        actions = self._get_mail_rules(['Send mail signal handled'
                                        ])._get_actions(self.signal)
        self.assertEqual(len(actions), 1)

        # Is it the only one that activates?
        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(set(actions), set(activated))

        # Check mail contents
        ma.apply(signal_id=self.signal.id)

        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(1, Feedback.objects.count())
        feedback = Feedback.objects.get(_signal__id=self.signal.id)

        message = mail.outbox[0]
        self.assertEqual(message.subject, f'Uw melding {self.signal.id}')
        self.assertEqual(message.to, [
            self.signal.reporter.email,
        ])
        self.assertEqual(mail.outbox[0].from_email,
                         settings.DEFAULT_FROM_EMAIL)

        positive_feedback_url, negative_feedback_url = get_feedback_urls(
            feedback)

        self.assertIn(positive_feedback_url, message.body)
        self.assertIn(negative_feedback_url, message.body)

        content, mime_type = message.alternatives[0]
        self.assertEqual(mime_type, 'text/html')
        self.assertIn(positive_feedback_url, content)
        self.assertIn(negative_feedback_url, content)

        # we want a history entry when a email was sent
        self.assertEqual(Note.objects.count(), 1)
Exemplo n.º 20
0
 def _get_mail_rules(self, action_names):
     """
     Get MailActions object instantiated with specific mail rules.
     """
     mail_rules = [
         r for r in SIGNAL_MAIL_RULES if r['name'] in set(action_names)
     ]
     return MailActions(mail_rules=mail_rules)
Exemplo n.º 21
0
    def test_send_mail_reporter_created_no_additional_context(self):
        # Is the intended rule activated?
        actions = self._get_mail_rules(['Send mail signal created'])._get_actions(self.signal)
        self.assertEqual(len(actions), 1)

        # We do not want to add extra context so we should remove the 'context'
        COPIED_SIGNAL_MAIL_RULES = [copy.deepcopy(r)
                                    for r in SIGNAL_MAIL_RULES if r['name'] == 'Send mail signal created']
        COPIED_SIGNAL_MAIL_RULES[0]['kwargs'].pop('context')

        # Is it the only one that activates?
        ma = MailActions(mail_rules=COPIED_SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(set(actions), set(activated))

        # Check mail contents
        ma.apply(signal_id=self.signal.id)
        self.assertEqual(len(mail.outbox), 1)
Exemplo n.º 22
0
    def test_reaction_requested_email(self):
        # "Reactie gevraagd" flow. Reporter is asked for additional information.
        status = StatusFactory.create(_signal=self.signal, state=workflow.REACTIE_GEVRAAGD, text='Was het mooi weer?')
        self.signal.status = status
        self.signal.save()

        # Is the intended rule activated?
        actions = self._get_mail_rules(['Send mail signal reaction request'])._get_actions(self.signal)
        self.assertEqual(len(actions), 1)

        # Is it the only one that activates?
        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(set(actions), set(activated))

        # Check mail contents
        ma.apply(signal_id=self.signal.id)
        self.assertEqual(len(mail.outbox), 1)
Exemplo n.º 23
0
    def test_email_template(self):
        self.assertEqual(str(self.email_template),
                         'Template title {{ signal_id }}')

        signal = SignalFactory.create(reporter__email=self.get_email())

        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        ma.apply(signal_id=signal.id)

        self.assertEqual(f'Template title {signal.id}', mail.outbox[0].subject)
        self.assertEqual(
            f'Template title\n\nThanks a lot for reporting {signal.id} {signal.text}\n'
            f'{settings.ORGANIZATION_NAME}\n\n', mail.outbox[0].body)

        body, mime_type = mail.outbox[0].alternatives[0]
        self.assertEqual(mime_type, 'text/html')
        self.assertIn('<h1>Template title</h1>', body)
        self.assertIn(
            f'<p>Thanks a lot for reporting <strong>{signal.id}</strong>',
            body)
Exemplo n.º 24
0
    def test_send_no_mail_reporter_status_changed_afgehandeld_after_verzoek_tot_heropenen(self):
        # Prepare signal with status change from `VERZOEK_TOT_HEROPENEN` to `AFGEHANDELD`,
        # this should not lead to an email being sent.
        StatusFactory.create(_signal=self.signal, state=workflow.VERZOEK_TOT_HEROPENEN)
        status = StatusFactory.create(_signal=self.signal, state=workflow.AFGEHANDELD)
        self.signal.status = status
        self.signal.save()

        # no mail rule should activate
        ma = MailActions(mail_rules=SIGNAL_MAIL_RULES)
        activated = ma._get_actions(self.signal)
        self.assertEqual(len(activated), 0)

        # Check mail contents
        ma.apply(signal_id=self.signal.id)
        self.assertEqual(0, Feedback.objects.count())
        self.assertEqual(len(mail.outbox), 0)

        # we want no history entry when no email was sent
        self.assertEqual(Note.objects.count(), 0)
Exemplo n.º 25
0
    def test_send_mail_reporter_created_only_once(self):
        signal = SignalFactory.create(reporter__email='*****@*****.**')

        status = StatusFactory.create(_signal=signal, state=workflow.BEHANDELING)
        signal.status = status
        signal.save()

        status = StatusFactory.create(_signal=signal, state=workflow.GEMELD)
        signal.status = status
        signal.save()

        MailActions(mail_rules=SIGNAL_MAIL_RULES).apply(signal_id=signal.id, send_mail=True)
        self.assertEqual(len(mail.outbox), 0)

        # we want no history entry when no email was sent
        self.assertEqual(Note.objects.count(), 0)
Exemplo n.º 26
0
def send_mail_reporter(pk):
    MailActions(mail_rules=SIGNAL_MAIL_RULES).apply(signal_id=pk)