test_emailnotification_middleware.py 6.5 KB

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