PageRenderTime 60ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

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