test_threads_bulkpatch_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 = [
  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 = [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_id in enumerate(self.ids):
  146. data = response_json[i]
  147. self.assertEqual(data['id'], str(thread_id))
  148. self.assertEqual(data['status'], '200')
  149. self.assertTrue(data['patch']['acl'])
  150. class ThreadsBulkChangeTitleApiTests(ThreadsBulkPatchApiTestCase):
  151. def test_change_thread_title(self):
  152. """api changes thread title and resyncs the category"""
  153. self.override_acl({'can_edit_threads': 2})
  154. response = self.patch(
  155. self.api_link,
  156. {
  157. 'ids': self.ids,
  158. 'ops': [
  159. {
  160. 'op': 'replace',
  161. 'path': 'title',
  162. 'value': 'Changed the title!',
  163. },
  164. ]
  165. }
  166. )
  167. self.assertEqual(response.status_code, 200)
  168. self.assertEqual(response.json(), [
  169. {
  170. 'id': str(thread_id),
  171. 'status': '200',
  172. 'patch': {
  173. 'title': 'Changed the title!',
  174. }
  175. } for thread_id in self.ids
  176. ])
  177. for thread in Thread.objects.filter(id__in=self.ids):
  178. self.assertEqual(thread.title, 'Changed the title!')
  179. category = Category.objects.get(pk=self.category.pk)
  180. self.assertEqual(category.last_thread_title, 'Changed the title!')
  181. def test_change_thread_title_no_permission(self):
  182. """api validates permission to change title, returns errors"""
  183. self.override_acl({'can_edit_threads': 0})
  184. response = self.patch(
  185. self.api_link,
  186. {
  187. 'ids': self.ids,
  188. 'ops': [
  189. {
  190. 'op': 'replace',
  191. 'path': 'title',
  192. 'value': 'Changed the title!',
  193. },
  194. ]
  195. }
  196. )
  197. self.assertEqual(response.status_code, 200)
  198. self.assertEqual(response.json(), [
  199. {
  200. 'id': str(thread_id),
  201. 'status': '403',
  202. 'detail': "You can't edit threads in this category.",
  203. } for thread_id in self.ids
  204. ])
  205. class ThreadsBulkMoveApiTests(ThreadsBulkPatchApiTestCase):
  206. def setUp(self):
  207. super(ThreadsBulkMoveApiTests, self).setUp()
  208. Category(
  209. name='Category B',
  210. slug='category-b',
  211. ).insert_at(
  212. self.category,
  213. position='last-child',
  214. save=True,
  215. )
  216. self.category_b = Category.objects.get(slug='category-b')
  217. def override_other_acl(self, acl):
  218. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  219. other_category_acl.update({
  220. 'can_see': 1,
  221. 'can_browse': 1,
  222. 'can_see_all_threads': 1,
  223. 'can_see_own_threads': 0,
  224. 'can_hide_threads': 0,
  225. 'can_approve_content': 0,
  226. })
  227. other_category_acl.update(acl)
  228. categories_acl = self.user.acl_cache['categories']
  229. categories_acl[self.category_b.pk] = other_category_acl
  230. visible_categories = [self.category.pk]
  231. if other_category_acl['can_see']:
  232. visible_categories.append(self.category_b.pk)
  233. override_acl(
  234. self.user, {
  235. 'visible_categories': visible_categories,
  236. 'categories': categories_acl,
  237. }
  238. )
  239. def test_move_thread(self):
  240. """api moves threads to other category and syncs both categories"""
  241. self.override_acl({'can_move_threads': True})
  242. self.override_other_acl({'can_start_threads': 2})
  243. response = self.patch(
  244. self.api_link,
  245. {
  246. 'ids': self.ids,
  247. 'ops': [
  248. {
  249. 'op': 'replace',
  250. 'path': 'category',
  251. 'value': self.category_b.pk,
  252. },
  253. {
  254. 'op': 'replace',
  255. 'path': 'flatten-categories',
  256. 'value': None,
  257. },
  258. ]
  259. }
  260. )
  261. self.assertEqual(response.status_code, 200)
  262. self.assertEqual(response.json(), [
  263. {
  264. 'id': str(thread_id),
  265. 'status': '200',
  266. 'patch': {
  267. 'category': self.category_b.pk,
  268. },
  269. } for thread_id in self.ids
  270. ])
  271. for thread in Thread.objects.filter(id__in=self.ids):
  272. self.assertEqual(thread.category_id, self.category_b.pk)
  273. category = Category.objects.get(pk=self.category.pk)
  274. self.assertEqual(category.threads, self.category.threads - 3)
  275. new_category = Category.objects.get(pk=self.category_b.pk)
  276. self.assertEqual(new_category.threads, 3)
  277. class ThreadsBulksHideApiTests(ThreadsBulkPatchApiTestCase):
  278. def test_hide_thread(self):
  279. """api makes it possible to hide thread"""
  280. self.override_acl({'can_hide_threads': 1})
  281. response = self.patch(
  282. self.api_link,
  283. {
  284. 'ids': self.ids,
  285. 'ops': [
  286. {
  287. 'op': 'replace',
  288. 'path': 'is-hidden',
  289. 'value': True,
  290. },
  291. ]
  292. }
  293. )
  294. self.assertEqual(response.status_code, 200)
  295. self.assertEqual(response.json(), [
  296. {
  297. 'id': str(thread_id),
  298. 'status': '200',
  299. 'patch': {
  300. 'is_hidden': True,
  301. },
  302. } for thread_id in self.ids
  303. ])
  304. for thread in Thread.objects.filter(id__in=self.ids):
  305. self.assertTrue(thread.is_hidden)
  306. category = Category.objects.get(pk=self.category.pk)
  307. self.assertNotIn(category.last_thread_id, self.ids)
  308. class ThreadsBulksApproveApiTests(ThreadsBulkPatchApiTestCase):
  309. def test_approve_thread(self):
  310. """api approvse threads and syncs category"""
  311. for thread in self.threads:
  312. thread.first_post.is_unapproved = True
  313. thread.first_post.save()
  314. thread.synchronize()
  315. thread.save()
  316. self.assertTrue(thread.is_unapproved)
  317. self.assertTrue(thread.has_unapproved_posts)
  318. self.category.synchronize()
  319. self.category.save()
  320. self.override_acl({'can_approve_content': 1})
  321. response = self.patch(
  322. self.api_link,
  323. {
  324. 'ids': self.ids,
  325. 'ops': [
  326. {
  327. 'op': 'replace',
  328. 'path': 'is-unapproved',
  329. 'value': False,
  330. },
  331. ]
  332. }
  333. )
  334. self.assertEqual(response.status_code, 200)
  335. self.assertEqual(response.json(), [
  336. {
  337. 'id': str(thread_id),
  338. 'status': '200',
  339. 'patch': {
  340. 'is_unapproved': False,
  341. 'has_unapproved_posts': False,
  342. },
  343. } for thread_id in self.ids
  344. ])
  345. for thread in Thread.objects.filter(id__in=self.ids):
  346. self.assertFalse(thread.is_unapproved)
  347. self.assertFalse(thread.has_unapproved_posts)
  348. category = Category.objects.get(pk=self.category.pk)
  349. self.assertIn(category.last_thread_id, self.ids)