test_thread_merge_api.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. from django.urls import reverse
  2. from misago.acl.testutils import override_acl
  3. from misago.categories.models import Category
  4. from misago.readtracker import poststracker
  5. from misago.threads import testutils
  6. from misago.threads.models import Poll, PollVote, Thread
  7. from .test_threads_api import ThreadsApiTestCase
  8. class ThreadMergeApiTests(ThreadsApiTestCase):
  9. def setUp(self):
  10. super(ThreadMergeApiTests, self).setUp()
  11. Category(
  12. name='Category B',
  13. slug='category-b',
  14. ).insert_at(
  15. self.category,
  16. position='last-child',
  17. save=True,
  18. )
  19. self.category_b = Category.objects.get(slug='category-b')
  20. self.api_link = reverse(
  21. 'misago:api:thread-merge', kwargs={
  22. 'pk': self.thread.pk,
  23. }
  24. )
  25. def override_other_acl(self, acl=None):
  26. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  27. other_category_acl.update({
  28. 'can_see': 1,
  29. 'can_browse': 1,
  30. 'can_see_all_threads': 1,
  31. 'can_see_own_threads': 0,
  32. 'can_hide_threads': 0,
  33. 'can_approve_content': 0,
  34. 'can_edit_posts': 0,
  35. 'can_hide_posts': 0,
  36. 'can_hide_own_posts': 0,
  37. 'can_merge_threads': 0,
  38. 'can_close_threads': 0,
  39. })
  40. if acl:
  41. other_category_acl.update(acl)
  42. categories_acl = self.user.acl_cache['categories']
  43. categories_acl[self.category_b.pk] = other_category_acl
  44. visible_categories = [self.category.pk]
  45. if other_category_acl['can_see']:
  46. visible_categories.append(self.category_b.pk)
  47. override_acl(
  48. self.user, {
  49. 'visible_categories': visible_categories,
  50. 'categories': categories_acl,
  51. }
  52. )
  53. def test_merge_no_permission(self):
  54. """api validates if thread can be merged with other one"""
  55. self.override_acl({'can_merge_threads': 0})
  56. response = self.client.post(self.api_link)
  57. self.assertEqual(response.status_code, 403)
  58. self.assertEqual(response.json(), {
  59. 'detail': "You can't merge threads in this category.",
  60. })
  61. def test_merge_no_url(self):
  62. """api validates if thread url was given"""
  63. self.override_acl({'can_merge_threads': 1})
  64. response = self.client.post(self.api_link)
  65. self.assertEqual(response.status_code, 400)
  66. self.assertEqual(response.json(), {
  67. 'other_thread': ["Enter link to new thread."],
  68. })
  69. def test_invalid_url(self):
  70. """api validates thread url"""
  71. self.override_acl({'can_merge_threads': 1})
  72. response = self.client.post(
  73. self.api_link, {
  74. 'other_thread': self.user.get_absolute_url(),
  75. }
  76. )
  77. self.assertEqual(response.status_code, 400)
  78. self.assertEqual(response.json(), {
  79. 'other_thread': ["This is not a valid thread link."],
  80. })
  81. def test_current_other_thread(self):
  82. """api validates if thread url given is to current thread"""
  83. self.override_acl({'can_merge_threads': 1})
  84. response = self.client.post(
  85. self.api_link, {
  86. 'other_thread': self.thread.get_absolute_url(),
  87. }
  88. )
  89. self.assertEqual(response.status_code, 400)
  90. self.assertEqual(response.json(), {
  91. 'other_thread': ["You can't merge thread with itself."],
  92. })
  93. def test_other_thread_exists(self):
  94. """api validates if other thread exists"""
  95. self.override_acl({'can_merge_threads': 1})
  96. self.override_other_acl()
  97. other_thread = testutils.post_thread(self.category_b)
  98. other_other_thread = other_thread.get_absolute_url()
  99. other_thread.delete()
  100. response = self.client.post(self.api_link, {
  101. 'other_thread': other_other_thread,
  102. })
  103. self.assertEqual(response.status_code, 400)
  104. self.assertEqual(
  105. response.json(),
  106. {
  107. 'other_thread': [
  108. "The thread you have entered link to doesn't exist or "
  109. "you don't have permission to see it."
  110. ],
  111. }
  112. )
  113. def test_other_thread_is_invisible(self):
  114. """api validates if other thread is visible"""
  115. self.override_acl({'can_merge_threads': 1})
  116. self.override_other_acl({'can_see': 0})
  117. other_thread = testutils.post_thread(self.category_b)
  118. response = self.client.post(
  119. self.api_link, {
  120. 'other_thread': other_thread.get_absolute_url(),
  121. }
  122. )
  123. self.assertEqual(response.status_code, 400)
  124. self.assertEqual(
  125. response.json(),
  126. {
  127. 'other_thread': [
  128. "The thread you have entered link to doesn't exist or "
  129. "you don't have permission to see it."
  130. ],
  131. },
  132. )
  133. def test_other_thread_isnt_mergeable(self):
  134. """api validates if other thread can be merged"""
  135. self.override_acl({'can_merge_threads': 1})
  136. self.override_other_acl({'can_merge_threads': 0})
  137. other_thread = testutils.post_thread(self.category_b)
  138. response = self.client.post(
  139. self.api_link, {
  140. 'other_thread': other_thread.get_absolute_url(),
  141. }
  142. )
  143. self.assertEqual(response.status_code, 400)
  144. self.assertEqual(
  145. response.json(), {
  146. 'other_thread': ["Other thread can't be merged with."],
  147. }
  148. )
  149. def test_thread_category_is_closed(self):
  150. """api validates if thread's category is open"""
  151. self.override_acl({'can_merge_threads': 1})
  152. self.override_other_acl({
  153. 'can_merge_threads': 1,
  154. 'can_reply_threads': 0,
  155. 'can_close_threads': 0,
  156. })
  157. other_thread = testutils.post_thread(self.category_b)
  158. self.category.is_closed = True
  159. self.category.save()
  160. response = self.client.post(
  161. self.api_link, {
  162. 'other_thread': other_thread.get_absolute_url(),
  163. }
  164. )
  165. self.assertEqual(response.status_code, 403)
  166. self.assertEqual(
  167. response.json(), {
  168. 'detail': "This category is closed. You can't merge it's threads.",
  169. }
  170. )
  171. def test_thread_is_closed(self):
  172. """api validates if thread is open"""
  173. self.override_acl({'can_merge_threads': 1})
  174. self.override_other_acl({
  175. 'can_merge_threads': 1,
  176. 'can_reply_threads': 0,
  177. 'can_close_threads': 0,
  178. })
  179. other_thread = testutils.post_thread(self.category_b)
  180. self.thread.is_closed = True
  181. self.thread.save()
  182. response = self.client.post(
  183. self.api_link, {
  184. 'other_thread': other_thread.get_absolute_url(),
  185. }
  186. )
  187. self.assertEqual(response.status_code, 403)
  188. self.assertEqual(
  189. response.json(),
  190. {
  191. 'detail': "This thread is closed. You can't merge it with other threads.",
  192. },
  193. )
  194. def test_other_thread_category_is_closed(self):
  195. """api validates if other thread's category is open"""
  196. self.override_acl({'can_merge_threads': 1})
  197. self.override_other_acl({
  198. 'can_merge_threads': 1,
  199. 'can_reply_threads': 0,
  200. 'can_close_threads': 0,
  201. })
  202. other_thread = testutils.post_thread(self.category_b)
  203. self.category_b.is_closed = True
  204. self.category_b.save()
  205. response = self.client.post(
  206. self.api_link, {
  207. 'other_thread': other_thread.get_absolute_url(),
  208. }
  209. )
  210. self.assertEqual(response.status_code, 400)
  211. self.assertEqual(
  212. response.json(),
  213. {
  214. 'other_thread': ["Other thread's category is closed. You can't merge with it."],
  215. },
  216. )
  217. def test_other_thread_is_closed(self):
  218. """api validates if other thread is open"""
  219. self.override_acl({'can_merge_threads': 1})
  220. self.override_other_acl({
  221. 'can_merge_threads': 1,
  222. 'can_reply_threads': 0,
  223. 'can_close_threads': 0,
  224. })
  225. other_thread = testutils.post_thread(self.category_b)
  226. other_thread.is_closed = True
  227. other_thread.save()
  228. response = self.client.post(
  229. self.api_link, {
  230. 'other_thread': other_thread.get_absolute_url(),
  231. }
  232. )
  233. self.assertEqual(response.status_code, 400)
  234. self.assertEqual(
  235. response.json(),
  236. {
  237. 'other_thread': ["Other thread is closed and can't be merged with."],
  238. }
  239. )
  240. def test_other_thread_isnt_replyable(self):
  241. """api validates if other thread can be replied, which is condition for merge"""
  242. self.override_acl({'can_merge_threads': 1})
  243. self.override_other_acl({
  244. 'can_merge_threads': 1,
  245. 'can_reply_threads': 0,
  246. })
  247. other_thread = testutils.post_thread(self.category_b)
  248. response = self.client.post(
  249. self.api_link, {
  250. 'other_thread': other_thread.get_absolute_url(),
  251. }
  252. )
  253. self.assertEqual(response.status_code, 400)
  254. self.assertEqual(
  255. response.json(),
  256. {
  257. 'other_thread': ["You can't merge this thread into thread you can't reply."],
  258. }
  259. )
  260. def test_merge_threads(self):
  261. """api merges two threads successfully"""
  262. self.override_acl({'can_merge_threads': 1})
  263. self.override_other_acl({'can_merge_threads': 1})
  264. other_thread = testutils.post_thread(self.category_b)
  265. response = self.client.post(
  266. self.api_link, {
  267. 'other_thread': other_thread.get_absolute_url(),
  268. }
  269. )
  270. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  271. # other thread has two posts now
  272. self.assertEqual(other_thread.post_set.count(), 3)
  273. # first thread is gone
  274. with self.assertRaises(Thread.DoesNotExist):
  275. Thread.objects.get(pk=self.thread.pk)
  276. def test_merge_threads_kept_reads(self):
  277. """api keeps both threads readtrackers after merge"""
  278. self.override_acl({'can_merge_threads': 1})
  279. self.override_other_acl({'can_merge_threads': 1})
  280. other_thread = testutils.post_thread(self.category_b)
  281. poststracker.save_read(self.user, self.thread.first_post)
  282. poststracker.save_read(self.user, other_thread.first_post)
  283. response = self.client.post(
  284. self.api_link, {
  285. 'other_thread': other_thread.get_absolute_url(),
  286. }
  287. )
  288. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  289. # posts reads are kept
  290. postreads = self.user.postread_set.filter(post__is_event=False).order_by('id')
  291. self.assertEqual(
  292. list(postreads.values_list('post_id', flat=True)),
  293. [self.thread.first_post_id, other_thread.first_post_id]
  294. )
  295. self.assertEqual(postreads.filter(thread=other_thread).count(), 2)
  296. self.assertEqual(postreads.filter(category=self.category_b).count(), 2)
  297. def test_merge_threads_kept_subs(self):
  298. """api keeps other thread's subscription after merge"""
  299. self.override_acl({'can_merge_threads': 1})
  300. self.override_other_acl({'can_merge_threads': 1})
  301. other_thread = testutils.post_thread(self.category_b)
  302. self.user.subscription_set.create(
  303. thread=self.thread,
  304. category=self.thread.category,
  305. last_read_on=self.thread.last_post_on,
  306. send_email=False,
  307. )
  308. self.assertEqual(self.user.subscription_set.count(), 1)
  309. self.user.subscription_set.get(thread=self.thread)
  310. self.user.subscription_set.get(category=self.category)
  311. response = self.client.post(
  312. self.api_link, {
  313. 'other_thread': other_thread.get_absolute_url(),
  314. }
  315. )
  316. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  317. # subscriptions are kept
  318. self.assertEqual(self.user.subscription_set.count(), 1)
  319. self.user.subscription_set.get(thread=other_thread)
  320. self.user.subscription_set.get(category=self.category_b)
  321. def test_merge_threads_moved_subs(self):
  322. """api keeps other thread's subscription after merge"""
  323. self.override_acl({'can_merge_threads': 1})
  324. self.override_other_acl({'can_merge_threads': 1})
  325. other_thread = testutils.post_thread(self.category_b)
  326. self.user.subscription_set.create(
  327. thread=other_thread,
  328. category=other_thread.category,
  329. last_read_on=other_thread.last_post_on,
  330. send_email=False,
  331. )
  332. self.assertEqual(self.user.subscription_set.count(), 1)
  333. self.user.subscription_set.get(thread=other_thread)
  334. self.user.subscription_set.get(category=self.category_b)
  335. response = self.client.post(
  336. self.api_link, {
  337. 'other_thread': other_thread.get_absolute_url(),
  338. }
  339. )
  340. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  341. # subscriptions are kept
  342. self.assertEqual(self.user.subscription_set.count(), 1)
  343. self.user.subscription_set.get(thread=other_thread)
  344. self.user.subscription_set.get(category=self.category_b)
  345. def test_merge_threads_handle_subs_colision(self):
  346. """api resolves conflicting thread subscriptions after merge"""
  347. self.override_acl({'can_merge_threads': 1})
  348. self.override_other_acl({'can_merge_threads': 1})
  349. self.user.subscription_set.create(
  350. thread=self.thread,
  351. category=self.thread.category,
  352. last_read_on=self.thread.last_post_on,
  353. send_email=False,
  354. )
  355. other_thread = testutils.post_thread(self.category_b)
  356. self.user.subscription_set.create(
  357. thread=other_thread,
  358. category=other_thread.category,
  359. last_read_on=other_thread.last_post_on,
  360. send_email=False,
  361. )
  362. self.assertEqual(self.user.subscription_set.count(), 2)
  363. self.user.subscription_set.get(thread=self.thread)
  364. self.user.subscription_set.get(category=self.category)
  365. self.user.subscription_set.get(thread=other_thread)
  366. self.user.subscription_set.get(category=self.category_b)
  367. response = self.client.post(
  368. self.api_link, {
  369. 'other_thread': other_thread.get_absolute_url(),
  370. }
  371. )
  372. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  373. # subscriptions are kept
  374. self.assertEqual(self.user.subscription_set.count(), 1)
  375. self.user.subscription_set.get(thread=other_thread)
  376. self.user.subscription_set.get(category=self.category_b)
  377. def test_merge_threads_kept_poll(self):
  378. """api merges two threads successfully, keeping poll from old thread"""
  379. self.override_acl({'can_merge_threads': 1})
  380. self.override_other_acl({'can_merge_threads': 1})
  381. other_thread = testutils.post_thread(self.category_b)
  382. poll = testutils.post_poll(other_thread, self.user)
  383. response = self.client.post(
  384. self.api_link, {
  385. 'other_thread': other_thread.get_absolute_url(),
  386. }
  387. )
  388. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  389. # other thread has two posts now
  390. self.assertEqual(other_thread.post_set.count(), 3)
  391. # first thread is gone
  392. with self.assertRaises(Thread.DoesNotExist):
  393. Thread.objects.get(pk=self.thread.pk)
  394. # poll and its votes were kept
  395. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=other_thread).count(), 1)
  396. self.assertEqual(PollVote.objects.filter(poll=poll, thread=other_thread).count(), 4)
  397. def test_merge_threads_moved_poll(self):
  398. """api merges two threads successfully, moving poll from other thread"""
  399. self.override_acl({'can_merge_threads': 1})
  400. self.override_other_acl({'can_merge_threads': 1})
  401. other_thread = testutils.post_thread(self.category_b)
  402. poll = testutils.post_poll(self.thread, self.user)
  403. response = self.client.post(
  404. self.api_link, {
  405. 'other_thread': other_thread.get_absolute_url(),
  406. }
  407. )
  408. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  409. # other thread has two posts now
  410. self.assertEqual(other_thread.post_set.count(), 3)
  411. # first thread is gone
  412. with self.assertRaises(Thread.DoesNotExist):
  413. Thread.objects.get(pk=self.thread.pk)
  414. # poll and its votes were moved
  415. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=other_thread).count(), 1)
  416. self.assertEqual(PollVote.objects.filter(poll=poll, thread=other_thread).count(), 4)
  417. def test_threads_merge_conflict(self):
  418. """api errors on merge conflict, returning list of available polls"""
  419. self.override_acl({'can_merge_threads': 1})
  420. self.override_other_acl({'can_merge_threads': 1})
  421. other_thread = testutils.post_thread(self.category_b)
  422. poll = testutils.post_poll(self.thread, self.user)
  423. other_poll = testutils.post_poll(other_thread, self.user)
  424. response = self.client.post(
  425. self.api_link, {
  426. 'other_thread': other_thread.get_absolute_url(),
  427. }
  428. )
  429. self.assertEqual(response.status_code, 400)
  430. self.assertEqual(
  431. response.json(), {
  432. 'polls': [
  433. ['0', "Delete all polls"],
  434. [str(poll.pk), poll.question],
  435. [str(other_poll.pk), other_poll.question],
  436. ]
  437. }
  438. )
  439. # polls and votes were untouched
  440. self.assertEqual(Poll.objects.count(), 2)
  441. self.assertEqual(PollVote.objects.count(), 8)
  442. def test_threads_merge_conflict_invalid_resolution(self):
  443. """api errors on invalid merge conflict resolution"""
  444. self.override_acl({'can_merge_threads': 1})
  445. self.override_other_acl({'can_merge_threads': 1})
  446. other_thread = testutils.post_thread(self.category_b)
  447. testutils.post_poll(self.thread, self.user)
  448. testutils.post_poll(other_thread, self.user)
  449. response = self.client.post(
  450. self.api_link, {
  451. 'other_thread': other_thread.get_absolute_url(),
  452. 'poll': 'jhdkajshdsak',
  453. }
  454. )
  455. self.assertEqual(response.status_code, 400)
  456. self.assertEqual(response.json(), {'poll': ["Invalid choice."]})
  457. # polls and votes were untouched
  458. self.assertEqual(Poll.objects.count(), 2)
  459. self.assertEqual(PollVote.objects.count(), 8)
  460. def test_threads_merge_conflict_delete_all(self):
  461. """api deletes all polls when delete all choice is selected"""
  462. self.override_acl({'can_merge_threads': 1})
  463. self.override_other_acl({'can_merge_threads': 1})
  464. other_thread = testutils.post_thread(self.category_b)
  465. testutils.post_poll(self.thread, self.user)
  466. testutils.post_poll(other_thread, self.user)
  467. response = self.client.post(
  468. self.api_link, {
  469. 'other_thread': other_thread.get_absolute_url(),
  470. 'poll': 0,
  471. }
  472. )
  473. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  474. # other thread has two posts now
  475. self.assertEqual(other_thread.post_set.count(), 3)
  476. # first thread is gone
  477. with self.assertRaises(Thread.DoesNotExist):
  478. Thread.objects.get(pk=self.thread.pk)
  479. # polls and votes are gone
  480. self.assertEqual(Poll.objects.count(), 0)
  481. self.assertEqual(PollVote.objects.count(), 0)
  482. def test_threads_merge_conflict_keep_first_poll(self):
  483. """api deletes other poll on merge"""
  484. self.override_acl({'can_merge_threads': 1})
  485. self.override_other_acl({'can_merge_threads': 1})
  486. other_thread = testutils.post_thread(self.category_b)
  487. poll = testutils.post_poll(self.thread, self.user)
  488. other_poll = testutils.post_poll(other_thread, self.user)
  489. response = self.client.post(
  490. self.api_link, {
  491. 'other_thread': other_thread.get_absolute_url(),
  492. 'poll': poll.pk,
  493. }
  494. )
  495. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  496. # other thread has two posts now
  497. self.assertEqual(other_thread.post_set.count(), 3)
  498. # first thread is gone
  499. with self.assertRaises(Thread.DoesNotExist):
  500. Thread.objects.get(pk=self.thread.pk)
  501. # other poll and its votes are gone
  502. self.assertEqual(Poll.objects.filter(thread=self.thread).count(), 0)
  503. self.assertEqual(PollVote.objects.filter(thread=self.thread).count(), 0)
  504. self.assertEqual(Poll.objects.filter(thread=other_thread).count(), 1)
  505. self.assertEqual(PollVote.objects.filter(thread=other_thread).count(), 4)
  506. Poll.objects.get(pk=poll.pk)
  507. with self.assertRaises(Poll.DoesNotExist):
  508. Poll.objects.get(pk=other_poll.pk)
  509. def test_threads_merge_conflict_keep_other_poll(self):
  510. """api deletes first poll on merge"""
  511. self.override_acl({'can_merge_threads': 1})
  512. self.override_other_acl({'can_merge_threads': 1})
  513. other_thread = testutils.post_thread(self.category_b)
  514. poll = testutils.post_poll(self.thread, self.user)
  515. other_poll = testutils.post_poll(other_thread, self.user)
  516. response = self.client.post(
  517. self.api_link, {
  518. 'other_thread': other_thread.get_absolute_url(),
  519. 'poll': other_poll.pk,
  520. }
  521. )
  522. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  523. # other thread has two posts now
  524. self.assertEqual(other_thread.post_set.count(), 3)
  525. # first thread is gone
  526. with self.assertRaises(Thread.DoesNotExist):
  527. Thread.objects.get(pk=self.thread.pk)
  528. # other poll and its votes are gone
  529. self.assertEqual(Poll.objects.filter(thread=self.thread).count(), 0)
  530. self.assertEqual(PollVote.objects.filter(thread=self.thread).count(), 0)
  531. self.assertEqual(Poll.objects.filter(thread=other_thread).count(), 1)
  532. self.assertEqual(PollVote.objects.filter(thread=other_thread).count(), 4)
  533. Poll.objects.get(pk=other_poll.pk)
  534. with self.assertRaises(Poll.DoesNotExist):
  535. Poll.objects.get(pk=poll.pk)