test_emailnotification_middleware.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.categories.models import Category
  9. from misago.threads import testutils
  10. from misago.threads.test import (
  11. patch_category_acl, patch_other_user_category_acl
  12. )
  13. from misago.users.testutils import AuthenticatedUserTestCase
  14. UserModel = get_user_model()
  15. class EmailNotificationTests(AuthenticatedUserTestCase):
  16. def setUp(self):
  17. super().setUp()
  18. self.category = Category.objects.get(slug='first-category')
  19. self.thread = testutils.post_thread(
  20. category=self.category,
  21. started_on=timezone.now() - timedelta(seconds=5),
  22. )
  23. self.api_link = reverse(
  24. 'misago:api:thread-post-list', kwargs={
  25. 'thread_pk': self.thread.pk,
  26. }
  27. )
  28. self.other_user = UserModel.objects.create_user('BobBobertson', 'bob@boberson.com')
  29. @patch_category_acl({"can_reply_threads": True})
  30. def test_no_subscriptions(self):
  31. """no emails are sent because noone subscibes to thread"""
  32. response = self.client.post(
  33. self.api_link, data={
  34. 'post': 'This is test response!',
  35. }
  36. )
  37. self.assertEqual(response.status_code, 200)
  38. self.assertEqual(len(mail.outbox), 0)
  39. @patch_category_acl({"can_reply_threads": True})
  40. def test_poster_not_notified(self):
  41. """no emails are sent because only poster subscribes to thread"""
  42. self.user.subscription_set.create(
  43. thread=self.thread,
  44. category=self.category,
  45. last_read_on=timezone.now(),
  46. send_email=True,
  47. )
  48. response = self.client.post(
  49. self.api_link, data={
  50. 'post': 'This is test response!',
  51. }
  52. )
  53. self.assertEqual(response.status_code, 200)
  54. self.assertEqual(len(mail.outbox), 0)
  55. @patch_category_acl({"can_reply_threads": True})
  56. def test_other_user_no_email_subscription(self):
  57. """no emails are sent because subscriber has e-mails off"""
  58. self.other_user.subscription_set.create(
  59. thread=self.thread,
  60. category=self.category,
  61. last_read_on=timezone.now(),
  62. send_email=False,
  63. )
  64. response = self.client.post(
  65. self.api_link, data={
  66. 'post': 'This is test response!',
  67. }
  68. )
  69. self.assertEqual(response.status_code, 200)
  70. self.assertEqual(len(mail.outbox), 0)
  71. @patch_category_acl({"can_reply_threads": True})
  72. @patch_other_user_category_acl({"can_see": False})
  73. def test_other_user_no_permission(self):
  74. """no emails are sent because subscriber has no permission to read thread"""
  75. self.other_user.subscription_set.create(
  76. thread=self.thread,
  77. category=self.category,
  78. last_read_on=timezone.now(),
  79. send_email=True,
  80. )
  81. response = self.client.post(
  82. self.api_link, data={
  83. 'post': 'This is test response!',
  84. }
  85. )
  86. self.assertEqual(response.status_code, 200)
  87. self.assertEqual(len(mail.outbox), 0)
  88. @patch_category_acl({"can_reply_threads": True})
  89. def test_moderation_queue(self):
  90. """no emails are sent because new post is moderated"""
  91. self.category.require_replies_approval = True
  92. self.category.save()
  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. response = self.client.post(
  100. self.api_link, data={
  101. 'post': 'This is test response!',
  102. }
  103. )
  104. self.assertEqual(response.status_code, 200)
  105. self.assertEqual(len(mail.outbox), 0)
  106. @patch_category_acl({"can_reply_threads": True})
  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. testutils.reply_thread(self.thread, posted_on=timezone.now())
  116. response = self.client.post(
  117. self.api_link, data={
  118. 'post': 'This is test response!',
  119. }
  120. )
  121. self.assertEqual(response.status_code, 200)
  122. self.assertEqual(len(mail.outbox), 0)
  123. @patch_category_acl({"can_reply_threads": True})
  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. response = self.client.post(
  133. self.api_link, data={
  134. 'post': 'This is test response!',
  135. }
  136. )
  137. self.assertEqual(response.status_code, 200)
  138. self.assertEqual(len(mail.outbox), 1)
  139. last_email = mail.outbox[-1]
  140. self.assertIn(self.user.username, last_email.subject)
  141. self.assertIn(self.thread.title, last_email.subject)
  142. message = smart_str(last_email.body)
  143. self.assertIn(self.user.username, message)
  144. self.assertIn(self.thread.title, message)
  145. self.assertIn(self.thread.get_absolute_url(), message)
  146. last_post = self.thread.post_set.order_by('id').last()
  147. self.assertIn(last_post.get_absolute_url(), message)
  148. @patch_category_acl({"can_reply_threads": True})
  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. response = self.client.post(
  158. self.api_link, data={
  159. 'post': 'This is test response!',
  160. }
  161. )
  162. self.assertEqual(response.status_code, 200)
  163. self.assertEqual(len(mail.outbox), 1)
  164. last_email = mail.outbox[-1]
  165. self.assertIn(self.user.username, last_email.subject)
  166. self.assertIn(self.thread.title, last_email.subject)
  167. message = smart_str(last_email.body)
  168. self.assertIn(self.user.username, message)
  169. self.assertIn(self.thread.title, message)
  170. self.assertIn(self.thread.get_absolute_url(), message)
  171. last_post = self.thread.post_set.order_by('id').last()
  172. self.assertIn(last_post.get_absolute_url(), message)