test_thread_postsplit_api.py 18 KB

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