test_emailnotification_middleware.py 6.3 KB

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