test_subscription_middleware.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. from __future__ import unicode_literals
  2. from django.contrib.auth import get_user_model
  3. from django.urls import reverse
  4. from misago.acl.testutils import override_acl
  5. from misago.categories.models import Category
  6. from misago.threads import testutils
  7. from misago.users.testutils import AuthenticatedUserTestCase
  8. UserModel = get_user_model()
  9. class SubscriptionMiddlewareTestCase(AuthenticatedUserTestCase):
  10. def setUp(self):
  11. super(SubscriptionMiddlewareTestCase, self).setUp()
  12. self.category = Category.objects.get(slug='first-category')
  13. self.override_acl()
  14. def override_acl(self):
  15. new_acl = self.user.acl_cache
  16. new_acl['can_omit_flood_protection'] = True
  17. new_acl['categories'][self.category.pk].update({
  18. 'can_see': 1,
  19. 'can_browse': 1,
  20. 'can_start_threads': 1,
  21. 'can_reply_threads': 1,
  22. })
  23. override_acl(self.user, new_acl)
  24. class SubscribeStartedThreadTests(SubscriptionMiddlewareTestCase):
  25. def setUp(self):
  26. super(SubscribeStartedThreadTests, self).setUp()
  27. self.api_link = reverse('misago:api:thread-list')
  28. def test_dont_subscribe(self):
  29. """middleware makes no subscription to thread"""
  30. self.user.subscribe_to_started_threads = UserModel.SUBSCRIBE_NONE
  31. self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_NOTIFY
  32. self.user.save()
  33. response = self.client.post(
  34. self.api_link,
  35. data={
  36. 'category': self.category.id,
  37. 'title': "This is an test thread!",
  38. 'post': "This is test response!",
  39. }
  40. )
  41. self.assertEqual(response.status_code, 200)
  42. # user has no subscriptions
  43. self.assertEqual(self.user.subscription_set.count(), 0)
  44. def test_subscribe(self):
  45. """middleware subscribes thread"""
  46. self.user.subscribe_to_started_threads = UserModel.SUBSCRIBE_NOTIFY
  47. self.user.save()
  48. response = self.client.post(
  49. self.api_link,
  50. data={
  51. 'category': self.category.id,
  52. 'title': "This is an test thread!",
  53. 'post': "This is test response!",
  54. }
  55. )
  56. self.assertEqual(response.status_code, 200)
  57. # user has subscribed to thread
  58. thread = self.user.thread_set.order_by('id').last()
  59. subscription = self.user.subscription_set.get(thread=thread)
  60. self.assertEqual(subscription.category_id, self.category.id)
  61. self.assertFalse(subscription.send_email)
  62. def test_email_subscribe(self):
  63. """middleware subscribes thread with an email"""
  64. self.user.subscribe_to_started_threads = UserModel.SUBSCRIBE_ALL
  65. self.user.save()
  66. response = self.client.post(
  67. self.api_link,
  68. data={
  69. 'category': self.category.id,
  70. 'title': "This is an test thread!",
  71. 'post': "This is test response!",
  72. }
  73. )
  74. self.assertEqual(response.status_code, 200)
  75. # user has subscribed to thread
  76. thread = self.user.thread_set.order_by('id').last()
  77. subscription = self.user.subscription_set.get(thread=thread)
  78. self.assertEqual(subscription.category_id, self.category.id)
  79. self.assertTrue(subscription.send_email)
  80. class SubscribeRepliedThreadTests(SubscriptionMiddlewareTestCase):
  81. def setUp(self):
  82. super(SubscribeRepliedThreadTests, self).setUp()
  83. self.thread = testutils.post_thread(self.category)
  84. self.api_link = reverse(
  85. 'misago:api:thread-post-list', kwargs={
  86. 'thread_pk': self.thread.pk,
  87. }
  88. )
  89. def test_dont_subscribe(self):
  90. """middleware makes no subscription to thread"""
  91. self.user.subscribe_to_started_threads = UserModel.SUBSCRIBE_NOTIFY
  92. self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_NONE
  93. self.user.save()
  94. response = self.client.post(
  95. self.api_link, data={
  96. 'post': "This is test response!",
  97. }
  98. )
  99. self.assertEqual(response.status_code, 200)
  100. # user has no subscriptions
  101. self.assertEqual(self.user.subscription_set.count(), 0)
  102. def test_subscribe(self):
  103. """middleware subscribes thread"""
  104. self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_NOTIFY
  105. self.user.save()
  106. response = self.client.post(
  107. self.api_link, data={
  108. 'post': "This is test response!",
  109. }
  110. )
  111. self.assertEqual(response.status_code, 200)
  112. # user has subscribed to thread
  113. subscription = self.user.subscription_set.get(thread=self.thread)
  114. self.assertEqual(subscription.category_id, self.category.id)
  115. self.assertFalse(subscription.send_email)
  116. def test_email_subscribe(self):
  117. """middleware subscribes thread with an email"""
  118. self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_ALL
  119. self.user.save()
  120. response = self.client.post(
  121. self.api_link, data={
  122. 'post': "This is test response!",
  123. }
  124. )
  125. self.assertEqual(response.status_code, 200)
  126. # user has subscribed to thread
  127. subscription = self.user.subscription_set.get(thread=self.thread)
  128. self.assertEqual(subscription.category_id, self.category.id)
  129. self.assertTrue(subscription.send_email)
  130. def test_subscribe_with_events(self):
  131. """middleware omits events when testing for replied thread"""
  132. self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_ALL
  133. self.user.save()
  134. # set event in thread
  135. testutils.reply_thread(self.thread, self.user, is_event=True)
  136. # reply thread
  137. response = self.client.post(
  138. self.api_link, data={
  139. 'post': "This is test response!",
  140. }
  141. )
  142. self.assertEqual(response.status_code, 200)
  143. # user has subscribed to thread
  144. subscription = self.user.subscription_set.get(thread=self.thread)
  145. self.assertEqual(subscription.category_id, self.category.id)
  146. self.assertTrue(subscription.send_email)
  147. def test_dont_subscribe_replied(self):
  148. """middleware omits threads user already replied"""
  149. self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_ALL
  150. self.user.save()
  151. response = self.client.post(
  152. self.api_link, data={
  153. 'post': "This is test response!",
  154. }
  155. )
  156. self.assertEqual(response.status_code, 200)
  157. # clear subscription
  158. self.user.subscription_set.all().delete()
  159. # reply again
  160. response = self.client.post(
  161. self.api_link, data={
  162. 'post': "This is test response!",
  163. }
  164. )
  165. self.assertEqual(response.status_code, 200)
  166. # user has no subscriptions
  167. self.assertEqual(self.user.subscription_set.count(), 0)