From bd945cf18739409cf4727f860d4eb9a6a68c80c9 Mon Sep 17 00:00:00 2001 From: Kelsey Gilmore-Innis Date: Tue, 24 Jan 2017 15:22:02 -0800 Subject: [PATCH 1/2] allow reports to be sent to multiple recipients --- .../0009_to_address_to_textfield.py | 20 +++++++++++++++++ callisto/delivery/models.py | 2 +- callisto/delivery/report_delivery.py | 4 ++-- tests/callistocore/test_views.py | 22 ++++++++++++++++++- 4 files changed, 44 insertions(+), 4 deletions(-) create mode 100644 callisto/delivery/migrations/0009_to_address_to_textfield.py diff --git a/callisto/delivery/migrations/0009_to_address_to_textfield.py b/callisto/delivery/migrations/0009_to_address_to_textfield.py new file mode 100644 index 000000000..0779ec62b --- /dev/null +++ b/callisto/delivery/migrations/0009_to_address_to_textfield.py @@ -0,0 +1,20 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-01-24 15:20 +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('delivery', '0008_make_salt_nullable'), + ] + + operations = [ + migrations.AlterField( + model_name='sentreport', + name='to_address', + field=models.CharField(max_length=4096), + ), + ] diff --git a/callisto/delivery/models.py b/callisto/delivery/models.py index 1e8c180c9..497b6f8fc 100644 --- a/callisto/delivery/models.py +++ b/callisto/delivery/models.py @@ -295,7 +295,7 @@ class SentReport(PolymorphicModel): """Report of one or more incidents, sent to the monitoring organization""" # TODO: store link to s3 backup https://github.com/SexualHealthInnovations/callisto-core/issues/14 sent = models.DateTimeField(auto_now_add=True) - to_address = models.EmailField(blank=False, null=False, max_length=256) + to_address = models.CharField(blank=False, null=False, max_length=4096) def _get_id_for_schools(self, is_match): return "{0}-{1}-{2}".format(settings.SCHOOL_REPORT_PREFIX, '%05d' % self.id, 0 if is_match else 1) diff --git a/callisto/delivery/report_delivery.py b/callisto/delivery/report_delivery.py index 194392855..0102f3716 100644 --- a/callisto/delivery/report_delivery.py +++ b/callisto/delivery/report_delivery.py @@ -271,9 +271,9 @@ def func(canvas, doc): def send_email_to_coordinator(self, pdf_to_attach, notification_name, report_id): notification = EmailNotification.objects.get(name=notification_name) - to = settings.COORDINATOR_EMAIL + to_addresses = [x.strip() for x in settings.COORDINATOR_EMAIL.split(',')] - email = EmailMultiAlternatives(notification.subject, notification.render_body_plain(), self.from_email, [to]) + email = EmailMultiAlternatives(notification.subject, notification.render_body_plain(), self.from_email, to_addresses) email.attach_alternative(notification.render_body(), "text/html") gpg = gnupg.GPG() diff --git a/tests/callistocore/test_views.py b/tests/callistocore/test_views.py index 91f28875d..a71c59b62 100644 --- a/tests/callistocore/test_views.py +++ b/tests/callistocore/test_views.py @@ -16,7 +16,9 @@ from django.test.utils import override_settings from callisto.delivery.forms import NewSecretKeyForm, SecretKeyForm -from callisto.delivery.models import EmailNotification, MatchReport, Report +from callisto.delivery.models import ( + EmailNotification, MatchReport, Report, SentFullReport, +) from callisto.evaluation.models import EvalRow from .forms import EncryptedFormWizard @@ -475,6 +477,24 @@ def test_submit_sends_report(self): self.assertEqual(message.to, ['titleix@example.com']) self.assertRegexpMatches(message.attachments[0][0], 'report_.*\\.pdf\\.gpg') + @override_settings(COORDINATOR_EMAIL='titleix1@example.com,titleix2@example.com') + def test_submit_sends_report_to_multiple_coordinators(self): + response = self.client.post((self.submission_url % self.report.pk), + data={'name': 'test submitter', + 'email': 'test@example.com', + 'phone_number': '555-555-1212', + 'email_confirmation': "False", + 'key': self.report_key}) + self.assertEqual(response.status_code, 200) + self.assertNotIn('submit_error', response.context) + self.assertEqual(len(mail.outbox), 1) + self.assertEqual(SentFullReport.objects.count(), 1) + message1 = mail.outbox[0] + self.assertEqual(message1.subject, 'test delivery') + self.assertIn('"Reports" Date: Tue, 24 Jan 2017 15:33:43 -0800 Subject: [PATCH 2/2] add test to cover migration away from EmailField --- tests/callistocore/test_migrations.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/callistocore/test_migrations.py b/tests/callistocore/test_migrations.py index 9b7fa54ae..d79ddecdc 100644 --- a/tests/callistocore/test_migrations.py +++ b/tests/callistocore/test_migrations.py @@ -92,3 +92,28 @@ def test_matches_after_encryption(self, mock_process): find_matches([identifier]) # have to use ANY because objects in migration tests are faked mock_process.assert_called_once_with([ANY, ANY], 'test_identifier', PDFMatchReport) + + +class MultipleRecipientMigrationTest(MigrationTest): + + app_name = 'delivery' + before = '0008_make_salt_nullable' + after = '0009_to_address_to_textfield' + + def test_recipient_data_is_migrated(self): + + user = User.objects.create_user(username="dummy", password="dummy") + Report = self.get_model_before('Report') + report = Report(owner_id=user.pk) + report.save() + SentFullReport = self.get_model_before('SentFullReport') + sent_report = SentFullReport.objects.create(report_id=report.pk, to_address="test@example.com") + sent_report.save() + self.assertEqual(SentFullReport.objects.count(), 1) + + self.run_migration() + + SentFullReport = self.get_model_after('SentFullReport') + self.assertEqual(SentFullReport.objects.count(), 1) + sent_report = SentFullReport.objects.first() + self.assertEqual(sent_report.to_address, 'test@example.com')