test_subscription_middleware.py 6.2 KB

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