PageRenderTime 1ms CodeModel.GetById 49ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/src/mailman/app/tests/test_membership.py

https://gitlab.com/pratik4/mailman
Python | 274 lines | 200 code | 24 blank | 50 comment | 4 complexity | 632e7083a05c0132b08b5e6d9bfb95bc MD5 | raw file
  1# Copyright (C) 2011-2016 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"""Tests of application level membership functions."""
 19
 20import unittest
 21
 22from mailman.app.lifecycle import create_list
 23from mailman.app.membership import add_member, delete_member
 24from mailman.core.constants import system_preferences
 25from mailman.interfaces.bans import IBanManager
 26from mailman.interfaces.member import (
 27    AlreadySubscribedError, DeliveryMode, MemberRole, MembershipIsBannedError,
 28    NotAMemberError)
 29from mailman.interfaces.subscriptions import RequestRecord
 30from mailman.interfaces.usermanager import IUserManager
 31from mailman.testing.layers import ConfigLayer
 32from mailman.utilities.datetime import now
 33from zope.component import getUtility
 34
 35
 36class TestAddMember(unittest.TestCase):
 37    layer = ConfigLayer
 38
 39    def setUp(self):
 40        self._mlist = create_list('test@example.com')
 41
 42    def test_add_member_new_user(self):
 43        # Test subscribing a user to a mailing list when the email address has
 44        # not yet been associated with a user.
 45        member = add_member(
 46            self._mlist,
 47            RequestRecord('aperson@example.com', 'Anne Person',
 48                          DeliveryMode.regular,
 49                          system_preferences.preferred_language))
 50        self.assertEqual(member.address.email, 'aperson@example.com')
 51        self.assertEqual(member.list_id, 'test.example.com')
 52        self.assertEqual(member.role, MemberRole.member)
 53
 54    def test_add_member_existing_user(self):
 55        # Test subscribing a user to a mailing list when the email address has
 56        # already been associated with a user.
 57        user_manager = getUtility(IUserManager)
 58        user_manager.create_user('aperson@example.com', 'Anne Person')
 59        member = add_member(
 60            self._mlist,
 61            RequestRecord('aperson@example.com', 'Anne Person',
 62                          DeliveryMode.regular,
 63                          system_preferences.preferred_language))
 64        self.assertEqual(member.address.email, 'aperson@example.com')
 65        self.assertEqual(member.list_id, 'test.example.com')
 66
 67    def test_add_member_banned(self):
 68        # Test that members who are banned by specific address cannot
 69        # subscribe to the mailing list.
 70        IBanManager(self._mlist).ban('anne@example.com')
 71        with self.assertRaises(MembershipIsBannedError) as cm:
 72            add_member(
 73                self._mlist,
 74                RequestRecord('anne@example.com', 'Anne Person',
 75                              DeliveryMode.regular,
 76                              system_preferences.preferred_language))
 77        self.assertEqual(
 78            str(cm.exception),
 79            'anne@example.com is not allowed to subscribe to test@example.com')
 80
 81    def test_add_member_globally_banned(self):
 82        # Test that members who are banned by specific address cannot
 83        # subscribe to the mailing list.
 84        IBanManager(None).ban('anne@example.com')
 85        self.assertRaises(
 86            MembershipIsBannedError,
 87            add_member, self._mlist,
 88            RequestRecord('anne@example.com', 'Anne Person',
 89                          DeliveryMode.regular,
 90                          system_preferences.preferred_language))
 91
 92    def test_add_member_banned_from_different_list(self):
 93        # Test that members who are banned by on a different list can still be
 94        # subscribed to other mlists.
 95        sample_list = create_list('sample@example.com')
 96        IBanManager(sample_list).ban('anne@example.com')
 97        member = add_member(
 98            self._mlist,
 99            RequestRecord('anne@example.com', 'Anne Person',
100                          DeliveryMode.regular,
101                          system_preferences.preferred_language))
102        self.assertEqual(member.address.email, 'anne@example.com')
103
104    def test_add_member_banned_by_pattern(self):
105        # Addresses matching regexp ban patterns cannot subscribe.
106        IBanManager(self._mlist).ban('^.*@example.com')
107        self.assertRaises(
108            MembershipIsBannedError,
109            add_member, self._mlist,
110            RequestRecord('anne@example.com', 'Anne Person',
111                          DeliveryMode.regular,
112                          system_preferences.preferred_language))
113
114    def test_add_member_globally_banned_by_pattern(self):
115        # Addresses matching global regexp ban patterns cannot subscribe.
116        IBanManager(None).ban('^.*@example.com')
117        self.assertRaises(
118            MembershipIsBannedError,
119            add_member, self._mlist,
120            RequestRecord('anne@example.com', 'Anne Person',
121                          DeliveryMode.regular,
122                          system_preferences.preferred_language))
123
124    def test_add_member_banned_from_different_list_by_pattern(self):
125        # Addresses matching regexp ban patterns on one list can still
126        # subscribe to other mailing lists.
127        sample_list = create_list('sample@example.com')
128        IBanManager(sample_list).ban('^.*@example.com')
129        member = add_member(
130            self._mlist,
131            RequestRecord('anne@example.com', 'Anne Person',
132                          DeliveryMode.regular,
133                          system_preferences.preferred_language))
134        self.assertEqual(member.address.email, 'anne@example.com')
135
136    def test_add_member_moderator(self):
137        # Test adding a moderator to a mailing list.
138        member = add_member(
139            self._mlist,
140            RequestRecord('aperson@example.com', 'Anne Person',
141                          DeliveryMode.regular,
142                          system_preferences.preferred_language),
143            MemberRole.moderator)
144        self.assertEqual(member.address.email, 'aperson@example.com')
145        self.assertEqual(member.list_id, 'test.example.com')
146        self.assertEqual(member.role, MemberRole.moderator)
147
148    def test_add_member_twice(self):
149        # Adding a member with the same role twice causes an
150        # AlreadySubscribedError to be raised.
151        add_member(
152            self._mlist,
153            RequestRecord('aperson@example.com', 'Anne Person',
154                          DeliveryMode.regular,
155                          system_preferences.preferred_language),
156            MemberRole.member)
157        with self.assertRaises(AlreadySubscribedError) as cm:
158            add_member(
159                self._mlist,
160                RequestRecord('aperson@example.com', 'Anne Person',
161                              DeliveryMode.regular,
162                              system_preferences.preferred_language),
163                MemberRole.member)
164        self.assertEqual(cm.exception.fqdn_listname, 'test@example.com')
165        self.assertEqual(cm.exception.email, 'aperson@example.com')
166        self.assertEqual(cm.exception.role, MemberRole.member)
167
168    def test_add_member_with_different_roles(self):
169        # Adding a member twice with different roles is okay.
170        member_1 = add_member(
171            self._mlist,
172            RequestRecord('aperson@example.com', 'Anne Person',
173                          DeliveryMode.regular,
174                          system_preferences.preferred_language),
175            MemberRole.member)
176        member_2 = add_member(
177            self._mlist,
178            RequestRecord('aperson@example.com', 'Anne Person',
179                          DeliveryMode.regular,
180                          system_preferences.preferred_language),
181            MemberRole.owner)
182        self.assertEqual(member_1.list_id, member_2.list_id)
183        self.assertEqual(member_1.address, member_2.address)
184        self.assertEqual(member_1.user, member_2.user)
185        self.assertNotEqual(member_1.member_id, member_2.member_id)
186        self.assertEqual(member_1.role, MemberRole.member)
187        self.assertEqual(member_2.role, MemberRole.owner)
188
189    def test_add_member_with_mixed_case_email(self):
190        # LP: #1425359 - Mailman is case-perserving, case-insensitive.  This
191        # test subscribes the lower case address and ensures the original
192        # mixed case address can't be subscribed.
193        email = 'APerson@example.com'
194        add_member(
195            self._mlist,
196            RequestRecord(email.lower(), 'Ann Person',
197                          DeliveryMode.regular,
198                          system_preferences.preferred_language))
199        with self.assertRaises(AlreadySubscribedError) as cm:
200            add_member(
201                self._mlist,
202                RequestRecord(email, 'Ann Person',
203                              DeliveryMode.regular,
204                              system_preferences.preferred_language))
205        self.assertEqual(cm.exception.email, email)
206
207    def test_add_member_with_lower_case_email(self):
208        # LP: #1425359 - Mailman is case-perserving, case-insensitive.  This
209        # test subscribes the mixed case address and ensures the lower cased
210        # address can't be added.
211        email = 'APerson@example.com'
212        add_member(
213            self._mlist,
214            RequestRecord(email, 'Ann Person',
215                          DeliveryMode.regular,
216                          system_preferences.preferred_language))
217        with self.assertRaises(AlreadySubscribedError) as cm:
218            add_member(
219                self._mlist,
220                RequestRecord(email.lower(), 'Ann Person',
221                              DeliveryMode.regular,
222                              system_preferences.preferred_language))
223        self.assertEqual(cm.exception.email, email.lower())
224
225    def test_delete_nonmembers_on_adding_member(self):
226        # GL: #237 - When a new address is subscribed, any existing nonmember
227        # subscriptions for this address; or any addresses also controlled by
228        # this user, are deleted.
229        anne_nonmember = add_member(
230            self._mlist,
231            RequestRecord('aperson@example.com', 'Anne Person',
232                          DeliveryMode.regular,
233                          system_preferences.preferred_language),
234            MemberRole.nonmember)
235        # Add a few other validated addresses to this user, and subscribe them
236        # as nonmembers.
237        for email in ('anne.person@example.com', 'a.person@example.com'):
238            address = anne_nonmember.user.register(email)
239            address.verified_on = now()
240            self._mlist.subscribe(address, MemberRole.nonmember)
241        # There are now three nonmembers.
242        self.assertEqual(
243            {address.email for address in self._mlist.nonmembers.addresses},
244            {'aperson@example.com',
245             'anne.person@example.com',
246             'a.person@example.com',
247             })
248        # Let's now add one of Anne's addresses as a member.  This deletes all
249        # of Anne's nonmember memberships.
250        anne_member = add_member(
251            self._mlist,
252            RequestRecord('a.person@example.com', 'Anne Person',
253                          DeliveryMode.regular,
254                          system_preferences.preferred_language),
255            MemberRole.member)
256        self.assertEqual(self._mlist.nonmembers.member_count, 0)
257        members = list(self._mlist.members.members)
258        self.assertEqual(len(members), 1)
259        self.assertEqual(members[0], anne_member)
260
261
262class TestDeleteMember(unittest.TestCase):
263    layer = ConfigLayer
264
265    def setUp(self):
266        self._mlist = create_list('test@example.com')
267
268    def test_delete_member_not_a_member(self):
269        # Try to delete an address which is not a member of the mailing list.
270        with self.assertRaises(NotAMemberError) as cm:
271            delete_member(self._mlist, 'noperson@example.com')
272        self.assertEqual(
273            str(cm.exception),
274            'noperson@example.com is not a member of test@example.com')