test_thread_bulkpatch_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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, 403)
  110. self.assertEqual(response.json(), {
  111. 'detail': "One or more threads to update could not be found.",
  112. })
  113. def test_ops_invalid(self):
  114. """api validates descriptions"""
  115. response = self.patch(self.api_link, {
  116. 'ids': self.ids[:1],
  117. 'ops': [{}],
  118. })
  119. self.assertEqual(response.status_code, 400)
  120. self.assertEqual(response.json(), [
  121. {'id': self.ids[0], 'detail': ['undefined op']},
  122. ])
  123. def test_anonymous_user(self):
  124. """anonymous users can't use bulk actions"""
  125. self.logout_user()
  126. response = self.patch(self.api_link, {
  127. 'ids': self.ids[:1],
  128. 'ops': [{}],
  129. })
  130. self.assertEqual(response.status_code, 403)
  131. class ThreadAddAclApiTests(ThreadsBulkPatchApiTestCase):
  132. def test_add_acl_true(self):
  133. """api adds current threads acl to response"""
  134. response = self.patch(self.api_link,
  135. {
  136. 'ids': self.ids,
  137. 'ops': [
  138. {
  139. 'op': 'add',
  140. 'path': 'acl',
  141. 'value': True,
  142. },
  143. ]
  144. })
  145. self.assertEqual(response.status_code, 200)
  146. response_json = response.json()
  147. for i, thread in enumerate(self.threads):
  148. self.assertEqual(response_json[i]['id'], thread.id)
  149. self.assertTrue(response_json[i]['acl'])
  150. class BulkThreadChangeTitleApiTests(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. response_json = response.json()
  169. for i, thread in enumerate(self.threads):
  170. self.assertEqual(response_json[i]['id'], thread.id)
  171. self.assertEqual(response_json[i]['title'], 'Changed the title!')
  172. for thread in Thread.objects.filter(id__in=self.ids):
  173. self.assertEqual(thread.title, 'Changed the title!')
  174. category = Category.objects.get(pk=self.category.pk)
  175. self.assertEqual(category.last_thread_title, 'Changed the title!')
  176. def test_change_thread_title_no_permission(self):
  177. """api validates permission to change title, returns errors"""
  178. self.override_acl({'can_edit_threads': 0})
  179. response = self.patch(
  180. self.api_link,
  181. {
  182. 'ids': self.ids,
  183. 'ops': [
  184. {
  185. 'op': 'replace',
  186. 'path': 'title',
  187. 'value': 'Changed the title!',
  188. },
  189. ]
  190. }
  191. )
  192. self.assertEqual(response.status_code, 400)
  193. response_json = response.json()
  194. for i, thread in enumerate(self.threads):
  195. self.assertEqual(response_json[i]['id'], thread.id)
  196. self.assertEqual(
  197. response_json[i]['detail'],
  198. ["You can't edit threads in this category."],
  199. )
  200. class BulkThreadMoveApiTests(ThreadsBulkPatchApiTestCase):
  201. def setUp(self):
  202. super(BulkThreadMoveApiTests, self).setUp()
  203. Category(
  204. name='Category B',
  205. slug='category-b',
  206. ).insert_at(
  207. self.category,
  208. position='last-child',
  209. save=True,
  210. )
  211. self.category_b = Category.objects.get(slug='category-b')
  212. def override_other_acl(self, acl):
  213. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  214. other_category_acl.update({
  215. 'can_see': 1,
  216. 'can_browse': 1,
  217. 'can_see_all_threads': 1,
  218. 'can_see_own_threads': 0,
  219. 'can_hide_threads': 0,
  220. 'can_approve_content': 0,
  221. })
  222. other_category_acl.update(acl)
  223. categories_acl = self.user.acl_cache['categories']
  224. categories_acl[self.category_b.pk] = other_category_acl
  225. visible_categories = [self.category.pk]
  226. if other_category_acl['can_see']:
  227. visible_categories.append(self.category_b.pk)
  228. override_acl(
  229. self.user, {
  230. 'visible_categories': visible_categories,
  231. 'categories': categories_acl,
  232. }
  233. )
  234. def test_move_thread(self):
  235. """api moves threads to other category and syncs both categories"""
  236. self.override_acl({'can_move_threads': True})
  237. self.override_other_acl({'can_start_threads': 2})
  238. response = self.patch(
  239. self.api_link,
  240. {
  241. 'ids': self.ids,
  242. 'ops': [
  243. {
  244. 'op': 'replace',
  245. 'path': 'category',
  246. 'value': self.category_b.pk,
  247. },
  248. {
  249. 'op': 'replace',
  250. 'path': 'flatten-categories',
  251. 'value': None,
  252. },
  253. ]
  254. }
  255. )
  256. self.assertEqual(response.status_code, 200)
  257. response_json = response.json()
  258. for i, thread in enumerate(self.threads):
  259. self.assertEqual(response_json[i]['id'], thread.id)
  260. self.assertEqual(response_json[i]['category'], self.category_b.pk)
  261. for thread in Thread.objects.filter(id__in=self.ids):
  262. self.assertEqual(thread.category_id, self.category_b.pk)
  263. category = Category.objects.get(pk=self.category.pk)
  264. self.assertEqual(category.threads, self.category.threads - 3)
  265. new_category = Category.objects.get(pk=self.category_b.pk)
  266. self.assertEqual(new_category.threads, 3)
  267. class BulkThreadsHideApiTests(ThreadsBulkPatchApiTestCase):
  268. def test_hide_thread(self):
  269. """api makes it possible to hide thread"""
  270. self.override_acl({'can_hide_threads': 1})
  271. response = self.patch(
  272. self.api_link,
  273. {
  274. 'ids': self.ids,
  275. 'ops': [
  276. {
  277. 'op': 'replace',
  278. 'path': 'is-hidden',
  279. 'value': True,
  280. },
  281. ]
  282. }
  283. )
  284. self.assertEqual(response.status_code, 200)
  285. response_json = response.json()
  286. for i, thread in enumerate(self.threads):
  287. self.assertEqual(response_json[i]['id'], thread.id)
  288. self.assertTrue(response_json[i]['is_hidden'])
  289. for thread in Thread.objects.filter(id__in=self.ids):
  290. self.assertTrue(thread.is_hidden)
  291. category = Category.objects.get(pk=self.category.pk)
  292. self.assertNotIn(category.last_thread_id, self.ids)
  293. class BulkThreadsApproveApiTests(ThreadsBulkPatchApiTestCase):
  294. def test_approve_thread(self):
  295. """api approvse threads and syncs category"""
  296. for thread in self.threads:
  297. thread.first_post.is_unapproved = True
  298. thread.first_post.save()
  299. thread.synchronize()
  300. thread.save()
  301. self.assertTrue(thread.is_unapproved)
  302. self.assertTrue(thread.has_unapproved_posts)
  303. self.category.synchronize()
  304. self.category.save()
  305. self.override_acl({'can_approve_content': 1})
  306. response = self.patch(
  307. self.api_link,
  308. {
  309. 'ids': self.ids,
  310. 'ops': [
  311. {
  312. 'op': 'replace',
  313. 'path': 'is-unapproved',
  314. 'value': False,
  315. },
  316. ]
  317. }
  318. )
  319. self.assertEqual(response.status_code, 200)
  320. response_json = response.json()
  321. for i, thread in enumerate(self.threads):
  322. self.assertEqual(response_json[i]['id'], thread.id)
  323. self.assertFalse(response_json[i]['is_unapproved'])
  324. self.assertFalse(response_json[i]['has_unapproved_posts'])
  325. for thread in Thread.objects.filter(id__in=self.ids):
  326. self.assertFalse(thread.is_unapproved)
  327. self.assertFalse(thread.has_unapproved_posts)
  328. category = Category.objects.get(pk=self.category.pk)
  329. self.assertIn(category.last_thread_id, self.ids)