test_emailnotification_middleware.py 6.3 KB

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