test_thread_postsplit_api.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  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.api.postendpoints.split import SPLIT_LIMIT
  9. from misago.threads.models import Thread
  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_no_posts_ids(self):
  92. """api rejects no posts ids"""
  93. response = self.client.post(
  94. self.api_link,
  95. json.dumps({
  96. 'posts': [],
  97. }),
  98. content_type="application/json",
  99. )
  100. self.assertContains(
  101. response, "You have to specify at least one post to split.", status_code=400
  102. )
  103. def test_invalid_posts_data(self):
  104. """api handles invalid data"""
  105. response = self.client.post(
  106. self.api_link,
  107. json.dumps({
  108. 'posts': 'string',
  109. }),
  110. content_type="application/json",
  111. )
  112. self.assertContains(
  113. response, "One or more post ids received were invalid.", status_code=400
  114. )
  115. def test_invalid_posts_ids(self):
  116. """api handles invalid post id"""
  117. response = self.client.post(
  118. self.api_link,
  119. json.dumps({
  120. 'posts': [1, 2, 'string'],
  121. }),
  122. content_type="application/json",
  123. )
  124. self.assertContains(
  125. response, "One or more post ids received were invalid.", status_code=400
  126. )
  127. def test_split_limit(self):
  128. """api rejects more posts than split limit"""
  129. response = self.client.post(
  130. self.api_link,
  131. json.dumps({
  132. 'posts': list(range(SPLIT_LIMIT + 1)),
  133. }),
  134. content_type="application/json",
  135. )
  136. self.assertContains(
  137. response, "No more than {} posts can be split".format(SPLIT_LIMIT), status_code=400
  138. )
  139. def test_split_invisible(self):
  140. """api validates posts visibility"""
  141. response = self.client.post(
  142. self.api_link,
  143. json.dumps({
  144. 'posts': [testutils.reply_thread(self.thread, is_unapproved=True).pk],
  145. }),
  146. content_type="application/json",
  147. )
  148. self.assertContains(
  149. response, "One or more posts to split could not be found.", status_code=400
  150. )
  151. def test_split_event(self):
  152. """api rejects events split"""
  153. response = self.client.post(
  154. self.api_link,
  155. json.dumps({
  156. 'posts': [testutils.reply_thread(self.thread, is_event=True).pk],
  157. }),
  158. content_type="application/json",
  159. )
  160. self.assertContains(response, "Events can't be split.", status_code=403)
  161. def test_split_first_post(self):
  162. """api rejects first post split"""
  163. response = self.client.post(
  164. self.api_link,
  165. json.dumps({
  166. 'posts': [self.thread.first_post_id],
  167. }),
  168. content_type="application/json",
  169. )
  170. self.assertContains(response, "You can't split thread's first post.", status_code=403)
  171. def test_split_hidden_posts(self):
  172. """api recjects attempt to split urneadable hidden post"""
  173. response = self.client.post(
  174. self.api_link,
  175. json.dumps({
  176. 'posts': [testutils.reply_thread(self.thread, is_hidden=True).pk],
  177. }),
  178. content_type="application/json",
  179. )
  180. self.assertContains(
  181. response, "You can't split posts the content you can't see.", status_code=403
  182. )
  183. def test_split_posts_closed_thread_no_permission(self):
  184. """api recjects attempt to split posts from closed thread"""
  185. self.thread.is_closed = True
  186. self.thread.save()
  187. self.override_acl({'can_close_threads': 0})
  188. response = self.client.post(
  189. self.api_link,
  190. json.dumps({
  191. 'posts': [testutils.reply_thread(self.thread).pk],
  192. }),
  193. content_type="application/json",
  194. )
  195. self.assertContains(
  196. response, "This thread is closed. You can't split posts in it.", status_code=403
  197. )
  198. def test_split_posts_closed_category_no_permission(self):
  199. """api recjects attempt to split posts from closed thread"""
  200. self.category.is_closed = True
  201. self.category.save()
  202. self.override_acl({'can_close_threads': 0})
  203. response = self.client.post(
  204. self.api_link,
  205. json.dumps({
  206. 'posts': [testutils.reply_thread(self.thread).pk],
  207. }),
  208. content_type="application/json",
  209. )
  210. self.assertContains(
  211. response, "This category is closed. You can't split posts in it.", status_code=403
  212. )
  213. def test_split_other_thread_posts(self):
  214. """api recjects attempt to split other thread's post"""
  215. other_thread = testutils.post_thread(self.category)
  216. response = self.client.post(
  217. self.api_link,
  218. json.dumps({
  219. 'posts': [testutils.reply_thread(other_thread, is_hidden=True).pk],
  220. }),
  221. content_type="application/json",
  222. )
  223. self.assertContains(
  224. response, "One or more posts to split could not be found.", status_code=400
  225. )
  226. def test_split_empty_new_thread_data(self):
  227. """api handles empty form data"""
  228. response = self.client.post(
  229. self.api_link,
  230. json.dumps({
  231. 'posts': self.posts,
  232. }),
  233. content_type="application/json",
  234. )
  235. self.assertEqual(response.status_code, 400)
  236. response_json = response.json()
  237. self.assertEqual(
  238. response_json, {
  239. 'title': ['This field is required.'],
  240. 'category': ['This field is required.'],
  241. }
  242. )
  243. def test_split_invalid_final_title(self):
  244. """api rejects split because final thread title was invalid"""
  245. response = self.client.post(
  246. self.api_link,
  247. json.dumps({
  248. 'posts': self.posts,
  249. 'title': '$$$',
  250. 'category': self.category.id,
  251. }),
  252. content_type="application/json",
  253. )
  254. self.assertEqual(response.status_code, 400)
  255. response_json = response.json()
  256. self.assertEqual(
  257. response_json, {
  258. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  259. }
  260. )
  261. def test_split_invalid_category(self):
  262. """api rejects split because final category was invalid"""
  263. self.override_other_acl({'can_see': 0})
  264. response = self.client.post(
  265. self.api_link,
  266. json.dumps({
  267. 'posts': self.posts,
  268. 'title': 'Valid thread title',
  269. 'category': self.category_b.id,
  270. }),
  271. content_type="application/json",
  272. )
  273. self.assertEqual(response.status_code, 400)
  274. response_json = response.json()
  275. self.assertEqual(
  276. response_json, {
  277. 'category': ["Requested category could not be found."],
  278. }
  279. )
  280. def test_split_unallowed_start_thread(self):
  281. """api rejects split because category isn't allowing starting threads"""
  282. self.override_acl({'can_start_threads': 0})
  283. response = self.client.post(
  284. self.api_link,
  285. json.dumps({
  286. 'posts': self.posts,
  287. 'title': 'Valid thread title',
  288. 'category': self.category.id,
  289. }),
  290. content_type="application/json",
  291. )
  292. self.assertEqual(response.status_code, 400)
  293. response_json = response.json()
  294. self.assertEqual(
  295. response_json, {
  296. 'category': ["You can't create new threads in selected category."],
  297. }
  298. )
  299. def test_split_invalid_weight(self):
  300. """api rejects split because final weight was invalid"""
  301. response = self.client.post(
  302. self.api_link,
  303. json.dumps({
  304. 'posts': self.posts,
  305. 'title': 'Valid thread title',
  306. 'category': self.category.id,
  307. 'weight': 4,
  308. }),
  309. content_type="application/json",
  310. )
  311. self.assertEqual(response.status_code, 400)
  312. response_json = response.json()
  313. self.assertEqual(
  314. response_json, {
  315. 'weight': ["Ensure this value is less than or equal to 2."],
  316. }
  317. )
  318. def test_split_unallowed_global_weight(self):
  319. """api rejects split because global weight was unallowed"""
  320. response = self.client.post(
  321. self.api_link,
  322. json.dumps({
  323. 'posts': self.posts,
  324. 'title': 'Valid thread title',
  325. 'category': self.category.id,
  326. 'weight': 2,
  327. }),
  328. content_type="application/json",
  329. )
  330. self.assertEqual(response.status_code, 400)
  331. response_json = response.json()
  332. self.assertEqual(
  333. response_json, {
  334. 'weight': ["You don't have permission to pin threads globally in this category."],
  335. }
  336. )
  337. def test_split_unallowed_local_weight(self):
  338. """api rejects split because local weight was unallowed"""
  339. response = self.client.post(
  340. self.api_link,
  341. json.dumps({
  342. 'posts': self.posts,
  343. 'title': 'Valid thread title',
  344. 'category': self.category.id,
  345. 'weight': 1,
  346. }),
  347. content_type="application/json"
  348. )
  349. self.assertEqual(response.status_code, 400)
  350. response_json = response.json()
  351. self.assertEqual(
  352. response_json, {
  353. 'weight': ["You don't have permission to pin threads in this category."],
  354. }
  355. )
  356. def test_split_allowed_local_weight(self):
  357. """api allows local weight"""
  358. self.override_acl({'can_pin_threads': 1})
  359. response = self.client.post(
  360. self.api_link,
  361. json.dumps({
  362. 'posts': self.posts,
  363. 'title': '$$$',
  364. 'category': self.category.id,
  365. 'weight': 1,
  366. }),
  367. content_type="application/json",
  368. )
  369. self.assertEqual(response.status_code, 400)
  370. response_json = response.json()
  371. self.assertEqual(
  372. response_json, {
  373. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  374. }
  375. )
  376. def test_split_allowed_global_weight(self):
  377. """api allows global weight"""
  378. self.override_acl({'can_pin_threads': 2})
  379. response = self.client.post(
  380. self.api_link,
  381. json.dumps({
  382. 'posts': self.posts,
  383. 'title': '$$$',
  384. 'category': self.category.id,
  385. 'weight': 2,
  386. }),
  387. content_type="application/json",
  388. )
  389. self.assertEqual(response.status_code, 400)
  390. response_json = response.json()
  391. self.assertEqual(
  392. response_json, {
  393. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  394. }
  395. )
  396. def test_split_unallowed_close(self):
  397. """api rejects split because closing thread was unallowed"""
  398. response = self.client.post(
  399. self.api_link,
  400. json.dumps({
  401. 'posts': self.posts,
  402. 'title': 'Valid thread title',
  403. 'category': self.category.id,
  404. 'is_closed': True,
  405. }),
  406. content_type="application/json",
  407. )
  408. self.assertEqual(response.status_code, 400)
  409. response_json = response.json()
  410. self.assertEqual(
  411. response_json, {
  412. 'is_closed': ["You don't have permission to close threads in this category."],
  413. }
  414. )
  415. def test_split_with_close(self):
  416. """api allows for closing thread"""
  417. self.override_acl({'can_close_threads': True})
  418. response = self.client.post(
  419. self.api_link,
  420. json.dumps({
  421. 'posts': self.posts,
  422. 'title': '$$$',
  423. 'category': self.category.id,
  424. 'weight': 0,
  425. 'is_closed': True,
  426. }),
  427. content_type="application/json",
  428. )
  429. self.assertEqual(response.status_code, 400)
  430. response_json = response.json()
  431. self.assertEqual(
  432. response_json, {
  433. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  434. }
  435. )
  436. def test_split_unallowed_hidden(self):
  437. """api rejects split because hidden thread was unallowed"""
  438. response = self.client.post(
  439. self.api_link,
  440. json.dumps({
  441. 'posts': self.posts,
  442. 'title': 'Valid thread title',
  443. 'category': self.category.id,
  444. 'is_hidden': True,
  445. }),
  446. content_type="application/json",
  447. )
  448. self.assertEqual(response.status_code, 400)
  449. response_json = response.json()
  450. self.assertEqual(
  451. response_json, {
  452. 'is_hidden': ["You don't have permission to hide threads in this category."],
  453. }
  454. )
  455. def test_split_with_hide(self):
  456. """api allows for hiding thread"""
  457. self.override_acl({'can_hide_threads': True})
  458. response = self.client.post(
  459. self.api_link,
  460. json.dumps({
  461. 'posts': self.posts,
  462. 'title': '$$$',
  463. 'category': self.category.id,
  464. 'weight': 0,
  465. 'is_hidden': True,
  466. }),
  467. content_type="application/json",
  468. )
  469. self.assertEqual(response.status_code, 400)
  470. response_json = response.json()
  471. self.assertEqual(
  472. response_json, {
  473. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  474. }
  475. )
  476. def test_split(self):
  477. """api splits posts to new thread"""
  478. self.refresh_thread()
  479. self.assertEqual(self.thread.replies, 2)
  480. response = self.client.post(
  481. self.api_link,
  482. json.dumps({
  483. 'posts': self.posts,
  484. 'title': 'Split thread.',
  485. 'category': self.category.id,
  486. }),
  487. content_type="application/json",
  488. )
  489. self.assertEqual(response.status_code, 200)
  490. # thread was created
  491. split_thread = self.category.thread_set.get(slug='split-thread')
  492. self.assertEqual(split_thread.replies, 1)
  493. # posts were removed from old thread
  494. self.refresh_thread()
  495. self.assertEqual(self.thread.replies, 0)
  496. # posts were moved to new thread
  497. self.assertEqual(split_thread.post_set.filter(pk__in=self.posts).count(), 2)
  498. def test_split_kitchensink(self):
  499. """api splits posts with kitchensink"""
  500. self.refresh_thread()
  501. self.assertEqual(self.thread.replies, 2)
  502. self.override_other_acl({
  503. 'can_start_threads': 2,
  504. 'can_close_threads': True,
  505. 'can_hide_threads': True,
  506. 'can_pin_threads': 2,
  507. })
  508. response = self.client.post(
  509. self.api_link,
  510. json.dumps({
  511. 'posts': self.posts,
  512. 'title': 'Split thread',
  513. 'category': self.category_b.id,
  514. 'weight': 2,
  515. 'is_closed': 1,
  516. 'is_hidden': 1,
  517. }),
  518. content_type="application/json",
  519. )
  520. self.assertEqual(response.status_code, 200)
  521. # thread was created
  522. split_thread = self.category_b.thread_set.get(slug='split-thread')
  523. self.assertEqual(split_thread.replies, 1)
  524. self.assertEqual(split_thread.weight, 2)
  525. self.assertTrue(split_thread.is_closed)
  526. self.assertTrue(split_thread.is_hidden)
  527. # posts were removed from old thread
  528. self.refresh_thread()
  529. self.assertEqual(self.thread.replies, 0)
  530. # posts were moved to new thread
  531. self.assertEqual(split_thread.post_set.filter(pk__in=self.posts).count(), 2)