test_threads_merge_api.py 40 KB

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