test_thread_postbulkpatch_api.py 9.8 KB

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