test_thread_postbulkpatch_api.py 9.8 KB

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