diff options
-rw-r--r-- | service/pixelated/adapter/pixelated_mail.py | 13 | ||||
-rw-r--r-- | service/test/adapter/pixelated_mail_test.py | 22 |
2 files changed, 32 insertions, 3 deletions
diff --git a/service/pixelated/adapter/pixelated_mail.py b/service/pixelated/adapter/pixelated_mail.py index d20a93f4..7d34ac4f 100644 --- a/service/pixelated/adapter/pixelated_mail.py +++ b/service/pixelated/adapter/pixelated_mail.py @@ -31,7 +31,7 @@ class PixelatedMail: mail.leap_mail = leap_mail mail.body = leap_mail.bdoc.content['raw'] mail.headers = mail._extract_headers() - mail.date = dateparser.parse(mail.headers['date']) + mail.date = PixelatedMail._get_date(mail.headers) mail.ident = leap_mail.getUID() mail.status = mail._extract_status() mail.security_casing = {} @@ -76,8 +76,10 @@ class PixelatedMail: def as_dict(self): tags = [tag.name for tag in self.tags] statuses = [status.name for status in self.status] + _headers = self.headers.copy() + _headers['date'] = self.date return { - 'header': self.headers, + 'header': _headers, 'ident': self.ident, 'tags': tags, 'status': statuses, @@ -103,6 +105,13 @@ class PixelatedMail: def from_dict(mail_dict): return from_dict(mail_dict) + @classmethod + def _get_date(cls, headers): + date = headers.get('date', None) + if not date: + date = headers['received'].split(";")[-1].strip() + return dateparser.parse(date).isoformat() + def from_dict(mail_dict): mail = PixelatedMail() diff --git a/service/test/adapter/pixelated_mail_test.py b/service/test/adapter/pixelated_mail_test.py index e0b9a662..c9146f24 100644 --- a/service/test/adapter/pixelated_mail_test.py +++ b/service/test/adapter/pixelated_mail_test.py @@ -33,6 +33,27 @@ class TestPixelatedMail(unittest.TestCase): 'tags': ['sent'] } + def test_parse_date_from_leap_mail_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-03 12:36:17-03:00" + + leap_mail = test_helper.leap_mail(headers={'date': leap_mail_date}) + + mail = PixelatedMail.from_leap_mail(leap_mail) + + self.assertEqual(str(mail.date), leap_mail_date_in_iso_format) + + def test_parse_date_from_leap_mail_fallback_to_received_header_if_date_header_isnt_available(self): + leap_mail_date = "Wed, 03 Sep 2014 13:11:15 -0300" + leap_mail_date_in_iso_format = "2014-09-03 13:11:15-03:00" + leap_mail_received_header = "by bitmask.local from 127.0.0.1 with ESMTP ;\n " + leap_mail_date + + leap_mail = test_helper.leap_mail(headers={'received': leap_mail_received_header}) + + mail = PixelatedMail.from_leap_mail(leap_mail) + + self.assertEqual(str(mail.date), leap_mail_date_in_iso_format) + def test_leap_recent_flag_is_translated_to_inbox_tag(self): pixelated_mail = PixelatedMail.from_leap_mail(test_helper.leap_mail(leap_flags=['\\Recent'])) self.assertIn(Tag('inbox'), pixelated_mail.tags) @@ -59,7 +80,6 @@ class TestPixelatedMail(unittest.TestCase): self.assertEquals(set([Tag('custom_tag'), Tag('inbox')]), pixelated_mail.tags) def test_from_dict(self): - mail = PixelatedMail.from_dict(self.mail_dict) self.assertEqual(mail.headers['cc'], ['cc@pixelated.org', 'anothercc@pixelated.org']) |