test_emailnotification_middleware.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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.core.urlresolvers 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('Bob', 'bob@boberson.com', 'pass123')
  27. def override_acl(self):
  28. new_acl = deepcopy(self.user.acl)
  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)
  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({
  48. 'can_browse': False
  49. })
  50. override_acl(self.other_user, new_acl)
  51. def test_no_subscriptions(self):
  52. """no emails are sent because noone subscibes to thread"""
  53. response = self.client.post(self.api_link, data={
  54. 'post': 'This is test response!'
  55. })
  56. self.assertEqual(response.status_code, 200)
  57. self.assertEqual(len(mail.outbox), 0)
  58. def test_poster_not_notified(self):
  59. """no emails are sent because only poster subscribes to thread"""
  60. self.user.subscription_set.create(
  61. thread=self.thread,
  62. category=self.category,
  63. last_read_on=timezone.now(),
  64. send_email=True
  65. )
  66. response = self.client.post(self.api_link, data={
  67. 'post': 'This is test response!'
  68. })
  69. self.assertEqual(response.status_code, 200)
  70. self.assertEqual(len(mail.outbox), 0)
  71. def test_other_user_no_email_subscription(self):
  72. """no emails are sent because subscriber has e-mails off"""
  73. self.other_user.subscription_set.create(
  74. thread=self.thread,
  75. category=self.category,
  76. last_read_on=timezone.now(),
  77. send_email=False
  78. )
  79. response = self.client.post(self.api_link, data={
  80. 'post': 'This is test response!'
  81. })
  82. self.assertEqual(response.status_code, 200)
  83. self.assertEqual(len(mail.outbox), 0)
  84. def test_other_user_no_permission(self):
  85. """no emails are sent because subscriber has no permission to read thread"""
  86. self.other_user.subscription_set.create(
  87. thread=self.thread,
  88. category=self.category,
  89. last_read_on=timezone.now(),
  90. send_email=True
  91. )
  92. self.override_other_user_acl(hide=True)
  93. response = self.client.post(self.api_link, data={
  94. 'post': 'This is test response!'
  95. })
  96. self.assertEqual(response.status_code, 200)
  97. self.assertEqual(len(mail.outbox), 0)
  98. def test_other_user_not_read(self):
  99. """no emails are sent because subscriber didn't read previous post"""
  100. self.other_user.subscription_set.create(
  101. thread=self.thread,
  102. category=self.category,
  103. last_read_on=timezone.now(),
  104. send_email=True
  105. )
  106. self.override_other_user_acl()
  107. testutils.reply_thread(self.thread, posted_on=timezone.now())
  108. response = self.client.post(self.api_link, data={
  109. 'post': 'This is test response!'
  110. })
  111. self.assertEqual(response.status_code, 200)
  112. self.assertEqual(len(mail.outbox), 0)
  113. def test_other_notified(self):
  114. """email is sent to subscriber"""
  115. self.other_user.subscription_set.create(
  116. thread=self.thread,
  117. category=self.category,
  118. last_read_on=timezone.now(),
  119. send_email=True
  120. )
  121. self.override_other_user_acl()
  122. response = self.client.post(self.api_link, data={
  123. 'post': 'This is test response!'
  124. })
  125. self.assertEqual(response.status_code, 200)
  126. self.assertEqual(len(mail.outbox), 1)
  127. last_email = mail.outbox[-1]
  128. self.assertIn(self.user.username, last_email.subject)
  129. self.assertIn(self.thread.title, last_email.subject)
  130. message = smart_str(last_email.body)
  131. self.assertIn(self.user.username, message)
  132. self.assertIn(self.thread.title, message)
  133. self.assertIn(self.thread.get_absolute_url(), message)
  134. last_post = self.thread.post_set.order_by('id').last()
  135. self.assertIn(last_post.get_absolute_url(), message)