test_thread_postbulkpatch_api.py 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  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. 'non_field_errors': ['"op" parameter must be defined.'],
  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_id in enumerate(self.ids):
  183. data = response_json[i]
  184. self.assertEqual(data['id'], str(post_id))
  185. self.assertEqual(data['status'], '200')
  186. self.assertTrue(data['patch']['acl'])
  187. class BulkPostProtectApiTests(ThreadPostBulkPatchApiTestCase):
  188. def test_protect_post(self):
  189. """api makes it possible to protect posts"""
  190. self.override_acl({
  191. 'can_protect_posts': 1,
  192. 'can_edit_posts': 2,
  193. })
  194. response = self.patch(
  195. self.api_link, {
  196. 'ids': self.ids,
  197. 'ops': [
  198. {
  199. 'op': 'replace',
  200. 'path': 'is-protected',
  201. 'value': True,
  202. },
  203. ]
  204. }
  205. )
  206. self.assertEqual(response.status_code, 200)
  207. self.assertEqual(response.json(), [
  208. {
  209. 'id': str(post_id),
  210. 'status': '200',
  211. 'patch': {
  212. 'is_protected': True,
  213. },
  214. } for post_id in self.ids
  215. ])
  216. for post in Post.objects.filter(id__in=self.ids):
  217. self.assertTrue(post.is_protected)
  218. def test_protect_post_no_permission(self):
  219. """api validates permission to protect posts and returns errors"""
  220. self.override_acl({'can_protect_posts': 0})
  221. response = self.patch(
  222. self.api_link, {
  223. 'ids': self.ids,
  224. 'ops': [
  225. {
  226. 'op': 'replace',
  227. 'path': 'is-protected',
  228. 'value': True,
  229. },
  230. ]
  231. }
  232. )
  233. self.assertEqual(response.status_code, 200)
  234. self.assertEqual(response.json(), [
  235. {
  236. 'id': str(post_id),
  237. 'status': '403',
  238. 'detail': "You can't protect posts in this category.",
  239. } for post_id in self.ids
  240. ])
  241. for post in Post.objects.filter(id__in=self.ids):
  242. self.assertFalse(post.is_protected)
  243. class BulkPostsApproveApiTests(ThreadPostBulkPatchApiTestCase):
  244. def test_approve_post(self):
  245. """api resyncs thread and categories on posts approval"""
  246. for post in self.posts:
  247. post.is_unapproved = True
  248. post.save()
  249. self.thread.synchronize()
  250. self.thread.save()
  251. self.assertNotIn(self.thread.last_post_id, self.ids)
  252. self.override_acl({'can_approve_content': 1})
  253. response = self.patch(
  254. self.api_link, {
  255. 'ids': self.ids,
  256. 'ops': [
  257. {
  258. 'op': 'replace',
  259. 'path': 'is-unapproved',
  260. 'value': False,
  261. },
  262. ]
  263. }
  264. )
  265. self.assertEqual(response.status_code, 200)
  266. self.assertEqual(response.json(), [
  267. {
  268. 'id': str(post_id),
  269. 'status': '200',
  270. 'patch': {
  271. 'is_unapproved': False,
  272. },
  273. } for post_id in self.ids
  274. ])
  275. for post in Post.objects.filter(id__in=self.ids):
  276. self.assertFalse(post.is_unapproved)
  277. thread = Thread.objects.get(pk=self.thread.pk)
  278. self.assertIn(thread.last_post_id, self.ids)
  279. category = Category.objects.get(pk=self.category.pk)
  280. self.assertEqual(category.posts, 4)