test_threads_merge_api.py 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021
  1. import json
  2. from django.urls import reverse
  3. from misago.acl import useracl
  4. from misago.acl.objectacl import add_acl_to_obj
  5. from misago.categories.models import Category
  6. from misago.conftest import get_cache_versions
  7. from misago.readtracker import poststracker
  8. from misago.threads import testutils
  9. from misago.threads.models import Poll, PollVote, Post, Thread
  10. from misago.threads.serializers import ThreadsListSerializer
  11. from misago.threads.serializers.moderation import THREADS_LIMIT
  12. from misago.threads.test import patch_category_acl, patch_other_category_acl
  13. from .test_threads_api import ThreadsApiTestCase
  14. cache_versions = get_cache_versions()
  15. class ThreadsMergeApiTests(ThreadsApiTestCase):
  16. def setUp(self):
  17. super().setUp()
  18. self.api_link = reverse('misago:api:thread-merge')
  19. Category(
  20. name='Other Category',
  21. slug='other-category',
  22. ).insert_at(
  23. self.category,
  24. position='last-child',
  25. save=True,
  26. )
  27. self.other_category = Category.objects.get(slug='other-category')
  28. def test_merge_no_threads(self):
  29. """api validates if we are trying to merge no threads"""
  30. response = self.client.post(self.api_link, content_type="application/json")
  31. self.assertEqual(response.status_code, 403)
  32. self.assertEqual(
  33. response.json(), {
  34. 'detail': "You have to select at least two threads to merge.",
  35. }
  36. )
  37. def test_merge_empty_threads(self):
  38. """api validates if we are trying to empty threads list"""
  39. response = self.client.post(
  40. self.api_link,
  41. json.dumps({
  42. 'threads': [],
  43. }),
  44. content_type="application/json",
  45. )
  46. self.assertEqual(response.status_code, 403)
  47. self.assertEqual(
  48. response.json(), {
  49. 'detail': "You have to select at least two threads to merge.",
  50. }
  51. )
  52. def test_merge_invalid_threads(self):
  53. """api validates if we are trying to merge invalid thread ids"""
  54. response = self.client.post(
  55. self.api_link,
  56. json.dumps({
  57. 'threads': 'abcd',
  58. }),
  59. content_type="application/json",
  60. )
  61. self.assertEqual(response.status_code, 403)
  62. self.assertEqual(response.json(), {
  63. "detail": 'Expected a list of items but got type "str".',
  64. })
  65. response = self.client.post(
  66. self.api_link,
  67. json.dumps({
  68. 'threads': ['a', '-', 'c'],
  69. }),
  70. content_type="application/json",
  71. )
  72. self.assertEqual(response.status_code, 403)
  73. self.assertEqual(
  74. response.json(), {
  75. 'detail': "One or more thread ids received were invalid.",
  76. }
  77. )
  78. def test_merge_single_thread(self):
  79. """api validates if we are trying to merge single thread"""
  80. response = self.client.post(
  81. self.api_link,
  82. json.dumps({
  83. 'threads': [self.thread.id],
  84. }),
  85. content_type="application/json",
  86. )
  87. self.assertEqual(response.status_code, 403)
  88. self.assertEqual(
  89. response.json(), {
  90. 'detail': "You have to select at least two threads to merge.",
  91. }
  92. )
  93. def test_merge_with_nonexisting_thread(self):
  94. """api validates if we are trying to merge with invalid thread"""
  95. response = self.client.post(
  96. self.api_link,
  97. json.dumps({
  98. 'threads': [self.thread.id, self.thread.id + 1000],
  99. }),
  100. content_type="application/json",
  101. )
  102. self.assertEqual(response.status_code, 403)
  103. self.assertEqual(
  104. response.json(), {
  105. 'detail': "One or more threads to merge could not be found.",
  106. }
  107. )
  108. def test_merge_with_invisible_thread(self):
  109. """api validates if we are trying to merge with inaccesible thread"""
  110. unaccesible_thread = testutils.post_thread(category=self.other_category)
  111. response = self.client.post(
  112. self.api_link,
  113. json.dumps({
  114. 'threads': [self.thread.id, unaccesible_thread.id],
  115. }),
  116. content_type="application/json",
  117. )
  118. self.assertEqual(response.status_code, 403)
  119. self.assertEqual(
  120. response.json(), {
  121. 'detail': "One or more threads to merge could not be found.",
  122. }
  123. )
  124. def test_merge_no_permission(self):
  125. """api validates permission to merge threads"""
  126. thread = testutils.post_thread(category=self.category)
  127. response = self.client.post(
  128. self.api_link,
  129. json.dumps({
  130. 'category': self.category.id,
  131. 'title': 'Lorem ipsum dolor',
  132. 'threads': [self.thread.id, thread.id],
  133. }),
  134. content_type="application/json",
  135. )
  136. self.assertEqual(response.status_code, 403)
  137. self.assertEqual(
  138. response.json(), [
  139. {
  140. 'id': thread.pk,
  141. 'title': thread.title,
  142. 'errors': ["You can't merge threads in this category."],
  143. },
  144. {
  145. 'id': self.thread.pk,
  146. 'title': self.thread.title,
  147. 'errors': ["You can't merge threads in this category."],
  148. },
  149. ]
  150. )
  151. @patch_other_category_acl()
  152. @patch_category_acl({"can_merge_threads": True, "can_close_threads": False})
  153. def test_thread_category_is_closed(self):
  154. """api validates if thread's category is open"""
  155. other_thread = testutils.post_thread(self.category)
  156. self.category.is_closed = True
  157. self.category.save()
  158. response = self.client.post(
  159. self.api_link,
  160. json.dumps({
  161. 'category': self.other_category.id,
  162. 'title': 'Lorem ipsum dolor',
  163. 'threads': [self.thread.id, other_thread.id],
  164. }),
  165. content_type="application/json",
  166. )
  167. self.assertEqual(response.status_code, 403)
  168. self.assertEqual(response.json(), [
  169. {
  170. "id": other_thread.id,
  171. "title": other_thread.title,
  172. "errors": ["This category is closed. You can't merge it's threads."],
  173. },
  174. {
  175. "id": self.thread.id,
  176. "title": self.thread.title,
  177. "errors": ["This category is closed. You can't merge it's threads."],
  178. },
  179. ])
  180. @patch_other_category_acl()
  181. @patch_category_acl({"can_merge_threads": True, "can_close_threads": False})
  182. def test_thread_is_closed(self):
  183. """api validates if thread is open"""
  184. other_thread = testutils.post_thread(self.category)
  185. other_thread.is_closed = True
  186. other_thread.save()
  187. response = self.client.post(
  188. self.api_link,
  189. json.dumps({
  190. 'category': self.other_category.id,
  191. 'title': 'Lorem ipsum dolor',
  192. 'threads': [self.thread.id, other_thread.id],
  193. }),
  194. content_type="application/json",
  195. )
  196. self.assertEqual(response.status_code, 403)
  197. self.assertEqual(response.json(), [
  198. {
  199. "id": other_thread.id,
  200. "title": other_thread.title,
  201. "errors": ["This thread is closed. You can't merge it with other threads."],
  202. },
  203. ])
  204. @patch_category_acl({"can_merge_threads": True})
  205. def test_merge_too_many_threads(self):
  206. """api rejects too many threads to merge"""
  207. threads = []
  208. for _ in range(THREADS_LIMIT + 1):
  209. threads.append(testutils.post_thread(category=self.category).pk)
  210. response = self.client.post(
  211. self.api_link,
  212. json.dumps({
  213. 'threads': threads,
  214. }),
  215. content_type="application/json",
  216. )
  217. self.assertEqual(response.status_code, 403)
  218. self.assertEqual(
  219. response.json(), {
  220. 'detail': "No more than %s threads can be merged at single time." % THREADS_LIMIT,
  221. }
  222. )
  223. @patch_category_acl({"can_merge_threads": True})
  224. def test_merge_no_final_thread(self):
  225. """api rejects merge because no data to merge threads was specified"""
  226. thread = testutils.post_thread(category=self.category)
  227. response = self.client.post(
  228. self.api_link,
  229. json.dumps({
  230. 'threads': [self.thread.id, thread.id],
  231. }),
  232. content_type="application/json",
  233. )
  234. self.assertEqual(response.status_code, 400)
  235. self.assertEqual(
  236. response.json(), {
  237. 'title': ['This field is required.'],
  238. 'category': ['This field is required.'],
  239. }
  240. )
  241. @patch_category_acl({"can_merge_threads": True})
  242. def test_merge_invalid_final_title(self):
  243. """api rejects merge because final thread title was invalid"""
  244. thread = testutils.post_thread(category=self.category)
  245. response = self.client.post(
  246. self.api_link,
  247. json.dumps({
  248. 'threads': [self.thread.id, thread.id],
  249. 'title': '$$$',
  250. 'category': self.category.id,
  251. }),
  252. content_type="application/json",
  253. )
  254. self.assertEqual(response.status_code, 400)
  255. self.assertEqual(
  256. response.json(), {
  257. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  258. }
  259. )
  260. @patch_category_acl({"can_merge_threads": True})
  261. def test_merge_invalid_category(self):
  262. """api rejects merge because final category was invalid"""
  263. thread = testutils.post_thread(category=self.category)
  264. response = self.client.post(
  265. self.api_link,
  266. json.dumps({
  267. 'threads': [self.thread.id, thread.id],
  268. 'title': 'Valid thread title',
  269. 'category': self.other_category.id,
  270. }),
  271. content_type="application/json",
  272. )
  273. self.assertEqual(response.status_code, 400)
  274. self.assertEqual(
  275. response.json(), {
  276. 'category': ["Requested category could not be found."],
  277. }
  278. )
  279. @patch_category_acl({"can_merge_threads": True, "can_start_threads": False})
  280. def test_merge_unallowed_start_thread(self):
  281. """api rejects merge because category isn't allowing starting threads"""
  282. thread = testutils.post_thread(category=self.category)
  283. response = self.client.post(
  284. self.api_link,
  285. json.dumps({
  286. 'threads': [self.thread.id, thread.id],
  287. 'title': 'Valid thread title',
  288. 'category': self.category.id,
  289. }),
  290. content_type="application/json",
  291. )
  292. self.assertEqual(response.status_code, 400)
  293. self.assertEqual(
  294. response.json(), {
  295. 'category': ["You can't create new threads in selected category."],
  296. }
  297. )
  298. @patch_category_acl({"can_merge_threads": True})
  299. def test_merge_invalid_weight(self):
  300. """api rejects merge because final weight was invalid"""
  301. thread = testutils.post_thread(category=self.category)
  302. response = self.client.post(
  303. self.api_link,
  304. json.dumps({
  305. 'threads': [self.thread.id, thread.id],
  306. 'title': 'Valid thread title',
  307. 'category': self.category.id,
  308. 'weight': 4,
  309. }),
  310. content_type="application/json",
  311. )
  312. self.assertEqual(response.status_code, 400)
  313. self.assertEqual(
  314. response.json(), {
  315. 'weight': ["Ensure this value is less than or equal to 2."],
  316. }
  317. )
  318. @patch_category_acl({"can_merge_threads": True})
  319. def test_merge_unallowed_global_weight(self):
  320. """api rejects merge because global weight was unallowed"""
  321. thread = testutils.post_thread(category=self.category)
  322. response = self.client.post(
  323. self.api_link,
  324. json.dumps({
  325. 'threads': [self.thread.id, thread.id],
  326. 'title': 'Valid thread title',
  327. 'category': self.category.id,
  328. 'weight': 2,
  329. }),
  330. content_type="application/json",
  331. )
  332. self.assertEqual(response.status_code, 400)
  333. self.assertEqual(
  334. response.json(), {
  335. 'weight': ["You don't have permission to pin threads globally in this category."],
  336. }
  337. )
  338. @patch_category_acl({"can_merge_threads": True})
  339. def test_merge_unallowed_local_weight(self):
  340. """api rejects merge because local weight was unallowed"""
  341. thread = testutils.post_thread(category=self.category)
  342. response = self.client.post(
  343. self.api_link,
  344. json.dumps({
  345. 'threads': [self.thread.id, thread.id],
  346. 'title': 'Valid thread title',
  347. 'category': self.category.id,
  348. 'weight': 1,
  349. }),
  350. content_type="application/json",
  351. )
  352. self.assertEqual(response.status_code, 400)
  353. self.assertEqual(
  354. response.json(), {
  355. 'weight': ["You don't have permission to pin threads in this category."],
  356. }
  357. )
  358. @patch_category_acl({"can_merge_threads": True, "can_pin_threads": 1})
  359. def test_merge_allowed_local_weight(self):
  360. """api allows local weight"""
  361. thread = testutils.post_thread(category=self.category)
  362. response = self.client.post(
  363. self.api_link,
  364. json.dumps({
  365. 'threads': [self.thread.id, thread.id],
  366. 'title': '$$$',
  367. 'category': self.category.id,
  368. 'weight': 1,
  369. }),
  370. content_type="application/json",
  371. )
  372. self.assertEqual(response.status_code, 400)
  373. self.assertEqual(
  374. response.json(), {
  375. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  376. }
  377. )
  378. @patch_category_acl({"can_merge_threads": True, "can_pin_threads": 2})
  379. def test_merge_allowed_global_weight(self):
  380. """api allows global weight"""
  381. thread = testutils.post_thread(category=self.category)
  382. response = self.client.post(
  383. self.api_link,
  384. json.dumps({
  385. 'threads': [self.thread.id, thread.id],
  386. 'title': '$$$',
  387. 'category': self.category.id,
  388. 'weight': 2,
  389. }),
  390. content_type="application/json",
  391. )
  392. self.assertEqual(response.status_code, 400)
  393. self.assertEqual(
  394. response.json(), {
  395. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  396. }
  397. )
  398. @patch_category_acl({"can_merge_threads": True, "can_close_threads": False})
  399. def test_merge_unallowed_close(self):
  400. """api rejects merge because closing thread was unallowed"""
  401. thread = testutils.post_thread(category=self.category)
  402. response = self.client.post(
  403. self.api_link,
  404. json.dumps({
  405. 'threads': [self.thread.id, thread.id],
  406. 'title': 'Valid thread title',
  407. 'category': self.category.id,
  408. 'is_closed': True,
  409. }),
  410. content_type="application/json",
  411. )
  412. self.assertEqual(response.status_code, 400)
  413. self.assertEqual(
  414. response.json(), {
  415. 'is_closed': ["You don't have permission to close threads in this category."],
  416. }
  417. )
  418. @patch_category_acl({"can_merge_threads": True, "can_close_threads": True})
  419. def test_merge_with_close(self):
  420. """api allows for closing thread"""
  421. thread = testutils.post_thread(category=self.category)
  422. response = self.client.post(
  423. self.api_link,
  424. json.dumps({
  425. 'threads': [self.thread.id, thread.id],
  426. 'title': '$$$',
  427. 'category': self.category.id,
  428. 'weight': 0,
  429. 'is_closed': True,
  430. }),
  431. content_type="application/json",
  432. )
  433. self.assertEqual(response.status_code, 400)
  434. self.assertEqual(
  435. response.json(), {
  436. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  437. }
  438. )
  439. @patch_category_acl({"can_merge_threads": True, "can_hide_threads": 0})
  440. def test_merge_unallowed_hidden(self):
  441. """api rejects merge because hidden thread was unallowed"""
  442. thread = testutils.post_thread(category=self.category)
  443. response = self.client.post(
  444. self.api_link,
  445. json.dumps({
  446. 'threads': [self.thread.id, thread.id],
  447. 'title': 'Valid thread title',
  448. 'category': self.category.id,
  449. 'is_hidden': True,
  450. }),
  451. content_type="application/json",
  452. )
  453. self.assertEqual(response.status_code, 400)
  454. self.assertEqual(
  455. response.json(), {
  456. 'is_hidden': ["You don't have permission to hide threads in this category."],
  457. }
  458. )
  459. @patch_category_acl({"can_merge_threads": True, "can_hide_threads": 1})
  460. def test_merge_with_hide(self):
  461. """api allows for hiding thread"""
  462. thread = testutils.post_thread(category=self.category)
  463. response = self.client.post(
  464. self.api_link,
  465. json.dumps({
  466. 'threads': [self.thread.id, thread.id],
  467. 'title': '$$$',
  468. 'category': self.category.id,
  469. 'weight': 0,
  470. 'is_hidden': True,
  471. }),
  472. content_type="application/json",
  473. )
  474. self.assertEqual(response.status_code, 400)
  475. self.assertEqual(
  476. response.json(), {
  477. 'title': ["Thread title should be at least 5 characters long (it has 3)."],
  478. }
  479. )
  480. @patch_category_acl({"can_merge_threads": True})
  481. def test_merge(self):
  482. """api performs basic merge"""
  483. posts_ids = [p.id for p in Post.objects.all()]
  484. thread = testutils.post_thread(category=self.category)
  485. response = self.client.post(
  486. self.api_link,
  487. json.dumps({
  488. 'threads': [self.thread.id, thread.id],
  489. 'title': 'Merged thread!',
  490. 'category': self.category.id,
  491. }),
  492. content_type="application/json",
  493. )
  494. self.assertEqual(response.status_code, 200)
  495. # is response json with new thread?
  496. response_json = response.json()
  497. new_thread = Thread.objects.get(pk=response_json['id'])
  498. new_thread.is_read = False
  499. new_thread.subscription = None
  500. user_acl = useracl.get_user_acl(self.user, cache_versions)
  501. add_acl_to_obj(user_acl, new_thread.category)
  502. add_acl_to_obj(user_acl, new_thread)
  503. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  504. # did posts move to new thread?
  505. for post in Post.objects.filter(id__in=posts_ids):
  506. self.assertEqual(post.thread_id, new_thread.id)
  507. # are old threads gone?
  508. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  509. @patch_category_acl({
  510. "can_merge_threads": True,
  511. "can_close_threads": True,
  512. "can_hide_threads": 1,
  513. "can_pin_threads": 2,
  514. })
  515. def test_merge_kitchensink(self):
  516. """api performs merge"""
  517. posts_ids = [p.id for p in Post.objects.all()]
  518. thread = testutils.post_thread(category=self.category)
  519. poststracker.save_read(self.user, self.thread.first_post)
  520. poststracker.save_read(self.user, thread.first_post)
  521. self.user.subscription_set.create(
  522. thread=self.thread,
  523. category=self.thread.category,
  524. last_read_on=self.thread.last_post_on,
  525. send_email=False,
  526. )
  527. self.user.subscription_set.create(
  528. thread=thread,
  529. category=thread.category,
  530. last_read_on=thread.last_post_on,
  531. send_email=False,
  532. )
  533. response = self.client.post(
  534. self.api_link,
  535. json.dumps({
  536. 'threads': [self.thread.id, thread.id],
  537. 'title': 'Merged thread!',
  538. 'category': self.category.id,
  539. 'is_closed': 1,
  540. 'is_hidden': 1,
  541. 'weight': 2,
  542. }),
  543. content_type="application/json",
  544. )
  545. self.assertEqual(response.status_code, 200)
  546. # is response json with new thread?
  547. response_json = response.json()
  548. new_thread = Thread.objects.get(pk=response_json['id'])
  549. new_thread.is_read = False
  550. new_thread.subscription = None
  551. self.assertEqual(new_thread.weight, 2)
  552. self.assertTrue(new_thread.is_closed)
  553. self.assertTrue(new_thread.is_hidden)
  554. user_acl = useracl.get_user_acl(self.user, cache_versions)
  555. add_acl_to_obj(user_acl, new_thread.category)
  556. add_acl_to_obj(user_acl, new_thread)
  557. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  558. # did posts move to new thread?
  559. for post in Post.objects.filter(id__in=posts_ids):
  560. self.assertEqual(post.thread_id, new_thread.id)
  561. # are old threads gone?
  562. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  563. # posts reads are kept
  564. postreads = self.user.postread_set.filter(post__is_event=False).order_by('id')
  565. self.assertEqual(
  566. list(postreads.values_list('post_id', flat=True)),
  567. [self.thread.first_post_id, thread.first_post_id],
  568. )
  569. self.assertEqual(postreads.filter(thread=new_thread).count(), 2)
  570. self.assertEqual(postreads.filter(category=self.category).count(), 2)
  571. # subscriptions are kept
  572. self.assertEqual(self.user.subscription_set.count(), 1)
  573. self.user.subscription_set.get(thread=new_thread)
  574. self.user.subscription_set.get(category=self.category)
  575. @patch_category_acl({"can_merge_threads": True})
  576. def test_merge_threads_merged_best_answer(self):
  577. """api merges two threads successfully, moving best answer to old thread"""
  578. other_thread = testutils.post_thread(self.category)
  579. best_answer = testutils.reply_thread(self.thread)
  580. self.thread.set_best_answer(self.user, best_answer)
  581. self.thread.save()
  582. response = self.client.post(
  583. self.api_link,
  584. json.dumps({
  585. 'threads': [self.thread.id, other_thread.id],
  586. 'title': 'Merged thread!',
  587. 'category': self.category.id,
  588. }),
  589. content_type="application/json",
  590. )
  591. self.assertEqual(response.status_code, 200)
  592. # best answer is set on new thread
  593. new_thread = Thread.objects.get(pk=response.json()['id'])
  594. self.assertEqual(new_thread.best_answer_id, best_answer.id)
  595. @patch_category_acl({"can_merge_threads": True})
  596. def test_merge_threads_merge_conflict_best_answer(self):
  597. """api errors on merge conflict, returning list of available best answers"""
  598. best_answer = testutils.reply_thread(self.thread)
  599. self.thread.set_best_answer(self.user, best_answer)
  600. self.thread.save()
  601. other_thread = testutils.post_thread(self.category)
  602. other_best_answer = testutils.reply_thread(other_thread)
  603. other_thread.set_best_answer(self.user, other_best_answer)
  604. other_thread.save()
  605. response = self.client.post(
  606. self.api_link,
  607. json.dumps({
  608. 'threads': [self.thread.id, other_thread.id],
  609. 'title': 'Merged thread!',
  610. 'category': self.category.id,
  611. }),
  612. content_type="application/json",
  613. )
  614. self.assertEqual(response.status_code, 400)
  615. self.assertEqual(response.json(), {
  616. 'best_answers': [
  617. ['0', "Unmark all best answers"],
  618. [str(self.thread.id), self.thread.title],
  619. [str(other_thread.id), other_thread.title],
  620. ]
  621. })
  622. # best answers were untouched
  623. self.assertEqual(self.thread.post_set.count(), 2)
  624. self.assertEqual(other_thread.post_set.count(), 2)
  625. self.assertEqual(Thread.objects.get(pk=self.thread.pk).best_answer_id, best_answer.id)
  626. self.assertEqual(
  627. Thread.objects.get(pk=other_thread.pk).best_answer_id, other_best_answer.id)
  628. @patch_category_acl({"can_merge_threads": True})
  629. def test_threads_merge_conflict_best_answer_invalid_resolution(self):
  630. """api errors on invalid merge conflict resolution"""
  631. best_answer = testutils.reply_thread(self.thread)
  632. self.thread.set_best_answer(self.user, best_answer)
  633. self.thread.save()
  634. other_thread = testutils.post_thread(self.category)
  635. other_best_answer = testutils.reply_thread(other_thread)
  636. other_thread.set_best_answer(self.user, other_best_answer)
  637. other_thread.save()
  638. response = self.client.post(
  639. self.api_link,
  640. json.dumps({
  641. 'threads': [self.thread.id, other_thread.id],
  642. 'title': 'Merged thread!',
  643. 'category': self.category.id,
  644. 'best_answer': other_thread.id + 10,
  645. }),
  646. content_type="application/json",
  647. )
  648. self.assertEqual(response.status_code, 400)
  649. self.assertEqual(response.json(), {'best_answer': ["Invalid choice."]})
  650. # best answers were untouched
  651. self.assertEqual(self.thread.post_set.count(), 2)
  652. self.assertEqual(other_thread.post_set.count(), 2)
  653. self.assertEqual(Thread.objects.get(pk=self.thread.pk).best_answer_id, best_answer.id)
  654. self.assertEqual(
  655. Thread.objects.get(pk=other_thread.pk).best_answer_id, other_best_answer.id)
  656. @patch_category_acl({"can_merge_threads": True})
  657. def test_threads_merge_conflict_unmark_all_best_answers(self):
  658. """api unmarks all best answers when unmark all choice is selected"""
  659. best_answer = testutils.reply_thread(self.thread)
  660. self.thread.set_best_answer(self.user, best_answer)
  661. self.thread.save()
  662. other_thread = testutils.post_thread(self.category)
  663. other_best_answer = testutils.reply_thread(other_thread)
  664. other_thread.set_best_answer(self.user, other_best_answer)
  665. other_thread.save()
  666. response = self.client.post(
  667. self.api_link,
  668. json.dumps({
  669. 'threads': [self.thread.id, other_thread.id],
  670. 'title': 'Merged thread!',
  671. 'category': self.category.id,
  672. 'best_answer': 0,
  673. }),
  674. content_type="application/json",
  675. )
  676. self.assertEqual(response.status_code, 200)
  677. # best answer is not set on new thread
  678. new_thread = Thread.objects.get(pk=response.json()['id'])
  679. self.assertFalse(new_thread.has_best_answer)
  680. self.assertIsNone(new_thread.best_answer_id)
  681. @patch_category_acl({"can_merge_threads": True})
  682. def test_threads_merge_conflict_keep_first_best_answer(self):
  683. """api unmarks other best answer on merge"""
  684. best_answer = testutils.reply_thread(self.thread)
  685. self.thread.set_best_answer(self.user, best_answer)
  686. self.thread.save()
  687. other_thread = testutils.post_thread(self.category)
  688. other_best_answer = testutils.reply_thread(other_thread)
  689. other_thread.set_best_answer(self.user, other_best_answer)
  690. other_thread.save()
  691. response = self.client.post(
  692. self.api_link,
  693. json.dumps({
  694. 'threads': [self.thread.id, other_thread.id],
  695. 'title': 'Merged thread!',
  696. 'category': self.category.id,
  697. 'best_answer': self.thread.pk,
  698. }),
  699. content_type="application/json",
  700. )
  701. self.assertEqual(response.status_code, 200)
  702. # selected best answer is set on new thread
  703. new_thread = Thread.objects.get(pk=response.json()['id'])
  704. self.assertEqual(new_thread.best_answer_id, best_answer.id)
  705. @patch_category_acl({"can_merge_threads": True})
  706. def test_threads_merge_conflict_keep_other_best_answer(self):
  707. """api unmarks first best answer on merge"""
  708. best_answer = testutils.reply_thread(self.thread)
  709. self.thread.set_best_answer(self.user, best_answer)
  710. self.thread.save()
  711. other_thread = testutils.post_thread(self.category)
  712. other_best_answer = testutils.reply_thread(other_thread)
  713. other_thread.set_best_answer(self.user, other_best_answer)
  714. other_thread.save()
  715. response = self.client.post(
  716. self.api_link,
  717. json.dumps({
  718. 'threads': [self.thread.id, other_thread.id],
  719. 'title': 'Merged thread!',
  720. 'category': self.category.id,
  721. 'best_answer': other_thread.pk,
  722. }),
  723. content_type="application/json",
  724. )
  725. self.assertEqual(response.status_code, 200)
  726. # selected best answer is set on new thread
  727. new_thread = Thread.objects.get(pk=response.json()['id'])
  728. self.assertEqual(new_thread.best_answer_id, other_best_answer.id)
  729. @patch_category_acl({"can_merge_threads": True})
  730. def test_merge_threads_kept_poll(self):
  731. """api merges two threads successfully, keeping poll from other thread"""
  732. other_thread = testutils.post_thread(self.category)
  733. poll = testutils.post_poll(other_thread, self.user)
  734. response = self.client.post(
  735. self.api_link,
  736. json.dumps({
  737. 'threads': [self.thread.id, other_thread.id],
  738. 'title': 'Merged thread!',
  739. 'category': self.category.id,
  740. }),
  741. content_type="application/json",
  742. )
  743. self.assertEqual(response.status_code, 200)
  744. new_thread = Thread.objects.get(pk=response.json()['id'])
  745. # poll and its votes were kept
  746. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=new_thread).count(), 1)
  747. self.assertEqual(PollVote.objects.filter(poll=poll, thread=new_thread).count(), 4)
  748. self.assertEqual(Poll.objects.count(), 1)
  749. self.assertEqual(PollVote.objects.count(), 4)
  750. @patch_category_acl({"can_merge_threads": True})
  751. def test_merge_threads_moved_poll(self):
  752. """api merges two threads successfully, moving poll from old thread"""
  753. other_thread = testutils.post_thread(self.category)
  754. poll = testutils.post_poll(self.thread, self.user)
  755. response = self.client.post(
  756. self.api_link,
  757. json.dumps({
  758. 'threads': [self.thread.id, other_thread.id],
  759. 'title': 'Merged thread!',
  760. 'category': self.category.id,
  761. }),
  762. content_type="application/json",
  763. )
  764. self.assertEqual(response.status_code, 200)
  765. new_thread = Thread.objects.get(pk=response.json()['id'])
  766. # poll and its votes were kept
  767. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=new_thread).count(), 1)
  768. self.assertEqual(PollVote.objects.filter(poll=poll, thread=new_thread).count(), 4)
  769. self.assertEqual(Poll.objects.count(), 1)
  770. self.assertEqual(PollVote.objects.count(), 4)
  771. @patch_category_acl({"can_merge_threads": True})
  772. def test_threads_merge_conflict_poll(self):
  773. """api errors on merge conflict, returning list of available polls"""
  774. other_thread = testutils.post_thread(self.category)
  775. poll = testutils.post_poll(self.thread, self.user)
  776. other_poll = testutils.post_poll(other_thread, self.user)
  777. response = self.client.post(
  778. self.api_link,
  779. json.dumps({
  780. 'threads': [self.thread.id, other_thread.id],
  781. 'title': 'Merged thread!',
  782. 'category': self.category.id,
  783. }),
  784. content_type="application/json",
  785. )
  786. self.assertEqual(response.status_code, 400)
  787. self.assertEqual(
  788. response.json(), {
  789. 'polls': [
  790. ['0', "Delete all polls"],
  791. [
  792. str(other_poll.pk),
  793. '%s (%s)' % (other_poll.question, other_poll.thread.title),
  794. ],
  795. [
  796. str(poll.pk),
  797. '%s (%s)' % (poll.question, poll.thread.title),
  798. ],
  799. ],
  800. }
  801. )
  802. # polls and votes were untouched
  803. self.assertEqual(Poll.objects.count(), 2)
  804. self.assertEqual(PollVote.objects.count(), 8)
  805. @patch_category_acl({"can_merge_threads": True})
  806. def test_threads_merge_conflict_poll_invalid_resolution(self):
  807. """api errors on invalid merge conflict resolution"""
  808. other_thread = testutils.post_thread(self.category)
  809. testutils.post_poll(self.thread, self.user)
  810. testutils.post_poll(other_thread, self.user)
  811. response = self.client.post(
  812. self.api_link,
  813. json.dumps({
  814. 'threads': [self.thread.id, other_thread.id],
  815. 'title': 'Merged thread!',
  816. 'category': self.category.id,
  817. 'poll': other_thread.poll.id + 10,
  818. }),
  819. content_type="application/json",
  820. )
  821. self.assertEqual(response.status_code, 400)
  822. self.assertEqual(response.json(), {
  823. 'poll': ["Invalid choice."],
  824. })
  825. # polls and votes were untouched
  826. self.assertEqual(Poll.objects.count(), 2)
  827. self.assertEqual(PollVote.objects.count(), 8)
  828. @patch_category_acl({"can_merge_threads": True})
  829. def test_threads_merge_conflict_delete_all_polls(self):
  830. """api deletes all polls when delete all choice is selected"""
  831. other_thread = testutils.post_thread(self.category)
  832. testutils.post_poll(self.thread, self.user)
  833. testutils.post_poll(other_thread, self.user)
  834. response = self.client.post(
  835. self.api_link,
  836. json.dumps({
  837. 'threads': [self.thread.id, other_thread.id],
  838. 'title': 'Merged thread!',
  839. 'category': self.category.id,
  840. 'poll': 0,
  841. }),
  842. content_type="application/json",
  843. )
  844. self.assertEqual(response.status_code, 200)
  845. # polls and votes are gone
  846. self.assertEqual(Poll.objects.count(), 0)
  847. self.assertEqual(PollVote.objects.count(), 0)
  848. @patch_category_acl({"can_merge_threads": True})
  849. def test_threads_merge_conflict_keep_first_poll(self):
  850. """api deletes other poll on merge"""
  851. other_thread = testutils.post_thread(self.category)
  852. poll = testutils.post_poll(self.thread, self.user)
  853. other_poll = testutils.post_poll(other_thread, self.user)
  854. response = self.client.post(
  855. self.api_link,
  856. json.dumps({
  857. 'threads': [self.thread.id, other_thread.id],
  858. 'title': 'Merged thread!',
  859. 'category': self.category.id,
  860. 'poll': poll.pk,
  861. }),
  862. content_type="application/json",
  863. )
  864. self.assertEqual(response.status_code, 200)
  865. # other poll and its votes are gone
  866. self.assertEqual(Poll.objects.count(), 1)
  867. self.assertEqual(PollVote.objects.count(), 4)
  868. Poll.objects.get(pk=poll.pk)
  869. with self.assertRaises(Poll.DoesNotExist):
  870. Poll.objects.get(pk=other_poll.pk)
  871. @patch_category_acl({"can_merge_threads": True})
  872. def test_threads_merge_conflict_keep_other_poll(self):
  873. """api deletes first poll on merge"""
  874. other_thread = testutils.post_thread(self.category)
  875. poll = testutils.post_poll(self.thread, self.user)
  876. other_poll = testutils.post_poll(other_thread, self.user)
  877. response = self.client.post(
  878. self.api_link,
  879. json.dumps({
  880. 'threads': [self.thread.id, other_thread.id],
  881. 'title': 'Merged thread!',
  882. 'category': self.category.id,
  883. 'poll': other_poll.pk,
  884. }),
  885. content_type="application/json",
  886. )
  887. self.assertEqual(response.status_code, 200)
  888. # other poll and its votes are gone
  889. self.assertEqual(Poll.objects.count(), 1)
  890. self.assertEqual(PollVote.objects.count(), 4)
  891. Poll.objects.get(pk=other_poll.pk)
  892. with self.assertRaises(Poll.DoesNotExist):
  893. Poll.objects.get(pk=poll.pk)