PageRenderTime 134ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://gitlab.com/noc0lour/mailman
Python | 237 lines | 172 code | 23 blank | 42 comment | 5 complexity | c5b5360461b07f643f5d1828cc6b36b2 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 zope.component import getUtility
  30. class TestAddMember(unittest.TestCase):
  31. layer = ConfigLayer
  32. def setUp(self):
  33. self._mlist = create_list('test@example.com')
  34. def test_add_member_new_user(self):
  35. # Test subscribing a user to a mailing list when the email address has
  36. # not yet been associated with a user.
  37. member = add_member(
  38. self._mlist,
  39. RequestRecord('aperson@example.com', 'Anne Person',
  40. DeliveryMode.regular,
  41. system_preferences.preferred_language))
  42. self.assertEqual(member.address.email, 'aperson@example.com')
  43. self.assertEqual(member.list_id, 'test.example.com')
  44. self.assertEqual(member.role, MemberRole.member)
  45. def test_add_member_existing_user(self):
  46. # Test subscribing a user to a mailing list when the email address has
  47. # already been associated with a user.
  48. user_manager = getUtility(IUserManager)
  49. user_manager.create_user('aperson@example.com', 'Anne Person')
  50. member = add_member(
  51. self._mlist,
  52. RequestRecord('aperson@example.com', 'Anne Person',
  53. DeliveryMode.regular,
  54. system_preferences.preferred_language))
  55. self.assertEqual(member.address.email, 'aperson@example.com')
  56. self.assertEqual(member.list_id, 'test.example.com')
  57. def test_add_member_banned(self):
  58. # Test that members who are banned by specific address cannot
  59. # subscribe to the mailing list.
  60. IBanManager(self._mlist).ban('anne@example.com')
  61. with self.assertRaises(MembershipIsBannedError) as cm:
  62. add_member(
  63. self._mlist,
  64. RequestRecord('anne@example.com', 'Anne Person',
  65. DeliveryMode.regular,
  66. system_preferences.preferred_language))
  67. self.assertEqual(
  68. str(cm.exception),
  69. 'anne@example.com is not allowed to subscribe to test@example.com')
  70. def test_add_member_globally_banned(self):
  71. # Test that members who are banned by specific address cannot
  72. # subscribe to the mailing list.
  73. IBanManager(None).ban('anne@example.com')
  74. self.assertRaises(
  75. MembershipIsBannedError,
  76. add_member, self._mlist,
  77. RequestRecord('anne@example.com', 'Anne Person',
  78. DeliveryMode.regular,
  79. system_preferences.preferred_language))
  80. def test_add_member_banned_from_different_list(self):
  81. # Test that members who are banned by on a different list can still be
  82. # subscribed to other mlists.
  83. sample_list = create_list('sample@example.com')
  84. IBanManager(sample_list).ban('anne@example.com')
  85. member = add_member(
  86. self._mlist,
  87. RequestRecord('anne@example.com', 'Anne Person',
  88. DeliveryMode.regular,
  89. system_preferences.preferred_language))
  90. self.assertEqual(member.address.email, 'anne@example.com')
  91. def test_add_member_banned_by_pattern(self):
  92. # Addresses matching regexp ban patterns cannot subscribe.
  93. IBanManager(self._mlist).ban('^.*@example.com')
  94. self.assertRaises(
  95. MembershipIsBannedError,
  96. add_member, self._mlist,
  97. RequestRecord('anne@example.com', 'Anne Person',
  98. DeliveryMode.regular,
  99. system_preferences.preferred_language))
  100. def test_add_member_globally_banned_by_pattern(self):
  101. # Addresses matching global regexp ban patterns cannot subscribe.
  102. IBanManager(None).ban('^.*@example.com')
  103. self.assertRaises(
  104. MembershipIsBannedError,
  105. add_member, self._mlist,
  106. RequestRecord('anne@example.com', 'Anne Person',
  107. DeliveryMode.regular,
  108. system_preferences.preferred_language))
  109. def test_add_member_banned_from_different_list_by_pattern(self):
  110. # Addresses matching regexp ban patterns on one list can still
  111. # subscribe to other mailing lists.
  112. sample_list = create_list('sample@example.com')
  113. IBanManager(sample_list).ban('^.*@example.com')
  114. member = add_member(
  115. self._mlist,
  116. RequestRecord('anne@example.com', 'Anne Person',
  117. DeliveryMode.regular,
  118. system_preferences.preferred_language))
  119. self.assertEqual(member.address.email, 'anne@example.com')
  120. def test_add_member_moderator(self):
  121. # Test adding a moderator to a mailing list.
  122. member = add_member(
  123. self._mlist,
  124. RequestRecord('aperson@example.com', 'Anne Person',
  125. DeliveryMode.regular,
  126. system_preferences.preferred_language),
  127. MemberRole.moderator)
  128. self.assertEqual(member.address.email, 'aperson@example.com')
  129. self.assertEqual(member.list_id, 'test.example.com')
  130. self.assertEqual(member.role, MemberRole.moderator)
  131. def test_add_member_twice(self):
  132. # Adding a member with the same role twice causes an
  133. # AlreadySubscribedError to be raised.
  134. add_member(
  135. self._mlist,
  136. RequestRecord('aperson@example.com', 'Anne Person',
  137. DeliveryMode.regular,
  138. system_preferences.preferred_language),
  139. MemberRole.member)
  140. with self.assertRaises(AlreadySubscribedError) as cm:
  141. add_member(
  142. self._mlist,
  143. RequestRecord('aperson@example.com', 'Anne Person',
  144. DeliveryMode.regular,
  145. system_preferences.preferred_language),
  146. MemberRole.member)
  147. self.assertEqual(cm.exception.fqdn_listname, 'test@example.com')
  148. self.assertEqual(cm.exception.email, 'aperson@example.com')
  149. self.assertEqual(cm.exception.role, MemberRole.member)
  150. def test_add_member_with_different_roles(self):
  151. # Adding a member twice with different roles is okay.
  152. member_1 = add_member(
  153. self._mlist,
  154. RequestRecord('aperson@example.com', 'Anne Person',
  155. DeliveryMode.regular,
  156. system_preferences.preferred_language),
  157. MemberRole.member)
  158. member_2 = add_member(
  159. self._mlist,
  160. RequestRecord('aperson@example.com', 'Anne Person',
  161. DeliveryMode.regular,
  162. system_preferences.preferred_language),
  163. MemberRole.owner)
  164. self.assertEqual(member_1.list_id, member_2.list_id)
  165. self.assertEqual(member_1.address, member_2.address)
  166. self.assertEqual(member_1.user, member_2.user)
  167. self.assertNotEqual(member_1.member_id, member_2.member_id)
  168. self.assertEqual(member_1.role, MemberRole.member)
  169. self.assertEqual(member_2.role, MemberRole.owner)
  170. def test_add_member_with_mixed_case_email(self):
  171. # LP: #1425359 - Mailman is case-perserving, case-insensitive. This
  172. # test subscribes the lower case address and ensures the original
  173. # mixed case address can't be subscribed.
  174. email = 'APerson@example.com'
  175. add_member(
  176. self._mlist,
  177. RequestRecord(email.lower(), 'Ann Person',
  178. DeliveryMode.regular,
  179. system_preferences.preferred_language))
  180. with self.assertRaises(AlreadySubscribedError) as cm:
  181. add_member(
  182. self._mlist,
  183. RequestRecord(email, 'Ann Person',
  184. DeliveryMode.regular,
  185. system_preferences.preferred_language))
  186. self.assertEqual(cm.exception.email, email)
  187. def test_add_member_with_lower_case_email(self):
  188. # LP: #1425359 - Mailman is case-perserving, case-insensitive. This
  189. # test subscribes the mixed case address and ensures the lower cased
  190. # address can't be added.
  191. email = 'APerson@example.com'
  192. add_member(
  193. self._mlist,
  194. RequestRecord(email, 'Ann Person',
  195. DeliveryMode.regular,
  196. system_preferences.preferred_language))
  197. with self.assertRaises(AlreadySubscribedError) as cm:
  198. add_member(
  199. self._mlist,
  200. RequestRecord(email.lower(), 'Ann Person',
  201. DeliveryMode.regular,
  202. system_preferences.preferred_language))
  203. self.assertEqual(cm.exception.email, email.lower())
  204. class TestDeleteMember(unittest.TestCase):
  205. layer = ConfigLayer
  206. def setUp(self):
  207. self._mlist = create_list('test@example.com')
  208. def test_delete_member_not_a_member(self):
  209. # Try to delete an address which is not a member of the mailing list.
  210. with self.assertRaises(NotAMemberError) as cm:
  211. delete_member(self._mlist, 'noperson@example.com')
  212. self.assertEqual(
  213. str(cm.exception),
  214. 'noperson@example.com is not a member of test@example.com')