diff options
-rw-r--r-- | service/pixelated/adapter/model/mail.py | 8 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mail.py | 14 |
2 files changed, 21 insertions, 1 deletions
diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index f262d420..65e87f54 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -305,7 +305,13 @@ class PixelatedMail(Mail): def _get_date(self): date = self.hdoc.content.get('date', None) if not date: - date = self.hdoc.content['received'].split(";")[-1].strip() + received = self.hdoc.content.get('received', None) + if received: + date = received.split(";")[-1].strip() + else: + # we can't get a date for this mail, so lets just use now + logger.warning('Encountered a mail with missing date and received header fields. Subject %s' % self.hdoc.content.get('subject', None)) + date = pixelated.support.date.iso_now() return dateparser.parse(date).isoformat() @property diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py index 317db5de..5bb46687 100644 --- a/service/test/unit/adapter/test_mail.py +++ b/service/test/unit/adapter/test_mail.py @@ -31,6 +31,9 @@ class TestPixelatedMail(unittest.TestCase): def setUp(self): self.querier = mock() + def tearDown(self): + unstub() + def test_parse_date_from_soledad_uses_date_header_if_available(self): leap_mail_date = 'Wed, 3 Sep 2014 12:36:17 -0300' leap_mail_date_in_iso_format = "2014-09-03T12:36:17-03:00" @@ -52,6 +55,17 @@ class TestPixelatedMail(unittest.TestCase): self.assertEqual(str(mail.headers['Date']), leap_mail_date_in_iso_format) + def test_parse_date_from_soledad_fallback_to_now_if_neither_date_nor_received_header(self): + leap_mail_date_in_iso_format = "2014-09-03T13:11:15-03:00" + + when(pixelated.support.date).iso_now().thenReturn(leap_mail_date_in_iso_format) + fdoc, hdoc, bdoc = test_helper.leap_mail() + del hdoc.content['date'] + + mail = PixelatedMail.from_soledad(fdoc, hdoc, bdoc, soledad_querier=self.querier) + + self.assertEqual(str(mail.headers['Date']), leap_mail_date_in_iso_format) + def test_update_tags_return_a_set_with_the_current_tags(self): soledad_docs = test_helper.leap_mail(extra_headers={'X-tags': '["custom_1", "custom_2"]'}) pixelated_mail = PixelatedMail.from_soledad(*soledad_docs, soledad_querier=self.querier) |