diff options
Diffstat (limited to 'src/mailman_pgp/commands/tests')
| -rw-r--r-- | src/mailman_pgp/commands/tests/__init__.py | 0 | ||||
| -rw-r--r-- | src/mailman_pgp/commands/tests/test_key.py | 123 |
2 files changed, 123 insertions, 0 deletions
diff --git a/src/mailman_pgp/commands/tests/__init__.py b/src/mailman_pgp/commands/tests/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/src/mailman_pgp/commands/tests/__init__.py diff --git a/src/mailman_pgp/commands/tests/test_key.py b/src/mailman_pgp/commands/tests/test_key.py new file mode 100644 index 0000000..8b44011 --- /dev/null +++ b/src/mailman_pgp/commands/tests/test_key.py @@ -0,0 +1,123 @@ +# Copyright (C) 2017 Jan Jancar +# +# This file is a part of the Mailman PGP plugin. +# +# This program is free software; you can redistribute it and/or modify it under +# the terms of the GNU General Public License as published by the Free +# Software Foundation, either version 3 of the License, or (at your option) +# any later version. +# +# This program is distributed in the hope that it will be useful, but WITHOUT +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +# more details. +# +# You should have received a copy of the GNU General Public License along with +# this program. If not, see <http://www.gnu.org/licenses/>. + +"""""" + +import unittest + +from mailman.app.lifecycle import create_list +from mailman.email.message import Message +from mailman.interfaces.subscriptions import ISubscriptionManager +from mailman.interfaces.usermanager import IUserManager +from mailman.runners.command import CommandRunner +from mailman.testing.helpers import make_testable_runner, get_queue_messages +from mailman.utilities.datetime import now +from zope.component import getUtility + +from mailman_pgp.config import mm_config +from mailman_pgp.database import transaction +from mailman_pgp.model.address import PGPAddress +from mailman_pgp.model.list import PGPMailingList +from mailman_pgp.pgp.mime import MIMEWrapper +from mailman_pgp.pgp.tests.base import load_key +from mailman_pgp.pgp.wrapper import PGPWrapper +from mailman_pgp.testing.layers import PGPConfigLayer +from mailman_pgp.workflows.base import CONFIRM_REQUEST +from mailman_pgp.workflows.subscription import OpenSubscriptionPolicy + + +class TestPreSubscription(unittest.TestCase): + layer = PGPConfigLayer + + def setUp(self): + self.mlist = create_list('test@example.com', style_name='pgp-default') + self.pgp_list = PGPMailingList.for_list(self.mlist) + self.pgp_list.generate_key(True) + + def test_set(self): + self.mlist.subscription_policy = OpenSubscriptionPolicy + bart = getUtility(IUserManager).create_address('bart@example.com', + 'Bart Person') + bart.verified_on = now() + token, token_owner, member = ISubscriptionManager(self.mlist).register( + bart) + + get_queue_messages('virgin') + + bart_key = load_key('rsa_1024.priv.asc') + + set_message = Message() + set_message['From'] = 'bart@example.com' + set_message['To'] = 'test@example.com' + set_message['Subject'] = 'Re: key set {}'.format(token) + set_message.set_type('multipart/mixed') + wrapped_set_message = MIMEWrapper(set_message) + set_message = wrapped_set_message.attach_key(bart_key.pubkey) + + mm_config.switchboards['command'].enqueue(set_message, + listid='test.example.com') + make_testable_runner(CommandRunner, 'command').run() + + pgp_address = PGPAddress.for_address(bart) + self.assertIsNotNone(pgp_address) + self.assertEqual(pgp_address.key.fingerprint, bart_key.fingerprint) + self.assertEqual(pgp_address.key_fingerprint, bart_key.fingerprint) + self.assertFalse(pgp_address.key_confirmed) + + items = get_queue_messages('virgin', expected_count=2) + if items[0].msg['Subject'] == 'The results of your email commands': + confirm_request = items[1].msg + else: + confirm_request = items[0].msg + + confirm_wrapped = PGPWrapper(confirm_request) + self.assertTrue(confirm_wrapped.is_encrypted()) + + def test_confirm(self): + self.mlist.subscription_policy = OpenSubscriptionPolicy + bart = getUtility(IUserManager).create_address('bart@example.com', + 'Bart Person') + bart.verified_on = now() + + bart_key = load_key('rsa_1024.priv.asc') + + token, token_owner, member = ISubscriptionManager(self.mlist).register( + bart, pubkey=bart_key.pubkey) + + get_queue_messages('virgin') + + with transaction() as t: + pgp_address = PGPAddress(bart) + pgp_address.key =bart_key.pubkey + t.add(pgp_address) + + confirm_message = Message() + confirm_message['From'] = 'bart@example.com' + confirm_message['To'] = 'test@example.com' + confirm_message['Subject'] = 'Re: key confirm {}'.format(token) + confirm_message.set_payload( + CONFIRM_REQUEST.format(bart_key.fingerprint, token)) + wrapped_confirm_message = MIMEWrapper(confirm_message) + confirm_message = wrapped_confirm_message.sign(bart_key) + + mm_config.switchboards['command'].enqueue(confirm_message, + listid='test.example.com') + make_testable_runner(CommandRunner, 'command').run() + + pgp_address = PGPAddress.for_address(bart) + self.assertTrue(pgp_address.key_confirmed) + self.assertTrue(self.mlist.is_subscribed(bart)) |
