From c37c3b425021eb485376b8b776c248afb2a9b2a6 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 23 Feb 2015 15:54:10 -0300 Subject: for #174, only 5.X.X are permanent bounces --- service/pixelated/adapter/model/mail.py | 10 +++++++++- service/test/unit/adapter/test_mail.py | 22 ++++++++++++++++++++++ 2 files changed, 31 insertions(+), 1 deletion(-) (limited to 'service') diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index a8d752c5..3ea7c95a 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -391,7 +391,10 @@ class PixelatedMail(Mail): def _extract_bounced_address(self, part): part_header = dict(part.get('headers', {})) if 'Final-Recipient' in part_header: - return part_header['Final-Recipient'].split(';')[1].strip() + if self._bounce_permanent(part_header): + return part_header['Final-Recipient'].split(';')[1].strip() + else: + return False elif 'part_map' in part: for subpart in part['part_map'].values(): result = self._extract_bounced_address(subpart) @@ -399,6 +402,11 @@ class PixelatedMail(Mail): return result else: continue + return False + + def _bounce_permanent(self, part_headers): + status = part_headers.get('Status', '') + return status.startswith('5') def as_dict(self): dict_mail = {'header': {k.lower(): v for k, v in self.headers.items()}, diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py index 9a99e450..4bf05d3f 100644 --- a/service/test/unit/adapter/test_mail.py +++ b/service/test/unit/adapter/test_mail.py @@ -227,6 +227,28 @@ class TestPixelatedMail(unittest.TestCase): self.assertEquals('this_mail_was_bounced@domain.com', bounced_mail.bounced) self.assertFalse(not_bounced_mail.bounced) + def test_ignore_transient_failures(self): + """ + Persistent errors should start with 5. + See: http://www.iana.org/assignments/smtp-enhanced-status-codes/smtp-enhanced-status-codes.xhtml + """ + bounced_mail_hdoc = os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'bounced_mail_hdoc.json') + with open(bounced_mail_hdoc) as f: + content = f.read() + # Change status to 4.XXX.YYY (only the first number is relevant here) + content = content.replace("5.1.1", "4.X.Y") + hdoc = json.loads(content) + + temporary_bounced_leap_mail = test_helper.leap_mail() + temporary_bounced_leap_mail[1].content = hdoc + temporary_bounced_mail = PixelatedMail.from_soledad(*temporary_bounced_leap_mail, soledad_querier=self.querier) + + not_bounced_leap_mail = test_helper.leap_mail() + not_bounced_mail = PixelatedMail.from_soledad(*not_bounced_leap_mail, soledad_querier=self.querier) + + self.assertFalse(temporary_bounced_mail.bounced) + self.assertFalse(not_bounced_mail.bounced) + def _create_bdoc(self, raw): class FakeBDoc: def __init__(self, raw): -- cgit v1.2.3