From cd35ec5f89cff3ba8c7780209efa7e8b0628744d Mon Sep 17 00:00:00 2001 From: Anton Khirnov Date: Sat, 30 Jan 2021 14:12:33 +0100 Subject: db/envelope: move to a new module "mail" It has nothing to do with the database. --- tests/commands/test_envelope.py | 2 +- tests/db/test_envelope.py | 113 ---------------------------------------- tests/mail/test_envelope.py | 113 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 114 deletions(-) delete mode 100644 tests/db/test_envelope.py create mode 100644 tests/mail/test_envelope.py (limited to 'tests') diff --git a/tests/commands/test_envelope.py b/tests/commands/test_envelope.py index d14cbf1f..9531cb57 100644 --- a/tests/commands/test_envelope.py +++ b/tests/commands/test_envelope.py @@ -24,7 +24,7 @@ import unittest from unittest import mock from alot.commands import envelope -from alot.db.envelope import Envelope +from alot.mail.envelope import Envelope from alot.errors import GPGProblem from alot.settings.errors import NoMatchingAccount from alot.settings.manager import SettingsManager diff --git a/tests/db/test_envelope.py b/tests/db/test_envelope.py deleted file mode 100644 index 0d46ba43..00000000 --- a/tests/db/test_envelope.py +++ /dev/null @@ -1,113 +0,0 @@ -# Copyright © 2017 Lucas Hoffmann -# Copyright © 2018 Dylan Baker -# -# This program is free software: you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation, either version 3 of the License, or -# (at your option) any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see . - -import email -import email.policy -import os -import tempfile -import unittest -from unittest import mock - -from alot.db import envelope - -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._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._compare_content(f, s) - with self.subTest('headers'): - self.assertListEqual(first.values(), second.values()) - - def test_setitem_stores_text_unchanged(self): - "Just ensure that the value is set and unchanged" - e = envelope.Envelope() - e['Subject'] = 'sm\xf8rebr\xf8d' - self.assertEqual(e['Subject'], 'sm\xf8rebr\xf8d') - - def _test_mail(self, envelope): - 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) - def test_construct_mail_simple(self): - """Very simple envelope with a To, From, Subject, and body.""" - headers = { - 'From': 'foo@example.com', - 'To': 'bar@example.com', - 'Subject': 'Test email', - } - e = envelope.Envelope(headers={k: [v] for k, v in headers.items()}, - bodytext='Test') - self._test_mail(e) - - @mock.patch('alot.db.envelope.settings', SETTINGS) - def test_construct_mail_with_attachment(self): - """Very simple envelope with a To, From, Subject, body and attachment. - """ - headers = { - 'From': 'foo@example.com', - 'To': 'bar@example.com', - 'Subject': 'Test email', - } - e = envelope.Envelope(headers={k: [v] for k, v in headers.items()}, - bodytext='Test') - with tempfile.NamedTemporaryFile(mode='wt', delete=False) as f: - f.write('blah') - self.addCleanup(os.unlink, f.name) - e.attach(f.name) - - self._test_mail(e) - - @mock.patch('alot.db.envelope.settings', SETTINGS) - def test_parse_template(self): - """Tests multi-line header and body parsing""" - raw = ( - 'From: foo@example.com\n' - 'To: bar@example.com,\n' - ' baz@example.com\n' - 'Subject: Fwd: Test email\n' - '\n' - 'Some body content: which is not a header.\n' - ) - envlp = envelope.Envelope() - envlp.parse_template(raw) - self.assertDictEqual(envlp.headers, { - 'From': ['foo@example.com'], - 'To': ['bar@example.com, baz@example.com'], - 'Subject': ['Fwd: Test email'] - }) - self.assertEqual(envlp.body, - 'Some body content: which is not a header.') diff --git a/tests/mail/test_envelope.py b/tests/mail/test_envelope.py new file mode 100644 index 00000000..e3034b48 --- /dev/null +++ b/tests/mail/test_envelope.py @@ -0,0 +1,113 @@ +# Copyright © 2017 Lucas Hoffmann +# Copyright © 2018 Dylan Baker +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see . + +import email +import email.policy +import os +import tempfile +import unittest +from unittest import mock + +from alot.mail import envelope + +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._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._compare_content(f, s) + with self.subTest('headers'): + self.assertListEqual(first.values(), second.values()) + + def test_setitem_stores_text_unchanged(self): + "Just ensure that the value is set and unchanged" + e = envelope.Envelope() + e['Subject'] = 'sm\xf8rebr\xf8d' + self.assertEqual(e['Subject'], 'sm\xf8rebr\xf8d') + + def _test_mail(self, envelope): + mail = envelope.construct_mail() + raw = mail.as_bytes() + actual = email.message_from_bytes(raw, policy = mail.policy) + self.assertEmailEqual(mail, actual) + + @mock.patch('alot.mail.envelope.settings', SETTINGS) + def test_construct_mail_simple(self): + """Very simple envelope with a To, From, Subject, and body.""" + headers = { + 'From': 'foo@example.com', + 'To': 'bar@example.com', + 'Subject': 'Test email', + } + e = envelope.Envelope(headers={k: [v] for k, v in headers.items()}, + bodytext='Test') + self._test_mail(e) + + @mock.patch('alot.mail.envelope.settings', SETTINGS) + def test_construct_mail_with_attachment(self): + """Very simple envelope with a To, From, Subject, body and attachment. + """ + headers = { + 'From': 'foo@example.com', + 'To': 'bar@example.com', + 'Subject': 'Test email', + } + e = envelope.Envelope(headers={k: [v] for k, v in headers.items()}, + bodytext='Test') + with tempfile.NamedTemporaryFile(mode='wt', delete=False) as f: + f.write('blah') + self.addCleanup(os.unlink, f.name) + e.attach(f.name) + + self._test_mail(e) + + @mock.patch('alot.mail.envelope.settings', SETTINGS) + def test_parse_template(self): + """Tests multi-line header and body parsing""" + raw = ( + 'From: foo@example.com\n' + 'To: bar@example.com,\n' + ' baz@example.com\n' + 'Subject: Fwd: Test email\n' + '\n' + 'Some body content: which is not a header.\n' + ) + envlp = envelope.Envelope() + envlp.parse_template(raw) + self.assertDictEqual(envlp.headers, { + 'From': ['foo@example.com'], + 'To': ['bar@example.com, baz@example.com'], + 'Subject': ['Fwd: Test email'] + }) + self.assertEqual(envlp.body, + 'Some body content: which is not a header.') -- cgit v1.2.3