summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorDylan Baker <dylan@pnwbakers.com>2017-08-10 14:03:07 -0700
committerDylan Baker <dylan@pnwbakers.com>2017-08-10 14:03:07 -0700
commit9770ecfbe656deb8e6e93e34c5a3e468f295e4b3 (patch)
treea238d91f67dd0a466a0e2aa5effc70ef5ce546a3 /tests
parent4f78529875c2687e58586c43512a71787f72fccb (diff)
tests: Add tests for SignCommand
I wrote these looking at a bug, but they turned out not to be helpful there, but they should be useful all the same.
Diffstat (limited to 'tests')
-rw-r--r--tests/commands/envelope_test.py140
1 files changed, 140 insertions, 0 deletions
diff --git a/tests/commands/envelope_test.py b/tests/commands/envelope_test.py
index 67caef02..1ae05514 100644
--- a/tests/commands/envelope_test.py
+++ b/tests/commands/envelope_test.py
@@ -27,6 +27,8 @@ import mock
from alot.commands import envelope
from alot.db.envelope import Envelope
+from alot.errors import GPGProblem
+from alot.settings.errors import NoMatchingAccount
# When using an assert from a mock a TestCase method might not use self. That's
# okay.
@@ -157,3 +159,141 @@ class TestTagCommands(unittest.TestCase):
def test_toggle_can_remove_and_add_in_one_run(self):
self._test(u'one,four', 'toggle', ['two', 'three', 'four'])
+
+
+class TestSignCommand(unittest.TestCase):
+
+ """Tests for the SignCommand class."""
+
+ @staticmethod
+ def _make_ui_mock():
+ """Create a mock for the ui and envelope and return them."""
+ envelope = mock.Mock()
+ envelope.sign = mock.sentinel.default
+ envelope.sign_key = mock.sentinel.default
+ envelope.headers = {'From': ['foo <foo@example.com>']}
+ ui = mock.Mock(current_buffer=mock.Mock(envelope=envelope))
+ return envelope, ui
+
+ @mock.patch('alot.commands.envelope.crypto.get_key',
+ mock.Mock(return_value=mock.sentinel.keyid))
+ def test_apply_keyid_success(self):
+ """If there is a valid keyid then key and to sign should be set.
+ """
+ env, ui = self._make_ui_mock()
+ # The actual keyid doesn't matter, since it'll be mocked anyway
+ cmd = envelope.SignCommand(action='sign', keyid=['a'])
+ cmd.apply(ui)
+
+ self.assertTrue(env.sign)
+ self.assertEqual(env.sign_key, mock.sentinel.keyid)
+
+ @mock.patch('alot.commands.envelope.crypto.get_key',
+ mock.Mock(side_effect=GPGProblem('sentinel', 0)))
+ def test_apply_keyid_gpgproblem(self):
+ """If there is an invalid keyid then the signing key and to sign should
+ be set to false and default.
+ """
+ env, ui = self._make_ui_mock()
+ # The actual keyid doesn't matter, since it'll be mocked anyway
+ cmd = envelope.SignCommand(action='sign', keyid=['a'])
+ cmd.apply(ui)
+ self.assertFalse(env.sign)
+ self.assertEqual(env.sign_key, mock.sentinel.default)
+ ui.notify.assert_called_once()
+
+ @mock.patch('alot.commands.envelope.settings.get_account_by_address',
+ mock.Mock(side_effect=NoMatchingAccount))
+ def test_apply_no_keyid_nomatchingaccount(self):
+ """If there is a nokeyid and no account can be found to match the From,
+ then the envelope should not be marked to sign.
+ """
+ env, ui = self._make_ui_mock()
+ # The actual keyid doesn't matter, since it'll be mocked anyway
+ cmd = envelope.SignCommand(action='sign', keyid=None)
+ cmd.apply(ui)
+
+ self.assertFalse(env.sign)
+ self.assertEqual(env.sign_key, mock.sentinel.default)
+ ui.notify.assert_called_once()
+
+ def test_apply_no_keyid_no_gpg_key(self):
+ """If there is a nokeyid and the account has no gpg key then the
+ signing key and to sign should be set to false and default.
+ """
+ env, ui = self._make_ui_mock()
+
+ with mock.patch('alot.commands.envelope.settings.get_account_by_address',
+ mock.Mock(return_value=mock.Mock(gpg_key=None))):
+ cmd = envelope.SignCommand(action='sign', keyid=None)
+ cmd.apply(ui)
+
+ self.assertFalse(env.sign)
+ self.assertEqual(env.sign_key, mock.sentinel.default)
+ ui.notify.assert_called_once()
+
+ def test_apply_no_keyid_default(self):
+ """If there is no keyid and the account has a gpg key, then that should
+ be used.
+ """
+ env, ui = self._make_ui_mock()
+
+ with mock.patch('alot.commands.envelope.settings.get_account_by_address',
+ mock.Mock(return_value=mock.Mock(gpg_key='sentinel'))):
+ cmd = envelope.SignCommand(action='sign', keyid=None)
+ cmd.apply(ui)
+
+ self.assertTrue(env.sign)
+ self.assertEqual(env.sign_key, 'sentinel')
+
+ @mock.patch('alot.commands.envelope.crypto.get_key',
+ mock.Mock(return_value=mock.sentinel.keyid))
+ def test_apply_no_sign(self):
+ """If signing with a valid keyid and valid key then set sign and
+ sign_key.
+ """
+ env, ui = self._make_ui_mock()
+ # The actual keyid doesn't matter, since it'll be mocked anyway
+ cmd = envelope.SignCommand(action='sign', keyid=['a'])
+ cmd.apply(ui)
+
+ self.assertTrue(env.sign)
+ self.assertEqual(env.sign_key, mock.sentinel.keyid)
+
+ @mock.patch('alot.commands.envelope.crypto.get_key',
+ mock.Mock(return_value=mock.sentinel.keyid))
+ def test_apply_unsign(self):
+ """Test that settingun sign sets the sign to False if all other
+ conditions allow for it.
+ """
+ env, ui = self._make_ui_mock()
+ env.sign = True
+ env.sign_key = mock.sentinel
+ # The actual keyid doesn't matter, since it'll be mocked anyway
+ cmd = envelope.SignCommand(action='unsign', keyid=['a'])
+ cmd.apply(ui)
+
+ self.assertFalse(env.sign)
+ self.assertIs(env.sign_key, None)
+
+ @mock.patch('alot.commands.envelope.crypto.get_key',
+ mock.Mock(return_value=mock.sentinel.keyid))
+ def test_apply_togglesign(self):
+ """Test that toggling changes the sign and sign_key as approriate if
+ other condtiions allow for it
+ """
+ env, ui = self._make_ui_mock()
+ env.sign = True
+ env.sign_key = mock.sentinel.keyid
+
+ # The actual keyid doesn't matter, since it'll be mocked anyway
+ # Test that togling from true to false works
+ cmd = envelope.SignCommand(action='toggle', keyid=['a'])
+ cmd.apply(ui)
+ self.assertFalse(env.sign)
+ self.assertIs(env.sign_key, None)
+
+ # Test that toggling back to True works
+ cmd.apply(ui)
+ self.assertTrue(env.sign)
+ self.assertIs(env.sign_key, mock.sentinel.keyid)