test_thread_postbulkpatch_api.py 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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(), {'detail': '"op" parameter must be defined.'})
  144. def test_anonymous_user(self):
  145. """anonymous users can't use bulk actions"""
  146. self.logout_user()
  147. response = self.patch(self.api_link, {
  148. 'ids': self.ids[:1],
  149. 'ops': [{}],
  150. })
  151. self.assertEqual(response.status_code, 403)
  152. def test_events(self):
  153. """cant use bulk actions for events"""
  154. for post in self.posts:
  155. post.is_event = True
  156. post.save()
  157. response = self.patch(self.api_link, {
  158. 'ids': self.ids,
  159. 'ops': [{}],
  160. })
  161. self.assertEqual(response.status_code, 403)
  162. self.assertEqual(response.json(), {
  163. 'detail': "One or more posts to update could not be found.",
  164. })
  165. class PostsAddAclApiTests(ThreadPostBulkPatchApiTestCase):
  166. def test_add_acl_true(self):
  167. """api adds posts acls to response"""
  168. response = self.patch(self.api_link, {
  169. 'ids': self.ids,
  170. 'ops': [
  171. {
  172. 'op': 'add',
  173. 'path': 'acl',
  174. 'value': True,
  175. },
  176. ]
  177. })
  178. self.assertEqual(response.status_code, 200)
  179. response_json = response.json()
  180. for i, post_id in enumerate(self.ids):
  181. data = response_json[i]
  182. self.assertEqual(data['id'], str(post_id))
  183. self.assertEqual(data['status'], '200')
  184. self.assertTrue(data['patch']['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. self.assertEqual(response.json(), [
  206. {
  207. 'id': str(post_id),
  208. 'status': '200',
  209. 'patch': {
  210. 'is_protected': True,
  211. },
  212. } for post_id in self.ids
  213. ])
  214. for post in Post.objects.filter(id__in=self.ids):
  215. self.assertTrue(post.is_protected)
  216. def test_protect_post_no_permission(self):
  217. """api validates permission to protect posts and returns errors"""
  218. self.override_acl({'can_protect_posts': 0})
  219. response = self.patch(
  220. self.api_link, {
  221. 'ids': self.ids,
  222. 'ops': [
  223. {
  224. 'op': 'replace',
  225. 'path': 'is-protected',
  226. 'value': True,
  227. },
  228. ]
  229. }
  230. )
  231. self.assertEqual(response.status_code, 200)
  232. self.assertEqual(response.json(), [
  233. {
  234. 'id': str(post_id),
  235. 'status': '403',
  236. 'detail': "You can't protect posts in this category.",
  237. } for post_id in self.ids
  238. ])
  239. for post in Post.objects.filter(id__in=self.ids):
  240. self.assertFalse(post.is_protected)
  241. class BulkPostsApproveApiTests(ThreadPostBulkPatchApiTestCase):
  242. def test_approve_post(self):
  243. """api resyncs thread and categories on posts approval"""
  244. for post in self.posts:
  245. post.is_unapproved = True
  246. post.save()
  247. self.thread.synchronize()
  248. self.thread.save()
  249. self.assertNotIn(self.thread.last_post_id, self.ids)
  250. self.override_acl({'can_approve_content': 1})
  251. response = self.patch(
  252. self.api_link, {
  253. 'ids': self.ids,
  254. 'ops': [
  255. {
  256. 'op': 'replace',
  257. 'path': 'is-unapproved',
  258. 'value': False,
  259. },
  260. ]
  261. }
  262. )
  263. self.assertEqual(response.status_code, 200)
  264. self.assertEqual(response.json(), [
  265. {
  266. 'id': str(post_id),
  267. 'status': '200',
  268. 'patch': {
  269. 'is_unapproved': False,
  270. },
  271. } for post_id in self.ids
  272. ])
  273. for post in Post.objects.filter(id__in=self.ids):
  274. self.assertFalse(post.is_unapproved)
  275. thread = Thread.objects.get(pk=self.thread.pk)
  276. self.assertIn(thread.last_post_id, self.ids)
  277. category = Category.objects.get(pk=self.category.pk)
  278. self.assertEqual(category.posts, 4)