test_subscription_middleware.py 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 test
  6. from misago.threads.test import patch_category_acl
  7. from misago.users.test import AuthenticatedUserTestCase
  8. User = 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 = User.SUBSCRIPTION_NONE
  21. self.user.subscribe_to_replied_threads = User.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 = User.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 = User.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 = test.post_thread(self.category)
  76. self.api_link = reverse(
  77. "misago:api:thread-post-list", kwargs={"thread_pk": self.thread.pk}
  78. )
  79. @patch_category_acl({"can_reply_threads": True})
  80. def test_dont_subscribe(self):
  81. """middleware makes no subscription to thread"""
  82. self.user.subscribe_to_started_threads = User.SUBSCRIPTION_NOTIFY
  83. self.user.subscribe_to_replied_threads = User.SUBSCRIPTION_NONE
  84. self.user.save()
  85. response = self.client.post(
  86. self.api_link, data={"post": "This is test response!"}
  87. )
  88. self.assertEqual(response.status_code, 200)
  89. # user has no subscriptions
  90. self.assertEqual(self.user.subscription_set.count(), 0)
  91. @patch_category_acl({"can_reply_threads": True})
  92. def test_subscribe(self):
  93. """middleware subscribes thread"""
  94. self.user.subscribe_to_replied_threads = User.SUBSCRIPTION_NOTIFY
  95. self.user.save()
  96. response = self.client.post(
  97. self.api_link, data={"post": "This is test response!"}
  98. )
  99. self.assertEqual(response.status_code, 200)
  100. # user has subscribed to thread
  101. subscription = self.user.subscription_set.get(thread=self.thread)
  102. self.assertEqual(subscription.category_id, self.category.id)
  103. self.assertFalse(subscription.send_email)
  104. @patch_category_acl({"can_reply_threads": True})
  105. def test_email_subscribe(self):
  106. """middleware subscribes thread with an email"""
  107. self.user.subscribe_to_replied_threads = User.SUBSCRIPTION_ALL
  108. self.user.save()
  109. response = self.client.post(
  110. self.api_link, data={"post": "This is test response!"}
  111. )
  112. self.assertEqual(response.status_code, 200)
  113. # user has subscribed to thread
  114. subscription = self.user.subscription_set.get(thread=self.thread)
  115. self.assertEqual(subscription.category_id, self.category.id)
  116. self.assertTrue(subscription.send_email)
  117. @patch_category_acl({"can_reply_threads": True})
  118. def test_subscribe_with_events(self):
  119. """middleware omits events when testing for replied thread"""
  120. self.user.subscribe_to_replied_threads = User.SUBSCRIPTION_ALL
  121. self.user.save()
  122. # set event in thread
  123. test.reply_thread(self.thread, self.user, is_event=True)
  124. # reply thread
  125. response = self.client.post(
  126. self.api_link, data={"post": "This is test response!"}
  127. )
  128. self.assertEqual(response.status_code, 200)
  129. # user has subscribed to thread
  130. subscription = self.user.subscription_set.get(thread=self.thread)
  131. self.assertEqual(subscription.category_id, self.category.id)
  132. self.assertTrue(subscription.send_email)
  133. @patch_category_acl({"can_reply_threads": True})
  134. @patch_user_acl({"can_omit_flood_protection": True})
  135. def test_dont_subscribe_replied(self):
  136. """middleware omits threads user already replied"""
  137. self.user.subscribe_to_replied_threads = User.SUBSCRIPTION_ALL
  138. self.user.save()
  139. response = self.client.post(
  140. self.api_link, data={"post": "This is test response!"}
  141. )
  142. self.assertEqual(response.status_code, 200)
  143. # clear subscription
  144. self.user.subscription_set.all().delete()
  145. # reply again
  146. response = self.client.post(
  147. self.api_link, data={"post": "This is test response!"}
  148. )
  149. self.assertEqual(response.status_code, 200)
  150. # user has no subscriptions
  151. self.assertEqual(self.user.subscription_set.count(), 0)