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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# Copyright (C) 2012-2014 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/>.
"""Testing various recipients stuff."""
from __future__ import absolute_import, print_function, unicode_literals
__metaclass__ = type
__all__ = [
'TestMemberRecipients',
'TestOwnerRecipients',
]
import six
import unittest
from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.interfaces.member import DeliveryMode, DeliveryStatus, MemberRole
from mailman.interfaces.usermanager import IUserManager
from mailman.testing.helpers import specialized_message_from_string as mfs
from mailman.testing.layers import ConfigLayer
from zope.component import getUtility
class TestMemberRecipients(unittest.TestCase):
"""Test regular member recipient calculation."""
layer = ConfigLayer
def setUp(self):
self._mlist = create_list('test@example.com')
self._manager = getUtility(IUserManager)
anne = self._manager.create_address('anne@example.com')
bart = self._manager.create_address('bart@example.com')
cris = self._manager.create_address('cris@example.com')
dave = self._manager.create_address('dave@example.com')
self._anne = self._mlist.subscribe(anne, MemberRole.member)
self._bart = self._mlist.subscribe(bart, MemberRole.member)
self._cris = self._mlist.subscribe(cris, MemberRole.member)
self._dave = self._mlist.subscribe(dave, MemberRole.member)
self._process = config.handlers['member-recipients'].process
self._msg = mfs("""\
From: Elle Person <elle@example.com>
To: test@example.com
""")
def test_shortcircuit(self):
# When there are already recipients in the message metadata, those are
# used instead of calculating them from the list membership.
recipients = set(('zperson@example.com', 'yperson@example.com'))
msgdata = dict(recipients=recipients)
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], recipients)
def test_calculate_recipients(self):
# The normal path just adds the list's regular members.
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], set(('anne@example.com',
'bart@example.com',
'cris@example.com',
'dave@example.com')))
def test_digest_members_not_included(self):
# Digest members are not included in the recipients calculated by this
# handler.
self._cris.preferences.delivery_mode = DeliveryMode.mime_digests
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], set(('anne@example.com',
'bart@example.com',
'dave@example.com')))
class TestOwnerRecipients(unittest.TestCase):
"""Test owner recipient calculation."""
layer = ConfigLayer
def setUp(self):
self._mlist = create_list('test@example.com')
self._manager = getUtility(IUserManager)
anne = self._manager.create_address('anne@example.com')
bart = self._manager.create_address('bart@example.com')
cris = self._manager.create_address('cris@example.com')
dave = self._manager.create_address('dave@example.com')
# Make Cris and Dave owners of the mailing list.
self._anne = self._mlist.subscribe(anne, MemberRole.member)
self._bart = self._mlist.subscribe(bart, MemberRole.member)
self._cris = self._mlist.subscribe(cris, MemberRole.owner)
self._dave = self._mlist.subscribe(dave, MemberRole.owner)
self._process = config.handlers['owner-recipients'].process
self._msg = mfs("""\
From: Elle Person <elle@example.com>
To: test-owner@example.com
""")
def test_shortcircuit(self):
# When there are already recipients in the message metadata, those are
# used instead of calculating them from the owner membership.
recipients = set(('zperson@example.com', 'yperson@example.com'))
msgdata = dict(recipients=recipients)
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], recipients)
def test_calculate_recipients(self):
# The normal path just adds the list's owners.
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], set(('cris@example.com',
'dave@example.com')))
def test_with_moderators(self):
# Moderators are included in the owner recipient list.
elle = self._manager.create_address('elle@example.com')
fred = self._manager.create_address('fred@example.com')
gwen = self._manager.create_address('gwen@example.com')
self._mlist.subscribe(elle, MemberRole.moderator)
self._mlist.subscribe(fred, MemberRole.moderator)
self._mlist.subscribe(gwen, MemberRole.owner)
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], set(('cris@example.com',
'dave@example.com',
'elle@example.com',
'fred@example.com',
'gwen@example.com')))
def test_dont_decorate(self):
# Messages to the administrators don't get decorated.
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertTrue(msgdata['nodecorate'])
def test_omit_disabled_owners(self):
# Owner memberships can be disabled, and these folks will not get the
# messages.
self._dave.preferences.delivery_status = DeliveryStatus.by_user
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], set(('cris@example.com',)))
def test_include_membership_disabled_owner_enabled(self):
# If an address is subscribed to a mailing list as both an owner and a
# member, and their membership is disabled but their ownership
# subscription is not, they still get owner email.
dave = self._manager.get_address('dave@example.com')
member = self._mlist.subscribe(dave, MemberRole.member)
member.preferences.delivery_status = DeliveryStatus.by_user
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], set(('cris@example.com',
'dave@example.com')))
# Dave disables his owner membership but re-enables his list
# membership. He will not get the owner emails now.
member.preferences.delivery_status = DeliveryStatus.enabled
self._dave.preferences.delivery_status = DeliveryStatus.by_user
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], set(('cris@example.com',)))
def test_all_owners_disabled(self):
# If all the owners are disabled, then the site owner gets the
# message. This prevents a list's -owner address from going into a
# black hole.
self._cris.preferences.delivery_status = DeliveryStatus.by_user
self._dave.preferences.delivery_status = DeliveryStatus.by_user
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], set(('noreply@example.com',)))
def test_no_owners(self):
# If a list has no owners or moderators, then the site owner gets the
# message. This prevents a list's -owner address from going into a
# black hole.
self._cris.unsubscribe()
self._dave.unsubscribe()
self.assertEqual(self._mlist.administrators.member_count, 0)
msgdata = {}
self._process(self._mlist, self._msg, msgdata)
self.assertEqual(msgdata['recipients'], set(('noreply@example.com',)))
def test_site_admin_unicode(self):
# Since the config file is read as bytes, the site_owner is also a
# bytes and must be converted to unicode when used as a fallback.
self._cris.unsubscribe()
self._dave.unsubscribe()
self.assertEqual(self._mlist.administrators.member_count, 0)
msgdata = {}
# In order to properly mimic the testing environment, use
# config.push()/config.pop() directly instead of using the
# configuration() context manager.
config.push('test_site_admin_unicode', b"""\
[mailman]
site_owner: siteadmin@example.com
""")
try:
self._process(self._mlist, self._msg, msgdata)
finally:
config.pop('test_site_admin_unicode')
self.assertEqual(len(msgdata['recipients']), 1)
self.assertIsInstance(list(msgdata['recipients'])[0], six.text_type)
|