test_privatethreads_api.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. from django.urls import reverse
  2. from misago.acl.test import patch_user_acl
  3. from misago.threads import testutils
  4. from misago.threads.models import Thread, ThreadParticipant
  5. from misago.threads.test import patch_private_threads_acl
  6. from .test_privatethreads import PrivateThreadsTestCase
  7. class PrivateThreadsListApiTests(PrivateThreadsTestCase):
  8. def setUp(self):
  9. super().setUp()
  10. self.api_link = reverse('misago:api:private-thread-list')
  11. def test_unauthenticated(self):
  12. """api requires user to sign in and be able to access it"""
  13. self.logout_user()
  14. response = self.client.get(self.api_link)
  15. self.assertEqual(response.status_code, 403)
  16. self.assertEqual(response.json(), {
  17. "detail": "You have to sign in to use private threads."
  18. })
  19. @patch_user_acl({"can_use_private_threads": False})
  20. def test_no_permission(self):
  21. """api requires user to have permission to be able to access it"""
  22. response = self.client.get(self.api_link)
  23. self.assertEqual(response.status_code, 403)
  24. self.assertEqual(response.json(), {
  25. "detail": "You can't use private threads."
  26. })
  27. @patch_user_acl({"can_use_private_threads": True})
  28. def test_empty_list(self):
  29. """api has no showstoppers on returning empty list"""
  30. response = self.client.get(self.api_link)
  31. self.assertEqual(response.status_code, 200)
  32. response_json = response.json()
  33. self.assertEqual(response_json['count'], 0)
  34. @patch_user_acl({"can_use_private_threads": True})
  35. def test_thread_visibility(self):
  36. """only participated threads are returned by private threads api"""
  37. visible = testutils.post_thread(category=self.category, poster=self.user)
  38. reported = testutils.post_thread(category=self.category, poster=self.user)
  39. # hidden thread
  40. testutils.post_thread(category=self.category, poster=self.user)
  41. ThreadParticipant.objects.add_participants(visible, [self.user])
  42. reported.has_reported_posts = True
  43. reported.save()
  44. response = self.client.get(self.api_link)
  45. self.assertEqual(response.status_code, 200)
  46. response_json = response.json()
  47. self.assertEqual(response_json['count'], 1)
  48. self.assertEqual(response_json['results'][0]['id'], visible.id)
  49. # threads with reported posts will also show to moderators
  50. with patch_user_acl({"can_moderate_private_threads": True}):
  51. response = self.client.get(self.api_link)
  52. self.assertEqual(response.status_code, 200)
  53. response_json = response.json()
  54. self.assertEqual(response_json['count'], 2)
  55. self.assertEqual(response_json['results'][0]['id'], reported.id)
  56. self.assertEqual(response_json['results'][1]['id'], visible.id)
  57. class PrivateThreadRetrieveApiTests(PrivateThreadsTestCase):
  58. def setUp(self):
  59. super().setUp()
  60. self.thread = testutils.post_thread(self.category, poster=self.user)
  61. self.api_link = self.thread.get_api_url()
  62. def test_anonymous(self):
  63. """anonymous user can't see private thread"""
  64. self.logout_user()
  65. response = self.client.get(self.api_link)
  66. self.assertEqual(response.status_code, 403)
  67. self.assertEqual(response.json(), {
  68. "detail": "You have to sign in to use private threads."
  69. })
  70. @patch_user_acl({"can_use_private_threads": False})
  71. def test_no_permission(self):
  72. """user needs to have permission to see private thread"""
  73. response = self.client.get(self.api_link)
  74. self.assertEqual(response.status_code, 403)
  75. self.assertEqual(response.json(), {
  76. "detail": "You can't use private threads."
  77. })
  78. @patch_user_acl({"can_use_private_threads": True})
  79. def test_no_participant(self):
  80. """user cant see thread he isn't part of"""
  81. response = self.client.get(self.api_link)
  82. self.assertEqual(response.status_code, 404)
  83. @patch_user_acl({
  84. "can_use_private_threads": True,
  85. "can_moderate_private_threads": True,
  86. })
  87. def test_mod_not_reported(self):
  88. """moderator can't see private thread that has no reports"""
  89. response = self.client.get(self.api_link)
  90. self.assertEqual(response.status_code, 404)
  91. @patch_user_acl({
  92. "can_use_private_threads": True,
  93. "can_moderate_private_threads": False,
  94. })
  95. def test_reported_not_mod(self):
  96. """non-mod can't see private thread that has reported posts"""
  97. self.thread.has_reported_posts = True
  98. self.thread.save()
  99. response = self.client.get(self.api_link)
  100. self.assertEqual(response.status_code, 404)
  101. @patch_user_acl({"can_use_private_threads": True})
  102. def test_can_see_owner(self):
  103. """user can see thread he is owner of"""
  104. ThreadParticipant.objects.set_owner(self.thread, self.user)
  105. response = self.client.get(self.api_link)
  106. self.assertEqual(response.status_code, 200)
  107. response_json = response.json()
  108. self.assertEqual(response_json['title'], self.thread.title)
  109. self.assertEqual(
  110. response_json['participants'], [
  111. {
  112. 'id': self.user.id,
  113. 'username': self.user.username,
  114. 'avatars': self.user.avatars,
  115. 'url': self.user.get_absolute_url(),
  116. 'is_owner': True,
  117. },
  118. ]
  119. )
  120. @patch_user_acl({"can_use_private_threads": True})
  121. def test_can_see_participant(self):
  122. """user can see thread he is participant of"""
  123. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  124. response = self.client.get(self.api_link)
  125. self.assertEqual(response.status_code, 200)
  126. response_json = response.json()
  127. self.assertEqual(response_json['title'], self.thread.title)
  128. self.assertEqual(
  129. response_json['participants'], [
  130. {
  131. 'id': self.user.id,
  132. 'username': self.user.username,
  133. 'avatars': self.user.avatars,
  134. 'url': self.user.get_absolute_url(),
  135. 'is_owner': False,
  136. },
  137. ]
  138. )
  139. @patch_user_acl({
  140. "can_use_private_threads": True,
  141. "can_moderate_private_threads": True,
  142. })
  143. def test_mod_can_see_reported(self):
  144. """moderator can see private thread that has reports"""
  145. self.thread.has_reported_posts = True
  146. self.thread.save()
  147. response = self.client.get(self.api_link)
  148. self.assertEqual(response.status_code, 200)
  149. response_json = response.json()
  150. self.assertEqual(response_json['title'], self.thread.title)
  151. self.assertEqual(response_json['participants'], [])
  152. class PrivateThreadDeleteApiTests(PrivateThreadsTestCase):
  153. def setUp(self):
  154. super().setUp()
  155. self.thread = testutils.post_thread(self.category, poster=self.user)
  156. self.api_link = self.thread.get_api_url()
  157. ThreadParticipant.objects.add_participants(self.thread, [self.user])
  158. @patch_private_threads_acl({"can_hide_threads": 0})
  159. def test_hide_thread_no_permission(self):
  160. """api tests permission to delete threads"""
  161. response = self.client.delete(self.api_link)
  162. self.assertEqual(response.status_code, 403)
  163. self.assertEqual(
  164. response.json()['detail'], "You can't delete threads in this category."
  165. )
  166. @patch_private_threads_acl({"can_hide_threads": 1})
  167. def test_delete_thread_no_permission(self):
  168. """api tests permission to delete threads"""
  169. response = self.client.delete(self.api_link)
  170. self.assertEqual(response.status_code, 403)
  171. self.assertEqual(
  172. response.json()['detail'], "You can't delete threads in this category."
  173. )
  174. @patch_private_threads_acl({"can_hide_threads": 2})
  175. def test_delete_thread(self):
  176. """DELETE to API link with permission deletes thread"""
  177. response = self.client.delete(self.api_link)
  178. self.assertEqual(response.status_code, 200)
  179. with self.assertRaises(Thread.DoesNotExist):
  180. Thread.objects.get(pk=self.thread.pk)