From 5dfe5a2831adbe3ec129d2ede1d9039739e98b71 Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 30 Jan 2021 13:33:27 +0100 Subject: db/envelope: switch to the "new" (EmailMessage) python API email.mime is a part of the old API, which does not mix well with the new one (i.e. when email.policy.SMTP is used), specifically when non-ASCII headers are used. Additionally, clean the APIs that accept either EmailMessage or a str to only expect EmailMessage. Supporting both just adds confusion and complexity. --- tests/db/test_envelope.py | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) (limited to 'tests') diff --git a/tests/db/test_envelope.py b/tests/db/test_envelope.py index 86e481bf..0d46ba43 100644 --- a/tests/db/test_envelope.py +++ b/tests/db/test_envelope.py @@ -14,7 +14,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . -import email.parser +import email import email.policy import os import tempfile @@ -27,21 +27,25 @@ SETTINGS = { 'user_agent': 'agent', } - class TestEnvelope(unittest.TestCase): + def _compare_content(self, first, second): + c1 = first.get_content().replace('\r\n', '\n') + c2 = second.get_content().replace('\r\n', '\n') + self.assertEqual(c1, c2) + def assertEmailEqual(self, first, second): with self.subTest('body'): self.assertEqual(first.is_multipart(), second.is_multipart()) if not first.is_multipart(): - self.assertEqual(first.get_payload(), second.get_payload()) + self._compare_content(first, second) else: for f, s in zip(first.walk(), second.walk()): if f.is_multipart() or s.is_multipart(): self.assertEqual(first.is_multipart(), second.is_multipart()) else: - self.assertEqual(f.get_payload(), s.get_payload()) + self._compare_content(f, s) with self.subTest('headers'): self.assertListEqual(first.values(), second.values()) @@ -52,9 +56,9 @@ class TestEnvelope(unittest.TestCase): self.assertEqual(e['Subject'], 'sm\xf8rebr\xf8d') def _test_mail(self, envelope): - mail = envelope.construct_mail() - raw = mail.as_string(policy=email.policy.SMTP) - actual = email.parser.Parser().parsestr(raw) + mail = envelope.construct_mail() + raw = mail.as_bytes() + actual = email.message_from_bytes(raw, policy = mail.policy) self.assertEmailEqual(mail, actual) @mock.patch('alot.db.envelope.settings', SETTINGS) -- cgit v1.2.3