test_threads_bulkpatch_api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import json
  2. from django.urls import reverse
  3. from misago.acl.testutils import override_acl
  4. from misago.categories.models import Category
  5. from misago.threads import testutils
  6. from misago.threads.models import Thread
  7. from .test_threads_api import ThreadsApiTestCase
  8. class ThreadsBulkPatchApiTestCase(ThreadsApiTestCase):
  9. def setUp(self):
  10. super(ThreadsBulkPatchApiTestCase, self).setUp()
  11. self.threads = list(reversed([
  12. testutils.post_thread(category=self.category),
  13. testutils.post_thread(category=self.category),
  14. testutils.post_thread(category=self.category),
  15. ]))
  16. self.ids = list(reversed([t.id for t in self.threads]))
  17. self.api_link = reverse('misago:api:thread-list')
  18. def patch(self, api_link, ops):
  19. return self.client.patch(api_link, json.dumps(ops), content_type="application/json")
  20. class BulkPatchSerializerTests(ThreadsBulkPatchApiTestCase):
  21. def test_invalid_input_type(self):
  22. """api rejects invalid input type"""
  23. response = self.patch(self.api_link, [1, 2, 3])
  24. self.assertEqual(response.status_code, 400)
  25. self.assertEqual(response.json(), {
  26. 'non_field_errors': [
  27. "Invalid data. Expected a dictionary, but got list.",
  28. ],
  29. })
  30. def test_missing_input_keys(self):
  31. """api rejects input with missing keys"""
  32. response = self.patch(self.api_link, {})
  33. self.assertEqual(response.status_code, 400)
  34. self.assertEqual(response.json(), {
  35. 'ids': [
  36. "This field is required.",
  37. ],
  38. 'ops': [
  39. "This field is required.",
  40. ],
  41. })
  42. def test_empty_input_keys(self):
  43. """api rejects input with empty keys"""
  44. response = self.patch(self.api_link, {
  45. 'ids': [],
  46. 'ops': [],
  47. })
  48. self.assertEqual(response.status_code, 400)
  49. self.assertEqual(response.json(), {
  50. 'ids': [
  51. "Ensure this field has at least 1 elements.",
  52. ],
  53. 'ops': [
  54. "Ensure this field has at least 1 elements.",
  55. ],
  56. })
  57. def test_invalid_input_keys(self):
  58. """api rejects input with invalid keys"""
  59. response = self.patch(self.api_link, {
  60. 'ids': ['a'],
  61. 'ops': [1],
  62. })
  63. self.assertEqual(response.status_code, 400)
  64. self.assertEqual(response.json(), {
  65. 'ids': [
  66. "A valid integer is required.",
  67. ],
  68. 'ops': [
  69. 'Expected a dictionary of items but got type "int".',
  70. ],
  71. })
  72. def test_too_small_id(self):
  73. """api rejects input with implausiple id"""
  74. response = self.patch(self.api_link, {
  75. 'ids': [0],
  76. 'ops': [{}],
  77. })
  78. self.assertEqual(response.status_code, 400)
  79. self.assertEqual(response.json(), {
  80. 'ids': [
  81. "Ensure this value is greater than or equal to 1.",
  82. ],
  83. })
  84. def test_too_large_input(self):
  85. """api rejects too large input"""
  86. response = self.patch(self.api_link, {
  87. 'ids': [i + 1 for i in range(200)],
  88. 'ops': [{} for i in range(200)],
  89. })
  90. self.assertEqual(response.status_code, 400)
  91. self.assertEqual(response.json(), {
  92. 'ids': [
  93. "Ensure this field has no more than 40 elements.",
  94. ],
  95. 'ops': [
  96. "Ensure this field has no more than 10 elements.",
  97. ],
  98. })
  99. def test_threads_not_found(self):
  100. """api fails to find threads"""
  101. threads = [
  102. testutils.post_thread(category=self.category, is_hidden=True),
  103. testutils.post_thread(category=self.category, is_unapproved=True),
  104. ]
  105. response = self.patch(self.api_link, {
  106. 'ids': [t.id for t in threads],
  107. 'ops': [{}],
  108. })
  109. self.assertEqual(response.status_code, 404)
  110. self.assertEqual(response.json(), {
  111. 'detail': "NOT FOUND",
  112. })
  113. def test_ops_invalid(self):
  114. """api validates descriptions"""
  115. response = self.patch(self.api_link, {
  116. 'ids': self.ids,
  117. 'ops': [{}],
  118. })
  119. self.assertEqual(response.status_code, 400)
  120. self.assertEqual(response.json(), {'detail': '"op" parameter must be defined.'})
  121. def test_anonymous_user(self):
  122. """anonymous users can't use bulk actions"""
  123. self.logout_user()
  124. response = self.patch(self.api_link, {
  125. 'ids': self.ids[:1],
  126. 'ops': [{}],
  127. })
  128. self.assertEqual(response.status_code, 403)
  129. class ThreadAddAclApiTests(ThreadsBulkPatchApiTestCase):
  130. def test_add_acl_true(self):
  131. """api adds current threads acl to response"""
  132. response = self.patch(self.api_link,
  133. {
  134. 'ids': self.ids,
  135. 'ops': [
  136. {
  137. 'op': 'add',
  138. 'path': 'acl',
  139. 'value': True,
  140. },
  141. ]
  142. })
  143. self.assertEqual(response.status_code, 200)
  144. response_json = response.json()
  145. for i, thread in enumerate(self.threads):
  146. self.assertEqual(response_json[i]['id'], thread.id)
  147. self.assertTrue(response_json[i]['acl'])
  148. class ThreadsBulkChangeTitleApiTests(ThreadsBulkPatchApiTestCase):
  149. def test_change_thread_title(self):
  150. """api changes thread title and resyncs the category"""
  151. self.override_acl({'can_edit_threads': 2})
  152. response = self.patch(
  153. self.api_link,
  154. {
  155. 'ids': self.ids,
  156. 'ops': [
  157. {
  158. 'op': 'replace',
  159. 'path': 'title',
  160. 'value': 'Changed the title!',
  161. },
  162. ]
  163. }
  164. )
  165. self.assertEqual(response.status_code, 200)
  166. response_json = response.json()
  167. for i, thread in enumerate(self.threads):
  168. self.assertEqual(response_json[i]['id'], thread.id)
  169. self.assertEqual(response_json[i]['title'], 'Changed the title!')
  170. for thread in Thread.objects.filter(id__in=self.ids):
  171. self.assertEqual(thread.title, 'Changed the title!')
  172. category = Category.objects.get(pk=self.category.pk)
  173. self.assertEqual(category.last_thread_title, 'Changed the title!')
  174. def test_change_thread_title_no_permission(self):
  175. """api validates permission to change title, returns errors"""
  176. self.override_acl({'can_edit_threads': 0})
  177. response = self.patch(
  178. self.api_link,
  179. {
  180. 'ids': self.ids,
  181. 'ops': [
  182. {
  183. 'op': 'replace',
  184. 'path': 'title',
  185. 'value': 'Changed the title!',
  186. },
  187. ]
  188. }
  189. )
  190. self.assertEqual(response.status_code, 200)
  191. self.assertEqual(response.json(), [
  192. {
  193. 'id': thread_id,
  194. 'status': 403,
  195. 'detail': "You can't edit threads in this category.",
  196. } for thread_id in sorted(self.ids, reverse=True)
  197. ])
  198. class ThreadsBulkMoveApiTests(ThreadsBulkPatchApiTestCase):
  199. def setUp(self):
  200. super(ThreadsBulkMoveApiTests, self).setUp()
  201. Category(
  202. name='Category B',
  203. slug='category-b',
  204. ).insert_at(
  205. self.category,
  206. position='last-child',
  207. save=True,
  208. )
  209. self.category_b = Category.objects.get(slug='category-b')
  210. def override_other_acl(self, acl):
  211. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  212. other_category_acl.update({
  213. 'can_see': 1,
  214. 'can_browse': 1,
  215. 'can_see_all_threads': 1,
  216. 'can_see_own_threads': 0,
  217. 'can_hide_threads': 0,
  218. 'can_approve_content': 0,
  219. })
  220. other_category_acl.update(acl)
  221. categories_acl = self.user.acl_cache['categories']
  222. categories_acl[self.category_b.pk] = other_category_acl
  223. visible_categories = [self.category.pk]
  224. if other_category_acl['can_see']:
  225. visible_categories.append(self.category_b.pk)
  226. override_acl(
  227. self.user, {
  228. 'visible_categories': visible_categories,
  229. 'categories': categories_acl,
  230. }
  231. )
  232. def test_move_thread(self):
  233. """api moves threads to other category and syncs both categories"""
  234. self.override_acl({'can_move_threads': True})
  235. self.override_other_acl({'can_start_threads': 2})
  236. response = self.patch(
  237. self.api_link,
  238. {
  239. 'ids': self.ids,
  240. 'ops': [
  241. {
  242. 'op': 'replace',
  243. 'path': 'category',
  244. 'value': self.category_b.pk,
  245. },
  246. {
  247. 'op': 'replace',
  248. 'path': 'flatten-categories',
  249. 'value': None,
  250. },
  251. ]
  252. }
  253. )
  254. self.assertEqual(response.status_code, 200)
  255. response_json = response.json()
  256. for i, thread in enumerate(self.threads):
  257. self.assertEqual(response_json[i]['id'], thread.id)
  258. self.assertEqual(response_json[i]['category'], self.category_b.pk)
  259. for thread in Thread.objects.filter(id__in=self.ids):
  260. self.assertEqual(thread.category_id, self.category_b.pk)
  261. category = Category.objects.get(pk=self.category.pk)
  262. self.assertEqual(category.threads, self.category.threads - 3)
  263. new_category = Category.objects.get(pk=self.category_b.pk)
  264. self.assertEqual(new_category.threads, 3)
  265. class ThreadsBulksHideApiTests(ThreadsBulkPatchApiTestCase):
  266. def test_hide_thread(self):
  267. """api makes it possible to hide thread"""
  268. self.override_acl({'can_hide_threads': 1})
  269. response = self.patch(
  270. self.api_link,
  271. {
  272. 'ids': self.ids,
  273. 'ops': [
  274. {
  275. 'op': 'replace',
  276. 'path': 'is-hidden',
  277. 'value': True,
  278. },
  279. ]
  280. }
  281. )
  282. self.assertEqual(response.status_code, 200)
  283. response_json = response.json()
  284. for i, thread in enumerate(self.threads):
  285. self.assertEqual(response_json[i]['id'], thread.id)
  286. self.assertTrue(response_json[i]['is_hidden'])
  287. for thread in Thread.objects.filter(id__in=self.ids):
  288. self.assertTrue(thread.is_hidden)
  289. category = Category.objects.get(pk=self.category.pk)
  290. self.assertNotIn(category.last_thread_id, self.ids)
  291. class ThreadsBulksApproveApiTests(ThreadsBulkPatchApiTestCase):
  292. def test_approve_thread(self):
  293. """api approvse threads and syncs category"""
  294. for thread in self.threads:
  295. thread.first_post.is_unapproved = True
  296. thread.first_post.save()
  297. thread.synchronize()
  298. thread.save()
  299. self.assertTrue(thread.is_unapproved)
  300. self.assertTrue(thread.has_unapproved_posts)
  301. self.category.synchronize()
  302. self.category.save()
  303. self.override_acl({'can_approve_content': 1})
  304. response = self.patch(
  305. self.api_link,
  306. {
  307. 'ids': self.ids,
  308. 'ops': [
  309. {
  310. 'op': 'replace',
  311. 'path': 'is-unapproved',
  312. 'value': False,
  313. },
  314. ]
  315. }
  316. )
  317. self.assertEqual(response.status_code, 200)
  318. response_json = response.json()
  319. for i, thread in enumerate(self.threads):
  320. self.assertEqual(response_json[i]['id'], thread.id)
  321. self.assertFalse(response_json[i]['is_unapproved'])
  322. self.assertFalse(response_json[i]['has_unapproved_posts'])
  323. for thread in Thread.objects.filter(id__in=self.ids):
  324. self.assertFalse(thread.is_unapproved)
  325. self.assertFalse(thread.has_unapproved_posts)
  326. category = Category.objects.get(pk=self.category.pk)
  327. self.assertIn(category.last_thread_id, self.ids)