test_emailnotification_middleware.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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('misago:api:thread-post-list', kwargs={
  25. 'thread_pk': self.thread.pk
  26. })
  27. self.other_user = UserModel.objects.create_user(
  28. 'Bob', 'bob@boberson.com', 'pass123')
  29. def override_acl(self):
  30. new_acl = deepcopy(self.user.acl)
  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)
  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(self.api_link, data={
  56. 'post': 'This is test response!'
  57. })
  58. self.assertEqual(response.status_code, 200)
  59. self.assertEqual(len(mail.outbox), 0)
  60. def test_poster_not_notified(self):
  61. """no emails are sent because only poster subscribes to thread"""
  62. self.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(self.api_link, data={
  69. 'post': 'This is test response!'
  70. })
  71. self.assertEqual(response.status_code, 200)
  72. self.assertEqual(len(mail.outbox), 0)
  73. def test_other_user_no_email_subscription(self):
  74. """no emails are sent because subscriber has e-mails off"""
  75. self.other_user.subscription_set.create(
  76. thread=self.thread,
  77. category=self.category,
  78. last_read_on=timezone.now(),
  79. send_email=False
  80. )
  81. response = self.client.post(self.api_link, data={
  82. 'post': 'This is test response!'
  83. })
  84. self.assertEqual(response.status_code, 200)
  85. self.assertEqual(len(mail.outbox), 0)
  86. def test_other_user_no_permission(self):
  87. """no emails are sent because subscriber has no permission to read thread"""
  88. self.other_user.subscription_set.create(
  89. thread=self.thread,
  90. category=self.category,
  91. last_read_on=timezone.now(),
  92. send_email=True
  93. )
  94. self.override_other_user_acl(hide=True)
  95. response = self.client.post(self.api_link, data={
  96. 'post': 'This is test response!'
  97. })
  98. self.assertEqual(response.status_code, 200)
  99. self.assertEqual(len(mail.outbox), 0)
  100. def test_other_user_not_read(self):
  101. """no emails are sent because subscriber didn't read previous post"""
  102. self.other_user.subscription_set.create(
  103. thread=self.thread,
  104. category=self.category,
  105. last_read_on=timezone.now(),
  106. send_email=True
  107. )
  108. self.override_other_user_acl()
  109. testutils.reply_thread(self.thread, posted_on=timezone.now())
  110. response = self.client.post(self.api_link, data={
  111. 'post': 'This is test response!'
  112. })
  113. self.assertEqual(response.status_code, 200)
  114. self.assertEqual(len(mail.outbox), 0)
  115. def test_other_notified(self):
  116. """email is sent to subscriber"""
  117. self.other_user.subscription_set.create(
  118. thread=self.thread,
  119. category=self.category,
  120. last_read_on=timezone.now(),
  121. send_email=True
  122. )
  123. self.override_other_user_acl()
  124. response = self.client.post(self.api_link, data={
  125. 'post': 'This is test response!'
  126. })
  127. self.assertEqual(response.status_code, 200)
  128. self.assertEqual(len(mail.outbox), 1)
  129. last_email = mail.outbox[-1]
  130. self.assertIn(self.user.username, last_email.subject)
  131. self.assertIn(self.thread.title, last_email.subject)
  132. message = smart_str(last_email.body)
  133. self.assertIn(self.user.username, message)
  134. self.assertIn(self.thread.title, message)
  135. self.assertIn(self.thread.get_absolute_url(), message)
  136. last_post = self.thread.post_set.order_by('id').last()
  137. self.assertIn(last_post.get_absolute_url(), message)
  138. def test_other_notified_after_reading(self):
  139. """email is sent to subscriber that had sub updated by read api"""
  140. self.other_user.subscription_set.create(
  141. thread=self.thread,
  142. category=self.category,
  143. last_read_on=self.thread.last_post_on,
  144. send_email=True
  145. )
  146. self.override_other_user_acl()
  147. response = self.client.post(self.api_link, data={
  148. 'post': 'This is test response!'
  149. })
  150. self.assertEqual(response.status_code, 200)
  151. self.assertEqual(len(mail.outbox), 1)
  152. last_email = mail.outbox[-1]
  153. self.assertIn(self.user.username, last_email.subject)
  154. self.assertIn(self.thread.title, last_email.subject)
  155. message = smart_str(last_email.body)
  156. self.assertIn(self.user.username, message)
  157. self.assertIn(self.thread.title, message)
  158. self.assertIn(self.thread.get_absolute_url(), message)
  159. last_post = self.thread.post_set.order_by('id').last()
  160. self.assertIn(last_post.get_absolute_url(), message)