test_emailnotification_middleware.py 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. from __future__ import unicode_literals
  2. from copy import deepcopy
  3. from datetime import timedelta
  4. from django.contrib.auth import get_user_model
  5. from django.core import mail
  6. from django.urls import reverse
  7. from django.utils import timezone
  8. from django.utils.encoding import smart_str
  9. from misago.acl.testutils import override_acl
  10. from misago.categories.models import Category
  11. from misago.threads import testutils
  12. from misago.users.testutils import AuthenticatedUserTestCase
  13. UserModel = get_user_model()
  14. class EmailNotificationTests(AuthenticatedUserTestCase):
  15. def setUp(self):
  16. super(EmailNotificationTests, self).setUp()
  17. self.category = Category.objects.get(slug='first-category')
  18. self.thread = testutils.post_thread(
  19. category=self.category,
  20. started_on=timezone.now() - timedelta(seconds=5),
  21. )
  22. self.override_acl()
  23. self.api_link = reverse(
  24. 'misago:api:thread-post-list', kwargs={
  25. 'thread_pk': self.thread.pk,
  26. }
  27. )
  28. self.other_user = UserModel.objects.create_user('Bob', 'bob@boberson.com', 'pass123')
  29. def override_acl(self):
  30. new_acl = deepcopy(self.user.acl_cache)
  31. new_acl['categories'][self.category.pk].update({
  32. 'can_see': 1,
  33. 'can_browse': 1,
  34. 'can_start_threads': 1,
  35. 'can_reply_threads': 1,
  36. 'can_edit_posts': 1,
  37. })
  38. override_acl(self.user, new_acl)
  39. def override_other_user_acl(self, hide=False):
  40. new_acl = deepcopy(self.other_user.acl_cache)
  41. new_acl['categories'][self.category.pk].update({
  42. 'can_see': 1,
  43. 'can_browse': 1,
  44. 'can_start_threads': 1,
  45. 'can_reply_threads': 1,
  46. 'can_edit_posts': 1,
  47. })
  48. if hide:
  49. new_acl['categories'][self.category.pk].update({
  50. 'can_browse': False,
  51. })
  52. override_acl(self.other_user, new_acl)
  53. def test_no_subscriptions(self):
  54. """no emails are sent because noone subscibes to thread"""
  55. response = self.client.post(
  56. self.api_link, data={
  57. 'post': 'This is test response!',
  58. }
  59. )
  60. self.assertEqual(response.status_code, 200)
  61. self.assertEqual(len(mail.outbox), 0)
  62. def test_poster_not_notified(self):
  63. """no emails are sent because only poster subscribes to thread"""
  64. self.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={
  72. 'post': 'This is test response!',
  73. }
  74. )
  75. self.assertEqual(response.status_code, 200)
  76. self.assertEqual(len(mail.outbox), 0)
  77. def test_other_user_no_email_subscription(self):
  78. """no emails are sent because subscriber has e-mails off"""
  79. self.other_user.subscription_set.create(
  80. thread=self.thread,
  81. category=self.category,
  82. last_read_on=timezone.now(),
  83. send_email=False,
  84. )
  85. response = self.client.post(
  86. self.api_link, data={
  87. 'post': 'This is test response!',
  88. }
  89. )
  90. self.assertEqual(response.status_code, 200)
  91. self.assertEqual(len(mail.outbox), 0)
  92. def test_other_user_no_permission(self):
  93. """no emails are sent because subscriber has no permission to read thread"""
  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. self.override_other_user_acl(hide=True)
  101. response = self.client.post(
  102. self.api_link, data={
  103. 'post': 'This is test response!',
  104. }
  105. )
  106. self.assertEqual(response.status_code, 200)
  107. self.assertEqual(len(mail.outbox), 0)
  108. def test_other_user_not_read(self):
  109. """no emails are sent because subscriber didn't read previous post"""
  110. self.other_user.subscription_set.create(
  111. thread=self.thread,
  112. category=self.category,
  113. last_read_on=timezone.now(),
  114. send_email=True,
  115. )
  116. self.override_other_user_acl()
  117. testutils.reply_thread(self.thread, posted_on=timezone.now())
  118. response = self.client.post(
  119. self.api_link, data={
  120. 'post': 'This is test response!',
  121. }
  122. )
  123. self.assertEqual(response.status_code, 200)
  124. self.assertEqual(len(mail.outbox), 0)
  125. def test_other_notified(self):
  126. """email is sent to subscriber"""
  127. self.other_user.subscription_set.create(
  128. thread=self.thread,
  129. category=self.category,
  130. last_read_on=timezone.now(),
  131. send_email=True,
  132. )
  133. self.override_other_user_acl()
  134. response = self.client.post(
  135. self.api_link, data={
  136. 'post': 'This is test response!',
  137. }
  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)
  150. def test_other_notified_after_reading(self):
  151. """email is sent to subscriber that had sub updated by read api"""
  152. self.other_user.subscription_set.create(
  153. thread=self.thread,
  154. category=self.category,
  155. last_read_on=self.thread.last_post_on,
  156. send_email=True,
  157. )
  158. self.override_other_user_acl()
  159. response = self.client.post(
  160. self.api_link, data={
  161. 'post': 'This is test response!',
  162. }
  163. )
  164. self.assertEqual(response.status_code, 200)
  165. self.assertEqual(len(mail.outbox), 1)
  166. last_email = mail.outbox[-1]
  167. self.assertIn(self.user.username, last_email.subject)
  168. self.assertIn(self.thread.title, last_email.subject)
  169. message = smart_str(last_email.body)
  170. self.assertIn(self.user.username, message)
  171. self.assertIn(self.thread.title, message)
  172. self.assertIn(self.thread.get_absolute_url(), message)
  173. last_post = self.thread.post_set.order_by('id').last()
  174. self.assertIn(last_post.get_absolute_url(), message)