test_threads_merge_api.py 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794
  1. import json
  2. from django.urls import reverse
  3. from django.utils.six.moves import range
  4. from misago.acl import add_acl
  5. from misago.categories.models import Category
  6. from misago.threads import testutils
  7. from misago.threads.api.threadendpoints.merge import MERGE_LIMIT
  8. from misago.threads.models import Poll, PollVote, Post, Thread
  9. from misago.threads.serializers import ThreadsListSerializer
  10. from .test_threads_api import ThreadsApiTestCase
  11. class ThreadsMergeApiTests(ThreadsApiTestCase):
  12. def setUp(self):
  13. super(ThreadsMergeApiTests, self).setUp()
  14. self.api_link = reverse('misago:api:thread-merge')
  15. Category(
  16. name='Category B',
  17. slug='category-b',
  18. ).insert_at(self.category, position='last-child', save=True)
  19. self.category_b = Category.objects.get(slug='category-b')
  20. def test_merge_no_threads(self):
  21. """api validates if we are trying to merge no threads"""
  22. response = self.client.post(self.api_link, content_type="application/json")
  23. self.assertEqual(response.status_code, 403)
  24. response_json = response.json()
  25. self.assertEqual(response_json, {
  26. 'detail': "You have to select at least two threads to merge."
  27. })
  28. def test_merge_empty_threads(self):
  29. """api validates if we are trying to empty threads list"""
  30. response = self.client.post(self.api_link, json.dumps({
  31. 'threads': []
  32. }), content_type="application/json")
  33. self.assertEqual(response.status_code, 403)
  34. response_json = response.json()
  35. self.assertEqual(response_json, {
  36. 'detail': "You have to select at least two threads to merge."
  37. })
  38. def test_merge_invalid_threads(self):
  39. """api validates if we are trying to merge invalid thread ids"""
  40. response = self.client.post(self.api_link, json.dumps({
  41. 'threads': 'abcd'
  42. }), content_type="application/json")
  43. self.assertEqual(response.status_code, 403)
  44. response_json = response.json()
  45. self.assertEqual(response_json, {
  46. 'detail': "One or more thread ids received were invalid."
  47. })
  48. response = self.client.post(self.api_link, json.dumps({
  49. 'threads': ['a', '-', 'c']
  50. }), content_type="application/json")
  51. self.assertEqual(response.status_code, 403)
  52. response_json = response.json()
  53. self.assertEqual(response_json, {
  54. 'detail': "One or more thread ids received were invalid."
  55. })
  56. def test_merge_single_thread(self):
  57. """api validates if we are trying to merge single thread"""
  58. response = self.client.post(self.api_link, json.dumps({
  59. 'threads': [self.thread.id]
  60. }), content_type="application/json")
  61. self.assertEqual(response.status_code, 403)
  62. response_json = response.json()
  63. self.assertEqual(response_json, {
  64. 'detail': "You have to select at least two threads to merge."
  65. })
  66. def test_merge_with_nonexisting_thread(self):
  67. """api validates if we are trying to merge with invalid thread"""
  68. testutils.post_thread(category=self.category_b)
  69. response = self.client.post(self.api_link, json.dumps({
  70. 'threads': [self.thread.id, self.thread.id + 1000]
  71. }), content_type="application/json")
  72. self.assertEqual(response.status_code, 403)
  73. response_json = response.json()
  74. self.assertEqual(response_json, {
  75. 'detail': "One or more threads to merge could not be found."
  76. })
  77. def test_merge_with_invisible_thread(self):
  78. """api validates if we are trying to merge with inaccesible thread"""
  79. unaccesible_thread = testutils.post_thread(category=self.category_b)
  80. response = self.client.post(self.api_link, json.dumps({
  81. 'threads': [self.thread.id, unaccesible_thread.id]
  82. }), content_type="application/json")
  83. self.assertEqual(response.status_code, 403)
  84. response_json = response.json()
  85. self.assertEqual(response_json, {
  86. 'detail': "One or more threads to merge could not be found."
  87. })
  88. def test_merge_no_permission(self):
  89. """api validates permission to merge threads"""
  90. thread = testutils.post_thread(category=self.category)
  91. response = self.client.post(self.api_link, json.dumps({
  92. 'threads': [self.thread.id, thread.id]
  93. }), content_type="application/json")
  94. self.assertEqual(response.status_code, 403)
  95. response_json = response.json()
  96. self.assertEqual(response_json, [
  97. {
  98. 'id': thread.pk,
  99. 'title': thread.title,
  100. 'errors': [
  101. "You don't have permission to merge this thread with others."
  102. ]
  103. },
  104. {
  105. 'id': self.thread.pk,
  106. 'title': self.thread.title,
  107. 'errors': [
  108. "You don't have permission to merge this thread with others."
  109. ]
  110. },
  111. ])
  112. def test_merge_too_many_threads(self):
  113. """api rejects too many threads to merge"""
  114. threads = []
  115. for _ in range(MERGE_LIMIT + 1):
  116. threads.append(testutils.post_thread(category=self.category).pk)
  117. self.override_acl({
  118. 'can_merge_threads': True,
  119. 'can_close_threads': False,
  120. 'can_edit_threads': False,
  121. 'can_reply_threads': False,
  122. })
  123. response = self.client.post(self.api_link, json.dumps({
  124. 'threads': threads
  125. }), content_type="application/json")
  126. self.assertEqual(response.status_code, 403)
  127. response_json = response.json()
  128. self.assertEqual(response_json, {
  129. 'detail': "No more than %s threads can be merged at single time." % MERGE_LIMIT
  130. })
  131. def test_merge_no_final_thread(self):
  132. """api rejects merge because no data to merge threads was specified"""
  133. self.override_acl({
  134. 'can_merge_threads': True,
  135. 'can_close_threads': False,
  136. 'can_edit_threads': False,
  137. 'can_reply_threads': False,
  138. })
  139. thread = testutils.post_thread(category=self.category)
  140. response = self.client.post(self.api_link, json.dumps({
  141. 'threads': [self.thread.id, thread.id]
  142. }), content_type="application/json")
  143. self.assertEqual(response.status_code, 400)
  144. response_json = response.json()
  145. self.assertEqual(response_json, {
  146. 'title': ['This field is required.'],
  147. 'category': ['This field is required.'],
  148. })
  149. def test_merge_invalid_final_title(self):
  150. """api rejects merge because final thread title was invalid"""
  151. self.override_acl({
  152. 'can_merge_threads': True,
  153. 'can_close_threads': False,
  154. 'can_edit_threads': False,
  155. 'can_reply_threads': False,
  156. })
  157. thread = testutils.post_thread(category=self.category)
  158. response = self.client.post(self.api_link, json.dumps({
  159. 'threads': [self.thread.id, thread.id],
  160. 'title': '$$$',
  161. 'category': self.category.id,
  162. }), content_type="application/json")
  163. self.assertEqual(response.status_code, 400)
  164. response_json = response.json()
  165. self.assertEqual(response_json, {
  166. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  167. })
  168. def test_merge_invalid_category(self):
  169. """api rejects merge because final category was invalid"""
  170. self.override_acl({
  171. 'can_merge_threads': True,
  172. 'can_close_threads': False,
  173. 'can_edit_threads': False,
  174. 'can_reply_threads': False,
  175. })
  176. thread = testutils.post_thread(category=self.category)
  177. response = self.client.post(self.api_link, json.dumps({
  178. 'threads': [self.thread.id, thread.id],
  179. 'title': 'Valid thread title',
  180. 'category': self.category_b.id,
  181. }), content_type="application/json")
  182. self.assertEqual(response.status_code, 400)
  183. response_json = response.json()
  184. self.assertEqual(response_json, {
  185. 'category': ["Requested category could not be found."]
  186. })
  187. def test_merge_unallowed_start_thread(self):
  188. """api rejects merge because category isn't allowing starting threads"""
  189. self.override_acl({
  190. 'can_merge_threads': True,
  191. 'can_close_threads': False,
  192. 'can_edit_threads': False,
  193. 'can_reply_threads': False,
  194. 'can_start_threads': 0
  195. })
  196. thread = testutils.post_thread(category=self.category)
  197. response = self.client.post(self.api_link, json.dumps({
  198. 'threads': [self.thread.id, thread.id],
  199. 'title': 'Valid thread title',
  200. 'category': self.category.id
  201. }), content_type="application/json")
  202. self.assertEqual(response.status_code, 400)
  203. response_json = response.json()
  204. self.assertEqual(response_json, {
  205. 'category': [
  206. "You can't create new threads in selected category."
  207. ]
  208. })
  209. def test_merge_invalid_weight(self):
  210. """api rejects merge because final weight was invalid"""
  211. self.override_acl({
  212. 'can_merge_threads': True,
  213. 'can_close_threads': False,
  214. 'can_edit_threads': False,
  215. 'can_reply_threads': False,
  216. })
  217. thread = testutils.post_thread(category=self.category)
  218. response = self.client.post(self.api_link, json.dumps({
  219. 'threads': [self.thread.id, thread.id],
  220. 'title': 'Valid thread title',
  221. 'category': self.category.id,
  222. 'weight': 4,
  223. }), content_type="application/json")
  224. self.assertEqual(response.status_code, 400)
  225. response_json = response.json()
  226. self.assertEqual(response_json, {
  227. 'weight': ["Ensure this value is less than or equal to 2."]
  228. })
  229. def test_merge_unallowed_global_weight(self):
  230. """api rejects merge because global weight was unallowed"""
  231. self.override_acl({
  232. 'can_merge_threads': True,
  233. 'can_close_threads': False,
  234. 'can_edit_threads': False,
  235. 'can_reply_threads': False,
  236. })
  237. thread = testutils.post_thread(category=self.category)
  238. response = self.client.post(self.api_link, json.dumps({
  239. 'threads': [self.thread.id, thread.id],
  240. 'title': 'Valid thread title',
  241. 'category': self.category.id,
  242. 'weight': 2,
  243. }), content_type="application/json")
  244. self.assertEqual(response.status_code, 400)
  245. response_json = response.json()
  246. self.assertEqual(response_json, {
  247. 'weight': [
  248. "You don't have permission to pin threads globally in this category."
  249. ]
  250. })
  251. def test_merge_unallowed_local_weight(self):
  252. """api rejects merge because local weight was unallowed"""
  253. self.override_acl({
  254. 'can_merge_threads': True,
  255. 'can_close_threads': False,
  256. 'can_edit_threads': False,
  257. 'can_reply_threads': False,
  258. })
  259. thread = testutils.post_thread(category=self.category)
  260. response = self.client.post(self.api_link, json.dumps({
  261. 'threads': [self.thread.id, thread.id],
  262. 'title': 'Valid thread title',
  263. 'category': self.category.id,
  264. 'weight': 1,
  265. }), content_type="application/json")
  266. self.assertEqual(response.status_code, 400)
  267. response_json = response.json()
  268. self.assertEqual(response_json, {
  269. 'weight': [
  270. "You don't have permission to pin threads in this category."
  271. ]
  272. })
  273. def test_merge_allowed_local_weight(self):
  274. """api allows local weight"""
  275. self.override_acl({
  276. 'can_merge_threads': True,
  277. 'can_close_threads': False,
  278. 'can_edit_threads': False,
  279. 'can_reply_threads': False,
  280. 'can_pin_threads': 1,
  281. })
  282. thread = testutils.post_thread(category=self.category)
  283. response = self.client.post(self.api_link, json.dumps({
  284. 'threads': [self.thread.id, thread.id],
  285. 'title': '$$$',
  286. 'category': self.category.id,
  287. 'weight': 1,
  288. }), content_type="application/json")
  289. self.assertEqual(response.status_code, 400)
  290. response_json = response.json()
  291. self.assertEqual(response_json, {
  292. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  293. })
  294. def test_merge_allowed_global_weight(self):
  295. """api allows global weight"""
  296. self.override_acl({
  297. 'can_merge_threads': True,
  298. 'can_close_threads': False,
  299. 'can_edit_threads': False,
  300. 'can_reply_threads': False,
  301. 'can_pin_threads': 2,
  302. })
  303. thread = testutils.post_thread(category=self.category)
  304. response = self.client.post(self.api_link, json.dumps({
  305. 'threads': [self.thread.id, thread.id],
  306. 'title': '$$$',
  307. 'category': self.category.id,
  308. 'weight': 2,
  309. }), content_type="application/json")
  310. self.assertEqual(response.status_code, 400)
  311. response_json = response.json()
  312. self.assertEqual(response_json, {
  313. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  314. })
  315. def test_merge_unallowed_close(self):
  316. """api rejects merge because closing thread was unallowed"""
  317. self.override_acl({
  318. 'can_merge_threads': True,
  319. 'can_close_threads': False,
  320. 'can_edit_threads': False,
  321. 'can_reply_threads': False,
  322. })
  323. thread = testutils.post_thread(category=self.category)
  324. response = self.client.post(self.api_link, json.dumps({
  325. 'threads': [self.thread.id, thread.id],
  326. 'title': 'Valid thread title',
  327. 'category': self.category.id,
  328. 'is_closed': True,
  329. }), content_type="application/json")
  330. self.assertEqual(response.status_code, 400)
  331. response_json = response.json()
  332. self.assertEqual(response_json, {
  333. 'is_closed': [
  334. "You don't have permission to close threads in this category."
  335. ]
  336. })
  337. def test_merge_with_close(self):
  338. """api allows for closing thread"""
  339. self.override_acl({
  340. 'can_merge_threads': True,
  341. 'can_edit_threads': False,
  342. 'can_reply_threads': False,
  343. 'can_close_threads': True,
  344. })
  345. thread = testutils.post_thread(category=self.category)
  346. response = self.client.post(self.api_link, json.dumps({
  347. 'threads': [self.thread.id, thread.id],
  348. 'title': '$$$',
  349. 'category': self.category.id,
  350. 'weight': 0,
  351. 'is_closed': True,
  352. }), content_type="application/json")
  353. self.assertEqual(response.status_code, 400)
  354. response_json = response.json()
  355. self.assertEqual(response_json, {
  356. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  357. })
  358. def test_merge_unallowed_hidden(self):
  359. """api rejects merge because hidden thread was unallowed"""
  360. self.override_acl({
  361. 'can_merge_threads': True,
  362. 'can_close_threads': False,
  363. 'can_edit_threads': False,
  364. 'can_reply_threads': False,
  365. 'can_hide_threads': 0,
  366. })
  367. thread = testutils.post_thread(category=self.category)
  368. response = self.client.post(self.api_link, json.dumps({
  369. 'threads': [self.thread.id, thread.id],
  370. 'title': 'Valid thread title',
  371. 'category': self.category.id,
  372. 'is_hidden': True,
  373. }), content_type="application/json")
  374. self.assertEqual(response.status_code, 400)
  375. response_json = response.json()
  376. self.assertEqual(response_json, {
  377. 'is_hidden': [
  378. "You don't have permission to hide threads in this category."
  379. ]
  380. })
  381. def test_merge_with_hide(self):
  382. """api allows for hiding thread"""
  383. self.override_acl({
  384. 'can_merge_threads': True,
  385. 'can_close_threads': False,
  386. 'can_edit_threads': False,
  387. 'can_reply_threads': False,
  388. 'can_hide_threads': 1,
  389. })
  390. thread = testutils.post_thread(category=self.category)
  391. response = self.client.post(self.api_link, json.dumps({
  392. 'threads': [self.thread.id, thread.id],
  393. 'title': '$$$',
  394. 'category': self.category.id,
  395. 'weight': 0,
  396. 'is_hidden': True,
  397. }), content_type="application/json")
  398. self.assertEqual(response.status_code, 400)
  399. response_json = response.json()
  400. self.assertEqual(response_json, {
  401. 'title': ["Thread title should be at least 5 characters long (it has 3)."]
  402. })
  403. def test_merge(self):
  404. """api performs basic merge"""
  405. posts_ids = [p.id for p in Post.objects.all()]
  406. self.override_acl({
  407. 'can_merge_threads': True,
  408. 'can_close_threads': False,
  409. 'can_edit_threads': False,
  410. 'can_reply_threads': False,
  411. })
  412. thread = testutils.post_thread(category=self.category)
  413. response = self.client.post(self.api_link, json.dumps({
  414. 'threads': [self.thread.id, thread.id],
  415. 'title': 'Merged thread!',
  416. 'category': self.category.id,
  417. }), content_type="application/json")
  418. self.assertEqual(response.status_code, 200)
  419. # is response json with new thread?
  420. response_json = response.json()
  421. new_thread = Thread.objects.get(pk=response_json['id'])
  422. new_thread.is_read = False
  423. new_thread.subscription = None
  424. new_thread.top_category = None
  425. add_acl(self.user, new_thread.category)
  426. add_acl(self.user, new_thread)
  427. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  428. # did posts move to new thread?
  429. for post in Post.objects.filter(id__in=posts_ids):
  430. self.assertEqual(post.thread_id, new_thread.id)
  431. # are old threads gone?
  432. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  433. def test_merge_kitchensink(self):
  434. """api performs merge"""
  435. posts_ids = [p.id for p in Post.objects.all()]
  436. self.override_acl({
  437. 'can_merge_threads': True,
  438. 'can_close_threads': True,
  439. 'can_hide_threads': 1,
  440. 'can_pin_threads': 2
  441. })
  442. thread = testutils.post_thread(category=self.category)
  443. response = self.client.post(self.api_link, json.dumps({
  444. 'threads': [self.thread.id, thread.id],
  445. 'title': 'Merged thread!',
  446. 'category': self.category.id,
  447. 'is_closed': 1,
  448. 'is_hidden': 1,
  449. 'weight': 2
  450. }), content_type="application/json")
  451. self.assertEqual(response.status_code, 200)
  452. # is response json with new thread?
  453. response_json = response.json()
  454. new_thread = Thread.objects.get(pk=response_json['id'])
  455. new_thread.is_read = False
  456. new_thread.subscription = None
  457. new_thread.top_category = None
  458. self.assertEqual(new_thread.weight, 2)
  459. self.assertTrue(new_thread.is_closed)
  460. self.assertTrue(new_thread.is_hidden)
  461. add_acl(self.user, new_thread.category)
  462. add_acl(self.user, new_thread)
  463. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  464. # did posts move to new thread?
  465. for post in Post.objects.filter(id__in=posts_ids):
  466. self.assertEqual(post.thread_id, new_thread.id)
  467. # are old threads gone?
  468. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  469. def test_merge_with_top_category(self):
  470. """api performs merge with top category"""
  471. posts_ids = [p.id for p in Post.objects.all()]
  472. self.override_acl({
  473. 'can_merge_threads': True,
  474. 'can_close_threads': False,
  475. 'can_edit_threads': False,
  476. 'can_reply_threads': False,
  477. })
  478. thread = testutils.post_thread(category=self.category)
  479. response = self.client.post(self.api_link, json.dumps({
  480. 'top_category': self.root.id,
  481. 'threads': [self.thread.id, thread.id],
  482. 'title': 'Merged thread!',
  483. 'category': self.category.id,
  484. }), content_type="application/json")
  485. self.assertEqual(response.status_code, 200)
  486. # is response json with new thread?
  487. response_json = response.json()
  488. new_thread = Thread.objects.get(pk=response_json['id'])
  489. new_thread.is_read = False
  490. new_thread.subscription = None
  491. new_thread.top_category = self.category
  492. add_acl(self.user, new_thread.category)
  493. add_acl(self.user, new_thread)
  494. self.assertEqual(response_json, ThreadsListSerializer(new_thread).data)
  495. # did posts move to new thread?
  496. for post in Post.objects.filter(id__in=posts_ids):
  497. self.assertEqual(post.thread_id, new_thread.id)
  498. # are old threads gone?
  499. self.assertEqual([t.pk for t in Thread.objects.all()], [new_thread.pk])
  500. def test_merge_threads_kept_poll(self):
  501. """api merges two threads successfully, keeping poll from old thread"""
  502. self.override_acl({
  503. 'can_merge_threads': True,
  504. })
  505. other_thread = testutils.post_thread(self.category)
  506. poll = testutils.post_poll(other_thread, self.user)
  507. response = self.client.post(self.api_link, json.dumps({
  508. 'threads': [self.thread.id, other_thread.id],
  509. 'title': 'Merged thread!',
  510. 'category': self.category.id,
  511. }), content_type="application/json")
  512. self.assertEqual(response.status_code, 200)
  513. response_json = response.json()
  514. new_thread = Thread.objects.get(pk=response_json['id'])
  515. # poll and its votes were kept
  516. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=new_thread).count(), 1)
  517. self.assertEqual(PollVote.objects.filter(poll=poll, thread=new_thread).count(), 4)
  518. self.assertEqual(Poll.objects.count(), 1)
  519. self.assertEqual(PollVote.objects.count(), 4)
  520. def test_merge_threads_moved_poll(self):
  521. """api merges two threads successfully, moving poll from other thread"""
  522. self.override_acl({
  523. 'can_merge_threads': True,
  524. })
  525. other_thread = testutils.post_thread(self.category)
  526. poll = testutils.post_poll(self.thread, self.user)
  527. response = self.client.post(self.api_link, json.dumps({
  528. 'threads': [self.thread.id, other_thread.id],
  529. 'title': 'Merged thread!',
  530. 'category': self.category.id,
  531. }), content_type="application/json")
  532. self.assertEqual(response.status_code, 200)
  533. response_json = response.json()
  534. new_thread = Thread.objects.get(pk=response_json['id'])
  535. # poll and its votes were kept
  536. self.assertEqual(Poll.objects.filter(pk=poll.pk, thread=new_thread).count(), 1)
  537. self.assertEqual(PollVote.objects.filter(poll=poll, thread=new_thread).count(), 4)
  538. self.assertEqual(Poll.objects.count(), 1)
  539. self.assertEqual(PollVote.objects.count(), 4)
  540. def test_threads_merge_conflict(self):
  541. """api errors on merge conflict, returning list of available polls"""
  542. self.override_acl({
  543. 'can_merge_threads': True,
  544. })
  545. other_thread = testutils.post_thread(self.category)
  546. poll = testutils.post_poll(self.thread, self.user)
  547. other_poll = testutils.post_poll(other_thread, self.user)
  548. response = self.client.post(self.api_link, json.dumps({
  549. 'threads': [self.thread.id, other_thread.id],
  550. 'title': 'Merged thread!',
  551. 'category': self.category.id,
  552. }), content_type="application/json")
  553. self.assertEqual(response.status_code, 400)
  554. self.assertEqual(response.json(), {
  555. 'polls': [
  556. [0, "Delete all polls"],
  557. [poll.pk, poll.question],
  558. [other_poll.pk, other_poll.question]
  559. ]
  560. })
  561. # polls and votes were untouched
  562. self.assertEqual(Poll.objects.count(), 2)
  563. self.assertEqual(PollVote.objects.count(), 8)
  564. def test_threads_merge_conflict_invalid_resolution(self):
  565. """api errors on invalid merge conflict resolution"""
  566. self.override_acl({
  567. 'can_merge_threads': True,
  568. })
  569. other_thread = testutils.post_thread(self.category)
  570. testutils.post_poll(self.thread, self.user)
  571. testutils.post_poll(other_thread, self.user)
  572. response = self.client.post(self.api_link, json.dumps({
  573. 'threads': [self.thread.id, other_thread.id],
  574. 'title': 'Merged thread!',
  575. 'category': self.category.id,
  576. 'poll': 'dsa7dsadsa9789'
  577. }), content_type="application/json")
  578. self.assertEqual(response.status_code, 400)
  579. self.assertEqual(response.json(), {
  580. 'detail': "Invalid choice."
  581. })
  582. # polls and votes were untouched
  583. self.assertEqual(Poll.objects.count(), 2)
  584. self.assertEqual(PollVote.objects.count(), 8)
  585. def test_threads_merge_conflict_delete_all(self):
  586. """api deletes all polls when delete all choice is selected"""
  587. self.override_acl({
  588. 'can_merge_threads': True,
  589. })
  590. other_thread = testutils.post_thread(self.category)
  591. testutils.post_poll(self.thread, self.user)
  592. testutils.post_poll(other_thread, self.user)
  593. response = self.client.post(self.api_link, json.dumps({
  594. 'threads': [self.thread.id, other_thread.id],
  595. 'title': 'Merged thread!',
  596. 'category': self.category.id,
  597. 'poll': 0
  598. }), content_type="application/json")
  599. self.assertEqual(response.status_code, 200)
  600. # polls and votes are gone
  601. self.assertEqual(Poll.objects.count(), 0)
  602. self.assertEqual(PollVote.objects.count(), 0)
  603. def test_threads_merge_conflict_keep_first_poll(self):
  604. """api deletes other poll on merge"""
  605. self.override_acl({
  606. 'can_merge_threads': True,
  607. })
  608. other_thread = testutils.post_thread(self.category)
  609. poll = testutils.post_poll(self.thread, self.user)
  610. other_poll = testutils.post_poll(other_thread, self.user)
  611. response = self.client.post(self.api_link, json.dumps({
  612. 'threads': [self.thread.id, other_thread.id],
  613. 'title': 'Merged thread!',
  614. 'category': self.category.id,
  615. 'poll': poll.pk
  616. }), content_type="application/json")
  617. self.assertEqual(response.status_code, 200)
  618. # other poll and its votes are gone
  619. self.assertEqual(Poll.objects.count(), 1)
  620. self.assertEqual(PollVote.objects.count(), 4)
  621. Poll.objects.get(pk=poll.pk)
  622. with self.assertRaises(Poll.DoesNotExist):
  623. Poll.objects.get(pk=other_poll.pk)
  624. def test_threads_merge_conflict_keep_other_poll(self):
  625. """api deletes first poll on merge"""
  626. self.override_acl({
  627. 'can_merge_threads': True,
  628. })
  629. other_thread = testutils.post_thread(self.category)
  630. poll = testutils.post_poll(self.thread, self.user)
  631. other_poll = testutils.post_poll(other_thread, self.user)
  632. response = self.client.post(self.api_link, json.dumps({
  633. 'threads': [self.thread.id, other_thread.id],
  634. 'title': 'Merged thread!',
  635. 'category': self.category.id,
  636. 'poll': other_poll.pk
  637. }), content_type="application/json")
  638. self.assertEqual(response.status_code, 200)
  639. # other poll and its votes are gone
  640. self.assertEqual(Poll.objects.count(), 1)
  641. self.assertEqual(PollVote.objects.count(), 4)
  642. Poll.objects.get(pk=other_poll.pk)
  643. with self.assertRaises(Poll.DoesNotExist):
  644. Poll.objects.get(pk=poll.pk)