test_thread_api.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. import json
  2. from misago.users.testutils import AuthenticatedUserTestCase
  3. from misago.categories.models import Category
  4. from misago.threads import testutils
  5. class ThreadApiTestCase(AuthenticatedUserTestCase):
  6. def setUp(self):
  7. super(ThreadApiTestCase, self).setUp()
  8. self.category = Category.objects.get(slug='first-category')
  9. self.thread = testutils.post_thread(category=self.category)
  10. self.api_link = '/api/threads/%s/' % self.thread.pk
  11. def get_thread_json(self):
  12. response = self.client.get('/api/threads/%s/' % self.thread.pk)
  13. self.assertEqual(response.status_code, 200)
  14. return json.loads(response.content)
  15. class ThreadsReadApiTests(ThreadApiTestCase):
  16. def setUp(self):
  17. super(ThreadSubscribeApiTests, self).setUp()
  18. self.api_link = '/api/threads/read/'
  19. def read_all_threads(self):
  20. """api sets all threads as read"""
  21. response = self.client.post(self.api_link)
  22. self.assertEqual(response.status_code, 200)
  23. self.assertEqual(self.user.categoryread_set.count(), 2)
  24. def read_threads_in_category(self):
  25. """api sets threads in category as read"""
  26. response = self.client.post(
  27. '%s?category=%s' % (self.api_link, self.category.pk))
  28. self.assertEqual(response.status_code, 200)
  29. self.assertEqual(self.user.categoryread_set.count(), 1)
  30. class ThreadSubscribeApiTests(ThreadApiTestCase):
  31. def setUp(self):
  32. super(ThreadSubscribeApiTests, self).setUp()
  33. self.api_link = '/api/threads/%s/subscribe/' % self.thread.pk
  34. def test_subscribe_thread(self):
  35. """api makes it possible to subscribe thread"""
  36. response = self.client.post(self.api_link, json.dumps({
  37. 'notify': True
  38. }),
  39. content_type="application/json")
  40. self.assertEqual(response.status_code, 200)
  41. thread_json = self.get_thread_json()
  42. self.assertFalse(thread_json['subscription'])
  43. subscription = self.user.subscription_set.get(thread=self.thread)
  44. self.assertFalse(subscription.send_email)
  45. def test_subscribe_thread_with_email(self):
  46. """api makes it possible to subscribe thread with emails"""
  47. response = self.client.post(self.api_link, json.dumps({
  48. 'email': True
  49. }),
  50. content_type="application/json")
  51. self.assertEqual(response.status_code, 200)
  52. thread_json = self.get_thread_json()
  53. self.assertTrue(thread_json['subscription'])
  54. subscription = self.user.subscription_set.get(thread=self.thread)
  55. self.assertTrue(subscription.send_email)
  56. def test_unsubscribe_thread(self):
  57. """api makes it possible to unsubscribe thread"""
  58. response = self.client.post(self.api_link, json.dumps({
  59. 'remove': True
  60. }),
  61. content_type="application/json")
  62. self.assertEqual(response.status_code, 200)
  63. thread_json = self.get_thread_json()
  64. self.assertIsNone(thread_json['subscription'])
  65. self.assertEqual(self.user.subscription_set.count(), 0)
  66. def test_subscribe_as_guest(self):
  67. """api makes it impossible to subscribe thread"""
  68. self.logout_user()
  69. response = self.client.post(self.api_link, json.dumps({
  70. 'notify': True
  71. }),
  72. content_type="application/json")
  73. self.assertEqual(response.status_code, 403)
  74. def test_subscribe_nonexistant_thread(self):
  75. """api makes it impossible to subscribe nonexistant thread"""
  76. bad_api_link = self.api_link.replace(
  77. unicode(self.thread.pk), unicode(self.thread.pk + 9))
  78. response = self.client.post(bad_api_link, json.dumps({
  79. 'notify': True
  80. }),
  81. content_type="application/json")
  82. self.assertEqual(response.status_code, 404)