test_privatethreads_api.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from django.urls import reverse
  2. from misago.acl.testutils import override_acl
  3. from .. import testutils
  4. from ..models import ThreadParticipant
  5. from .test_privatethreads import PrivateThreadsTestCase
  6. class PrivateThreadsApiListTests(PrivateThreadsTestCase):
  7. def setUp(self):
  8. super(PrivateThreadsApiListTests, self).setUp()
  9. self.api_link = reverse('misago:api:private-thread-list')
  10. def test_unauthenticated(self):
  11. """api requires user to sign in and be able to access it"""
  12. self.logout_user()
  13. response = self.client.get(self.api_link)
  14. self.assertContains(response, "sign in to use private threads", status_code=403)
  15. def test_no_permission(self):
  16. """api requires user to have permission to be able to access it"""
  17. override_acl(self.user, {
  18. 'can_use_private_threads': 0
  19. })
  20. response = self.client.get(self.api_link)
  21. self.assertContains(response, "can't use private threads", status_code=403)
  22. def test_empty_list(self):
  23. """api has no showstoppers on returning empty list"""
  24. response = self.client.get(self.api_link)
  25. self.assertEqual(response.status_code, 200)
  26. response_json = response.json()
  27. self.assertEqual(response_json['count'], 0)
  28. def test_thread_visibility(self):
  29. """only participated threads are returned by private threads api"""
  30. visible = testutils.post_thread(category=self.category, poster=self.user)
  31. hidden = testutils.post_thread(category=self.category, poster=self.user)
  32. reported = testutils.post_thread(category=self.category, poster=self.user)
  33. ThreadParticipant.objects.add_participants(visible, [self.user])
  34. reported.has_reported_posts = True
  35. reported.save()
  36. response = self.client.get(self.api_link)
  37. self.assertEqual(response.status_code, 200)
  38. response_json = response.json()
  39. self.assertEqual(response_json['count'], 1)
  40. self.assertEqual(response_json['results'][0]['id'], visible.id)
  41. # threads with reported posts will also show to moderators
  42. override_acl(self.user, {
  43. 'can_moderate_private_threads': 1
  44. })
  45. response = self.client.get(self.api_link)
  46. self.assertEqual(response.status_code, 200)
  47. response_json = response.json()
  48. self.assertEqual(response_json['count'], 2)
  49. self.assertEqual(response_json['results'][0]['id'], reported.id)
  50. self.assertEqual(response_json['results'][1]['id'], visible.id)
  51. class PrivateThreadsApiGetTests(PrivateThreadsTestCase):
  52. def setUp(self):
  53. super(PrivateThreadsApiGetTests, self).setUp()
  54. self.thread = testutils.post_thread(self.category, poster=self.user)
  55. self.api_url = self.thread.get_api_url()
  56. def test_anonymous(self):
  57. """anonymous user can't see private thread"""
  58. self.logout_user()
  59. response = self.client.get(self.api_url)
  60. self.assertContains(response, "sign in to use private threads", status_code=403)
  61. def test_no_permission(self):
  62. """user needs to have permission to see private thread"""
  63. override_acl(self.user, {
  64. 'can_use_private_threads': 0
  65. })
  66. response = self.client.get(self.api_url)
  67. self.assertContains(response, "t use private threads", status_code=403)
  68. def test_no_participant(self):
  69. """user cant see thread he isn't part of"""
  70. response = self.client.get(self.api_url)
  71. self.assertEqual(response.status_code, 404)
  72. def test_mod_not_reported(self):
  73. """moderator can't see private thread that has no reports"""
  74. override_acl(self.user, {
  75. 'can_moderate_private_threads': 1
  76. })
  77. response = self.client.get(self.api_url)
  78. self.assertEqual(response.status_code, 404)
  79. def test_reported_not_mod(self):
  80. """non-mod can't see private thread that has reported posts"""
  81. self.thread.has_reported_posts = True
  82. self.thread.save()
  83. response = self.client.get(self.api_url)
  84. self.assertEqual(response.status_code, 404)
  85. def test_can_see_owner(self):
  86. """user can see thread he is owner of"""
  87. ThreadParticipant.objects.set_owner(self.thread, self.user)
  88. response = self.client.get(self.api_url)
  89. self.assertContains(response, self.thread.title)
  90. def test_can_see_participant(self):
  91. """user can see thread he is participant of"""
  92. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  93. response = self.client.get(self.api_url)
  94. self.assertContains(response, self.thread.title)
  95. def test_mod_can_see_reported(self):
  96. """moderator can see private thread that has reports"""
  97. override_acl(self.user, {
  98. 'can_moderate_private_threads': 1
  99. })
  100. self.thread.has_reported_posts = True
  101. self.thread.save()
  102. response = self.client.get(self.api_url)
  103. self.assertContains(response, self.thread.title)