test_threads_merge_api.py 28 KB

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