diff options
author | mfrankie <mfrankie@eumfranckie.local> | 2016-05-17 16:56:55 +0200 |
---|---|---|
committer | mfrankie <mfrankie@eumfranckie.local> | 2016-05-19 13:30:18 +0200 |
commit | 3fd43a0a3f45bfbb0ab6bde36ba7b353dbf535ef (patch) | |
tree | 1b6bd18bcbde7a6bca00f362c0502f7ce2d901d0 /service | |
parent | 053d51e8c6484f95b43c7b45e5b42fcb3e89784c (diff) |
remove duplicated mails, wip
Diffstat (limited to 'service')
-rw-r--r-- | service/pixelated/adapter/services/mail_sender.py | 18 | ||||
-rw-r--r-- | service/test/support/test_helper.py | 14 | ||||
-rw-r--r-- | service/test/unit/adapter/services/test_mail_sender.py | 26 |
3 files changed, 56 insertions, 2 deletions
diff --git a/service/pixelated/adapter/services/mail_sender.py b/service/pixelated/adapter/services/mail_sender.py index 4499adbd..08e81872 100644 --- a/service/pixelated/adapter/services/mail_sender.py +++ b/service/pixelated/adapter/services/mail_sender.py @@ -63,6 +63,9 @@ class MailSender(object): bccs = mail.bcc deferreds = [] + recipients = self._remove_canonical(mail, recipients) + self._remove_duplicates_form_cc_and_to(mail) + for recipient in recipients: self._define_bcc_field(mail, recipient, bccs) smtp_recipient = self._create_twisted_smtp_recipient(recipient) @@ -70,6 +73,20 @@ class MailSender(object): return defer.DeferredList(deferreds, fireOnOneErrback=False, consumeErrors=True) + def _remove_duplicates_form_cc_and_to(self, mail): + mail.headers['To'] = list(set(self._remove_duplicates(mail.to)).difference(set(mail.bcc))) + mail.headers['Cc'] = list((set(self._remove_duplicates(mail.cc)).difference(set(mail.bcc)).difference(set(mail.to)))) + + def _remove_canonical(self, mail, recipients): + mail.headers['To'] = self._remove_duplicates(map(self._remove_canonical_recipient, mail.to)) + mail.headers['Cc'] = self._remove_duplicates(map(self._remove_canonical_recipient, mail.cc)) + mail.headers['Bcc'] = self._remove_duplicates(map(self._remove_canonical_recipient, mail.bcc)) + + return self._remove_duplicates(map(self._remove_canonical_recipient, recipients)) + + def _remove_duplicates(self, recipient): + return list(set(recipient)) + def _define_bcc_field(self, mail, recipient, bccs): if recipient in bccs: mail.headers['Bcc'] = [recipient] @@ -92,7 +109,6 @@ class MailSender(object): def _create_twisted_smtp_recipient(self, recipient): # TODO: Better is fix Twisted instead - recipient = self._remove_canonical_recipient(recipient) return User(str(recipient), NOT_NEEDED, NOT_NEEDED, NOT_NEEDED) def _remove_canonical_recipient(self, recipient): diff --git a/service/test/support/test_helper.py b/service/test/support/test_helper.py index ebe8a897..57f41a6e 100644 --- a/service/test/support/test_helper.py +++ b/service/test/support/test_helper.py @@ -45,6 +45,20 @@ def mail_dict(): } +def duplicates_in_fields_mail_dict(): + return { + 'header': { + 'to': ['to@pixelated.org', 'another@pixelated.org', 'third@pixelated.org'], + 'cc': ['cc@pixelated.org', 'another@pixelated.org', 'third@pixelated.org'], + 'bcc': ['bcc@pixelated.org', 'another@pixelated.org'], + 'subject': 'Subject' + }, + 'body': 'Body', + 'ident': '', + 'tags': [] + } + + class TestDoc(object): def __init__(self, content): self.content = content diff --git a/service/test/unit/adapter/services/test_mail_sender.py b/service/test/unit/adapter/services/test_mail_sender.py index 9a7e8fea..77435b66 100644 --- a/service/test/unit/adapter/services/test_mail_sender.py +++ b/service/test/unit/adapter/services/test_mail_sender.py @@ -22,7 +22,7 @@ from pixelated.adapter.services.mail_sender import MailSender, MailSenderExcepti from pixelated.adapter.model.mail import InputMail from pixelated.bitmask_libraries.smtp import LeapSMTPConfig from pixelated.support.functional import flatten -from test.support.test_helper import mail_dict +from test.support.test_helper import mail_dict, duplicates_in_fields_mail_dict from twisted.internet import reactor, defer from twisted.internet.defer import Deferred from mockito.matchers import Matcher @@ -101,6 +101,30 @@ class MailSenderTest(unittest.TestCase): for recipient in flatten([input_mail.to, input_mail.cc, input_mail.bcc]): verify(OutgoingMail).send_message(MailToSmtpFormatCapture(recipient, bccs), TwistedSmtpUserCapture(recipient)) + + @defer.inlineCallbacks + def test_if_recipent_doubled_in_fields_send_only_in_bcc(self): + input_mail = InputMail.from_dict(duplicates_in_fields_mail_dict(), from_address='pixelated@org') + when(OutgoingMail).send_message(any(), any()).thenReturn(defer.succeed(None)) + + yield self.sender.sendmail(input_mail) + + self.assertIn('to@pixelated.org', input_mail.to) + self.assertNotIn('another@pixelated.org', input_mail.to) + #self.assertIn('another@pixelated.org', input_mail.bcc) + + @defer.inlineCallbacks + def test_if_recipent_doubled_in_fields_send_only_in_to(self): + input_mail = InputMail.from_dict(duplicates_in_fields_mail_dict(), from_address='pixelated@org') + when(OutgoingMail).send_message(any(), any()).thenReturn(defer.succeed(None)) + + yield self.sender.sendmail(input_mail) + + self.assertIn('third@pixelated.org', input_mail.to) + self.assertNotIn('third@pixelated.org', input_mail.cc) + self.assertIn('cc@pixelated.org', input_mail.cc) + self.assertNotIn(['third@pixelated.org', 'another@pixelated.org'], input_mail.cc) + def test_remove_canonical_recipient_when_it_is_not_canonical(self): recipient = u'user@pixelated.org' |