test_thread_postsplit_api.py 18 KB

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