summaryrefslogtreecommitdiff
path: root/tests/commands
diff options
context:
space:
mode:
authorLucas Hoffmann <l-m-h@web.de>2017-06-22 23:47:58 +0200
committerLucas Hoffmann <l-m-h@web.de>2017-06-22 23:48:33 +0200
commitfb9659ee1869fb6b930ae9c15ad7ad24723596c3 (patch)
treeb320f0ad6f9fbe80db869afbd24dfd08e13f57c9 /tests/commands
parent1024e63ddcef5f7f198be6b25e81de29800a5992 (diff)
Add first test for commands.thread.determine_sender
Diffstat (limited to 'tests/commands')
-rw-r--r--tests/commands/thread_test.py72
1 files changed, 70 insertions, 2 deletions
diff --git a/tests/commands/thread_test.py b/tests/commands/thread_test.py
index 704039c1..4496fb03 100644
--- a/tests/commands/thread_test.py
+++ b/tests/commands/thread_test.py
@@ -6,9 +6,13 @@
"""Test suite for alot.commands.thread module."""
from __future__ import absolute_import
+import email
import unittest
+import mock
+
from alot.commands import thread
+from alot.account import Account
# Good descriptive test names often don't fit PEP8, which is meant to cover
# functions meant to be called by humans.
@@ -59,8 +63,8 @@ class TestClearMyAddress(unittest.TestCase):
def test_only_my_emails_result_in_empty_list(self):
expected = []
- actual = thread.ReplyCommand.clear_my_address(self.mine,
- self.mine+[self.me_named])
+ actual = thread.ReplyCommand.clear_my_address(
+ self.mine, self.mine+[self.me_named])
self.assertListEqual(actual, expected)
def test_other_emails_are_untouched(self):
@@ -87,3 +91,67 @@ class TestClearMyAddress(unittest.TestCase):
mine = 'alot team'
actual = thread.ReplyCommand.clear_my_address(mine, expected)
self.assertListEqual(actual, expected)
+
+
+class _AccountTestClass(Account):
+ """Implements stubs for ABC methods."""
+
+ def send_mail(self, mail):
+ pass
+
+
+class TestDetermineSender(unittest.TestCase):
+
+ header_priority = ["From", "To", "Cc", "Envelope-To", "X-Envelope-To",
+ "Delivered-To"]
+ mailstring = '\n'.join([
+ "From: from@example.com",
+ "To: to@example.com",
+ "Cc: cc@example.com",
+ "Envelope-To: envelope-to@example.com",
+ "X-Envelope-To: x-envelope-to@example.com",
+ "Delivered-To: delivered-to@example.com",
+ "Subject: Alot test",
+ "\n",
+ "Some content",
+ ])
+ mail = email.message_from_string(mailstring)
+
+ def _test(self, accounts=(), expected=(), mail=None, header_priority=None,
+ force_realname=False, force_address=False):
+ """This method collects most of the steps that need to be done for most
+ tests. Especially a closure to mock settings.get and a mock for
+ settings.get_accounts are set up."""
+ mail = self.mail if mail is None else mail
+ header_priority = self.header_priority if header_priority is None \
+ else header_priority
+
+ def settings_get(arg):
+ """Mock function for setting.get()"""
+ if arg == "reply_account_header_priority":
+ return header_priority
+ elif arg.endswith('_force_realname'):
+ return force_realname
+ elif arg.endswith('_force_address'):
+ return force_address
+
+ with mock.patch('alot.commands.thread.settings.get_accounts',
+ mock.Mock(return_value=accounts)):
+ with mock.patch('alot.commands.thread.settings.get', settings_get):
+ actual = thread.determine_sender(mail)
+ self.assertTupleEqual(actual, expected)
+
+ def test_assert_that_some_accounts_are_defined(self):
+ with mock.patch('alot.commands.thread.settings.get_accounts',
+ mock.Mock(return_value=[])) as cm1:
+ with self.assertRaises(AssertionError) as cm2:
+ thread.determine_sender(None)
+ expected = ('no accounts set!',)
+ cm1.assert_called_once_with()
+ self.assertTupleEqual(cm2.exception.args, expected)
+
+ def test_default_account_is_used_if_no_match_is_found(self):
+ account1 = _AccountTestClass(address='foo@example.com')
+ account2 = _AccountTestClass(address='bar@example.com')
+ expected = ('foo@example.com', account1)
+ self._test(accounts=[account1, account2], expected=expected)