PageRenderTime 86ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/src/mailman/handlers/tests/test_recipients.py

https://gitlab.com/noc0lour/mailman
Python | 202 lines | 177 code | 7 blank | 18 comment | 0 complexity | f85546d3bbf2cbd70e30cda8e181126a MD5 | raw file
  1. # Copyright (C) 2012-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. """Testing various recipients stuff."""
  18. import unittest
  19. from mailman.app.lifecycle import create_list
  20. from mailman.config import config
  21. from mailman.interfaces.member import DeliveryMode, DeliveryStatus, MemberRole
  22. from mailman.interfaces.usermanager import IUserManager
  23. from mailman.testing.helpers import (
  24. configuration, specialized_message_from_string as mfs)
  25. from mailman.testing.layers import ConfigLayer
  26. from zope.component import getUtility
  27. class TestMemberRecipients(unittest.TestCase):
  28. """Test regular member recipient calculation."""
  29. layer = ConfigLayer
  30. def setUp(self):
  31. self._mlist = create_list('test@example.com')
  32. self._manager = getUtility(IUserManager)
  33. anne = self._manager.create_address('anne@example.com')
  34. bart = self._manager.create_address('bart@example.com')
  35. cris = self._manager.create_address('cris@example.com')
  36. dave = self._manager.create_address('dave@example.com')
  37. self._anne = self._mlist.subscribe(anne, MemberRole.member)
  38. self._bart = self._mlist.subscribe(bart, MemberRole.member)
  39. self._cris = self._mlist.subscribe(cris, MemberRole.member)
  40. self._dave = self._mlist.subscribe(dave, MemberRole.member)
  41. self._process = config.handlers['member-recipients'].process
  42. self._msg = mfs("""\
  43. From: Elle Person <elle@example.com>
  44. To: test@example.com
  45. """)
  46. def test_shortcircuit(self):
  47. # When there are already recipients in the message metadata, those are
  48. # used instead of calculating them from the list membership.
  49. recipients = set(('zperson@example.com', 'yperson@example.com'))
  50. msgdata = dict(recipients=recipients)
  51. self._process(self._mlist, self._msg, msgdata)
  52. self.assertEqual(msgdata['recipients'], recipients)
  53. def test_calculate_recipients(self):
  54. # The normal path just adds the list's regular members.
  55. msgdata = {}
  56. self._process(self._mlist, self._msg, msgdata)
  57. self.assertEqual(msgdata['recipients'], set(('anne@example.com',
  58. 'bart@example.com',
  59. 'cris@example.com',
  60. 'dave@example.com')))
  61. def test_digest_members_not_included(self):
  62. # Digest members are not included in the recipients calculated by this
  63. # handler.
  64. self._cris.preferences.delivery_mode = DeliveryMode.mime_digests
  65. msgdata = {}
  66. self._process(self._mlist, self._msg, msgdata)
  67. self.assertEqual(msgdata['recipients'], set(('anne@example.com',
  68. 'bart@example.com',
  69. 'dave@example.com')))
  70. class TestOwnerRecipients(unittest.TestCase):
  71. """Test owner recipient calculation."""
  72. layer = ConfigLayer
  73. def setUp(self):
  74. self._mlist = create_list('test@example.com')
  75. self._manager = getUtility(IUserManager)
  76. anne = self._manager.create_address('anne@example.com')
  77. bart = self._manager.create_address('bart@example.com')
  78. cris = self._manager.create_address('cris@example.com')
  79. dave = self._manager.create_address('dave@example.com')
  80. # Make Cris and Dave owners of the mailing list.
  81. self._anne = self._mlist.subscribe(anne, MemberRole.member)
  82. self._bart = self._mlist.subscribe(bart, MemberRole.member)
  83. self._cris = self._mlist.subscribe(cris, MemberRole.owner)
  84. self._dave = self._mlist.subscribe(dave, MemberRole.owner)
  85. self._process = config.handlers['owner-recipients'].process
  86. self._msg = mfs("""\
  87. From: Elle Person <elle@example.com>
  88. To: test-owner@example.com
  89. """)
  90. def test_shortcircuit(self):
  91. # When there are already recipients in the message metadata, those are
  92. # used instead of calculating them from the owner membership.
  93. recipients = set(('zperson@example.com', 'yperson@example.com'))
  94. msgdata = dict(recipients=recipients)
  95. self._process(self._mlist, self._msg, msgdata)
  96. self.assertEqual(msgdata['recipients'], recipients)
  97. def test_calculate_recipients(self):
  98. # The normal path just adds the list's owners.
  99. msgdata = {}
  100. self._process(self._mlist, self._msg, msgdata)
  101. self.assertEqual(msgdata['recipients'], set(('cris@example.com',
  102. 'dave@example.com')))
  103. def test_with_moderators(self):
  104. # Moderators are included in the owner recipient list.
  105. elle = self._manager.create_address('elle@example.com')
  106. fred = self._manager.create_address('fred@example.com')
  107. gwen = self._manager.create_address('gwen@example.com')
  108. self._mlist.subscribe(elle, MemberRole.moderator)
  109. self._mlist.subscribe(fred, MemberRole.moderator)
  110. self._mlist.subscribe(gwen, MemberRole.owner)
  111. msgdata = {}
  112. self._process(self._mlist, self._msg, msgdata)
  113. self.assertEqual(msgdata['recipients'], set(('cris@example.com',
  114. 'dave@example.com',
  115. 'elle@example.com',
  116. 'fred@example.com',
  117. 'gwen@example.com')))
  118. def test_dont_decorate(self):
  119. # Messages to the administrators don't get decorated.
  120. msgdata = {}
  121. self._process(self._mlist, self._msg, msgdata)
  122. self.assertTrue(msgdata['nodecorate'])
  123. def test_omit_disabled_owners(self):
  124. # Owner memberships can be disabled, and these folks will not get the
  125. # messages.
  126. self._dave.preferences.delivery_status = DeliveryStatus.by_user
  127. msgdata = {}
  128. self._process(self._mlist, self._msg, msgdata)
  129. self.assertEqual(msgdata['recipients'], set(('cris@example.com',)))
  130. def test_include_membership_disabled_owner_enabled(self):
  131. # If an address is subscribed to a mailing list as both an owner and a
  132. # member, and their membership is disabled but their ownership
  133. # subscription is not, they still get owner email.
  134. dave = self._manager.get_address('dave@example.com')
  135. member = self._mlist.subscribe(dave, MemberRole.member)
  136. member.preferences.delivery_status = DeliveryStatus.by_user
  137. msgdata = {}
  138. self._process(self._mlist, self._msg, msgdata)
  139. self.assertEqual(msgdata['recipients'], set(('cris@example.com',
  140. 'dave@example.com')))
  141. # Dave disables his owner membership but re-enables his list
  142. # membership. He will not get the owner emails now.
  143. member.preferences.delivery_status = DeliveryStatus.enabled
  144. self._dave.preferences.delivery_status = DeliveryStatus.by_user
  145. msgdata = {}
  146. self._process(self._mlist, self._msg, msgdata)
  147. self.assertEqual(msgdata['recipients'], set(('cris@example.com',)))
  148. def test_all_owners_disabled(self):
  149. # If all the owners are disabled, then the site owner gets the
  150. # message. This prevents a list's -owner address from going into a
  151. # black hole.
  152. self._cris.preferences.delivery_status = DeliveryStatus.by_user
  153. self._dave.preferences.delivery_status = DeliveryStatus.by_user
  154. msgdata = {}
  155. self._process(self._mlist, self._msg, msgdata)
  156. self.assertEqual(msgdata['recipients'], set(('noreply@example.com',)))
  157. def test_no_owners(self):
  158. # If a list has no owners or moderators, then the site owner gets the
  159. # message. This prevents a list's -owner address from going into a
  160. # black hole.
  161. self._cris.unsubscribe()
  162. self._dave.unsubscribe()
  163. self.assertEqual(self._mlist.administrators.member_count, 0)
  164. msgdata = {}
  165. self._process(self._mlist, self._msg, msgdata)
  166. self.assertEqual(msgdata['recipients'], set(('noreply@example.com',)))
  167. @configuration('mailman', site_owner='siteadmin@example.com')
  168. def test_no_owners_site_owner_fallback(self):
  169. # The list has no owners or moderators, but there is a non-default
  170. # site owner defined. That owner gets the message.
  171. self._cris.unsubscribe()
  172. self._dave.unsubscribe()
  173. self.assertEqual(self._mlist.administrators.member_count, 0)
  174. msgdata = {}
  175. self._process(self._mlist, self._msg, msgdata)
  176. self.assertEqual(msgdata['recipients'],
  177. set(('siteadmin@example.com',)))