test_emailnotification_middleware.py 6.3 KB

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