test_thread_api.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 ThreadSubscribeApiTests(ThreadApiTestCase):
  16. def setUp(self):
  17. super(ThreadSubscribeApiTests, self).setUp()
  18. self.api_link = '/api/threads/%s/subscribe/' % self.thread.pk
  19. def test_subscribe_thread(self):
  20. """api makes it possible to subscribe thread"""
  21. response = self.client.post(self.api_link, json.dumps({
  22. 'notify': True
  23. }),
  24. content_type="application/json")
  25. self.assertEqual(response.status_code, 200)
  26. thread_json = self.get_thread_json()
  27. self.assertFalse(thread_json['subscription'])
  28. subscription = self.user.subscription_set.get(thread=self.thread)
  29. self.assertFalse(subscription.send_email)
  30. def test_subscribe_thread_with_email(self):
  31. """api makes it possible to subscribe thread with emails"""
  32. response = self.client.post(self.api_link, json.dumps({
  33. 'email': True
  34. }),
  35. content_type="application/json")
  36. self.assertEqual(response.status_code, 200)
  37. thread_json = self.get_thread_json()
  38. self.assertTrue(thread_json['subscription'])
  39. subscription = self.user.subscription_set.get(thread=self.thread)
  40. self.assertTrue(subscription.send_email)
  41. def test_unsubscribe_thread(self):
  42. """api makes it possible to unsubscribe thread"""
  43. response = self.client.post(self.api_link, json.dumps({
  44. 'remove': True
  45. }),
  46. content_type="application/json")
  47. self.assertEqual(response.status_code, 200)
  48. thread_json = self.get_thread_json()
  49. self.assertIsNone(thread_json['subscription'])
  50. self.assertEqual(self.user.subscription_set.count(), 0)
  51. def test_subscribe_as_guest(self):
  52. """api makes it impossible to subscribe thread"""
  53. self.logout_user()
  54. response = self.client.post(self.api_link, json.dumps({
  55. 'notify': True
  56. }),
  57. content_type="application/json")
  58. self.assertEqual(response.status_code, 403)
  59. def test_subscribe_nonexistant_thread(self):
  60. """api makes it impossible to subscribe nonexistant thread"""
  61. bad_api_link = self.api_link.replace(
  62. unicode(self.thread.pk), unicode(self.thread.pk + 9))
  63. response = self.client.post(bad_api_link, json.dumps({
  64. 'notify': True
  65. }),
  66. content_type="application/json")
  67. self.assertEqual(response.status_code, 404)