test_threads_bulkpatch_api.py 13 KB

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