From b981bfe0c61d9aa55652b4b0a01b846aaa9c993a Mon Sep 17 00:00:00 2001 From: vrs Date: Sat, 8 Dec 2018 22:20:05 +0100 Subject: attach accounts to envelopes simplifies a few tests, fixes #1177 --- tests/commands/envelope_test.py | 14 +++---- tests/commands/global_test.py | 87 +++++++++++++++-------------------------- tests/commands/utils_tests.py | 10 ++--- 3 files changed, 41 insertions(+), 70 deletions(-) (limited to 'tests') diff --git a/tests/commands/envelope_test.py b/tests/commands/envelope_test.py index 3efce261..b09cddaa 100644 --- a/tests/commands/envelope_test.py +++ b/tests/commands/envelope_test.py @@ -206,11 +206,10 @@ class TestSignCommand(unittest.TestCase): signing key and to sign should be set to false and default. """ env, ui = self._make_ui_mock() + env.account = mock.Mock(gpg_key=None) - 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) + cmd = envelope.SignCommand(action='sign', keyid=None) + cmd.apply(ui) self.assertFalse(env.sign) self.assertEqual(env.sign_key, mock.sentinel.default) @@ -221,11 +220,10 @@ class TestSignCommand(unittest.TestCase): be used. """ env, ui = self._make_ui_mock() + env.account = mock.Mock(gpg_key='sentinel') - 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) + cmd = envelope.SignCommand(action='sign', keyid=None) + cmd.apply(ui) self.assertTrue(env.sign) self.assertEqual(env.sign_key, 'sentinel') diff --git a/tests/commands/global_test.py b/tests/commands/global_test.py index e5503305..9b026e6a 100644 --- a/tests/commands/global_test.py +++ b/tests/commands/global_test.py @@ -58,21 +58,15 @@ class TestComposeCommand(unittest.TestCase): @utilities.async_test async def test_apply_sign_by_default_okay(self): envelope = self._make_envelope_mock() - account = self._make_account_mock() + envelope.account = self._make_account_mock() cmd = g_commands.ComposeCommand(envelope=envelope) - # This whole mess is required becasue ComposeCommand.apply is waaaaay - # too complicated, it needs to be split into more manageable segments. - with mock.patch('alot.commands.globals.settings.get_account_by_address', - mock.Mock(return_value=account)): - with mock.patch('alot.commands.globals.settings.get_accounts', - mock.Mock(return_value=[account])): - with mock.patch('alot.commands.globals.settings.get_addressbooks', - mock.Mock(side_effect=Stop)): - try: - await cmd.apply(mock.Mock()) - except Stop: - pass + with mock.patch('alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)): + try: + await cmd.apply(mock.Mock()) + except Stop: + pass self.assertTrue(envelope.sign) self.assertIs(envelope.sign_key, mock.sentinel.gpg_key) @@ -80,21 +74,15 @@ class TestComposeCommand(unittest.TestCase): @utilities.async_test async def test_apply_sign_by_default_false_doesnt_set_key(self): envelope = self._make_envelope_mock() - account = self._make_account_mock(sign_by_default=False) + envelope.account = self._make_account_mock(sign_by_default=False) cmd = g_commands.ComposeCommand(envelope=envelope) - # This whole mess is required becasue ComposeCommand.apply is waaaaay - # too complicated, it needs to be split into more manageable segments. - with mock.patch('alot.commands.globals.settings.get_account_by_address', - mock.Mock(return_value=account)): - with mock.patch('alot.commands.globals.settings.get_accounts', - mock.Mock(return_value=[account])): - with mock.patch('alot.commands.globals.settings.get_addressbooks', - mock.Mock(side_effect=Stop)): - try: - await cmd.apply(mock.Mock()) - except Stop: - pass + with mock.patch('alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)): + try: + await cmd.apply(mock.Mock()) + except Stop: + pass self.assertFalse(envelope.sign) self.assertIs(envelope.sign_key, None) @@ -102,22 +90,15 @@ class TestComposeCommand(unittest.TestCase): @utilities.async_test async def test_apply_sign_by_default_but_no_key(self): envelope = self._make_envelope_mock() - account = self._make_account_mock(gpg_key=None) + envelope.account = self._make_account_mock(gpg_key=None) cmd = g_commands.ComposeCommand(envelope=envelope) - # This whole mess is required becasue ComposeCommand.apply is waaaaay - # too complicated, it needs to be split into more manageable segments. - with mock.patch('alot.commands.globals.settings.get_account_by_address', - mock.Mock(return_value=account)): - with mock.patch('alot.commands.globals.settings.get_accounts', - mock.Mock(return_value=[account])): - with mock.patch('alot.commands.globals.settings.get_addressbooks', - mock.Mock(side_effect=Stop)): - with self.assertLogs(level=logging.WARNING): - try: - await cmd.apply(mock.Mock()) - except Stop: - pass + with mock.patch('alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)): + try: + await cmd.apply(mock.Mock()) + except Stop: + pass self.assertFalse(envelope.sign) self.assertIs(envelope.sign_key, None) @@ -138,7 +119,7 @@ class TestComposeCommand(unittest.TestCase): # Crutch to exit the giant `apply` method early. with mock.patch( - 'alot.commands.globals.settings.get_account_by_address', + 'alot.commands.globals.settings.get_accounts', mock.Mock(side_effect=Stop)): try: await cmd.apply(mock.Mock()) @@ -155,23 +136,17 @@ class TestComposeCommand(unittest.TestCase): # issue #1277 envelope = self._make_envelope_mock() del envelope.headers['From'] - account = self._make_account_mock() - account.realname = "foo" - account.address = 1 # maybe this should be a real Address? + envelope.account = self._make_account_mock() + envelope.account.realname = "foo" + envelope.account.address = 1 # maybe this should be a real Address? cmd = g_commands.ComposeCommand(envelope=envelope) - # This whole mess is required becasue ComposeCommand.apply is waaaaay - # too complicated, it needs to be split into more manageable segments. - with mock.patch('alot.commands.globals.settings.get_account_by_address', - mock.Mock(return_value=account)): - with mock.patch('alot.commands.globals.settings.get_accounts', - mock.Mock(return_value=[account])): - with mock.patch('alot.commands.globals.settings.get_addressbooks', - mock.Mock(side_effect=Stop)): - try: - await cmd.apply(mock.Mock()) - except Stop: - pass + with mock.patch('alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)): + try: + await cmd.apply(mock.Mock()) + except Stop: + pass class TestExternalCommand(unittest.TestCase): diff --git a/tests/commands/utils_tests.py b/tests/commands/utils_tests.py index 639fb1cd..0642ad32 100644 --- a/tests/commands/utils_tests.py +++ b/tests/commands/utils_tests.py @@ -183,9 +183,8 @@ class TestSetEncrypt(unittest.TestCase): envelope['To'] = 'ambig@example.com' gpg_key = crypto.get_key(FPR) account = _Account(encrypt_to_self=True, gpg_key=gpg_key) - with mock.patch('alot.commands.thread.settings.get_account_by_address', - mock.Mock(return_value=account)): - await utils.update_keys(ui, envelope) + envelope.account = account + await utils.update_keys(ui, envelope) self.assertTrue(envelope.encrypt) self.assertIn(FPR, envelope.encrypt_keys) self.assertEqual(gpg_key, envelope.encrypt_keys[FPR]) @@ -198,8 +197,7 @@ class TestSetEncrypt(unittest.TestCase): envelope['To'] = 'ambig@example.com' gpg_key = crypto.get_key(FPR) account = _Account(encrypt_to_self=False, gpg_key=gpg_key) - with mock.patch('alot.commands.thread.settings.get_account_by_address', - mock.Mock(return_value=account)): - await utils.update_keys(ui, envelope) + envelope.account = account + await utils.update_keys(ui, envelope) self.assertTrue(envelope.encrypt) self.assertNotIn(FPR, envelope.encrypt_keys) -- cgit v1.2.3