aboutsummaryrefslogtreecommitdiff
path: root/src/mailman_pgp/commands/tests/test_key.py
blob: 95f9766ff31b4076aae19d37f1a442cb74c2a10c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
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 get_queue_messages, make_testable_runner
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))