test_emailnotification_middleware.py 6.8 KB

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