test_thread_merge_api.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887
  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 and an event 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_best_answer(self):
  378. """api merges two threads successfully, keeping best answer 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. best_answer = testutils.reply_thread(other_thread)
  383. other_thread.set_best_answer(self.user, best_answer)
  384. other_thread.save()
  385. response = self.client.post(
  386. self.api_link, {
  387. 'other_thread': other_thread.get_absolute_url(),
  388. }
  389. )
  390. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  391. # other thread has three posts and an event now
  392. self.assertEqual(other_thread.post_set.count(), 4)
  393. # first thread is gone
  394. with self.assertRaises(Thread.DoesNotExist):
  395. Thread.objects.get(pk=self.thread.pk)
  396. # best answer is kept in other thread
  397. other_thread = Thread.objects.get(pk=other_thread.pk)
  398. self.assertEqual(other_thread.best_answer, best_answer)
  399. def test_merge_threads_moved_best_answer(self):
  400. """api merges two threads successfully, moving best answer to old thread"""
  401. self.override_acl({'can_merge_threads': 1})
  402. self.override_other_acl({'can_merge_threads': 1})
  403. other_thread = testutils.post_thread(self.category_b)
  404. best_answer = testutils.reply_thread(self.thread)
  405. self.thread.set_best_answer(self.user, best_answer)
  406. self.thread.save()
  407. response = self.client.post(
  408. self.api_link, {
  409. 'other_thread': other_thread.get_absolute_url(),
  410. }
  411. )
  412. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  413. # other thread has three posts and an event now
  414. self.assertEqual(other_thread.post_set.count(), 4)
  415. # first thread is gone
  416. with self.assertRaises(Thread.DoesNotExist):
  417. Thread.objects.get(pk=self.thread.pk)
  418. # best answer is kept in other thread
  419. other_thread = Thread.objects.get(pk=other_thread.pk)
  420. self.assertEqual(other_thread.best_answer, best_answer)
  421. def test_merge_threads_merge_conflict_best_answer(self):
  422. """api errors on merge conflict, returning list of available best answers"""
  423. self.override_acl({'can_merge_threads': 1})
  424. self.override_other_acl({'can_merge_threads': 1})
  425. best_answer = testutils.reply_thread(self.thread)
  426. self.thread.set_best_answer(self.user, best_answer)
  427. self.thread.save()
  428. other_thread = testutils.post_thread(self.category_b)
  429. other_best_answer = testutils.reply_thread(other_thread)
  430. other_thread.set_best_answer(self.user, other_best_answer)
  431. other_thread.save()
  432. response = self.client.post(
  433. self.api_link, {
  434. 'other_thread': other_thread.get_absolute_url(),
  435. }
  436. )
  437. self.assertEqual(response.status_code, 400)
  438. self.assertEqual(response.json(), {
  439. 'best_answers': [
  440. ['0', "Unmark all best answers"],
  441. [str(self.thread.id), self.thread.title],
  442. [str(other_thread.id), other_thread.title],
  443. ]
  444. })
  445. # best answers were untouched
  446. self.assertEqual(self.thread.post_set.count(), 2)
  447. self.assertEqual(other_thread.post_set.count(), 2)
  448. self.assertEqual(Thread.objects.get(pk=self.thread.pk).best_answer_id, best_answer.id)
  449. self.assertEqual(
  450. Thread.objects.get(pk=other_thread.pk).best_answer_id, other_best_answer.id)
  451. def test_threads_merge_conflict_best_answer_invalid_resolution(self):
  452. """api errors on invalid merge conflict resolution"""
  453. self.override_acl({'can_merge_threads': 1})
  454. self.override_other_acl({'can_merge_threads': 1})
  455. best_answer = testutils.reply_thread(self.thread)
  456. self.thread.set_best_answer(self.user, best_answer)
  457. self.thread.save()
  458. other_thread = testutils.post_thread(self.category_b)
  459. other_best_answer = testutils.reply_thread(other_thread)
  460. other_thread.set_best_answer(self.user, other_best_answer)
  461. other_thread.save()
  462. response = self.client.post(
  463. self.api_link, {
  464. 'other_thread': other_thread.get_absolute_url(),
  465. 'best_answer': other_thread.id + 10,
  466. }
  467. )
  468. self.assertEqual(response.status_code, 400)
  469. self.assertEqual(response.json(), {
  470. 'best_answer': ["Invalid choice."],
  471. })
  472. # best answers were untouched
  473. self.assertEqual(self.thread.post_set.count(), 2)
  474. self.assertEqual(other_thread.post_set.count(), 2)
  475. self.assertEqual(Thread.objects.get(pk=self.thread.pk).best_answer_id, best_answer.id)
  476. self.assertEqual(
  477. Thread.objects.get(pk=other_thread.pk).best_answer_id, other_best_answer.id)
  478. def test_threads_merge_conflict_unmark_all_best_answers(self):
  479. """api unmarks all best answers when unmark all choice is selected"""
  480. self.override_acl({'can_merge_threads': 1})
  481. self.override_other_acl({'can_merge_threads': 1})
  482. best_answer = testutils.reply_thread(self.thread)
  483. self.thread.set_best_answer(self.user, best_answer)
  484. self.thread.save()
  485. other_thread = testutils.post_thread(self.category_b)
  486. other_best_answer = testutils.reply_thread(other_thread)
  487. other_thread.set_best_answer(self.user, other_best_answer)
  488. other_thread.save()
  489. response = self.client.post(
  490. self.api_link, {
  491. 'other_thread': other_thread.get_absolute_url(),
  492. 'best_answer': 0,
  493. }
  494. )
  495. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  496. # other thread has four posts and an event now
  497. self.assertEqual(other_thread.post_set.count(), 5)
  498. # first thread is gone
  499. with self.assertRaises(Thread.DoesNotExist):
  500. Thread.objects.get(pk=self.thread.pk)
  501. # final thread has no marked best answer
  502. self.assertIsNone(Thread.objects.get(pk=other_thread.pk).best_answer_id)
  503. def test_threads_merge_conflict_keep_first_best_answer(self):
  504. """api unmarks other best answer on merge"""
  505. self.override_acl({'can_merge_threads': 1})
  506. self.override_other_acl({'can_merge_threads': 1})
  507. best_answer = testutils.reply_thread(self.thread)
  508. self.thread.set_best_answer(self.user, best_answer)
  509. self.thread.save()
  510. other_thread = testutils.post_thread(self.category_b)
  511. other_best_answer = testutils.reply_thread(other_thread)
  512. other_thread.set_best_answer(self.user, other_best_answer)
  513. other_thread.save()
  514. response = self.client.post(
  515. self.api_link, {
  516. 'other_thread': other_thread.get_absolute_url(),
  517. 'best_answer': self.thread.pk,
  518. }
  519. )
  520. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  521. # other thread has four posts and an event now
  522. self.assertEqual(other_thread.post_set.count(), 5)
  523. # first thread is gone
  524. with self.assertRaises(Thread.DoesNotExist):
  525. Thread.objects.get(pk=self.thread.pk)
  526. # other thread's best answer was unchanged
  527. self.assertEqual(Thread.objects.get(pk=other_thread.pk).best_answer_id, best_answer.id)
  528. def test_threads_merge_conflict_keep_other_best_answer(self):
  529. """api unmarks first best answer on merge"""
  530. self.override_acl({'can_merge_threads': 1})
  531. self.override_other_acl({'can_merge_threads': 1})
  532. best_answer = testutils.reply_thread(self.thread)
  533. self.thread.set_best_answer(self.user, best_answer)
  534. self.thread.save()
  535. other_thread = testutils.post_thread(self.category_b)
  536. other_best_answer = testutils.reply_thread(other_thread)
  537. other_thread.set_best_answer(self.user, other_best_answer)
  538. other_thread.save()
  539. response = self.client.post(
  540. self.api_link, {
  541. 'other_thread': other_thread.get_absolute_url(),
  542. 'best_answer': other_thread.pk,
  543. }
  544. )
  545. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  546. # other thread has four posts and an event now
  547. self.assertEqual(other_thread.post_set.count(), 5)
  548. # first thread is gone
  549. with self.assertRaises(Thread.DoesNotExist):
  550. Thread.objects.get(pk=self.thread.pk)
  551. # other thread's best answer was changed to merged in thread's answer
  552. self.assertEqual(
  553. Thread.objects.get(pk=other_thread.pk).best_answer_id, other_best_answer.id)
  554. def test_merge_threads_kept_poll(self):
  555. """api merges two threads successfully, keeping poll from other thread"""
  556. self.override_acl({'can_merge_threads': 1})
  557. self.override_other_acl({'can_merge_threads': 1})
  558. other_thread = testutils.post_thread(self.category_b)
  559. poll = testutils.post_poll(other_thread, self.user)
  560. response = self.client.post(
  561. self.api_link, {
  562. 'other_thread': other_thread.get_absolute_url(),
  563. }
  564. )
  565. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  566. # other thread has two posts and an event now
  567. self.assertEqual(other_thread.post_set.count(), 3)
  568. # first thread is gone
  569. with self.assertRaises(Thread.DoesNotExist):
  570. Thread.objects.get(pk=self.thread.pk)
  571. # poll and its votes were kept
  572. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=other_thread).count(), 1)
  573. self.assertEqual(PollVote.objects.filter(poll=poll, thread=other_thread).count(), 4)
  574. def test_merge_threads_moved_poll(self):
  575. """api merges two threads successfully, moving poll from old thread"""
  576. self.override_acl({'can_merge_threads': 1})
  577. self.override_other_acl({'can_merge_threads': 1})
  578. other_thread = testutils.post_thread(self.category_b)
  579. poll = testutils.post_poll(self.thread, self.user)
  580. response = self.client.post(
  581. self.api_link, {
  582. 'other_thread': other_thread.get_absolute_url(),
  583. }
  584. )
  585. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  586. # other thread has two posts and an event now
  587. self.assertEqual(other_thread.post_set.count(), 3)
  588. # first thread is gone
  589. with self.assertRaises(Thread.DoesNotExist):
  590. Thread.objects.get(pk=self.thread.pk)
  591. # poll and its votes were moved
  592. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=other_thread).count(), 1)
  593. self.assertEqual(PollVote.objects.filter(poll=poll, thread=other_thread).count(), 4)
  594. def test_threads_merge_conflict_polls(self):
  595. """api errors on merge conflict, returning list of available polls"""
  596. self.override_acl({'can_merge_threads': 1})
  597. self.override_other_acl({'can_merge_threads': 1})
  598. other_thread = testutils.post_thread(self.category_b)
  599. poll = testutils.post_poll(self.thread, self.user)
  600. other_poll = testutils.post_poll(other_thread, self.user)
  601. response = self.client.post(
  602. self.api_link, {
  603. 'other_thread': other_thread.get_absolute_url(),
  604. }
  605. )
  606. self.assertEqual(response.status_code, 400)
  607. self.assertEqual(
  608. response.json(), {
  609. 'polls': [
  610. ['0', "Delete all polls"],
  611. [
  612. str(poll.pk),
  613. u'{} ({})'.format(poll.question, poll.thread.title),
  614. ],
  615. [
  616. str(other_poll.pk),
  617. u'{} ({})'.format(other_poll.question, other_poll.thread.title),
  618. ],
  619. ]
  620. }
  621. )
  622. # polls and votes were untouched
  623. self.assertEqual(Poll.objects.count(), 2)
  624. self.assertEqual(PollVote.objects.count(), 8)
  625. def test_threads_merge_conflict_poll_invalid_resolution(self):
  626. """api errors on invalid merge conflict resolution"""
  627. self.override_acl({'can_merge_threads': 1})
  628. self.override_other_acl({'can_merge_threads': 1})
  629. other_thread = testutils.post_thread(self.category_b)
  630. testutils.post_poll(self.thread, self.user)
  631. testutils.post_poll(other_thread, self.user)
  632. response = self.client.post(
  633. self.api_link, {
  634. 'other_thread': other_thread.get_absolute_url(),
  635. 'poll': Poll.objects.all()[0].pk + 10,
  636. }
  637. )
  638. self.assertEqual(response.status_code, 400)
  639. self.assertEqual(response.json(), {'poll': ["Invalid choice."]})
  640. # polls and votes were untouched
  641. self.assertEqual(Poll.objects.count(), 2)
  642. self.assertEqual(PollVote.objects.count(), 8)
  643. def test_threads_merge_conflict_delete_all_polls(self):
  644. """api deletes all polls when delete all choice is selected"""
  645. self.override_acl({'can_merge_threads': 1})
  646. self.override_other_acl({'can_merge_threads': 1})
  647. other_thread = testutils.post_thread(self.category_b)
  648. testutils.post_poll(self.thread, self.user)
  649. testutils.post_poll(other_thread, self.user)
  650. response = self.client.post(
  651. self.api_link, {
  652. 'other_thread': other_thread.get_absolute_url(),
  653. 'poll': 0,
  654. }
  655. )
  656. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  657. # other thread has two posts and an event now
  658. self.assertEqual(other_thread.post_set.count(), 3)
  659. # first thread is gone
  660. with self.assertRaises(Thread.DoesNotExist):
  661. Thread.objects.get(pk=self.thread.pk)
  662. # polls and votes are gone
  663. self.assertEqual(Poll.objects.count(), 0)
  664. self.assertEqual(PollVote.objects.count(), 0)
  665. def test_threads_merge_conflict_keep_first_poll(self):
  666. """api deletes other poll on merge"""
  667. self.override_acl({'can_merge_threads': 1})
  668. self.override_other_acl({'can_merge_threads': 1})
  669. other_thread = testutils.post_thread(self.category_b)
  670. poll = testutils.post_poll(self.thread, self.user)
  671. other_poll = testutils.post_poll(other_thread, self.user)
  672. response = self.client.post(
  673. self.api_link, {
  674. 'other_thread': other_thread.get_absolute_url(),
  675. 'poll': poll.pk,
  676. }
  677. )
  678. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  679. # other thread has two posts and an event now
  680. self.assertEqual(other_thread.post_set.count(), 3)
  681. # first thread is gone
  682. with self.assertRaises(Thread.DoesNotExist):
  683. Thread.objects.get(pk=self.thread.pk)
  684. # other poll and its votes are gone
  685. self.assertEqual(Poll.objects.filter(thread=self.thread).count(), 0)
  686. self.assertEqual(PollVote.objects.filter(thread=self.thread).count(), 0)
  687. self.assertEqual(Poll.objects.filter(thread=other_thread).count(), 1)
  688. self.assertEqual(PollVote.objects.filter(thread=other_thread).count(), 4)
  689. Poll.objects.get(pk=poll.pk)
  690. with self.assertRaises(Poll.DoesNotExist):
  691. Poll.objects.get(pk=other_poll.pk)
  692. def test_threads_merge_conflict_keep_other_poll(self):
  693. """api deletes first poll on merge"""
  694. self.override_acl({'can_merge_threads': 1})
  695. self.override_other_acl({'can_merge_threads': 1})
  696. other_thread = testutils.post_thread(self.category_b)
  697. poll = testutils.post_poll(self.thread, self.user)
  698. other_poll = testutils.post_poll(other_thread, self.user)
  699. response = self.client.post(
  700. self.api_link, {
  701. 'other_thread': other_thread.get_absolute_url(),
  702. 'poll': other_poll.pk,
  703. }
  704. )
  705. self.assertContains(response, other_thread.get_absolute_url(), status_code=200)
  706. # other thread has two posts and an event now
  707. self.assertEqual(other_thread.post_set.count(), 3)
  708. # first thread is gone
  709. with self.assertRaises(Thread.DoesNotExist):
  710. Thread.objects.get(pk=self.thread.pk)
  711. # other poll and its votes are gone
  712. self.assertEqual(Poll.objects.filter(thread=self.thread).count(), 0)
  713. self.assertEqual(PollVote.objects.filter(thread=self.thread).count(), 0)
  714. self.assertEqual(Poll.objects.filter(thread=other_thread).count(), 1)
  715. self.assertEqual(PollVote.objects.filter(thread=other_thread).count(), 4)
  716. Poll.objects.get(pk=other_poll.pk)
  717. with self.assertRaises(Poll.DoesNotExist):
  718. Poll.objects.get(pk=poll.pk)