test_threads_merge_api.py 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190
  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().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.assertFalse(new_thread.has_best_answer)
  790. self.assertIsNone(new_thread.best_answer_id)
  791. def test_threads_merge_conflict_keep_first_best_answer(self):
  792. """api unmarks other best answer on merge"""
  793. self.override_acl({'can_merge_threads': 1})
  794. best_answer = testutils.reply_thread(self.thread)
  795. self.thread.set_best_answer(self.user, best_answer)
  796. self.thread.save()
  797. other_thread = testutils.post_thread(self.category)
  798. other_best_answer = testutils.reply_thread(other_thread)
  799. other_thread.set_best_answer(self.user, other_best_answer)
  800. other_thread.save()
  801. response = self.client.post(
  802. self.api_link,
  803. json.dumps({
  804. 'threads': [self.thread.id, other_thread.id],
  805. 'title': 'Merged thread!',
  806. 'category': self.category.id,
  807. 'best_answer': self.thread.pk,
  808. }),
  809. content_type="application/json",
  810. )
  811. self.assertEqual(response.status_code, 200)
  812. # selected best answer is set on new thread
  813. new_thread = Thread.objects.get(pk=response.json()['id'])
  814. self.assertEqual(new_thread.best_answer_id, best_answer.id)
  815. def test_threads_merge_conflict_keep_other_best_answer(self):
  816. """api unmarks first best answer on merge"""
  817. self.override_acl({'can_merge_threads': 1})
  818. best_answer = testutils.reply_thread(self.thread)
  819. self.thread.set_best_answer(self.user, best_answer)
  820. self.thread.save()
  821. other_thread = testutils.post_thread(self.category)
  822. other_best_answer = testutils.reply_thread(other_thread)
  823. other_thread.set_best_answer(self.user, other_best_answer)
  824. other_thread.save()
  825. response = self.client.post(
  826. self.api_link,
  827. json.dumps({
  828. 'threads': [self.thread.id, other_thread.id],
  829. 'title': 'Merged thread!',
  830. 'category': self.category.id,
  831. 'best_answer': other_thread.pk,
  832. }),
  833. content_type="application/json",
  834. )
  835. self.assertEqual(response.status_code, 200)
  836. # selected best answer is set on new thread
  837. new_thread = Thread.objects.get(pk=response.json()['id'])
  838. self.assertEqual(new_thread.best_answer_id, other_best_answer.id)
  839. def test_merge_threads_kept_poll(self):
  840. """api merges two threads successfully, keeping poll from other thread"""
  841. self.override_acl({'can_merge_threads': True})
  842. other_thread = testutils.post_thread(self.category)
  843. poll = testutils.post_poll(other_thread, self.user)
  844. response = self.client.post(
  845. self.api_link,
  846. json.dumps({
  847. 'threads': [self.thread.id, other_thread.id],
  848. 'title': 'Merged thread!',
  849. 'category': self.category.id,
  850. }),
  851. content_type="application/json",
  852. )
  853. self.assertEqual(response.status_code, 200)
  854. response_json = response.json()
  855. new_thread = Thread.objects.get(pk=response_json['id'])
  856. # poll and its votes were kept
  857. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=new_thread).count(), 1)
  858. self.assertEqual(PollVote.objects.filter(poll=poll, thread=new_thread).count(), 4)
  859. self.assertEqual(Poll.objects.count(), 1)
  860. self.assertEqual(PollVote.objects.count(), 4)
  861. def test_merge_threads_moved_poll(self):
  862. """api merges two threads successfully, moving poll from old thread"""
  863. self.override_acl({'can_merge_threads': True})
  864. other_thread = testutils.post_thread(self.category)
  865. poll = testutils.post_poll(self.thread, self.user)
  866. response = self.client.post(
  867. self.api_link,
  868. json.dumps({
  869. 'threads': [self.thread.id, other_thread.id],
  870. 'title': 'Merged thread!',
  871. 'category': self.category.id,
  872. }),
  873. content_type="application/json",
  874. )
  875. self.assertEqual(response.status_code, 200)
  876. response_json = response.json()
  877. new_thread = Thread.objects.get(pk=response_json['id'])
  878. # poll and its votes were kept
  879. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=new_thread).count(), 1)
  880. self.assertEqual(PollVote.objects.filter(poll=poll, thread=new_thread).count(), 4)
  881. self.assertEqual(Poll.objects.count(), 1)
  882. self.assertEqual(PollVote.objects.count(), 4)
  883. def test_threads_merge_conflict_poll(self):
  884. """api errors on merge conflict, returning list of available polls"""
  885. self.override_acl({'can_merge_threads': True})
  886. other_thread = testutils.post_thread(self.category)
  887. poll = testutils.post_poll(self.thread, self.user)
  888. other_poll = testutils.post_poll(other_thread, self.user)
  889. response = self.client.post(
  890. self.api_link,
  891. json.dumps({
  892. 'threads': [self.thread.id, other_thread.id],
  893. 'title': 'Merged thread!',
  894. 'category': self.category.id,
  895. }),
  896. content_type="application/json",
  897. )
  898. self.assertEqual(response.status_code, 400)
  899. self.assertEqual(
  900. response.json(), {
  901. 'polls': [
  902. ['0', "Delete all polls"],
  903. [
  904. str(other_poll.pk),
  905. '{} ({})'.format(other_poll.question, other_poll.thread.title),
  906. ],
  907. [
  908. str(poll.pk),
  909. '{} ({})'.format(poll.question, poll.thread.title),
  910. ],
  911. ],
  912. }
  913. )
  914. # polls and votes were untouched
  915. self.assertEqual(Poll.objects.count(), 2)
  916. self.assertEqual(PollVote.objects.count(), 8)
  917. def test_threads_merge_conflict_poll_invalid_resolution(self):
  918. """api errors on invalid merge conflict resolution"""
  919. self.override_acl({'can_merge_threads': True})
  920. other_thread = testutils.post_thread(self.category)
  921. testutils.post_poll(self.thread, self.user)
  922. testutils.post_poll(other_thread, self.user)
  923. response = self.client.post(
  924. self.api_link,
  925. json.dumps({
  926. 'threads': [self.thread.id, other_thread.id],
  927. 'title': 'Merged thread!',
  928. 'category': self.category.id,
  929. 'poll': other_thread.poll.id + 10,
  930. }),
  931. content_type="application/json",
  932. )
  933. self.assertEqual(response.status_code, 400)
  934. self.assertEqual(response.json(), {
  935. 'poll': ["Invalid choice."],
  936. })
  937. # polls and votes were untouched
  938. self.assertEqual(Poll.objects.count(), 2)
  939. self.assertEqual(PollVote.objects.count(), 8)
  940. def test_threads_merge_conflict_delete_all_polls(self):
  941. """api deletes all polls when delete all choice is selected"""
  942. self.override_acl({'can_merge_threads': True})
  943. other_thread = testutils.post_thread(self.category)
  944. testutils.post_poll(self.thread, self.user)
  945. testutils.post_poll(other_thread, self.user)
  946. response = self.client.post(
  947. self.api_link,
  948. json.dumps({
  949. 'threads': [self.thread.id, other_thread.id],
  950. 'title': 'Merged thread!',
  951. 'category': self.category.id,
  952. 'poll': 0,
  953. }),
  954. content_type="application/json",
  955. )
  956. self.assertEqual(response.status_code, 200)
  957. # polls and votes are gone
  958. self.assertEqual(Poll.objects.count(), 0)
  959. self.assertEqual(PollVote.objects.count(), 0)
  960. def test_threads_merge_conflict_keep_first_poll(self):
  961. """api deletes other poll on merge"""
  962. self.override_acl({'can_merge_threads': True})
  963. other_thread = testutils.post_thread(self.category)
  964. poll = testutils.post_poll(self.thread, self.user)
  965. other_poll = testutils.post_poll(other_thread, self.user)
  966. response = self.client.post(
  967. self.api_link,
  968. json.dumps({
  969. 'threads': [self.thread.id, other_thread.id],
  970. 'title': 'Merged thread!',
  971. 'category': self.category.id,
  972. 'poll': poll.pk,
  973. }),
  974. content_type="application/json",
  975. )
  976. self.assertEqual(response.status_code, 200)
  977. # other poll and its votes are gone
  978. self.assertEqual(Poll.objects.count(), 1)
  979. self.assertEqual(PollVote.objects.count(), 4)
  980. Poll.objects.get(pk=poll.pk)
  981. with self.assertRaises(Poll.DoesNotExist):
  982. Poll.objects.get(pk=other_poll.pk)
  983. def test_threads_merge_conflict_keep_other_poll(self):
  984. """api deletes first poll on merge"""
  985. self.override_acl({'can_merge_threads': True})
  986. other_thread = testutils.post_thread(self.category)
  987. poll = testutils.post_poll(self.thread, self.user)
  988. other_poll = testutils.post_poll(other_thread, self.user)
  989. response = self.client.post(
  990. self.api_link,
  991. json.dumps({
  992. 'threads': [self.thread.id, other_thread.id],
  993. 'title': 'Merged thread!',
  994. 'category': self.category.id,
  995. 'poll': other_poll.pk,
  996. }),
  997. content_type="application/json",
  998. )
  999. self.assertEqual(response.status_code, 200)
  1000. # other poll and its votes are gone
  1001. self.assertEqual(Poll.objects.count(), 1)
  1002. self.assertEqual(PollVote.objects.count(), 4)
  1003. Poll.objects.get(pk=other_poll.pk)
  1004. with self.assertRaises(Poll.DoesNotExist):
  1005. Poll.objects.get(pk=poll.pk)