test_subscription_middleware.py 5.9 KB

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