test_emailnotification_middleware.py 6.5 KB

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