test_thread_postbulkpatch_api.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. import json
  2. from datetime import timedelta
  3. from django.urls import reverse
  4. from django.utils import timezone
  5. from misago.acl.testutils import override_acl
  6. from misago.categories.models import Category
  7. from misago.threads import testutils
  8. from misago.threads.models import Post, Thread
  9. from misago.users.testutils import AuthenticatedUserTestCase
  10. class ThreadPostBulkPatchApiTestCase(AuthenticatedUserTestCase):
  11. def setUp(self):
  12. super().setUp()
  13. self.category = Category.objects.get(slug='first-category')
  14. self.thread = testutils.post_thread(category=self.category)
  15. self.posts = [
  16. testutils.reply_thread(self.thread, poster=self.user),
  17. testutils.reply_thread(self.thread),
  18. testutils.reply_thread(self.thread, poster=self.user),
  19. ]
  20. self.ids = [p.id for p in self.posts]
  21. self.api_link = reverse(
  22. 'misago:api:thread-post-list',
  23. kwargs={
  24. 'thread_pk': self.thread.pk,
  25. }
  26. )
  27. def patch(self, api_link, ops):
  28. return self.client.patch(api_link, json.dumps(ops), content_type="application/json")
  29. def override_acl(self, extra_acl=None):
  30. new_acl = self.user.acl_cache
  31. new_acl['categories'][self.category.pk].update({
  32. 'can_see': 1,
  33. 'can_browse': 1,
  34. 'can_start_threads': 0,
  35. 'can_reply_threads': 0,
  36. 'can_edit_posts': 1,
  37. })
  38. if extra_acl:
  39. new_acl['categories'][self.category.pk].update(extra_acl)
  40. override_acl(self.user, new_acl)
  41. class BulkPatchSerializerTests(ThreadPostBulkPatchApiTestCase):
  42. def test_invalid_input_type(self):
  43. """api rejects invalid input type"""
  44. response = self.patch(self.api_link, [1, 2, 3])
  45. self.assertEqual(response.status_code, 400)
  46. self.assertEqual(response.json(), {
  47. 'non_field_errors': [
  48. "Invalid data. Expected a dictionary, but got list.",
  49. ],
  50. })
  51. def test_missing_input_keys(self):
  52. """api rejects input with missing keys"""
  53. response = self.patch(self.api_link, {})
  54. self.assertEqual(response.status_code, 400)
  55. self.assertEqual(response.json(), {
  56. 'ids': [
  57. "This field is required.",
  58. ],
  59. 'ops': [
  60. "This field is required.",
  61. ],
  62. })
  63. def test_empty_input_keys(self):
  64. """api rejects input with empty keys"""
  65. response = self.patch(self.api_link, {
  66. 'ids': [],
  67. 'ops': [],
  68. })
  69. self.assertEqual(response.status_code, 400)
  70. self.assertEqual(response.json(), {
  71. 'ids': [
  72. "Ensure this field has at least 1 elements.",
  73. ],
  74. 'ops': [
  75. "Ensure this field has at least 1 elements.",
  76. ],
  77. })
  78. def test_invalid_input_keys(self):
  79. """api rejects input with invalid keys"""
  80. response = self.patch(self.api_link, {
  81. 'ids': ['a'],
  82. 'ops': [1],
  83. })
  84. self.assertEqual(response.status_code, 400)
  85. self.assertEqual(response.json(), {
  86. 'ids': [
  87. "A valid integer is required.",
  88. ],
  89. 'ops': [
  90. 'Expected a dictionary of items but got type "int".',
  91. ],
  92. })
  93. def test_too_small_id(self):
  94. """api rejects input with implausiple id"""
  95. response = self.patch(self.api_link, {
  96. 'ids': [0],
  97. 'ops': [{}],
  98. })
  99. self.assertEqual(response.status_code, 400)
  100. self.assertEqual(response.json(), {
  101. 'ids': [
  102. "Ensure this value is greater than or equal to 1.",
  103. ],
  104. })
  105. def test_too_large_input(self):
  106. """api rejects too large input"""
  107. response = self.patch(self.api_link, {
  108. 'ids': [i + 1 for i in range(200)],
  109. 'ops': [{} for i in range(200)],
  110. })
  111. self.assertEqual(response.status_code, 400)
  112. self.assertEqual(response.json(), {
  113. 'ids': [
  114. "Ensure this field has no more than 24 elements.",
  115. ],
  116. 'ops': [
  117. "Ensure this field has no more than 10 elements.",
  118. ],
  119. })
  120. def test_posts_not_found(self):
  121. """api fails to find posts"""
  122. posts = [
  123. testutils.reply_thread(self.thread, is_hidden=True),
  124. testutils.reply_thread(self.thread, is_unapproved=True),
  125. ]
  126. response = self.patch(self.api_link, {
  127. 'ids': [p.id for p in posts],
  128. 'ops': [{}],
  129. })
  130. self.assertEqual(response.status_code, 403)
  131. self.assertEqual(response.json(), {
  132. 'detail': "One or more posts to update could not be found.",
  133. })
  134. def test_ops_invalid(self):
  135. """api validates descriptions"""
  136. response = self.patch(self.api_link, {
  137. 'ids': self.ids[:1],
  138. 'ops': [{}],
  139. })
  140. self.assertEqual(response.status_code, 400)
  141. self.assertEqual(response.json(), [
  142. {'id': self.ids[0], 'detail': ['undefined op']},
  143. ])
  144. def test_anonymous_user(self):
  145. """anonymous users can't use bulk actions"""
  146. self.logout_user()
  147. response = self.patch(self.api_link, {
  148. 'ids': self.ids[:1],
  149. 'ops': [{}],
  150. })
  151. self.assertEqual(response.status_code, 403)
  152. def test_events(self):
  153. """cant use bulk actions for events"""
  154. for post in self.posts:
  155. post.is_event = True
  156. post.save()
  157. response = self.patch(self.api_link, {
  158. 'ids': self.ids,
  159. 'ops': [{}],
  160. })
  161. self.assertEqual(response.status_code, 403)
  162. self.assertEqual(response.json(), {
  163. 'detail': "One or more posts to update could not be found.",
  164. })
  165. class PostsAddAclApiTests(ThreadPostBulkPatchApiTestCase):
  166. def test_add_acl_true(self):
  167. """api adds posts acls to response"""
  168. response = self.patch(self.api_link, {
  169. 'ids': self.ids,
  170. 'ops': [
  171. {
  172. 'op': 'add',
  173. 'path': 'acl',
  174. 'value': True,
  175. },
  176. ]
  177. })
  178. self.assertEqual(response.status_code, 200)
  179. response_json = response.json()
  180. for i, post in enumerate(self.posts):
  181. self.assertEqual(response_json[i]['id'], post.id)
  182. self.assertTrue(response_json[i]['acl'])
  183. class BulkPostProtectApiTests(ThreadPostBulkPatchApiTestCase):
  184. def test_protect_post(self):
  185. """api makes it possible to protect posts"""
  186. self.override_acl({
  187. 'can_protect_posts': 1,
  188. 'can_edit_posts': 2,
  189. })
  190. response = self.patch(
  191. self.api_link, {
  192. 'ids': self.ids,
  193. 'ops': [
  194. {
  195. 'op': 'replace',
  196. 'path': 'is-protected',
  197. 'value': True,
  198. },
  199. ]
  200. }
  201. )
  202. self.assertEqual(response.status_code, 200)
  203. response_json = response.json()
  204. for i, post in enumerate(self.posts):
  205. self.assertEqual(response_json[i]['id'], post.id)
  206. self.assertTrue(response_json[i]['is_protected'])
  207. for post in Post.objects.filter(id__in=self.ids):
  208. self.assertTrue(post.is_protected)
  209. def test_protect_post_no_permission(self):
  210. """api validates permission to protect posts and returns errors"""
  211. self.override_acl({'can_protect_posts': 0})
  212. response = self.patch(
  213. self.api_link, {
  214. 'ids': self.ids,
  215. 'ops': [
  216. {
  217. 'op': 'replace',
  218. 'path': 'is-protected',
  219. 'value': True,
  220. },
  221. ]
  222. }
  223. )
  224. self.assertEqual(response.status_code, 400)
  225. response_json = response.json()
  226. for i, post in enumerate(self.posts):
  227. self.assertEqual(response_json[i]['id'], post.id)
  228. self.assertEqual(
  229. response_json[i]['detail'],
  230. ["You can't protect posts in this category."],
  231. )
  232. for post in Post.objects.filter(id__in=self.ids):
  233. self.assertFalse(post.is_protected)
  234. class BulkPostsApproveApiTests(ThreadPostBulkPatchApiTestCase):
  235. def test_approve_post(self):
  236. """api resyncs thread and categories on posts approval"""
  237. for post in self.posts:
  238. post.is_unapproved = True
  239. post.save()
  240. self.thread.synchronize()
  241. self.thread.save()
  242. self.assertNotIn(self.thread.last_post_id, self.ids)
  243. self.override_acl({'can_approve_content': 1})
  244. response = self.patch(
  245. self.api_link, {
  246. 'ids': self.ids,
  247. 'ops': [
  248. {
  249. 'op': 'replace',
  250. 'path': 'is-unapproved',
  251. 'value': False,
  252. },
  253. ]
  254. }
  255. )
  256. self.assertEqual(response.status_code, 200)
  257. response_json = response.json()
  258. for i, post in enumerate(self.posts):
  259. self.assertEqual(response_json[i]['id'], post.id)
  260. self.assertFalse(response_json[i]['is_unapproved'])
  261. for post in Post.objects.filter(id__in=self.ids):
  262. self.assertFalse(post.is_unapproved)
  263. thread = Thread.objects.get(pk=self.thread.pk)
  264. self.assertIn(thread.last_post_id, self.ids)
  265. category = Category.objects.get(pk=self.category.pk)
  266. self.assertEqual(category.posts, 4)