summaryrefslogtreecommitdiff
path: root/src/mailman/app/tests/test_notifications.py
blob: 05ce4cf2f9fe26c1cbbb6cd22c97483af8c64d6a (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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright (C) 2012-2017 by the Free Software Foundation, Inc.
#
# This file is part of GNU Mailman.
#
# GNU Mailman 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.
#
# GNU Mailman 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
# GNU Mailman.  If not, see <http://www.gnu.org/licenses/>.

"""Test notifications."""

import os
import unittest

from contextlib import ExitStack
from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.interfaces.languages import ILanguageManager
from mailman.interfaces.member import MemberRole
from mailman.interfaces.template import ITemplateManager
from mailman.interfaces.usermanager import IUserManager
from mailman.testing.helpers import (
    get_queue_messages, set_preferred, subscribe)
from mailman.testing.layers import ConfigLayer
from mailman.utilities.datetime import now
from tempfile import TemporaryDirectory
from zope.component import getUtility


class TestNotifications(unittest.TestCase):
    """Test notifications."""

    layer = ConfigLayer
    maxDiff = None

    def setUp(self):
        resources = ExitStack()
        self.addCleanup(resources.close)
        self.var_dir = resources.enter_context(TemporaryDirectory())
        self._mlist = create_list('test@example.com')
        self._mlist.display_name = 'Test List'
        getUtility(ITemplateManager).set(
            'user:ack:welcome', self._mlist.list_id, 'mailman:///welcome.txt')
        config.push('template config', """\
        [paths.testing]
        template_dir: {}/templates
        """.format(self.var_dir))
        resources.callback(config.pop, 'template config')
        # Populate the template directories with a few fake templates.
        path = os.path.join(self.var_dir, 'templates', 'site', 'en')
        os.makedirs(path)
        full_path = os.path.join(path, 'list:user:notice:welcome.txt')
        with open(full_path, 'w', encoding='utf-8') as fp:
            print("""\
Welcome to the $list_name mailing list.

    Posting address: $fqdn_listname
    Help and other requests: $list_requests
    Your name: $user_name
    Your address: $user_address""", file=fp)
        # Write a list-specific welcome message.
        path = os.path.join(self.var_dir, 'templates', 'lists',
                            'test@example.com', 'xx')
        os.makedirs(path)
        full_path = os.path.join(path, 'list:user:notice:welcome.txt')
        with open(full_path, 'w', encoding='utf-8') as fp:
            print('You just joined the $list_name mailing list!', file=fp)

    def test_welcome_message(self):
        subscribe(self._mlist, 'Anne', email='anne@example.com')
        # Now there's one message in the virgin queue.
        items = get_queue_messages('virgin', expected_count=1)
        message = items[0].msg
        self.assertEqual(str(message['subject']),
                         'Welcome to the "Test List" mailing list')
        self.assertMultiLineEqual(message.get_payload(), """\
Welcome to the Test List mailing list.

    Posting address: test@example.com
    Help and other requests: test-request@example.com
    Your name: Anne Person
    Your address: anne@example.com
""")

    def test_more_specific_welcome_message_nonenglish(self):
        # The welcome message url can contain placeholders for the fqdn list
        # name and language.
        getUtility(ITemplateManager).set(
            'user:ack:welcome', self._mlist.list_id,
            'mailman:///$listname/$language/welcome.txt')
        # Add the xx language and subscribe Anne using it.
        manager = getUtility(ILanguageManager)
        manager.add('xx', 'us-ascii', 'Xlandia')
        # We can't use the subscribe() helper because that would send the
        # welcome message before we set the member's preferred language.
        address = getUtility(IUserManager).create_address(
            'anne@example.com', 'Anne Person')
        address.preferences.preferred_language = 'xx'
        self._mlist.subscribe(address)
        # Now there's one message in the virgin queue.
        items = get_queue_messages('virgin', expected_count=1)
        message = items[0].msg
        self.assertEqual(str(message['subject']),
                         'Welcome to the "Test List" mailing list')
        self.assertMultiLineEqual(
            message.get_payload(),
            'You just joined the Test List mailing list!')

    def test_no_welcome_message_to_owners(self):
        # Welcome messages go only to mailing list members, not to owners.
        subscribe(self._mlist, 'Anne', MemberRole.owner, 'anne@example.com')
        # There is no welcome message in the virgin queue.
        get_queue_messages('virgin', expected_count=0)

    def test_no_welcome_message_to_nonmembers(self):
        # Welcome messages go only to mailing list members, not to nonmembers.
        subscribe(self._mlist, 'Anne', MemberRole.nonmember,
                  'anne@example.com')
        # There is no welcome message in the virgin queue.
        get_queue_messages('virgin', expected_count=0)

    def test_no_welcome_message_to_moderators(self):
        # Welcome messages go only to mailing list members, not to moderators.
        subscribe(self._mlist, 'Anne', MemberRole.moderator,
                  'anne@example.com')
        # There is no welcome message in the virgin queue.
        get_queue_messages('virgin', expected_count=0)

    def test_member_susbcribed_address_has_display_name(self):
        address = getUtility(IUserManager).create_address(
            'anne@example.com', 'Anne Person')
        address.verified_on = now()
        self._mlist.subscribe(address)
        items = get_queue_messages('virgin', expected_count=1)
        message = items[0].msg
        self.assertEqual(message['to'], 'Anne Person <anne@example.com>')

    def test_member_subscribed_address_has_no_display_name(self):
        address = getUtility(IUserManager).create_address('anne@example.com')
        address.verified_on = now()
        self._mlist.subscribe(address)
        items = get_queue_messages('virgin', expected_count=1)
        message = items[0].msg
        self.assertEqual(message['to'], 'anne@example.com')

    def test_member_is_user_and_has_display_name(self):
        user = getUtility(IUserManager).create_user(
            'anne@example.com', 'Anne Person')
        set_preferred(user)
        self._mlist.subscribe(user)
        items = get_queue_messages('virgin', expected_count=1)
        message = items[0].msg
        self.assertEqual(message['to'], 'Anne Person <anne@example.com>')

    def test_member_is_user_and_has_no_display_name(self):
        user = getUtility(IUserManager).create_user('anne@example.com')
        set_preferred(user)
        self._mlist.subscribe(user)
        items = get_queue_messages('virgin', expected_count=1)
        message = items[0].msg
        self.assertEqual(message['to'], 'anne@example.com')

    def test_member_has_linked_user_display_name(self):
        user = getUtility(IUserManager).create_user(
            'anne@example.com', 'Anne Person')
        set_preferred(user)
        address = getUtility(IUserManager).create_address('anne2@example.com')
        address.verified_on = now()
        user.link(address)
        self._mlist.subscribe(address)
        items = get_queue_messages('virgin', expected_count=1)
        message = items[0].msg
        self.assertEqual(message['to'], 'Anne Person <anne2@example.com>')

    def test_member_has_no_linked_display_name(self):
        user = getUtility(IUserManager).create_user('anne@example.com')
        set_preferred(user)
        address = getUtility(IUserManager).create_address('anne2@example.com')
        address.verified_on = now()
        user.link(address)
        self._mlist.subscribe(address)
        items = get_queue_messages('virgin', expected_count=1)
        message = items[0].msg
        self.assertEqual(message['to'], 'anne2@example.com')

    def test_member_has_address_and_user_display_name(self):
        user = getUtility(IUserManager).create_user(
            'anne@example.com', 'Anne Person')
        set_preferred(user)
        address = getUtility(IUserManager).create_address(
            'anne2@example.com', 'Anne X Person')
        address.verified_on = now()
        user.link(address)
        self._mlist.subscribe(address)
        items = get_queue_messages('virgin', expected_count=1)
        message = items[0].msg
        self.assertEqual(message['to'], 'Anne X Person <anne2@example.com>')