test_subscription_middleware.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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={'thread_pk': self.thread.pk}
  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(self.api_link, data={'post': "This is test response!"})
  94. self.assertEqual(response.status_code, 200)
  95. # user has no subscriptions
  96. self.assertEqual(self.user.subscription_set.count(), 0)
  97. def test_subscribe(self):
  98. """middleware subscribes thread"""
  99. self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_NOTIFY
  100. self.user.save()
  101. response = self.client.post(self.api_link, data={'post': "This is test response!"})
  102. self.assertEqual(response.status_code, 200)
  103. # user has subscribed to thread
  104. subscription = self.user.subscription_set.get(thread=self.thread)
  105. self.assertEqual(subscription.category_id, self.category.id)
  106. self.assertFalse(subscription.send_email)
  107. def test_email_subscribe(self):
  108. """middleware subscribes thread with an email"""
  109. self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_ALL
  110. self.user.save()
  111. response = self.client.post(self.api_link, data={'post': "This is test response!"})
  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. def test_dont_subscribe_replied(self):
  118. """middleware omits threads user already replied"""
  119. self.user.subscribe_to_replied_threads = UserModel.SUBSCRIBE_ALL
  120. self.user.save()
  121. response = self.client.post(self.api_link, data={'post': "This is test response!"})
  122. self.assertEqual(response.status_code, 200)
  123. # clear subscription
  124. self.user.subscription_set.all().delete()
  125. # reply again
  126. response = self.client.post(self.api_link, data={'post': "This is test response!"})
  127. self.assertEqual(response.status_code, 200)
  128. # user has no subscriptions
  129. self.assertEqual(self.user.subscription_set.count(), 0)