test_emailnotification_middleware.py 6.4 KB

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