diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/commands/envelope_test.py | 12 | ||||
-rw-r--r-- | tests/commands/global_test.py | 97 | ||||
-rw-r--r-- | tests/commands/thread_test.py | 4 | ||||
-rw-r--r-- | tests/commands/utils_tests.py | 6 | ||||
-rw-r--r-- | tests/completion_test.py | 6 | ||||
-rw-r--r-- | tests/crypto_test.py | 24 | ||||
-rw-r--r-- | tests/db/utils_test.py | 34 |
7 files changed, 118 insertions, 65 deletions
diff --git a/tests/commands/envelope_test.py b/tests/commands/envelope_test.py index ee1c0acc..30e21c03 100644 --- a/tests/commands/envelope_test.py +++ b/tests/commands/envelope_test.py @@ -230,8 +230,10 @@ class TestSignCommand(unittest.TestCase): """ 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))): + func_patcher = mock.patch( + 'alot.commands.envelope.settings.get_account_by_address', + mock.Mock(return_value=mock.Mock(gpg_key=None))) + with func_patcher: cmd = envelope.SignCommand(action='sign', keyid=None) cmd.apply(ui) @@ -245,8 +247,10 @@ class TestSignCommand(unittest.TestCase): """ 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'))): + func_patcher = mock.patch( + 'alot.commands.envelope.settings.get_account_by_address', + mock.Mock(return_value=mock.Mock(gpg_key='sentinel'))) + with func_patcher: cmd = envelope.SignCommand(action='sign', keyid=None) cmd.apply(ui) diff --git a/tests/commands/global_test.py b/tests/commands/global_test.py index e3d91f8a..59943e5e 100644 --- a/tests/commands/global_test.py +++ b/tests/commands/global_test.py @@ -17,6 +17,8 @@ """Tests for global commands.""" from __future__ import absolute_import + +import contextlib import os import tempfile @@ -48,7 +50,8 @@ class TestComposeCommand(unittest.TestCase): return envelope @staticmethod - def _make_account_mock(sign_by_default=True, gpg_key=mock.sentinel.gpg_key): + def _make_account_mock( + sign_by_default=True, gpg_key=mock.sentinel.gpg_key): account = mock.Mock() account.sign_by_default = sign_by_default account.gpg_key = gpg_key @@ -63,16 +66,21 @@ class TestComposeCommand(unittest.TestCase): # 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: - yield cmd.apply(mock.Mock()) - except Stop: - pass + func_patcher_get_account_by_address = mock.patch( + 'alot.commands.globals.settings.get_account_by_address', + mock.Mock(return_value=account)) + func_patcher_get_accounts = mock.patch( + 'alot.commands.globals.settings.get_accounts', + mock.Mock(return_value=[account])) + func_patcher_get_addressbooks = mock.patch( + 'alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)) + with contextlib.ExitStack() as stack: + stack.enter_context(func_patcher_get_account_by_address) + stack.enter_context(func_patcher_get_accounts) + stack.enter_context(func_patcher_get_addressbooks) + with self.assertRaises(Stop): + yield cmd.apply(mock.Mock()) self.assertTrue(envelope.sign) self.assertIs(envelope.sign_key, mock.sentinel.gpg_key) @@ -85,16 +93,21 @@ class TestComposeCommand(unittest.TestCase): # 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: - yield cmd.apply(mock.Mock()) - except Stop: - pass + func_patcher_get_account_by_address = mock.patch( + 'alot.commands.globals.settings.get_account_by_address', + mock.Mock(return_value=account)) + func_patcher_get_accounts = mock.patch( + 'alot.commands.globals.settings.get_accounts', + mock.Mock(return_value=[account])) + func_patcher_get_addressbooks = mock.patch( + 'alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)) + with contextlib.ExitStack() as stack: + stack.enter_context(func_patcher_get_account_by_address) + stack.enter_context(func_patcher_get_accounts) + stack.enter_context(func_patcher_get_addressbooks) + with self.assertRaises(Stop): + yield cmd.apply(mock.Mock()) self.assertFalse(envelope.sign) self.assertIs(envelope.sign_key, None) @@ -107,16 +120,21 @@ class TestComposeCommand(unittest.TestCase): # 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: - yield cmd.apply(mock.Mock()) - except Stop: - pass + func_patcher_get_account_by_address = mock.patch( + 'alot.commands.globals.settings.get_account_by_address', + mock.Mock(return_value=account)) + func_patcher_get_accounts = mock.patch( + 'alot.commands.globals.settings.get_accounts', + mock.Mock(return_value=[account])) + func_patcher_get_addressbooks = mock.patch( + 'alot.commands.globals.settings.get_addressbooks', + mock.Mock(side_effect=Stop)) + with contextlib.ExitStack() as stack: + stack.enter_context(func_patcher_get_account_by_address) + stack.enter_context(func_patcher_get_accounts) + stack.enter_context(func_patcher_get_addressbooks) + with self.assertRaises(Stop): + yield cmd.apply(mock.Mock()) self.assertFalse(envelope.sign) self.assertIs(envelope.sign_key, None) @@ -136,8 +154,9 @@ class TestComposeCommand(unittest.TestCase): cmd = g_commands.ComposeCommand(template=f.name) # Crutch to exit the giant `apply` method early. - with mock.patch('alot.commands.globals.settings.get_account_by_address', - mock.Mock(side_effect=Stop)): + with mock.patch( + 'alot.commands.globals.settings.get_account_by_address', + mock.Mock(side_effect=Stop)): try: yield cmd.apply(mock.Mock()) except Stop: @@ -172,7 +191,8 @@ class TestExternalCommand(unittest.TestCase): def test_no_spawn_stdin_attached(self): ui = utilities.make_ui() - cmd = g_commands.ExternalCommand(u"test -t 0", stdin=u'0', refocus=False) + cmd = g_commands.ExternalCommand( + u"test -t 0", stdin=u'0', refocus=False) cmd.apply(ui) ui.notify.assert_called_once_with('', priority='error') @@ -182,7 +202,8 @@ class TestExternalCommand(unittest.TestCase): cmd.apply(ui) ui.notify.assert_called_once_with('', priority='error') - @mock.patch('alot.commands.globals.settings.get', mock.Mock(return_value='')) + @mock.patch( + 'alot.commands.globals.settings.get', mock.Mock(return_value='')) @mock.patch.dict(os.environ, {'DISPLAY': ':0'}) def test_spawn_no_stdin_success(self): ui = utilities.make_ui() @@ -190,7 +211,8 @@ class TestExternalCommand(unittest.TestCase): cmd.apply(ui) ui.notify.assert_not_called() - @mock.patch('alot.commands.globals.settings.get', mock.Mock(return_value='')) + @mock.patch( + 'alot.commands.globals.settings.get', mock.Mock(return_value='')) @mock.patch.dict(os.environ, {'DISPLAY': ':0'}) def test_spawn_stdin_success(self): ui = utilities.make_ui() @@ -200,7 +222,8 @@ class TestExternalCommand(unittest.TestCase): cmd.apply(ui) ui.notify.assert_not_called() - @mock.patch('alot.commands.globals.settings.get', mock.Mock(return_value='')) + @mock.patch( + 'alot.commands.globals.settings.get', mock.Mock(return_value='')) @mock.patch.dict(os.environ, {'DISPLAY': ':0'}) def test_spawn_failure(self): ui = utilities.make_ui() diff --git a/tests/commands/thread_test.py b/tests/commands/thread_test.py index 6897a953..81328410 100644 --- a/tests/commands/thread_test.py +++ b/tests/commands/thread_test.py @@ -163,7 +163,7 @@ class TestDetermineSender(unittest.TestCase): expected = (u'to@example.com', account2) self._test(accounts=[account1, account2, account3], expected=expected) - def test_force_realname_includes_real_name_in_returned_address_if_defined(self): + def test_force_realname_has_real_name_in_returned_address_if_defined(self): account1 = _AccountTestClass(address=u'foo@example.com') account2 = _AccountTestClass(address=u'to@example.com', realname='Bar') account3 = _AccountTestClass(address=u'baz@example.com') @@ -179,7 +179,7 @@ class TestDetermineSender(unittest.TestCase): self._test(accounts=[account1, account2, account3], expected=expected, force_realname=True) - def test_with_force_address_main_address_is_used_regardless_of_matching_address(self): + def test_with_force_address_main_address_is_always_used(self): # In python 3.4 this and the next test could be written as subtests. account1 = _AccountTestClass(address=u'foo@example.com') account2 = _AccountTestClass(address=u'bar@example.com', diff --git a/tests/commands/utils_tests.py b/tests/commands/utils_tests.py index 487d0d3b..c17473dc 100644 --- a/tests/commands/utils_tests.py +++ b/tests/commands/utils_tests.py @@ -57,7 +57,8 @@ def setUpModule(): with gpg.core.Context(armor=True) as ctx: # Add the public and private keys. They have no password - search_dir = os.path.join(os.path.dirname(__file__), '../static/gpg-keys') + search_dir = os.path.join( + os.path.dirname(__file__), '../static/gpg-keys') for each in os.listdir(search_dir): if os.path.splitext(each)[1] == '.gpg': with open(os.path.join(search_dir, each)) as f: @@ -106,7 +107,8 @@ class TestGetKeys(unittest.TestCase): @inlineCallbacks def test_get_keys_ambiguous(self): """Test gettings keys when when the key is ambiguous.""" - key = crypto.get_key(FPR, validate=True, encrypt=True, signed_only=False) + key = crypto.get_key( + FPR, validate=True, encrypt=True, signed_only=False) ui = utilities.make_ui() # Creat a ui.choice object that can satisfy twisted, but can also be diff --git a/tests/completion_test.py b/tests/completion_test.py index 5b335779..15a80a89 100644 --- a/tests/completion_test.py +++ b/tests/completion_test.py @@ -57,7 +57,8 @@ class AbooksCompleterTest(unittest.TestCase): self.assertTupleEqual(actual[0], expected[0]) def test_empty_real_name_returns_plain_email_address(self): - actual = self.__class__.example_abook_completer.complete("real-name", 9) + actual = self.__class__.example_abook_completer.complete( + "real-name", 9) expected = [("no-real-name@example.com", 24)] self._assert_only_one_list_entry(actual, expected) @@ -79,7 +80,8 @@ class AbooksCompleterTest(unittest.TestCase): def test_real_name_double_quotes(self): actual = self.__class__.example_abook_completer.complete("dquote", 6) expected = [("", 0)] - expected = [(r""""double \"quote\" person" <dquote@example.com>""", 46)] + expected = [ + (r""""double \"quote\" person" <dquote@example.com>""", 46)] self._assert_only_one_list_entry(actual, expected) def test_real_name_with_quotes_and_comma(self): diff --git a/tests/crypto_test.py b/tests/crypto_test.py index 25cce8ba..c3db1055 100644 --- a/tests/crypto_test.py +++ b/tests/crypto_test.py @@ -59,7 +59,8 @@ def tearDownModule(): lookfor = 'gpg-agent --homedir {}'.format(os.environ['GNUPGHOME']) out = subprocess.check_output( - ['ps', 'xo', 'pid,cmd'], stderr=DEVNULL).decode(urwid.util.detected_encoding) + ['ps', 'xo', 'pid,cmd'], + stderr=DEVNULL).decode(urwid.util.detected_encoding) for each in out.strip().split('\n'): pid, cmd = each.strip().split(' ', 1) if cmd.startswith(lookfor): @@ -113,7 +114,8 @@ class TestDetachedSignatureFor(unittest.TestCase): def test_valid_signature_generated(self): to_sign = b"this is some text.\nit is more than nothing.\n" with gpg.core.Context() as ctx: - _, detached = crypto.detached_signature_for(to_sign, [ctx.get_key(FPR)]) + _, detached = crypto.detached_signature_for( + to_sign, [ctx.get_key(FPR)]) with tempfile.NamedTemporaryFile(delete=False) as f: f.write(detached) @@ -135,7 +137,8 @@ class TestVerifyDetached(unittest.TestCase): def test_verify_signature_good(self): to_sign = b"this is some text.\nIt's something\n." with gpg.core.Context() as ctx: - _, detached = crypto.detached_signature_for(to_sign, [ctx.get_key(FPR)]) + _, detached = crypto.detached_signature_for( + to_sign, [ctx.get_key(FPR)]) try: crypto.verify_detached(to_sign, detached) @@ -146,7 +149,8 @@ class TestVerifyDetached(unittest.TestCase): to_sign = b"this is some text.\nIt's something\n." similar = b"this is some text.\r\n.It's something\r\n." with gpg.core.Context() as ctx: - _, detached = crypto.detached_signature_for(to_sign, [ctx.get_key(FPR)]) + _, detached = crypto.detached_signature_for( + to_sign, [ctx.get_key(FPR)]) with self.assertRaises(GPGProblem): crypto.verify_detached(similar, detached) @@ -180,7 +184,8 @@ class TestValidateKey(unittest.TestCase): def test_encrypt(self): with self.assertRaises(GPGProblem) as caught: - crypto.validate_key(utilities.make_key(can_encrypt=False), encrypt=True) + crypto.validate_key( + utilities.make_key(can_encrypt=False), encrypt=True) self.assertEqual(caught.exception.code, GPGCode.KEY_CANNOT_ENCRYPT) @@ -286,7 +291,8 @@ class TestGetKey(unittest.TestCase): # once. with gpg.core.Context() as ctx: expected = ctx.get_key(FPR).uids[0].uid - actual = crypto.get_key(FPR, validate=True, encrypt=True, sign=True).uids[0].uid + actual = crypto.get_key( + FPR, validate=True, encrypt=True, sign=True).uids[0].uid self.assertEqual(expected, actual) def test_missing_key(self): @@ -306,7 +312,8 @@ class TestGetKey(unittest.TestCase): except GPGProblem as e: raise AssertionError(e) - @mock.patch('alot.crypto.check_uid_validity', mock.Mock(return_value=False)) + @mock.patch( + 'alot.crypto.check_uid_validity', mock.Mock(return_value=False)) def test_signed_only_false(self): with self.assertRaises(GPGProblem) as e: crypto.get_key(FPR, signed_only=True) @@ -370,7 +377,8 @@ class TestEncrypt(unittest.TestCase): enc_file = f.name self.addCleanup(os.unlink, enc_file) - dec = subprocess.check_output(['gpg', '--decrypt', enc_file], stderr=DEVNULL) + dec = subprocess.check_output( + ['gpg', '--decrypt', enc_file], stderr=DEVNULL) self.assertEqual(to_encrypt, dec) diff --git a/tests/db/utils_test.py b/tests/db/utils_test.py index b1187fe3..25c3641d 100644 --- a/tests/db/utils_test.py +++ b/tests/db/utils_test.py @@ -263,7 +263,9 @@ class TestDecodeHeader(unittest.TestCase): """ string = unicode_string.encode(encoding) b64 = base64.encodebytes(string).strip() - return (b'=?' + encoding.encode('utf-8') + b'?B?' + b64 + b'?=').decode('ascii') + result_bytes = b'=?' + encoding.encode('utf-8') + b'?B?' + b64 + b'?=' + result = result_bytes.decode('ascii') + return result def _test(self, teststring, expected): @@ -317,7 +319,11 @@ class TestDecodeHeader(unittest.TestCase): ' again: ' + self._quote(part, 'utf-8') + \ ' latin1: ' + self._base64(part, 'iso-8859-1') + \ ' and ' + self._quote(part, 'iso-8859-1') - expected = u'utf-8: ÄÖÜäöü again: ÄÖÜäöü latin1: ÄÖÜäöü and ÄÖÜäöü' + expected = u' '.join([ + u'utf-8: ÄÖÜäöü', + u'again: ÄÖÜäöü', + u'latin1: ÄÖÜäöü and ÄÖÜäöü', + ]) self._test(text, expected) def test_tabs_are_expanded_to_align_with_eigth_spaces(self): @@ -379,7 +385,8 @@ class TestAddSignatureHeaders(unittest.TestCase): self.assertIn((utils.X_SIGNATURE_VALID_HEADER, u'True'), mail.headers) self.assertIn( - (utils.X_SIGNATURE_MESSAGE_HEADER, u'Untrusted: mocked'), mail.headers) + (utils.X_SIGNATURE_MESSAGE_HEADER, u'Untrusted: mocked'), + mail.headers) def test_unicode_as_bytes(self): mail = self.FakeMail() @@ -389,7 +396,8 @@ class TestAddSignatureHeaders(unittest.TestCase): self.assertIn((utils.X_SIGNATURE_VALID_HEADER, u'True'), mail.headers) self.assertIn( - (utils.X_SIGNATURE_MESSAGE_HEADER, u'Valid: Andreá'), mail.headers) + (utils.X_SIGNATURE_MESSAGE_HEADER, u'Valid: Andreá'), + mail.headers) def test_error_message_unicode(self): mail = self.check(mock.Mock(), mock.Mock(), u'error message') @@ -427,7 +435,8 @@ class TestMessageFromFile(TestCaseClassCleanup): with open(os.path.join(search_dir, each)) as f: ctx.op_import(f) - cls.keys = [ctx.get_key("DD19862809A7573A74058FF255937AFBB156245D")] + cls.keys = [ + ctx.get_key("DD19862809A7573A74058FF255937AFBB156245D")] def test_erase_alot_header_signature_valid(self): """Alot uses special headers for passing certain kinds of information, @@ -484,7 +493,8 @@ class TestMessageFromFile(TestCaseClassCleanup): m = self._make_signed() m = utils.message_from_file(io.StringIO(m.as_string())) # Don't test for valid/invalid since that might change - self.assertIn('ambig <ambig@example.com>', m[utils.X_SIGNATURE_MESSAGE_HEADER]) + self.assertIn( + 'ambig <ambig@example.com>', m[utils.X_SIGNATURE_MESSAGE_HEADER]) def test_signed_wrong_mimetype_second_payload(self): m = self._make_signed() @@ -595,7 +605,8 @@ class TestMessageFromFile(TestCaseClassCleanup): m = self._make_encrypted(True) m = utils.message_from_file(io.StringIO(m.as_string())) self.assertIn(utils.X_SIGNATURE_MESSAGE_HEADER, m) - self.assertIn('ambig <ambig@example.com>', m[utils.X_SIGNATURE_MESSAGE_HEADER]) + self.assertIn( + 'ambig <ambig@example.com>', m[utils.X_SIGNATURE_MESSAGE_HEADER]) # TODO: tests for the RFC 2440 style combined signed/encrypted blob @@ -722,7 +733,8 @@ class TestExtractBody(unittest.TestCase): @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>' + expected = ( + '<!DOCTYPE html><html><body>This is an html email</body></html>') mail = self._make_mixed_plain_html() actual = utils.extract_body(mail) @@ -749,7 +761,8 @@ class TestExtractBody(unittest.TestCase): '<!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>' + expected = ( + '<!DOCTYPE html><html><body>This is an html email</body></html>') self.assertEqual(actual, expected) @@ -762,7 +775,8 @@ class TestExtractBody(unittest.TestCase): '<!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>' + expected = ( + '<!DOCTYPE html><html><body>This is an html email</body></html>') self.assertEqual(actual, expected) |