test_subscription_middleware.py 7.0 KB

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