test_thread_postmove_api.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import json
  4. from django.urls import reverse
  5. from misago.acl.testutils import override_acl
  6. from misago.categories.models import Category
  7. from misago.threads import testutils
  8. from misago.threads.models import Thread
  9. from misago.threads.serializers.moderation import POSTS_LIMIT
  10. from misago.users.testutils import AuthenticatedUserTestCase
  11. class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
  12. def setUp(self):
  13. super(ThreadPostMoveApiTestCase, self).setUp()
  14. self.category = Category.objects.get(slug='first-category')
  15. self.thread = testutils.post_thread(category=self.category)
  16. self.api_link = reverse(
  17. 'misago:api:thread-post-move', kwargs={
  18. 'thread_pk': self.thread.pk,
  19. }
  20. )
  21. Category(
  22. name='Category B',
  23. slug='category-b',
  24. ).insert_at(
  25. self.category,
  26. position='last-child',
  27. save=True,
  28. )
  29. self.category_b = Category.objects.get(slug='category-b')
  30. self.override_acl()
  31. self.override_other_acl()
  32. def refresh_thread(self):
  33. self.thread = Thread.objects.get(pk=self.thread.pk)
  34. def override_acl(self, extra_acl=None):
  35. new_acl = self.user.acl_cache
  36. new_acl['categories'][self.category.pk].update({
  37. 'can_see': 1,
  38. 'can_browse': 1,
  39. 'can_start_threads': 1,
  40. 'can_reply_threads': 1,
  41. 'can_edit_posts': 1,
  42. 'can_approve_content': 0,
  43. 'can_move_posts': 1,
  44. })
  45. if extra_acl:
  46. new_acl['categories'][self.category.pk].update(extra_acl)
  47. override_acl(self.user, new_acl)
  48. def override_other_acl(self, extra_acl=None):
  49. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  50. other_category_acl.update({
  51. 'can_see': 1,
  52. 'can_browse': 1,
  53. 'can_start_threads': 0,
  54. 'can_reply_threads': 0,
  55. 'can_edit_posts': 1,
  56. 'can_approve_content': 0,
  57. 'can_move_posts': 1,
  58. })
  59. if extra_acl:
  60. other_category_acl.update(extra_acl)
  61. categories_acl = self.user.acl_cache['categories']
  62. categories_acl[self.category_b.pk] = other_category_acl
  63. visible_categories = [self.category.pk]
  64. if other_category_acl['can_see']:
  65. visible_categories.append(self.category_b.pk)
  66. override_acl(
  67. self.user, {
  68. 'visible_categories': visible_categories,
  69. 'categories': categories_acl,
  70. }
  71. )
  72. def test_anonymous_user(self):
  73. """you need to authenticate to move posts"""
  74. self.logout_user()
  75. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  76. self.assertEqual(response.status_code, 403)
  77. def test_invalid_data(self):
  78. """api handles post that is invalid type"""
  79. self.override_acl()
  80. response = self.client.post(self.api_link, '[]', content_type="application/json")
  81. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  82. self.override_acl()
  83. response = self.client.post(self.api_link, '123', content_type="application/json")
  84. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  85. self.override_acl()
  86. response = self.client.post(self.api_link, '"string"', content_type="application/json")
  87. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  88. self.override_acl()
  89. response = self.client.post(self.api_link, 'malformed', content_type="application/json")
  90. self.assertContains(response, "JSON parse error", status_code=400)
  91. def test_no_permission(self):
  92. """api validates permission to move"""
  93. self.override_acl({'can_move_posts': 0})
  94. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  95. self.assertContains(response, "You can't move posts in this thread.", status_code=403)
  96. def test_move_no_new_thread_url(self):
  97. """api validates if new thread url was given"""
  98. response = self.client.post(self.api_link)
  99. self.assertContains(response, "Enter link to new thread.", status_code=400)
  100. def test_invalid_new_thread_url(self):
  101. """api validates new thread url"""
  102. response = self.client.post(self.api_link, {
  103. 'new_thread': self.user.get_absolute_url(),
  104. })
  105. self.assertContains(response, "This is not a valid thread link.", status_code=400)
  106. def test_current_new_thread_url(self):
  107. """api validates if new thread url points to current thread"""
  108. response = self.client.post(
  109. self.api_link, {
  110. 'new_thread': self.thread.get_absolute_url(),
  111. }
  112. )
  113. self.assertContains(
  114. response, "Thread to move posts to is same as current one.", status_code=400
  115. )
  116. def test_other_thread_exists(self):
  117. """api validates if other thread exists"""
  118. self.override_other_acl()
  119. other_thread = testutils.post_thread(self.category_b)
  120. other_new_thread = other_thread.get_absolute_url()
  121. other_thread.delete()
  122. response = self.client.post(self.api_link, {
  123. 'new_thread': other_new_thread,
  124. })
  125. self.assertContains(
  126. response, "The thread you have entered link to doesn't exist", status_code=400
  127. )
  128. def test_other_thread_is_invisible(self):
  129. """api validates if other thread is visible"""
  130. self.override_other_acl({'can_see': 0})
  131. other_thread = testutils.post_thread(self.category_b)
  132. response = self.client.post(
  133. self.api_link, {
  134. 'new_thread': other_thread.get_absolute_url(),
  135. }
  136. )
  137. self.assertContains(
  138. response, "The thread you have entered link to doesn't exist", status_code=400
  139. )
  140. def test_other_thread_isnt_replyable(self):
  141. """api validates if other thread can be replied"""
  142. self.override_other_acl({'can_reply_threads': 0})
  143. other_thread = testutils.post_thread(self.category_b)
  144. response = self.client.post(
  145. self.api_link, {
  146. 'new_thread': other_thread.get_absolute_url(),
  147. }
  148. )
  149. self.assertContains(
  150. response, "You can't move posts to threads you can't reply.", status_code=400
  151. )
  152. def test_empty_data(self):
  153. """api handles empty data"""
  154. other_thread = testutils.post_thread(self.category)
  155. response = self.client.post(self.api_link)
  156. self.assertContains(
  157. response, "Enter link to new thread.", status_code=400
  158. )
  159. def test_empty_posts_data_json(self):
  160. """api handles empty json data"""
  161. other_thread = testutils.post_thread(self.category)
  162. response = self.client.post(
  163. self.api_link,
  164. json.dumps({
  165. 'new_thread': other_thread.get_absolute_url(),
  166. }),
  167. content_type="application/json",
  168. )
  169. self.assertContains(
  170. response, "You have to specify at least one post to move.", status_code=400
  171. )
  172. def test_empty_posts_data_form(self):
  173. """api handles empty form data"""
  174. other_thread = testutils.post_thread(self.category)
  175. response = self.client.post(
  176. self.api_link,
  177. {
  178. 'new_thread': other_thread.get_absolute_url(),
  179. },
  180. )
  181. self.assertContains(
  182. response, "You have to specify at least one post to move.", status_code=400
  183. )
  184. def test_no_posts_ids(self):
  185. """api rejects no posts ids"""
  186. other_thread = testutils.post_thread(self.category)
  187. response = self.client.post(
  188. self.api_link,
  189. json.dumps({
  190. 'new_thread': other_thread.get_absolute_url(),
  191. 'posts': [],
  192. }),
  193. content_type="application/json",
  194. )
  195. self.assertContains(
  196. response, "You have to specify at least one post to move.", status_code=400
  197. )
  198. def test_invalid_posts_data(self):
  199. """api handles invalid data"""
  200. other_thread = testutils.post_thread(self.category)
  201. response = self.client.post(
  202. self.api_link,
  203. json.dumps({
  204. 'new_thread': other_thread.get_absolute_url(),
  205. 'posts': 'string',
  206. }),
  207. content_type="application/json",
  208. )
  209. self.assertContains(
  210. response, "Expected a list of items", status_code=400
  211. )
  212. def test_invalid_posts_ids(self):
  213. """api handles invalid post id"""
  214. other_thread = testutils.post_thread(self.category)
  215. response = self.client.post(
  216. self.api_link,
  217. json.dumps({
  218. 'new_thread': other_thread.get_absolute_url(),
  219. 'posts': [1, 2, 'string'],
  220. }),
  221. content_type="application/json",
  222. )
  223. self.assertContains(
  224. response, "One or more post ids received were invalid.", status_code=400
  225. )
  226. def test_move_limit(self):
  227. """api rejects more posts than move limit"""
  228. other_thread = testutils.post_thread(self.category)
  229. response = self.client.post(
  230. self.api_link,
  231. json.dumps({
  232. 'new_thread': other_thread.get_absolute_url(),
  233. 'posts': list(range(POSTS_LIMIT + 1)),
  234. }),
  235. content_type="application/json",
  236. )
  237. self.assertContains(
  238. response, "No more than {} posts can be moved".format(POSTS_LIMIT), status_code=400
  239. )
  240. def test_move_invisible(self):
  241. """api validates posts visibility"""
  242. other_thread = testutils.post_thread(self.category)
  243. response = self.client.post(
  244. self.api_link,
  245. json.dumps({
  246. 'new_thread': other_thread.get_absolute_url(),
  247. 'posts': [testutils.reply_thread(self.thread, is_unapproved=True).pk],
  248. }),
  249. content_type="application/json",
  250. )
  251. self.assertContains(
  252. response, "One or more posts to move could not be found.", status_code=400
  253. )
  254. def test_move_other_thread_posts(self):
  255. """api recjects attempt to move other thread's post"""
  256. other_thread = testutils.post_thread(self.category)
  257. response = self.client.post(
  258. self.api_link,
  259. json.dumps({
  260. 'new_thread': other_thread.get_absolute_url(),
  261. 'posts': [testutils.reply_thread(other_thread, is_hidden=True).pk],
  262. }),
  263. content_type="application/json",
  264. )
  265. self.assertContains(
  266. response, "One or more posts to move could not be found.", status_code=400
  267. )
  268. def test_move_event(self):
  269. """api rejects events move"""
  270. other_thread = testutils.post_thread(self.category)
  271. response = self.client.post(
  272. self.api_link,
  273. json.dumps({
  274. 'new_thread': other_thread.get_absolute_url(),
  275. 'posts': [testutils.reply_thread(self.thread, is_event=True).pk],
  276. }),
  277. content_type="application/json",
  278. )
  279. self.assertContains(response, "Events can't be moved.", status_code=400)
  280. def test_move_first_post(self):
  281. """api rejects first post move"""
  282. other_thread = testutils.post_thread(self.category)
  283. response = self.client.post(
  284. self.api_link,
  285. json.dumps({
  286. 'new_thread': other_thread.get_absolute_url(),
  287. 'posts': [self.thread.first_post_id],
  288. }),
  289. content_type="application/json",
  290. )
  291. self.assertContains(response, "You can't move thread's first post.", status_code=400)
  292. def test_move_hidden_posts(self):
  293. """api recjects attempt to move urneadable hidden post"""
  294. other_thread = testutils.post_thread(self.category)
  295. response = self.client.post(
  296. self.api_link,
  297. json.dumps({
  298. 'new_thread': other_thread.get_absolute_url(),
  299. 'posts': [testutils.reply_thread(self.thread, is_hidden=True).pk],
  300. }),
  301. content_type="application/json",
  302. )
  303. self.assertContains(
  304. response, "You can't move posts the content you can't see.", status_code=400
  305. )
  306. def test_move_posts_closed_thread_no_permission(self):
  307. """api recjects attempt to move posts from closed thread"""
  308. other_thread = testutils.post_thread(self.category)
  309. self.thread.is_closed = True
  310. self.thread.save()
  311. self.override_acl({'can_close_threads': 0})
  312. response = self.client.post(
  313. self.api_link,
  314. json.dumps({
  315. 'new_thread': other_thread.get_absolute_url(),
  316. 'posts': [testutils.reply_thread(self.thread).pk],
  317. }),
  318. content_type="application/json",
  319. )
  320. self.assertContains(
  321. response, "This thread is closed. You can't move posts in it.", status_code=400
  322. )
  323. def test_move_posts_closed_category_no_permission(self):
  324. """api recjects attempt to move posts from closed thread"""
  325. other_thread = testutils.post_thread(self.category_b)
  326. self.category.is_closed = True
  327. self.category.save()
  328. self.override_acl({'can_close_threads': 0})
  329. self.override_other_acl({'can_reply_threads': 1})
  330. response = self.client.post(
  331. self.api_link,
  332. json.dumps({
  333. 'new_thread': other_thread.get_absolute_url(),
  334. 'posts': [testutils.reply_thread(self.thread).pk],
  335. }),
  336. content_type="application/json",
  337. )
  338. self.assertContains(
  339. response, "This category is closed. You can't move posts in it.", status_code=400
  340. )
  341. def test_move_posts(self):
  342. """api moves posts to other thread"""
  343. self.override_other_acl({'can_reply_threads': 1})
  344. other_thread = testutils.post_thread(self.category_b)
  345. posts = (
  346. testutils.reply_thread(self.thread).pk, testutils.reply_thread(self.thread).pk,
  347. testutils.reply_thread(self.thread).pk, testutils.reply_thread(self.thread).pk,
  348. )
  349. self.refresh_thread()
  350. self.assertEqual(self.thread.replies, 4)
  351. response = self.client.post(
  352. self.api_link,
  353. json.dumps({
  354. 'new_thread': other_thread.get_absolute_url(),
  355. 'posts': posts,
  356. }),
  357. content_type="application/json",
  358. )
  359. self.assertEqual(response.status_code, 200)
  360. # replies were moved
  361. self.refresh_thread()
  362. self.assertEqual(self.thread.replies, 0)
  363. other_thread = Thread.objects.get(pk=other_thread.pk)
  364. self.assertEqual(other_thread.post_set.filter(pk__in=posts).count(), 4)
  365. self.assertEqual(other_thread.replies, 4)