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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
|
# Copyright (C) 2011-2012 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/>.
"""REST membership tests."""
from __future__ import absolute_import, unicode_literals
__metaclass__ = type
__all__ = [
]
import unittest
from urllib2 import HTTPError
from zope.component import getUtility
from mailman.app.lifecycle import create_list
from mailman.config import config
from mailman.interfaces.usermanager import IUserManager
from mailman.testing.helpers import call_api
from mailman.testing.layers import RESTLayer
from mailman.utilities.datetime import now
class TestMembership(unittest.TestCase):
layer = RESTLayer
def setUp(self):
self._mlist = create_list('test@example.com')
config.db.commit()
self._usermanager = getUtility(IUserManager)
def test_try_to_join_missing_list(self):
# A user tries to join a non-existent list.
try:
# For Python 2.6.
call_api('http://localhost:9001/3.0/members', {
'fqdn_listname': 'missing@example.com',
'subscriber': 'nobody@example.com',
})
except HTTPError as exc:
self.assertEqual(exc.code, 400)
self.assertEqual(exc.msg, 'No such list')
else:
raise AssertionError('Expected HTTPError')
def test_try_to_leave_missing_list(self):
# A user tries to leave a non-existent list.
try:
# For Python 2.6.
call_api('http://localhost:9001/3.0/lists/missing@example.com'
'/member/nobody@example.com',
method='DELETE')
except HTTPError as exc:
self.assertEqual(exc.code, 404)
self.assertEqual(exc.msg, '404 Not Found')
else:
raise AssertionError('Expected HTTPError')
def test_try_to_leave_list_with_bogus_address(self):
# Try to leave a mailing list using an invalid membership address.
try:
# For Python 2.6.
call_api('http://localhost:9001/3.0/members/1', method='DELETE')
except HTTPError as exc:
self.assertEqual(exc.code, 404)
self.assertEqual(exc.msg, '404 Not Found')
else:
raise AssertionError('Expected HTTPError')
def test_try_to_leave_a_list_twice(self):
anne = self._usermanager.create_address('anne@example.com')
self._mlist.subscribe(anne)
config.db.commit()
url = 'http://localhost:9001/3.0/members/1'
content, response = call_api(url, method='DELETE')
# For a successful DELETE, the response code is 204 and there is no
# content.
self.assertEqual(content, None)
self.assertEqual(response.status, 204)
try:
# For Python 2.6.
call_api(url, method='DELETE')
except HTTPError as exc:
self.assertEqual(exc.code, 404)
self.assertEqual(exc.msg, '404 Not Found')
else:
raise AssertionError('Expected HTTPError')
def test_try_to_join_a_list_twice(self):
anne = self._usermanager.create_address('anne@example.com')
self._mlist.subscribe(anne)
config.db.commit()
try:
# For Python 2.6.
call_api('http://localhost:9001/3.0/members', {
'fqdn_listname': 'test@example.com',
'subscriber': 'anne@example.com',
})
except HTTPError as exc:
self.assertEqual(exc.code, 409)
self.assertEqual(exc.msg, 'Member already subscribed')
else:
raise AssertionError('Expected HTTPError')
def test_join_with_invalid_delivery_mode(self):
try:
call_api('http://localhost:9001/3.0/members', {
'fqdn_listname': 'test@example.com',
'subscriber': 'anne@example.com',
'real_name': 'Anne Person',
'delivery_mode': 'invalid-mode',
})
except HTTPError as exc:
self.assertEqual(exc.code, 400)
self.assertEqual(exc.msg,
'Cannot convert parameters: delivery_mode')
else:
raise AssertionError('Expected HTTPError')
def test_join_email_contains_slash(self):
content, response = call_api('http://localhost:9001/3.0/members', {
'fqdn_listname': 'test@example.com',
'subscriber': 'hugh/person@example.com',
'real_name': 'Hugh Person',
})
self.assertEqual(content, None)
self.assertEqual(response.status, 201)
self.assertEqual(response['location'],
'http://localhost:9001/3.0/members/1')
# Reset any current transaction.
config.db.abort()
members = list(self._mlist.members.members)
self.assertEqual(len(members), 1)
self.assertEqual(members[0].address.email, 'hugh/person@example.com')
def test_join_as_user_with_preferred_address(self):
anne = self._usermanager.create_user('anne@example.com')
preferred = list(anne.addresses)[0]
preferred.verified_on = now()
anne.preferred_address = preferred
self._mlist.subscribe(anne)
config.db.commit()
content, response = call_api('http://localhost:9001/3.0/members')
self.assertEqual(response.status, 200)
self.assertEqual(int(content['total_size']), 1)
entry_0 = content['entries'][0]
self.assertEqual(entry_0['self_link'],
'http://localhost:9001/3.0/members/1')
self.assertEqual(entry_0['role'], 'member')
self.assertEqual(entry_0['user'], 'http://localhost:9001/3.0/users/1')
self.assertEqual(entry_0['address'], 'anne@example.com')
self.assertEqual(entry_0['fqdn_listname'], 'test@example.com')
def test_member_changes_preferred_address(self):
anne = self._usermanager.create_user('anne@example.com')
preferred = list(anne.addresses)[0]
preferred.verified_on = now()
anne.preferred_address = preferred
self._mlist.subscribe(anne)
config.db.commit()
# Take a look at Anne's current membership.
content, response = call_api('http://localhost:9001/3.0/members')
self.assertEqual(int(content['total_size']), 1)
entry_0 = content['entries'][0]
self.assertEqual(entry_0['address'], 'anne@example.com')
# Anne registers a new address and makes it her preferred address.
# There are no changes to her membership.
new_preferred = anne.register('aperson@example.com')
new_preferred.verified_on = now()
anne.preferred_address = new_preferred
config.db.commit()
# Take another look at Anne's current membership.
content, response = call_api('http://localhost:9001/3.0/members')
self.assertEqual(int(content['total_size']), 1)
entry_0 = content['entries'][0]
self.assertEqual(entry_0['address'], 'aperson@example.com')
def test_get_nonexistent_member(self):
# /members/<bogus> returns 404
try:
# For Python 2.6
call_api('http://localhost:9001/3.0/members/bogus')
except HTTPError as exc:
self.assertEqual(exc.code, 404)
else:
raise AssertionError('Expected HTTPError')
def test_patch_nonexistent_member(self):
# /members/<missing> PATCH returns 404
try:
# For Python 2.6
call_api('http://localhost:9001/3.0/members/801', method='PATCH')
except HTTPError as exc:
self.assertEqual(exc.code, 404)
else:
raise AssertionError('Expected HTTPError')
def test_patch_member_bogus_attribute(self):
# /members/<id> PATCH 'bogus' returns 400
anne = self._usermanager.create_address('anne@example.com')
self._mlist.subscribe(anne)
config.db.commit()
try:
# For Python 2.6
call_api('http://localhost:9001/3.0/members/1', {
'powers': 'super',
}, method='PATCH')
except HTTPError as exc:
self.assertEqual(exc.code, 400)
self.assertEqual(exc.msg, 'Unexpected parameters: powers')
else:
raise AssertionError('Expected HTTPError')
def test_member_all_without_preferences(self):
# /members/<id>/all should return a 404 when it isn't trailed by
# `preferences`
try:
# For Python 2.6
call_api('http://localhost:9001/3.0/members/1/all')
except HTTPError as exc:
self.assertEqual(exc.code, 404)
else:
raise AssertionError('Expected HTTPError')
|