def test_no_address_formatting(self):
        EmailTemplate.objects.create(
            key=EmailTemplate.SIGNAL_CREATED,
            title='Template title {{ signal_id }}',
            body=
            '{% if address %}{{ address|format_address:"O hl, P W" }}{% else %}Locatie is gepind op de kaart{% endif %}'  # noqa
        )

        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=None,
            location__stadsdeel=None,
            location__address=None,
        )

        MailService.status_mail(signal=signal)

        self.assertEqual(f'Template title {signal.id}', mail.outbox[0].subject)
        self.assertEqual('Locatie is gepind op de kaart\n\n',
                         mail.outbox[0].body)
    def test_address_formatting(self):
        EmailTemplate.objects.create(
            key=EmailTemplate.SIGNAL_CREATED,
            title='Template title {{ signal_id }}',
            body='{{ address|format_address:"O hl, P W" }}',
        )

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

        signal = SignalFactory.create(
            status__state='m',
            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,
        )

        MailService.status_mail(signal=signal)

        self.assertEqual(len(mail.outbox), 1)
        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)
예제 #3
0
    def test_organization_name_contains_quote(self):
        signal = SignalFactory.create(reporter__email='*****@*****.**')

        with self.settings(ORGANIZATION_NAME='Gemeente \'s-Hertogenbosch'):
            MailService.status_mail(signal=signal)

        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)
예제 #4
0
    def test_evil_input(self):
        evil_signal = SignalFactory.create(
            reporter__email='*****@*****.**',
            text='<script>alert("something evil");</script>')

        MailService.status_mail(signal=evil_signal)

        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)
예제 #5
0
    def test_only_send_feedback_negative_contact_mail(self):
        """
        Test to see if when a status is changed from VERZOEK_TOT_AFHANDELING to AFGEHANDELD and has allows_contact on
        the feedback to only send one email
        """
        self.assertEqual(len(mail.outbox), 0)
        signal = SignalFactory.create(
            status__state=workflow.VERZOEK_TOT_AFHANDELING,
            reporter__email='*****@*****.**')
        status = StatusFactory.create(_signal=signal,
                                      state=workflow.AFGEHANDELD)
        feedback = FeedbackFactory.create(
            allows_contact=True,
            _signal=signal,
        )
        feedback.save()
        signal.status = status  # change to new status AFGEHANDELD
        signal.save()

        self.assertTrue(MailService.status_mail(signal))
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject,
                         f'Meer over uw melding {signal.get_id_display()}')
        self.assertEqual(mail.outbox[0].to, [
            signal.reporter.email,
        ])
        self.assertEqual(mail.outbox[0].from_email,
                         settings.DEFAULT_FROM_EMAIL)
        self.assertEqual(Note.objects.count(), 1)
예제 #6
0
    def test_send_system_mail_feedback_received(self):
        """
        Test the sending of the sending mail from the mail services
        """
        EmailTemplate.objects.create(
            key=EmailTemplate.SIGNAL_FEEDBACK_RECEIVED,
            title='Uw Feedback is ontvangen',
            body='test text {{ feedback_text }} {{ feedback_text_extra }}')
        signal = SignalFactory.create(status__state=workflow.GEMELD,
                                      reporter__email='*****@*****.**')

        text = 'my text _1234567'
        text_extra = 'my extra text _extra_987654321'
        feedback = FeedbackFactory.create(
            _signal=signal,
            text=text,
            text_extra=text_extra,
        )
        result = MailService.system_mail(signal=signal,
                                         action_name='feedback_received',
                                         feedback=feedback)

        self.assertTrue(result)
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].to, [
            signal.reporter.email,
        ])
        self.assertEqual(mail.outbox[0].from_email,
                         settings.DEFAULT_FROM_EMAIL)
        self.assertIn(text, mail.outbox[0].body)
        self.assertIn(text_extra, mail.outbox[0].body)
        self.assertEqual(Note.objects.count(), 1)
예제 #7
0
    def test_send_email_no_actions(self):
        MailService._status_actions = ()

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

        signal = SignalFactory.create(status__state=workflow.GEMELD,
                                      reporter__email='*****@*****.**')
        self.assertFalse(MailService.status_mail(signal.pk))
        self.assertEqual(len(mail.outbox), 0)
예제 #8
0
    def test_email_template(self):
        self.assertEqual(str(self.email_template),
                         'Template title {{ signal_id }}')

        signal = SignalFactory.create(reporter__email='*****@*****.**')

        MailService.status_mail(signal=signal)

        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)
예제 #9
0
    def update(self, instance, validated_data):
        # TODO: consider whether using a StandardAnswer while overriding the
        # is_satisfied field should be considered an error condition and return
        # an HTTP 400.
        validated_data['submitted_at'] = timezone.now()

        # Check whether the relevant Signal instance should possibly be
        # reopened (i.e. transition to VERZOEK_TOT_HEROPENEN state).
        is_satisfied = validated_data['is_satisfied']

        # @TODO: When text field is depricated the following can be removed
        validated_data = merge_texts(validated_data)
        instance.text = None
        instance.text_list = validated_data['text_list']

        reopen = False
        if not is_satisfied:
            reopen = validate_answers(validated_data)

        # Reopen the Signal (melding) if need be.
        if reopen:
            signal = instance._signal

            # Only allow a request to reopen when in state workflow.AFGEHANDELD
            if signal.status.state == workflow.AFGEHANDELD:
                payload = {
                    'text':
                    'De melder is niet tevreden blijkt uit feedback. Zo nodig heropenen.',
                    'state': workflow.VERZOEK_TOT_HEROPENEN,
                }
                Signal.actions.update_status(payload, signal)

        instance = super().update(instance, validated_data)
        # trigger the mail to be after the instance update to have the new data
        if not is_satisfied and instance._signal.allows_contact:
            MailService.system_mail(signal=instance._signal,
                                    action_name='feedback_received',
                                    feedback=instance)
        return instance
예제 #10
0
    def test_send_status_email(self):
        self.assertEqual(len(mail.outbox), 0)

        signal = SignalFactory.create(status__state=workflow.GEMELD,
                                      reporter__email='*****@*****.**')
        self.assertTrue(MailService.status_mail(signal))
        self.assertEqual(len(mail.outbox), 1)
        self.assertEqual(mail.outbox[0].subject,
                         f'Uw melding {signal.get_id_display()}')
        self.assertEqual(mail.outbox[0].to, [
            signal.reporter.email,
        ])
        self.assertEqual(mail.outbox[0].from_email,
                         settings.DEFAULT_FROM_EMAIL)
        self.assertEqual(Note.objects.count(), 1)
예제 #11
0
def send_mail_reporter(pk):
    MailService.status_mail(signal=pk)