diff options
author | Lisa Junger <ljunger@thoughtworks.com> | 2015-04-02 14:29:44 +0200 |
---|---|---|
committer | Lisa Junger <ljunger@thoughtworks.com> | 2015-04-02 17:54:22 +0200 |
commit | 98ec21fedd2b27c6f28be993006380446f6b4abe (patch) | |
tree | 9fc14286c1a7a2e93cc26e5338a2d558a97dad93 | |
parent | f11d9b2811373de173f0e02ec334c85e46571172 (diff) |
catch invalid data in received header when parsing date.
- Issue #347
-rw-r--r-- | service/pixelated/adapter/model/mail.py | 22 | ||||
-rw-r--r-- | service/test/unit/adapter/test_mail.py | 15 |
2 files changed, 26 insertions, 11 deletions
diff --git a/service/pixelated/adapter/model/mail.py b/service/pixelated/adapter/model/mail.py index ff58b330..618b980a 100644 --- a/service/pixelated/adapter/model/mail.py +++ b/service/pixelated/adapter/model/mail.py @@ -323,15 +323,19 @@ class PixelatedMail(Mail): def _get_date(self): date = self.hdoc.content.get('date', None) - if not date: - 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() + try: + if not date: + 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() + except (ValueError, TypeError) as e: + date = pixelated.support.date.iso_now() + return dateparser.parse(date).isoformat() @property def security_casing(self): diff --git a/service/test/unit/adapter/test_mail.py b/service/test/unit/adapter/test_mail.py index dc683bb6..d77816cd 100644 --- a/service/test/unit/adapter/test_mail.py +++ b/service/test/unit/adapter/test_mail.py @@ -68,16 +68,27 @@ class TestPixelatedMail(unittest.TestCase): self.assertEqual(str(mail.headers['Date']), leap_mail_date_in_iso_format) def test_use_datetime_now_as_fallback_for_invalid_date(self): - leap_mail_date = 'söme däte' + leap_mail_date = u'söme däte' date_expected = "2014-09-03T13:11:15-03:00" - when(pixelated.support.date).iso_now().thenReturn(date_expected) + when(pixelated.support.date).iso_now().thenReturn(date_expected) leap_mail = test_helper.leap_mail(headers={'date': leap_mail_date}) mail = PixelatedMail.from_soledad(*leap_mail, soledad_querier=self.querier) self.assertEqual(str(mail.headers['Date']), date_expected) + def test_fall_back_to_ascii_if_invalid_received_header(self): + leap_mail_received_header = u"söme invalid received heäder\n" + date_expected = "2014-09-03T13:11:15-03:00" + + when(pixelated.support.date).iso_now().thenReturn(date_expected) + leap_mail = test_helper.leap_mail(headers={'received': leap_mail_received_header}) + + mail = PixelatedMail.from_soledad(*leap_mail, soledad_querier=self.querier) + + self.assertEqual(mail.headers['Date'], date_expected) + 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) |