test_emailnotification_middleware.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. from copy import deepcopy
  2. from datetime import timedelta
  3. from django.contrib.auth import get_user_model
  4. from django.core import mail
  5. from django.urls import reverse
  6. from django.utils import timezone
  7. from django.utils.encoding import smart_str
  8. from misago.acl.testutils import override_acl
  9. from misago.categories.models import Category
  10. from misago.threads import testutils
  11. from misago.users.testutils import AuthenticatedUserTestCase
  12. UserModel = get_user_model()
  13. class EmailNotificationTests(AuthenticatedUserTestCase):
  14. def setUp(self):
  15. super(EmailNotificationTests, self).setUp()
  16. self.category = Category.objects.get(slug='first-category')
  17. self.thread = testutils.post_thread(
  18. category=self.category,
  19. started_on=timezone.now() - timedelta(seconds=5),
  20. )
  21. self.override_acl()
  22. self.api_link = reverse(
  23. 'misago:api:thread-post-list', kwargs={
  24. 'thread_pk': self.thread.pk,
  25. }
  26. )
  27. self.other_user = UserModel.objects.create_user('Bob', 'bob@boberson.com', 'pass123')
  28. def override_acl(self):
  29. new_acl = deepcopy(self.user.acl_cache)
  30. new_acl['categories'][self.category.pk].update({
  31. 'can_see': 1,
  32. 'can_browse': 1,
  33. 'can_start_threads': 1,
  34. 'can_reply_threads': 1,
  35. 'can_edit_posts': 1,
  36. })
  37. override_acl(self.user, new_acl)
  38. def override_other_user_acl(self, hide=False):
  39. new_acl = deepcopy(self.other_user.acl_cache)
  40. new_acl['categories'][self.category.pk].update({
  41. 'can_see': 1,
  42. 'can_browse': 1,
  43. 'can_start_threads': 1,
  44. 'can_reply_threads': 1,
  45. 'can_edit_posts': 1,
  46. })
  47. if hide:
  48. new_acl['categories'][self.category.pk].update({
  49. 'can_browse': False,
  50. })
  51. override_acl(self.other_user, new_acl)
  52. def test_no_subscriptions(self):
  53. """no emails are sent because noone subscibes to thread"""
  54. response = self.client.post(
  55. self.api_link, data={
  56. 'post': 'This is test response!',
  57. }
  58. )
  59. self.assertEqual(response.status_code, 200)
  60. self.assertEqual(len(mail.outbox), 0)
  61. def test_poster_not_notified(self):
  62. """no emails are sent because only poster subscribes to thread"""
  63. self.user.subscription_set.create(
  64. thread=self.thread,
  65. category=self.category,
  66. last_read_on=timezone.now(),
  67. send_email=True,
  68. )
  69. response = self.client.post(
  70. self.api_link, data={
  71. 'post': 'This is test response!',
  72. }
  73. )
  74. self.assertEqual(response.status_code, 200)
  75. self.assertEqual(len(mail.outbox), 0)
  76. def test_other_user_no_email_subscription(self):
  77. """no emails are sent because subscriber has e-mails off"""
  78. self.other_user.subscription_set.create(
  79. thread=self.thread,
  80. category=self.category,
  81. last_read_on=timezone.now(),
  82. send_email=False,
  83. )
  84. response = self.client.post(
  85. self.api_link, data={
  86. 'post': 'This is test response!',
  87. }
  88. )
  89. self.assertEqual(response.status_code, 200)
  90. self.assertEqual(len(mail.outbox), 0)
  91. def test_other_user_no_permission(self):
  92. """no emails are sent because subscriber has no permission to read thread"""
  93. self.other_user.subscription_set.create(
  94. thread=self.thread,
  95. category=self.category,
  96. last_read_on=timezone.now(),
  97. send_email=True,
  98. )
  99. self.override_other_user_acl(hide=True)
  100. response = self.client.post(
  101. self.api_link, data={
  102. 'post': 'This is test response!',
  103. }
  104. )
  105. self.assertEqual(response.status_code, 200)
  106. self.assertEqual(len(mail.outbox), 0)
  107. def test_other_user_not_read(self):
  108. """no emails are sent because subscriber didn't read previous post"""
  109. self.other_user.subscription_set.create(
  110. thread=self.thread,
  111. category=self.category,
  112. last_read_on=timezone.now(),
  113. send_email=True,
  114. )
  115. self.override_other_user_acl()
  116. testutils.reply_thread(self.thread, posted_on=timezone.now())
  117. response = self.client.post(
  118. self.api_link, data={
  119. 'post': 'This is test response!',
  120. }
  121. )
  122. self.assertEqual(response.status_code, 200)
  123. self.assertEqual(len(mail.outbox), 0)
  124. def test_other_notified(self):
  125. """email is sent to subscriber"""
  126. self.other_user.subscription_set.create(
  127. thread=self.thread,
  128. category=self.category,
  129. last_read_on=timezone.now(),
  130. send_email=True,
  131. )
  132. self.override_other_user_acl()
  133. response = self.client.post(
  134. self.api_link, data={
  135. 'post': 'This is test response!',
  136. }
  137. )
  138. self.assertEqual(response.status_code, 200)
  139. self.assertEqual(len(mail.outbox), 1)
  140. last_email = mail.outbox[-1]
  141. self.assertIn(self.user.username, last_email.subject)
  142. self.assertIn(self.thread.title, last_email.subject)
  143. message = smart_str(last_email.body)
  144. self.assertIn(self.user.username, message)
  145. self.assertIn(self.thread.title, message)
  146. self.assertIn(self.thread.get_absolute_url(), message)
  147. last_post = self.thread.post_set.order_by('id').last()
  148. self.assertIn(last_post.get_absolute_url(), message)
  149. def test_other_notified_after_reading(self):
  150. """email is sent to subscriber that had sub updated by read api"""
  151. self.other_user.subscription_set.create(
  152. thread=self.thread,
  153. category=self.category,
  154. last_read_on=self.thread.last_post_on,
  155. send_email=True,
  156. )
  157. self.override_other_user_acl()
  158. response = self.client.post(
  159. self.api_link, data={
  160. 'post': 'This is test response!',
  161. }
  162. )
  163. self.assertEqual(response.status_code, 200)
  164. self.assertEqual(len(mail.outbox), 1)
  165. last_email = mail.outbox[-1]
  166. self.assertIn(self.user.username, last_email.subject)
  167. self.assertIn(self.thread.title, last_email.subject)
  168. message = smart_str(last_email.body)
  169. self.assertIn(self.user.username, message)
  170. self.assertIn(self.thread.title, message)
  171. self.assertIn(self.thread.get_absolute_url(), message)
  172. last_post = self.thread.post_set.order_by('id').last()
  173. self.assertIn(last_post.get_absolute_url(), message)