test_thread_postsplit_api.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  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 ThreadPostSplitApiTestCase(AuthenticatedUserTestCase):
  12. def setUp(self):
  13. super(ThreadPostSplitApiTestCase, 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).pk, testutils.reply_thread(self.thread).pk
  18. ]
  19. self.api_link = reverse(
  20. 'misago:api:thread-post-split', kwargs={
  21. 'thread_pk': self.thread.pk,
  22. }
  23. )
  24. Category(
  25. name='Category B',
  26. slug='category-b',
  27. ).insert_at(
  28. self.category,
  29. position='last-child',
  30. save=True,
  31. )
  32. self.category_b = Category.objects.get(slug='category-b')
  33. self.override_acl()
  34. self.override_other_acl()
  35. def refresh_thread(self):
  36. self.thread = Thread.objects.get(pk=self.thread.pk)
  37. def override_acl(self, extra_acl=None):
  38. new_acl = self.user.acl_cache
  39. new_acl['categories'][self.category.pk].update({
  40. 'can_see': 1,
  41. 'can_browse': 1,
  42. 'can_start_threads': 1,
  43. 'can_reply_threads': 1,
  44. 'can_edit_posts': 1,
  45. 'can_approve_content': 0,
  46. 'can_move_posts': 1,
  47. })
  48. if extra_acl:
  49. new_acl['categories'][self.category.pk].update(extra_acl)
  50. override_acl(self.user, new_acl)
  51. def override_other_acl(self, acl=None):
  52. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  53. other_category_acl.update({
  54. 'can_see': 1,
  55. 'can_browse': 1,
  56. 'can_start_threads': 0,
  57. 'can_reply_threads': 0,
  58. 'can_edit_posts': 1,
  59. 'can_approve_content': 0,
  60. 'can_move_posts': 1,
  61. })
  62. if acl:
  63. other_category_acl.update(acl)
  64. categories_acl = self.user.acl_cache['categories']
  65. categories_acl[self.category_b.pk] = other_category_acl
  66. visible_categories = [self.category.pk]
  67. if other_category_acl['can_see']:
  68. visible_categories.append(self.category_b.pk)
  69. override_acl(
  70. self.user, {
  71. 'visible_categories': visible_categories,
  72. 'categories': categories_acl,
  73. }
  74. )
  75. def test_anonymous_user(self):
  76. """you need to authenticate to split posts"""
  77. self.logout_user()
  78. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  79. self.assertEqual(response.status_code, 403)
  80. def test_no_permission(self):
  81. """api validates permission to split"""
  82. self.override_acl({'can_move_posts': 0})
  83. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  84. self.assertContains(response, "You can't split posts from this thread.", status_code=403)
  85. def test_empty_data(self):
  86. """api handles empty data"""
  87. response = self.client.post(self.api_link)
  88. self.assertContains(
  89. response, "You have to specify at least one post to split.", status_code=400
  90. )
  91. def test_invalid_data(self):
  92. """api handles post that is invalid type"""
  93. self.override_acl()
  94. response = self.client.post(self.api_link, '[]', content_type="application/json")
  95. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  96. self.override_acl()
  97. response = self.client.post(self.api_link, '123', content_type="application/json")
  98. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  99. self.override_acl()
  100. response = self.client.post(self.api_link, '"string"', content_type="application/json")
  101. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  102. self.override_acl()
  103. response = self.client.post(self.api_link, 'malformed', content_type="application/json")
  104. self.assertContains(response, "JSON parse error", status_code=400)
  105. def test_no_posts_ids(self):
  106. """api rejects no posts ids"""
  107. response = self.client.post(
  108. self.api_link,
  109. json.dumps({}),
  110. content_type="application/json",
  111. )
  112. self.assertContains(
  113. response, "You have to specify at least one post to split.", status_code=400
  114. )
  115. def test_empty_posts_ids(self):
  116. """api rejects empty posts ids list"""
  117. response = self.client.post(
  118. self.api_link,
  119. json.dumps({
  120. 'posts': [],
  121. }),
  122. content_type="application/json",
  123. )
  124. self.assertContains(
  125. response, "You have to specify at least one post to split.", status_code=400
  126. )
  127. def test_invalid_posts_data(self):
  128. """api handles invalid data"""
  129. response = self.client.post(
  130. self.api_link,
  131. json.dumps({
  132. 'posts': 'string',
  133. }),
  134. content_type="application/json",
  135. )
  136. self.assertContains(
  137. response, "Expected a list of items but got type", status_code=400
  138. )
  139. def test_invalid_posts_ids(self):
  140. """api handles invalid post id"""
  141. response = self.client.post(
  142. self.api_link,
  143. json.dumps({
  144. 'posts': [1, 2, 'string'],
  145. }),
  146. content_type="application/json",
  147. )
  148. self.assertContains(
  149. response, "One or more post ids received were invalid.", status_code=400
  150. )
  151. def test_split_limit(self):
  152. """api rejects more posts than split limit"""
  153. response = self.client.post(
  154. self.api_link,
  155. json.dumps({
  156. 'posts': list(range(POSTS_LIMIT + 1)),
  157. }),
  158. content_type="application/json",
  159. )
  160. self.assertContains(
  161. response, "No more than {} posts can be split".format(POSTS_LIMIT), status_code=400
  162. )
  163. def test_split_invisible(self):
  164. """api validates posts visibility"""
  165. response = self.client.post(
  166. self.api_link,
  167. json.dumps({
  168. 'posts': [testutils.reply_thread(self.thread, is_unapproved=True).pk],
  169. }),
  170. content_type="application/json",
  171. )
  172. self.assertContains(
  173. response, "One or more posts to split could not be found.", status_code=400
  174. )
  175. def test_split_event(self):
  176. """api rejects events split"""
  177. response = self.client.post(
  178. self.api_link,
  179. json.dumps({
  180. 'posts': [testutils.reply_thread(self.thread, is_event=True).pk],
  181. }),
  182. content_type="application/json",
  183. )
  184. self.assertContains(response, "Events can't be split.", status_code=400)
  185. def test_split_first_post(self):
  186. """api rejects first post split"""
  187. response = self.client.post(
  188. self.api_link,
  189. json.dumps({
  190. 'posts': [self.thread.first_post_id],
  191. }),
  192. content_type="application/json",
  193. )
  194. self.assertContains(response, "You can't split thread's first post.", status_code=400)
  195. def test_split_hidden_posts(self):
  196. """api recjects attempt to split urneadable hidden post"""
  197. response = self.client.post(
  198. self.api_link,
  199. json.dumps({
  200. 'posts': [testutils.reply_thread(self.thread, is_hidden=True).pk],
  201. }),
  202. content_type="application/json",
  203. )
  204. self.assertContains(
  205. response, "You can't split posts the content you can't see.", status_code=400
  206. )
  207. def test_split_posts_closed_thread_no_permission(self):
  208. """api recjects attempt to split posts from closed thread"""
  209. self.thread.is_closed = True
  210. self.thread.save()
  211. self.override_acl({'can_close_threads': 0})
  212. response = self.client.post(
  213. self.api_link,
  214. json.dumps({
  215. 'posts': [testutils.reply_thread(self.thread).pk],
  216. }),
  217. content_type="application/json",
  218. )
  219. self.assertContains(
  220. response, "This thread is closed. You can't split posts in it.", status_code=400
  221. )
  222. def test_split_posts_closed_category_no_permission(self):
  223. """api recjects attempt to split posts from closed thread"""
  224. self.category.is_closed = True
  225. self.category.save()
  226. self.override_acl({'can_close_threads': 0})
  227. response = self.client.post(
  228. self.api_link,
  229. json.dumps({
  230. 'posts': [testutils.reply_thread(self.thread).pk],
  231. }),
  232. content_type="application/json",
  233. )
  234. self.assertContains(
  235. response, "This category is closed. You can't split posts in it.", status_code=400
  236. )
  237. def test_split_other_thread_posts(self):
  238. """api recjects attempt to split other thread's post"""
  239. other_thread = testutils.post_thread(self.category)
  240. response = self.client.post(
  241. self.api_link,
  242. json.dumps({
  243. 'posts': [testutils.reply_thread(other_thread, is_hidden=True).pk],
  244. }),
  245. content_type="application/json",
  246. )
  247. self.assertContains(
  248. response, "One or more posts to split could not be found.", status_code=400
  249. )
  250. def test_split_empty_new_thread_data(self):
  251. """api handles empty form data"""
  252. response = self.client.post(
  253. self.api_link,
  254. json.dumps({
  255. 'posts': self.posts,
  256. }),
  257. content_type="application/json",
  258. )
  259. self.assertEqual(response.status_code, 400)
  260. response_json = response.json()
  261. self.assertEqual(
  262. response_json, {
  263. 'title': ['This field is required.'],
  264. 'category': ['This field is required.'],
  265. }
  266. )
  267. def test_split_invalid_final_title(self):
  268. """api rejects split because final thread title was invalid"""
  269. response = self.client.post(
  270. self.api_link,
  271. json.dumps({
  272. 'posts': self.posts,
  273. 'title': '$$$',
  274. 'category': self.category.id,
  275. }),
  276. content_type="application/json",
  277. )
  278. self.assertEqual(response.status_code, 400)
  279. response_json = response.json()
  280. self.assertEqual(
  281. response_json, {
  282. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  283. }
  284. )
  285. def test_split_invalid_category(self):
  286. """api rejects split because final category was invalid"""
  287. self.override_other_acl({'can_see': 0})
  288. response = self.client.post(
  289. self.api_link,
  290. json.dumps({
  291. 'posts': self.posts,
  292. 'title': 'Valid thread title',
  293. 'category': self.category_b.id,
  294. }),
  295. content_type="application/json",
  296. )
  297. self.assertEqual(response.status_code, 400)
  298. response_json = response.json()
  299. self.assertEqual(
  300. response_json, {
  301. 'category': ["Requested category could not be found."],
  302. }
  303. )
  304. def test_split_unallowed_start_thread(self):
  305. """api rejects split because category isn't allowing starting threads"""
  306. self.override_acl({'can_start_threads': 0})
  307. response = self.client.post(
  308. self.api_link,
  309. json.dumps({
  310. 'posts': self.posts,
  311. 'title': 'Valid thread title',
  312. 'category': self.category.id,
  313. }),
  314. content_type="application/json",
  315. )
  316. self.assertEqual(response.status_code, 400)
  317. response_json = response.json()
  318. self.assertEqual(
  319. response_json, {
  320. 'category': ["You can't create new threads in selected category."],
  321. }
  322. )
  323. def test_split_invalid_weight(self):
  324. """api rejects split because final weight was invalid"""
  325. response = self.client.post(
  326. self.api_link,
  327. json.dumps({
  328. 'posts': self.posts,
  329. 'title': 'Valid thread title',
  330. 'category': self.category.id,
  331. 'weight': 4,
  332. }),
  333. content_type="application/json",
  334. )
  335. self.assertEqual(response.status_code, 400)
  336. response_json = response.json()
  337. self.assertEqual(
  338. response_json, {
  339. 'weight': ["Ensure this value is less than or equal to 2."],
  340. }
  341. )
  342. def test_split_unallowed_global_weight(self):
  343. """api rejects split because global weight was unallowed"""
  344. response = self.client.post(
  345. self.api_link,
  346. json.dumps({
  347. 'posts': self.posts,
  348. 'title': 'Valid thread title',
  349. 'category': self.category.id,
  350. 'weight': 2,
  351. }),
  352. content_type="application/json",
  353. )
  354. self.assertEqual(response.status_code, 400)
  355. response_json = response.json()
  356. self.assertEqual(
  357. response_json, {
  358. 'weight': ["You don't have permission to pin threads globally in this category."],
  359. }
  360. )
  361. def test_split_unallowed_local_weight(self):
  362. """api rejects split because local weight was unallowed"""
  363. response = self.client.post(
  364. self.api_link,
  365. json.dumps({
  366. 'posts': self.posts,
  367. 'title': 'Valid thread title',
  368. 'category': self.category.id,
  369. 'weight': 1,
  370. }),
  371. content_type="application/json"
  372. )
  373. self.assertEqual(response.status_code, 400)
  374. response_json = response.json()
  375. self.assertEqual(
  376. response_json, {
  377. 'weight': ["You don't have permission to pin threads in this category."],
  378. }
  379. )
  380. def test_split_allowed_local_weight(self):
  381. """api allows local weight"""
  382. self.override_acl({'can_pin_threads': 1})
  383. response = self.client.post(
  384. self.api_link,
  385. json.dumps({
  386. 'posts': self.posts,
  387. 'title': '$$$',
  388. 'category': self.category.id,
  389. 'weight': 1,
  390. }),
  391. content_type="application/json",
  392. )
  393. self.assertEqual(response.status_code, 400)
  394. response_json = response.json()
  395. self.assertEqual(
  396. response_json, {
  397. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  398. }
  399. )
  400. def test_split_allowed_global_weight(self):
  401. """api allows global weight"""
  402. self.override_acl({'can_pin_threads': 2})
  403. response = self.client.post(
  404. self.api_link,
  405. json.dumps({
  406. 'posts': self.posts,
  407. 'title': '$$$',
  408. 'category': self.category.id,
  409. 'weight': 2,
  410. }),
  411. content_type="application/json",
  412. )
  413. self.assertEqual(response.status_code, 400)
  414. response_json = response.json()
  415. self.assertEqual(
  416. response_json, {
  417. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  418. }
  419. )
  420. def test_split_unallowed_close(self):
  421. """api rejects split because closing thread was unallowed"""
  422. response = self.client.post(
  423. self.api_link,
  424. json.dumps({
  425. 'posts': self.posts,
  426. 'title': 'Valid thread title',
  427. 'category': self.category.id,
  428. 'is_closed': True,
  429. }),
  430. content_type="application/json",
  431. )
  432. self.assertEqual(response.status_code, 400)
  433. response_json = response.json()
  434. self.assertEqual(
  435. response_json, {
  436. 'is_closed': ["You don't have permission to close threads in this category."],
  437. }
  438. )
  439. def test_split_with_close(self):
  440. """api allows for closing thread"""
  441. self.override_acl({'can_close_threads': True})
  442. response = self.client.post(
  443. self.api_link,
  444. json.dumps({
  445. 'posts': self.posts,
  446. 'title': '$$$',
  447. 'category': self.category.id,
  448. 'weight': 0,
  449. 'is_closed': True,
  450. }),
  451. content_type="application/json",
  452. )
  453. self.assertEqual(response.status_code, 400)
  454. response_json = response.json()
  455. self.assertEqual(
  456. response_json, {
  457. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  458. }
  459. )
  460. def test_split_unallowed_hidden(self):
  461. """api rejects split because hidden thread was unallowed"""
  462. response = self.client.post(
  463. self.api_link,
  464. json.dumps({
  465. 'posts': self.posts,
  466. 'title': 'Valid thread title',
  467. 'category': self.category.id,
  468. 'is_hidden': True,
  469. }),
  470. content_type="application/json",
  471. )
  472. self.assertEqual(response.status_code, 400)
  473. response_json = response.json()
  474. self.assertEqual(
  475. response_json, {
  476. 'is_hidden': ["You don't have permission to hide threads in this category."],
  477. }
  478. )
  479. def test_split_with_hide(self):
  480. """api allows for hiding thread"""
  481. self.override_acl({'can_hide_threads': True})
  482. response = self.client.post(
  483. self.api_link,
  484. json.dumps({
  485. 'posts': self.posts,
  486. 'title': '$$$',
  487. 'category': self.category.id,
  488. 'weight': 0,
  489. 'is_hidden': True,
  490. }),
  491. content_type="application/json",
  492. )
  493. self.assertEqual(response.status_code, 400)
  494. response_json = response.json()
  495. self.assertEqual(
  496. response_json, {
  497. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  498. }
  499. )
  500. def test_split(self):
  501. """api splits posts to new thread"""
  502. self.refresh_thread()
  503. self.assertEqual(self.thread.replies, 2)
  504. response = self.client.post(
  505. self.api_link,
  506. json.dumps({
  507. 'posts': self.posts,
  508. 'title': 'Split thread.',
  509. 'category': self.category.id,
  510. }),
  511. content_type="application/json",
  512. )
  513. self.assertEqual(response.status_code, 200)
  514. # thread was created
  515. split_thread = self.category.thread_set.get(slug='split-thread')
  516. self.assertEqual(split_thread.replies, 1)
  517. # posts were removed from old thread
  518. self.refresh_thread()
  519. self.assertEqual(self.thread.replies, 0)
  520. # posts were moved to new thread
  521. self.assertEqual(split_thread.post_set.filter(pk__in=self.posts).count(), 2)
  522. def test_split_kitchensink(self):
  523. """api splits posts with kitchensink"""
  524. self.refresh_thread()
  525. self.assertEqual(self.thread.replies, 2)
  526. self.override_other_acl({
  527. 'can_start_threads': 2,
  528. 'can_close_threads': True,
  529. 'can_hide_threads': True,
  530. 'can_pin_threads': 2,
  531. })
  532. response = self.client.post(
  533. self.api_link,
  534. json.dumps({
  535. 'posts': self.posts,
  536. 'title': 'Split thread',
  537. 'category': self.category_b.id,
  538. 'weight': 2,
  539. 'is_closed': 1,
  540. 'is_hidden': 1,
  541. }),
  542. content_type="application/json",
  543. )
  544. self.assertEqual(response.status_code, 200)
  545. # thread was created
  546. split_thread = self.category_b.thread_set.get(slug='split-thread')
  547. self.assertEqual(split_thread.replies, 1)
  548. self.assertEqual(split_thread.weight, 2)
  549. self.assertTrue(split_thread.is_closed)
  550. self.assertTrue(split_thread.is_hidden)
  551. # posts were removed from old thread
  552. self.refresh_thread()
  553. self.assertEqual(self.thread.replies, 0)
  554. # posts were moved to new thread
  555. self.assertEqual(split_thread.post_set.filter(pk__in=self.posts).count(), 2)