summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorAnton Khirnov <anton@khirnov.net>2021-01-30 13:33:27 +0100
committerAnton Khirnov <anton@khirnov.net>2021-01-30 13:33:27 +0100
commit5dfe5a2831adbe3ec129d2ede1d9039739e98b71 (patch)
treebf54920d084544c2e12cd532c94cf3bdcaa37f95 /tests
parent27e9478faefecf5b290c0fbd3df5b1fe9e18c97f (diff)
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.
Diffstat (limited to 'tests')
-rw-r--r--tests/db/test_envelope.py18
1 files changed, 11 insertions, 7 deletions
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 <http://www.gnu.org/licenses/>.
-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)