test_threads_merge_api.py 33 KB

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