test_emailnotification_middleware.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 patch_category_acl, patch_other_user_category_acl
  11. from misago.users.testutils import AuthenticatedUserTestCase
  12. UserModel = get_user_model()
  13. class EmailNotificationTests(AuthenticatedUserTestCase):
  14. def setUp(self):
  15. super().setUp()
  16. self.category = Category.objects.get(slug="first-category")
  17. self.thread = testutils.post_thread(
  18. category=self.category, started_on=timezone.now() - timedelta(seconds=5)
  19. )
  20. self.api_link = reverse(
  21. "misago:api:thread-post-list", kwargs={"thread_pk": self.thread.pk}
  22. )
  23. self.other_user = UserModel.objects.create_user(
  24. "BobBobertson", "bob@boberson.com"
  25. )
  26. @patch_category_acl({"can_reply_threads": True})
  27. def test_no_subscriptions(self):
  28. """no emails are sent because noone subscibes to thread"""
  29. response = self.client.post(
  30. self.api_link, data={"post": "This is test response!"}
  31. )
  32. self.assertEqual(response.status_code, 200)
  33. self.assertEqual(len(mail.outbox), 0)
  34. @patch_category_acl({"can_reply_threads": True})
  35. def test_poster_not_notified(self):
  36. """no emails are sent because only poster subscribes to thread"""
  37. self.user.subscription_set.create(
  38. thread=self.thread,
  39. category=self.category,
  40. last_read_on=timezone.now(),
  41. send_email=True,
  42. )
  43. response = self.client.post(
  44. self.api_link, data={"post": "This is test response!"}
  45. )
  46. self.assertEqual(response.status_code, 200)
  47. self.assertEqual(len(mail.outbox), 0)
  48. @patch_category_acl({"can_reply_threads": True})
  49. def test_other_user_no_email_subscription(self):
  50. """no emails are sent because subscriber has e-mails off"""
  51. self.other_user.subscription_set.create(
  52. thread=self.thread,
  53. category=self.category,
  54. last_read_on=timezone.now(),
  55. send_email=False,
  56. )
  57. response = self.client.post(
  58. self.api_link, data={"post": "This is test response!"}
  59. )
  60. self.assertEqual(response.status_code, 200)
  61. self.assertEqual(len(mail.outbox), 0)
  62. @patch_category_acl({"can_reply_threads": True})
  63. @patch_other_user_category_acl({"can_see": False})
  64. def test_other_user_no_permission(self):
  65. """no emails are sent because subscriber has no permission to read thread"""
  66. self.other_user.subscription_set.create(
  67. thread=self.thread,
  68. category=self.category,
  69. last_read_on=timezone.now(),
  70. send_email=True,
  71. )
  72. response = self.client.post(
  73. self.api_link, data={"post": "This is test response!"}
  74. )
  75. self.assertEqual(response.status_code, 200)
  76. self.assertEqual(len(mail.outbox), 0)
  77. @patch_category_acl({"can_reply_threads": True})
  78. def test_moderation_queue(self):
  79. """no emails are sent because new post is moderated"""
  80. self.category.require_replies_approval = True
  81. self.category.save()
  82. self.other_user.subscription_set.create(
  83. thread=self.thread,
  84. category=self.category,
  85. last_read_on=timezone.now(),
  86. send_email=True,
  87. )
  88. response = self.client.post(
  89. self.api_link, data={"post": "This is test response!"}
  90. )
  91. self.assertEqual(response.status_code, 200)
  92. self.assertEqual(len(mail.outbox), 0)
  93. @patch_category_acl({"can_reply_threads": True})
  94. def test_other_user_not_read(self):
  95. """no emails are sent because subscriber didn't read previous post"""
  96. self.other_user.subscription_set.create(
  97. thread=self.thread,
  98. category=self.category,
  99. last_read_on=timezone.now(),
  100. send_email=True,
  101. )
  102. testutils.reply_thread(self.thread, posted_on=timezone.now())
  103. response = self.client.post(
  104. self.api_link, data={"post": "This is test response!"}
  105. )
  106. self.assertEqual(response.status_code, 200)
  107. self.assertEqual(len(mail.outbox), 0)
  108. @patch_category_acl({"can_reply_threads": True})
  109. def test_other_notified(self):
  110. """email is sent to subscriber"""
  111. self.other_user.subscription_set.create(
  112. thread=self.thread,
  113. category=self.category,
  114. last_read_on=timezone.now(),
  115. send_email=True,
  116. )
  117. response = self.client.post(
  118. self.api_link, data={"post": "This is test response!"}
  119. )
  120. self.assertEqual(response.status_code, 200)
  121. self.assertEqual(len(mail.outbox), 1)
  122. last_email = mail.outbox[-1]
  123. self.assertIn(self.user.username, last_email.subject)
  124. self.assertIn(self.thread.title, last_email.subject)
  125. message = smart_str(last_email.body)
  126. self.assertIn(self.user.username, message)
  127. self.assertIn(self.thread.title, message)
  128. self.assertIn(self.thread.get_absolute_url(), message)
  129. last_post = self.thread.post_set.order_by("id").last()
  130. self.assertIn(last_post.get_absolute_url(), message)
  131. @patch_category_acl({"can_reply_threads": True})
  132. def test_other_notified_after_reading(self):
  133. """email is sent to subscriber that had sub updated by read api"""
  134. self.other_user.subscription_set.create(
  135. thread=self.thread,
  136. category=self.category,
  137. last_read_on=self.thread.last_post_on,
  138. send_email=True,
  139. )
  140. response = self.client.post(
  141. self.api_link, data={"post": "This is test response!"}
  142. )
  143. self.assertEqual(response.status_code, 200)
  144. self.assertEqual(len(mail.outbox), 1)
  145. last_email = mail.outbox[-1]
  146. self.assertIn(self.user.username, last_email.subject)
  147. self.assertIn(self.thread.title, last_email.subject)
  148. message = smart_str(last_email.body)
  149. self.assertIn(self.user.username, message)
  150. self.assertIn(self.thread.title, message)
  151. self.assertIn(self.thread.get_absolute_url(), message)
  152. last_post = self.thread.post_set.order_by("id").last()
  153. self.assertIn(last_post.get_absolute_url(), message)