test_emailnotification_middleware.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. from datetime import timedelta
  2. from django.core import mail
  3. from django.urls import reverse
  4. from django.utils import timezone
  5. from django.utils.encoding import smart_str
  6. from ...conf.test import override_dynamic_settings
  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. @override_dynamic_settings(forum_address="http://test.com/")
  105. @patch_category_acl({"can_reply_threads": True})
  106. def test_other_notified(self):
  107. """email is sent to subscriber"""
  108. self.other_user.subscription_set.create(
  109. thread=self.thread,
  110. category=self.category,
  111. last_read_on=timezone.now(),
  112. send_email=True,
  113. )
  114. response = self.client.post(
  115. self.api_link, data={"post": "This is test response!"}
  116. )
  117. self.assertEqual(response.status_code, 200)
  118. self.assertEqual(len(mail.outbox), 1)
  119. last_email = mail.outbox[-1]
  120. self.assertIn(self.user.username, last_email.subject)
  121. self.assertIn(self.thread.title, last_email.subject)
  122. message = smart_str(last_email.body)
  123. self.assertIn(self.user.username, message)
  124. self.assertIn(self.thread.title, message)
  125. self.assertIn(self.thread.get_absolute_url(), message)
  126. last_post = self.thread.post_set.order_by("id").last()
  127. self.assertIn(last_post.get_absolute_url(), message)
  128. @override_dynamic_settings(forum_address="http://test.com/")
  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)