test_thread_postmerge_api.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. from __future__ import unicode_literals
  2. import json
  3. from django.urls import reverse
  4. from misago.acl.testutils import override_acl
  5. from misago.categories.models import Category
  6. from misago.readtracker import poststracker
  7. from misago.threads import testutils
  8. from misago.threads.models import Post, Thread
  9. from misago.threads.serializers.moderation import POSTS_LIMIT
  10. from misago.users.testutils import AuthenticatedUserTestCase
  11. class ThreadPostMergeApiTestCase(AuthenticatedUserTestCase):
  12. def setUp(self):
  13. super(ThreadPostMergeApiTestCase, self).setUp()
  14. self.category = Category.objects.get(slug='first-category')
  15. self.thread = testutils.post_thread(category=self.category)
  16. self.post = testutils.reply_thread(self.thread, poster=self.user)
  17. self.api_link = reverse(
  18. 'misago:api:thread-post-merge', kwargs={
  19. 'thread_pk': self.thread.pk,
  20. }
  21. )
  22. self.override_acl()
  23. def refresh_thread(self):
  24. self.thread = Thread.objects.get(pk=self.thread.pk)
  25. def override_acl(self, extra_acl=None):
  26. new_acl = self.user.acl_cache
  27. new_acl['categories'][self.category.pk].update({
  28. 'can_see': 1,
  29. 'can_browse': 1,
  30. 'can_start_threads': 0,
  31. 'can_reply_threads': 0,
  32. 'can_edit_posts': 1,
  33. 'can_approve_content': 0,
  34. 'can_merge_posts': 1,
  35. })
  36. if extra_acl:
  37. new_acl['categories'][self.category.pk].update(extra_acl)
  38. override_acl(self.user, new_acl)
  39. def test_anonymous_user(self):
  40. """you need to authenticate to merge posts"""
  41. self.logout_user()
  42. response = self.client.post(
  43. self.api_link,
  44. json.dumps({}),
  45. content_type="application/json",
  46. )
  47. self.assertEqual(response.status_code, 403)
  48. def test_no_permission(self):
  49. """api validates permission to merge"""
  50. self.override_acl({'can_merge_posts': 0})
  51. response = self.client.post(
  52. self.api_link,
  53. json.dumps({}),
  54. content_type="application/json",
  55. )
  56. self.assertContains(response, "You can't merge posts in this thread.", status_code=403)
  57. def test_empty_data_json(self):
  58. """api handles empty json data"""
  59. response = self.client.post(
  60. self.api_link, json.dumps({}), content_type="application/json"
  61. )
  62. self.assertContains(
  63. response, "You have to select at least two posts to merge.", status_code=400
  64. )
  65. def test_empty_data_form(self):
  66. """api handles empty form data"""
  67. response = self.client.post(self.api_link, {})
  68. self.assertContains(
  69. response, "You have to select at least two posts to merge.", status_code=400
  70. )
  71. def test_invalid_data(self):
  72. """api handles post that is invalid type"""
  73. self.override_acl()
  74. response = self.client.post(self.api_link, '[]', content_type="application/json")
  75. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  76. self.override_acl()
  77. response = self.client.post(self.api_link, '123', content_type="application/json")
  78. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  79. self.override_acl()
  80. response = self.client.post(self.api_link, '"string"', 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, 'malformed', content_type="application/json")
  84. self.assertContains(response, "JSON parse error", status_code=400)
  85. def test_no_posts_ids(self):
  86. """api rejects no posts ids"""
  87. response = self.client.post(
  88. self.api_link,
  89. json.dumps({
  90. 'posts': []
  91. }),
  92. content_type="application/json",
  93. )
  94. self.assertContains(
  95. response, "You have to select at least two posts to merge.", 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,
  101. json.dumps({
  102. 'posts': 'string'
  103. }),
  104. content_type="application/json",
  105. )
  106. self.assertContains(
  107. response, "Expected a list of items but got type", status_code=400
  108. )
  109. def test_invalid_posts_ids(self):
  110. """api handles invalid post id"""
  111. response = self.client.post(
  112. self.api_link,
  113. json.dumps({
  114. 'posts': [1, 2, 'string']
  115. }),
  116. content_type="application/json",
  117. )
  118. self.assertContains(
  119. response, "One or more post ids received were invalid.", status_code=400
  120. )
  121. def test_one_post_id(self):
  122. """api rejects one post id"""
  123. response = self.client.post(
  124. self.api_link,
  125. json.dumps({
  126. 'posts': [1]
  127. }),
  128. content_type="application/json",
  129. )
  130. self.assertContains(
  131. response, "You have to select at least two posts to merge.", status_code=400
  132. )
  133. def test_merge_limit(self):
  134. """api rejects more posts than merge limit"""
  135. response = self.client.post(
  136. self.api_link,
  137. json.dumps({
  138. 'posts': list(range(POSTS_LIMIT + 1))
  139. }),
  140. content_type="application/json",
  141. )
  142. self.assertContains(
  143. response, "No more than {} posts can be merged".format(POSTS_LIMIT), status_code=400
  144. )
  145. def test_merge_event(self):
  146. """api recjects events"""
  147. event = testutils.reply_thread(self.thread, is_event=True, poster=self.user)
  148. response = self.client.post(
  149. self.api_link,
  150. json.dumps({
  151. 'posts': [self.post.pk, event.pk]
  152. }),
  153. content_type="application/json",
  154. )
  155. self.assertContains(response, "Events can't be merged.", status_code=400)
  156. def test_merge_notfound_pk(self):
  157. """api recjects nonexistant pk's"""
  158. response = self.client.post(
  159. self.api_link,
  160. json.dumps({
  161. 'posts': [self.post.pk, self.post.pk * 1000]
  162. }),
  163. content_type="application/json",
  164. )
  165. self.assertContains(
  166. response, "One or more posts to merge could not be found.", status_code=400
  167. )
  168. def test_merge_cross_threads(self):
  169. """api recjects attempt to merge with post made in other thread"""
  170. other_thread = testutils.post_thread(category=self.category)
  171. other_post = testutils.reply_thread(other_thread, poster=self.user)
  172. response = self.client.post(
  173. self.api_link,
  174. json.dumps({
  175. 'posts': [self.post.pk, other_post.pk]
  176. }),
  177. content_type="application/json",
  178. )
  179. self.assertContains(
  180. response, "One or more posts to merge could not be found.", status_code=400
  181. )
  182. def test_merge_authenticated_with_guest_post(self):
  183. """api recjects attempt to merge with post made by deleted user"""
  184. other_post = testutils.reply_thread(self.thread)
  185. response = self.client.post(
  186. self.api_link,
  187. json.dumps({
  188. 'posts': [self.post.pk, other_post.pk]
  189. }),
  190. content_type="application/json",
  191. )
  192. self.assertContains(
  193. response, "Posts made by different users can't be merged.", status_code=400
  194. )
  195. def test_merge_guest_with_authenticated_post(self):
  196. """api recjects attempt to merge with post made by deleted user"""
  197. other_post = testutils.reply_thread(self.thread)
  198. response = self.client.post(
  199. self.api_link,
  200. json.dumps({
  201. 'posts': [other_post.pk, self.post.pk]
  202. }),
  203. content_type="application/json",
  204. )
  205. self.assertContains(
  206. response, "Posts made by different users can't be merged.", status_code=400
  207. )
  208. def test_merge_guest_posts_different_usernames(self):
  209. """api recjects attempt to merge posts made by different guests"""
  210. response = self.client.post(
  211. self.api_link,
  212. json.dumps({
  213. 'posts': [
  214. testutils.reply_thread(self.thread, poster="Bob").pk,
  215. testutils.reply_thread(self.thread, poster="Miku").pk,
  216. ]
  217. }),
  218. content_type="application/json",
  219. )
  220. self.assertContains(
  221. response, "Posts made by different users can't be merged.", status_code=400
  222. )
  223. def test_merge_different_visibility(self):
  224. """api recjects attempt to merge posts with different visibility"""
  225. self.override_acl({'can_hide_posts': 1})
  226. response = self.client.post(
  227. self.api_link,
  228. json.dumps({
  229. 'posts': [
  230. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True).pk,
  231. testutils.reply_thread(self.thread, poster=self.user, is_hidden=False).pk,
  232. ]
  233. }),
  234. content_type="application/json",
  235. )
  236. self.assertContains(
  237. response, "Posts with different visibility can't be merged.", status_code=400
  238. )
  239. def test_merge_different_approval(self):
  240. """api recjects attempt to merge posts with different approval"""
  241. self.override_acl({'can_approve_content': 1})
  242. response = self.client.post(
  243. self.api_link,
  244. json.dumps({
  245. 'posts': [
  246. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True).pk,
  247. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=False).pk,
  248. ]
  249. }),
  250. content_type="application/json",
  251. )
  252. self.assertContains(
  253. response, "Posts with different visibility can't be merged.", status_code=400
  254. )
  255. def test_closed_thread(self):
  256. """api validates permission to merge in closed thread"""
  257. self.thread.is_closed = True
  258. self.thread.save()
  259. posts = [
  260. testutils.reply_thread(self.thread, poster=self.user).pk,
  261. testutils.reply_thread(self.thread, poster=self.user).pk,
  262. ]
  263. response = self.client.post(
  264. self.api_link,
  265. json.dumps({'posts': posts}),
  266. content_type="application/json",
  267. )
  268. self.assertContains(
  269. response,
  270. "This thread is closed. You can't merge posts in it.",
  271. status_code=400,
  272. )
  273. # allow closing threads
  274. self.override_acl({'can_close_threads': 1})
  275. response = self.client.post(
  276. self.api_link,
  277. json.dumps({'posts': posts}),
  278. content_type="application/json",
  279. )
  280. self.assertEqual(response.status_code, 200)
  281. def test_closed_category(self):
  282. """api validates permission to merge in closed category"""
  283. self.category.is_closed = True
  284. self.category.save()
  285. posts = [
  286. testutils.reply_thread(self.thread, poster=self.user).pk,
  287. testutils.reply_thread(self.thread, poster=self.user).pk,
  288. ]
  289. response = self.client.post(
  290. self.api_link,
  291. json.dumps({'posts': posts}),
  292. content_type="application/json",
  293. )
  294. self.assertContains(
  295. response,
  296. "This category is closed. You can't merge posts in it.",
  297. status_code=400,
  298. )
  299. # allow closing threads
  300. self.override_acl({'can_close_threads': 1})
  301. response = self.client.post(
  302. self.api_link,
  303. json.dumps({'posts': posts}),
  304. content_type="application/json",
  305. )
  306. self.assertEqual(response.status_code, 200)
  307. def test_merge_best_answer_first_post(self):
  308. """api recjects attempt to merge best_answer with first post"""
  309. self.thread.first_post.poster = self.user
  310. self.thread.first_post.save()
  311. self.post.poster = self.user
  312. self.post.save()
  313. self.thread.set_best_answer(self.user, self.post)
  314. self.thread.save()
  315. response = self.client.post(
  316. self.api_link,
  317. json.dumps({
  318. 'posts': [
  319. self.thread.first_post.pk,
  320. self.post.pk,
  321. ]
  322. }),
  323. content_type="application/json",
  324. )
  325. self.assertEqual(response.status_code, 400)
  326. self.assertEqual(response.json(), {
  327. 'detail': "Post marked as best answer can't be merged with thread's first post."
  328. })
  329. def test_merge_posts(self):
  330. """api merges two posts"""
  331. post_a = testutils.reply_thread(self.thread, poster=self.user, message="Battęry")
  332. post_b = testutils.reply_thread(self.thread, poster=self.user, message="Hórse")
  333. thread_replies = self.thread.replies
  334. response = self.client.post(
  335. self.api_link,
  336. json.dumps({
  337. 'posts': [post_a.pk, post_b.pk]
  338. }),
  339. content_type="application/json",
  340. )
  341. self.assertEqual(response.status_code, 200)
  342. self.refresh_thread()
  343. self.assertEqual(self.thread.replies, thread_replies - 1)
  344. with self.assertRaises(Post.DoesNotExist):
  345. Post.objects.get(pk=post_b.pk)
  346. merged_post = Post.objects.get(pk=post_a.pk)
  347. self.assertEqual(merged_post.parsed, '{}\n{}'.format(post_a.parsed, post_b.parsed))
  348. def test_merge_guest_posts(self):
  349. """api recjects attempt to merge posts made by same guest"""
  350. response = self.client.post(
  351. self.api_link,
  352. json.dumps({
  353. 'posts': [
  354. testutils.reply_thread(self.thread, poster="Bob").pk,
  355. testutils.reply_thread(self.thread, poster="Bob").pk,
  356. ]
  357. }),
  358. content_type="application/json",
  359. )
  360. self.assertEqual(response.status_code, 200)
  361. def test_merge_hidden_posts(self):
  362. """api merges two hidden posts"""
  363. self.override_acl({'can_hide_posts': 1})
  364. response = self.client.post(
  365. self.api_link,
  366. json.dumps({
  367. 'posts': [
  368. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True).pk,
  369. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True).pk,
  370. ]
  371. }),
  372. content_type="application/json",
  373. )
  374. self.assertEqual(response.status_code, 200)
  375. def test_merge_unapproved_posts(self):
  376. """api merges two unapproved posts"""
  377. self.override_acl({'can_approve_content': 1})
  378. response = self.client.post(
  379. self.api_link,
  380. json.dumps({
  381. 'posts': [
  382. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True).pk,
  383. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True).pk,
  384. ]
  385. }),
  386. content_type="application/json",
  387. )
  388. self.assertEqual(response.status_code, 200)
  389. def test_merge_with_hidden_thread(self):
  390. """api excludes thread's first post from visibility checks"""
  391. self.thread.first_post.is_hidden = True
  392. self.thread.first_post.poster = self.user
  393. self.thread.first_post.save()
  394. post_visible = testutils.reply_thread(self.thread, poster=self.user, is_hidden=False)
  395. self.override_acl({'can_hide_threads': 1})
  396. response = self.client.post(
  397. self.api_link,
  398. json.dumps({
  399. 'posts': [self.thread.first_post.pk, post_visible.pk]
  400. }),
  401. content_type="application/json",
  402. )
  403. self.assertEqual(response.status_code, 200)
  404. def test_merge_protected(self):
  405. """api preserves protected status after merge"""
  406. response = self.client.post(
  407. self.api_link,
  408. json.dumps({
  409. 'posts': [
  410. testutils.reply_thread(self.thread, poster="Bob", is_protected=True).pk,
  411. testutils.reply_thread(self.thread, poster="Bob", is_protected=False).pk,
  412. ]
  413. }),
  414. content_type="application/json",
  415. )
  416. self.assertEqual(response.status_code, 200)
  417. merged_post = self.thread.post_set.order_by('-id')[0]
  418. self.assertTrue(merged_post.is_protected)
  419. def test_merge_best_answer(self):
  420. """api merges best answer with other post"""
  421. best_answer = testutils.reply_thread(self.thread, poster="Bob")
  422. self.thread.set_best_answer(self.user, best_answer)
  423. self.thread.save()
  424. response = self.client.post(
  425. self.api_link,
  426. json.dumps({
  427. 'posts': [
  428. best_answer.pk,
  429. testutils.reply_thread(self.thread, poster="Bob").pk,
  430. ]
  431. }),
  432. content_type="application/json",
  433. )
  434. self.assertEqual(response.status_code, 200)
  435. self.refresh_thread()
  436. self.assertEqual(self.thread.best_answer, best_answer)
  437. def test_merge_best_answer_in(self):
  438. """api merges best answer into other post"""
  439. other_post = testutils.reply_thread(self.thread, poster="Bob")
  440. best_answer = testutils.reply_thread(self.thread, poster="Bob")
  441. self.thread.set_best_answer(self.user, best_answer)
  442. self.thread.save()
  443. response = self.client.post(
  444. self.api_link,
  445. json.dumps({
  446. 'posts': [
  447. best_answer.pk,
  448. other_post.pk,
  449. ]
  450. }),
  451. content_type="application/json",
  452. )
  453. self.assertEqual(response.status_code, 200)
  454. self.refresh_thread()
  455. self.assertEqual(self.thread.best_answer, other_post)
  456. def test_merge_best_answer_in_protected(self):
  457. """api merges best answer into protected post"""
  458. best_answer = testutils.reply_thread(self.thread, poster="Bob")
  459. self.thread.set_best_answer(self.user, best_answer)
  460. self.thread.save()
  461. response = self.client.post(
  462. self.api_link,
  463. json.dumps({
  464. 'posts': [
  465. best_answer.pk,
  466. testutils.reply_thread(self.thread, poster="Bob", is_protected=True).pk,
  467. ]
  468. }),
  469. content_type="application/json",
  470. )
  471. self.assertEqual(response.status_code, 200)
  472. self.refresh_thread()
  473. self.assertEqual(self.thread.best_answer, best_answer)
  474. self.assertTrue(self.thread.best_answer.is_protected)
  475. self.assertTrue(self.thread.best_answer_is_protected)
  476. def test_merge_remove_reads(self):
  477. """two posts merge removes read tracker from post"""
  478. post_a = testutils.reply_thread(self.thread, poster=self.user, message="Battęry")
  479. post_b = testutils.reply_thread(self.thread, poster=self.user, message="Hórse")
  480. poststracker.save_read(self.user, post_a)
  481. poststracker.save_read(self.user, post_b)
  482. response = self.client.post(
  483. self.api_link,
  484. json.dumps({
  485. 'posts': [post_a.pk, post_b.pk]
  486. }),
  487. content_type="application/json",
  488. )
  489. self.assertEqual(response.status_code, 200)
  490. # both post's were removed from readtracker
  491. self.assertEqual(self.user.postread_set.count(), 0)