test_subscription_middleware.py 6.8 KB

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