test_subscription_middleware.py 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.core.urlresolvers import reverse
  4. from misago.acl.testutils import override_acl
  5. from misago.categories.models import Category
  6. from misago.users.models import (
  7. AUTO_SUBSCRIBE_NONE, AUTO_SUBSCRIBE_NOTIFY, AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL)
  8. from misago.users.testutils import AuthenticatedUserTestCase
  9. from .. import testutils
  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
  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 = AUTO_SUBSCRIBE_NONE
  32. self.user.subscribe_to_replied_threads = AUTO_SUBSCRIBE_NOTIFY
  33. self.user.save()
  34. response = self.client.post(self.api_link, data={
  35. 'category': self.category.id,
  36. 'title': "This is an test thread!",
  37. 'post': "This is test response!"
  38. })
  39. self.assertEqual(response.status_code, 200)
  40. # user has no subscriptions
  41. self.assertEqual(self.user.subscription_set.count(), 0)
  42. def test_subscribe(self):
  43. """middleware subscribes thread"""
  44. self.user.subscribe_to_started_threads = AUTO_SUBSCRIBE_NOTIFY
  45. self.user.save()
  46. response = self.client.post(self.api_link, data={
  47. 'category': self.category.id,
  48. 'title': "This is an test thread!",
  49. 'post': "This is test response!"
  50. })
  51. self.assertEqual(response.status_code, 200)
  52. # user has subscribed to thread
  53. thread = self.user.thread_set.order_by('id').last()
  54. subscription = self.user.subscription_set.get(thread=thread)
  55. self.assertEqual(subscription.category_id, self.category.id)
  56. self.assertFalse(subscription.send_email)
  57. def test_email_subscribe(self):
  58. """middleware subscribes thread with an email"""
  59. self.user.subscribe_to_started_threads = AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL
  60. self.user.save()
  61. response = self.client.post(self.api_link, data={
  62. 'category': self.category.id,
  63. 'title': "This is an test thread!",
  64. 'post': "This is test response!"
  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(SubscribeRepliedThreadTests, self).setUp()
  75. self.thread = testutils.post_thread(self.category)
  76. self.api_link = reverse('misago:api:thread-post-list', kwargs={
  77. 'thread_pk': self.thread.pk
  78. })
  79. def test_dont_subscribe(self):
  80. """middleware makes no subscription to thread"""
  81. self.user.subscribe_to_started_threads = AUTO_SUBSCRIBE_NOTIFY
  82. self.user.subscribe_to_replied_threads = AUTO_SUBSCRIBE_NONE
  83. self.user.save()
  84. response = self.client.post(self.api_link, data={
  85. 'post': "This is test response!"
  86. })
  87. self.assertEqual(response.status_code, 200)
  88. # user has no subscriptions
  89. self.assertEqual(self.user.subscription_set.count(), 0)
  90. def test_subscribe(self):
  91. """middleware subscribes thread"""
  92. self.user.subscribe_to_replied_threads = AUTO_SUBSCRIBE_NOTIFY
  93. self.user.save()
  94. response = self.client.post(self.api_link, data={
  95. 'post': "This is test response!"
  96. })
  97. self.assertEqual(response.status_code, 200)
  98. # user has subscribed to thread
  99. subscription = self.user.subscription_set.get(thread=self.thread)
  100. self.assertEqual(subscription.category_id, self.category.id)
  101. self.assertFalse(subscription.send_email)
  102. def test_email_subscribe(self):
  103. """middleware subscribes thread with an email"""
  104. self.user.subscribe_to_replied_threads = AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL
  105. self.user.save()
  106. response = self.client.post(self.api_link, data={
  107. 'post': "This is test response!"
  108. })
  109. self.assertEqual(response.status_code, 200)
  110. # user has subscribed to thread
  111. subscription = self.user.subscription_set.get(thread=self.thread)
  112. self.assertEqual(subscription.category_id, self.category.id)
  113. self.assertTrue(subscription.send_email)
  114. def test_dont_subscribe_replied(self):
  115. """middleware omits threads user already replied"""
  116. self.user.subscribe_to_replied_threads = AUTO_SUBSCRIBE_NOTIFY_AND_EMAIL
  117. self.user.save()
  118. response = self.client.post(self.api_link, data={
  119. 'post': "This is test response!"
  120. })
  121. self.assertEqual(response.status_code, 200)
  122. # clear subscription
  123. self.user.subscription_set.all().delete()
  124. # reply again
  125. response = self.client.post(self.api_link, data={
  126. 'post': "This is test response!"
  127. })
  128. self.assertEqual(response.status_code, 200)
  129. # user has no subscriptions
  130. self.assertEqual(self.user.subscription_set.count(), 0)