From 1ce0d130d0a042c8d8c4971efcd7848b35b0148d Mon Sep 17 00:00:00 2001 From: Michael J Gruber Date: Fri, 6 Sep 2019 10:52:49 +0200 Subject: tests: test prefer_text with HTML-only mails --- tests/db/test_utils.py | 37 ++++++++++++++++++++++++++++++++++--- 1 file changed, 34 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/db/test_utils.py b/tests/db/test_utils.py index ac6555c0..59a46435 100644 --- a/tests/db/test_utils.py +++ b/tests/db/test_utils.py @@ -641,7 +641,6 @@ class TestExtractBody(unittest.TestCase): self.assertEqual(actual, expected) def _make_mixed_plain_html(self): - mail = EmailMessage() self._set_basic_headers(mail) mail.set_content('This is an email') @@ -651,7 +650,7 @@ class TestExtractBody(unittest.TestCase): return mail @mock.patch('alot.db.utils.settings.get', mock.Mock(return_value=True)) - def test_prefer_plaintext(self): + def test_prefer_plaintext_mixed(self): expected = 'This is an email\n' mail = self._make_mixed_plain_html() actual = utils.extract_body(mail) @@ -663,19 +662,51 @@ class TestExtractBody(unittest.TestCase): @mock.patch('alot.db.utils.settings.get', mock.Mock(return_value=False)) @mock.patch('alot.db.utils.settings.mailcap_find_match', mock.Mock(return_value=(None, {'view': 'cat'}))) - def test_prefer_html(self): + def test_prefer_html_mixed(self): expected = 'This is an html email\n' mail = self._make_mixed_plain_html() actual = utils.extract_body(mail) self.assertEqual(actual, expected) + def _make_html_only(self): + mail = EmailMessage() + self._set_basic_headers(mail) + mail.set_content( + 'This is an html email', + subtype='html') + return mail + + @unittest.expectedFailure + @mock.patch('alot.db.utils.settings.get', mock.Mock(return_value=True)) + @mock.patch('alot.db.utils.settings.mailcap_find_match', + mock.Mock(return_value=(None, {'view': 'cat'}))) + def test_prefer_plaintext_only(self): + expected = 'This is an html email\n' + mail = self._make_html_only() + actual = utils.extract_body(mail) + + self.assertEqual(actual, expected) + + # Mock the handler to cat, so that no transformations of the html are made + # making the result non-deterministic + @mock.patch('alot.db.utils.settings.get', mock.Mock(return_value=False)) + @mock.patch('alot.db.utils.settings.mailcap_find_match', + mock.Mock(return_value=(None, {'view': 'cat'}))) + def test_prefer_html_only(self): + expected = 'This is an html email\n' + mail = self._make_html_only() + actual = utils.extract_body(mail) + + self.assertEqual(actual, expected) + def test_simple_utf8_file(self): mail = email.message_from_binary_file( open('tests/static/mail/utf8.eml', 'rb'), _class=email.message.EmailMessage) actual = utils.extract_body(mail) expected = "Liebe Grüße!\n" + self.assertEqual(actual, expected) class TestMessageFromString(unittest.TestCase): -- cgit v1.2.3