~barry/mailman/events-and-web

« back to all changes in this revision

Viewing changes to src/mailman/app/tests/test_registration.py

  • Committer: Barry Warsaw
  • Date: 2012-11-25 20:48:08 UTC
  • Revision ID: barry@list.org-20121125204808-g0pvuil2xq559k4p
Interim commit for refactoring out web-centric logic.

* Use SubscriptionEvent to handle the sending of the welcome message.
* Use ConfirmationNeededEvent to send the confirmation message.
* For now, hard code the adminurl and comment out the listinfo_uri.
* Pend the list-id instead of the fqdn-listname
* Use the new configuration support for Postfix settings.
* Get rid of the IDomain's url_host, base_url, and scheme attributes, as well
  as the confirm_url() method. 
* Get rid of the IMailingList's script_url() method.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (C) 2012 by the Free Software Foundation, Inc.
 
2
#
 
3
# This file is part of GNU Mailman.
 
4
#
 
5
# GNU Mailman is free software: you can redistribute it and/or modify it under
 
6
# the terms of the GNU General Public License as published by the Free
 
7
# Software Foundation, either version 3 of the License, or (at your option)
 
8
# any later version.
 
9
#
 
10
# GNU Mailman is distributed in the hope that it will be useful, but WITHOUT
 
11
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 
12
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 
13
# more details.
 
14
#
 
15
# You should have received a copy of the GNU General Public License along with
 
16
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.
 
17
 
 
18
"""Test email address registration."""
 
19
 
 
20
from __future__ import absolute_import, print_function, unicode_literals
 
21
 
 
22
__metaclass__ = type
 
23
__all__ = [
 
24
    'TestEmailValidation',
 
25
    'TestRegistration',
 
26
    ]
 
27
 
 
28
 
 
29
import unittest
 
30
 
 
31
from zope.component import getUtility
 
32
 
 
33
from mailman.app.lifecycle import create_list
 
34
from mailman.interfaces.address import InvalidEmailAddressError
 
35
from mailman.interfaces.pending import IPendings
 
36
from mailman.interfaces.registrar import ConfirmationNeededEvent, IRegistrar
 
37
from mailman.testing.helpers import event_subscribers
 
38
from mailman.testing.layers import ConfigLayer
 
39
 
 
40
 
 
41
 
 
42
class TestEmailValidation(unittest.TestCase):
 
43
    """Test basic email validation."""
 
44
 
 
45
    layer = ConfigLayer
 
46
 
 
47
    def setUp(self):
 
48
        self.registrar = getUtility(IRegistrar)
 
49
        self.mlist = create_list('alpha@example.com')
 
50
 
 
51
    def test_empty_string_is_invalid(self):
 
52
        self.assertRaises(InvalidEmailAddressError,
 
53
                          self.registrar.register, self.mlist,
 
54
                          '')
 
55
 
 
56
    def test_no_spaces_allowed(self):
 
57
        self.assertRaises(InvalidEmailAddressError,
 
58
                          self.registrar.register, self.mlist,
 
59
                          'some name@example.com')
 
60
 
 
61
    def test_no_angle_brackets(self):
 
62
        self.assertRaises(InvalidEmailAddressError,
 
63
                          self.registrar.register, self.mlist,
 
64
                          '<script>@example.com')
 
65
 
 
66
    def test_ascii_only(self):
 
67
        self.assertRaises(InvalidEmailAddressError,
 
68
                          self.registrar.register, self.mlist,
 
69
                          '\xa0@example.com')
 
70
 
 
71
    def test_domain_required(self):
 
72
        self.assertRaises(InvalidEmailAddressError,
 
73
                          self.registrar.register, self.mlist,
 
74
                          'noatsign')
 
75
 
 
76
    def test_full_domain_required(self):
 
77
        self.assertRaises(InvalidEmailAddressError,
 
78
                          self.registrar.register, self.mlist,
 
79
                          'nodom@ain')
 
80
 
 
81
 
 
82
 
 
83
class TestRegistration(unittest.TestCase):
 
84
    """Test registration."""
 
85
 
 
86
    layer = ConfigLayer
 
87
 
 
88
    def setUp(self):
 
89
        self.registrar = getUtility(IRegistrar)
 
90
        self.mlist = create_list('alpha@example.com')
 
91
 
 
92
    def test_confirmation_event_received(self):
 
93
        # Registering an email address generates an event.
 
94
        def capture_event(event):
 
95
            self.assertIsInstance(event, ConfirmationNeededEvent)
 
96
        with event_subscribers(capture_event):
 
97
            self.registrar.register(self.mlist, 'anne@example.com')
 
98
 
 
99
    def test_event_mlist(self):
 
100
        # The event has a reference to the mailing list being subscribed to.
 
101
        def capture_event(event):
 
102
            self.assertIs(event.mlist, self.mlist)
 
103
        with event_subscribers(capture_event):
 
104
            self.registrar.register(self.mlist, 'anne@example.com')
 
105
 
 
106
    def test_event_pendable(self):
 
107
        # The event has an IPendable which contains additional information.
 
108
        def capture_event(event):
 
109
            pendable = event.pendable
 
110
            self.assertEqual(pendable['type'], 'registration')
 
111
            self.assertEqual(pendable['email'], 'anne@example.com')
 
112
            # The key is present, but the value is None.
 
113
            self.assertIsNone(pendable['display_name'])
 
114
            # The default is regular delivery.
 
115
            self.assertEqual(pendable['delivery_mode'], 'regular')
 
116
            self.assertEqual(pendable['list_id'], 'alpha.example.com')
 
117
        with event_subscribers(capture_event):
 
118
            self.registrar.register(self.mlist, 'anne@example.com')
 
119
 
 
120
    def test_token(self):
 
121
        # Registering the email address returns a token, and this token links
 
122
        # back to the pendable.
 
123
        captured_events = []
 
124
        def capture_event(event):
 
125
            captured_events.append(event)
 
126
        with event_subscribers(capture_event):
 
127
            token = self.registrar.register(self.mlist, 'anne@example.com')
 
128
        self.assertEqual(len(captured_events), 1)
 
129
        event = captured_events[0]
 
130
        self.assertEqual(event.token, token)
 
131
        pending = getUtility(IPendings).confirm(token)
 
132
        self.assertEqual(pending, event.pendable)