summaryrefslogtreecommitdiff
path: root/tests/db
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2017-08-23 09:05:37 -0700
committerDylan Baker <dylan@pnwbakers.com>2017-08-28 15:07:15 -0700
commit599dfa61c82c3192c6ac9ef59a8950225e4575d0 (patch)
tree44bb90091f1473f776acd38379036ff68aba161b /tests/db
parentaeed119b86c6d1acb13b103eeafb6bf775d99762 (diff)
tests/db/utils: Add tests for extract_body function
there are probably still some corners with mailcap handling (parameters like copiousoutput) that are untested, but this covers a large swath of the functionality.
Diffstat (limited to 'tests/db')
-rw-r--r--tests/db/utils_test.py121
1 files changed, 121 insertions, 0 deletions
diff --git a/tests/db/utils_test.py b/tests/db/utils_test.py
index af5f2517..03db8500 100644
--- a/tests/db/utils_test.py
+++ b/tests/db/utils_test.py
@@ -1,5 +1,6 @@
# encoding: utf-8
# Copyright (C) 2017 Lucas Hoffmann
+# Copyright © 2017 Dylan Baker
# This file is released under the GNU GPL, version 3 or a later revision.
# For further details see the COPYING file
from __future__ import absolute_import
@@ -650,3 +651,123 @@ class TestMessageFromFile(TestCaseClassCleanup):
self.assertIn('This is some text', [n.get_payload() for n in m.walk()])
self.assertIn(utils.X_SIGNATURE_VALID_HEADER, m)
self.assertIn(utils.X_SIGNATURE_MESSAGE_HEADER, m)
+
+
+class TestExtractBody(unittest.TestCase):
+
+ @staticmethod
+ def _set_basic_headers(mail):
+ mail['Subject'] = 'Test email'
+ mail['To'] = 'foo@example.com'
+ mail['From'] = 'bar@example.com'
+
+ def test_single_text_plain(self):
+ mail = email.mime.text.MIMEText('This is an email')
+ self._set_basic_headers(mail)
+ actual = utils.extract_body(mail)
+
+ expected = 'This is an email'
+
+ self.assertEqual(actual, expected)
+
+ def test_two_text_plain(self):
+ mail = email.mime.multipart.MIMEMultipart()
+ self._set_basic_headers(mail)
+ mail.attach(email.mime.text.MIMEText('This is an email'))
+ mail.attach(email.mime.text.MIMEText('This is a second part'))
+
+ actual = utils.extract_body(mail)
+ expected = 'This is an email\n\nThis is a second part'
+
+ self.assertEqual(actual, expected)
+
+ def test_text_plain_and_other(self):
+ mail = email.mime.multipart.MIMEMultipart()
+ self._set_basic_headers(mail)
+ mail.attach(email.mime.text.MIMEText('This is an email'))
+ mail.attach(email.mime.application.MIMEApplication(b'1'))
+
+ actual = utils.extract_body(mail)
+ expected = 'This is an email'
+
+ self.assertEqual(actual, expected)
+
+ def test_text_plain_with_attachment_text(self):
+ mail = email.mime.multipart.MIMEMultipart()
+ self._set_basic_headers(mail)
+ mail.attach(email.mime.text.MIMEText('This is an email'))
+ attachment = email.mime.text.MIMEText('this shouldnt be displayed')
+ attachment['Content-Disposition'] = 'attachment'
+ mail.attach(attachment)
+
+ actual = utils.extract_body(mail)
+ expected = 'This is an email'
+
+ self.assertEqual(actual, expected)
+
+ def _make_mixed_plain_html(self):
+ mail = email.mime.multipart.MIMEMultipart()
+ self._set_basic_headers(mail)
+ mail.attach(email.mime.text.MIMEText('This is an email'))
+ mail.attach(email.mime.text.MIMEText(
+ '<!DOCTYPE html><html><body>This is an html email</body></html>',
+ 'html'))
+ return mail
+
+ @mock.patch('alot.db.utils.settings.get', mock.Mock(return_value=True))
+ def test_prefer_plaintext(self):
+ expected = 'This is an email'
+ mail = self._make_mixed_plain_html()
+ 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(self):
+ expected = '<!DOCTYPE html><html><body>This is an html email</body></html>'
+ mail = self._make_mixed_plain_html()
+ actual = utils.extract_body(mail)
+
+ self.assertEqual(actual, expected)
+
+ @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_types_provided(self):
+ # This should not return html, even though html is set to preferred
+ # since a types variable is passed
+ expected = 'This is an email'
+ mail = self._make_mixed_plain_html()
+ actual = utils.extract_body(mail, types=['text/plain'])
+
+ self.assertEqual(actual, expected)
+
+ @mock.patch('alot.db.utils.settings.mailcap_find_match',
+ mock.Mock(return_value=(None, {'view': 'cat'})))
+ def test_require_mailcap_stdin(self):
+ mail = email.mime.multipart.MIMEMultipart()
+ self._set_basic_headers(mail)
+ mail.attach(email.mime.text.MIMEText(
+ '<!DOCTYPE html><html><body>This is an html email</body></html>',
+ 'html'))
+ actual = utils.extract_body(mail)
+ expected = '<!DOCTYPE html><html><body>This is an html email</body></html>'
+
+ self.assertEqual(actual, expected)
+
+ @mock.patch('alot.db.utils.settings.mailcap_find_match',
+ mock.Mock(return_value=(None, {'view': 'cat %s'})))
+ def test_require_mailcap_file(self):
+ mail = email.mime.multipart.MIMEMultipart()
+ self._set_basic_headers(mail)
+ mail.attach(email.mime.text.MIMEText(
+ '<!DOCTYPE html><html><body>This is an html email</body></html>',
+ 'html'))
+ actual = utils.extract_body(mail)
+ expected = '<!DOCTYPE html><html><body>This is an html email</body></html>'
+
+ self.assertEqual(actual, expected)