test_thread_polledit_api.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619
  1. from datetime import timedelta
  2. from django.urls import reverse
  3. from django.utils import timezone
  4. from misago.acl import add_acl
  5. from misago.core.utils import serialize_datetime
  6. from misago.threads.models import Poll
  7. from misago.threads.serializers.poll import MAX_POLL_OPTIONS
  8. from .test_thread_poll_api import ThreadPollApiTestCase
  9. class ThreadPollEditTests(ThreadPollApiTestCase):
  10. def setUp(self):
  11. super(ThreadPollEditTests, self).setUp()
  12. self.mock_poll()
  13. def test_anonymous(self):
  14. """api requires you to sign in to edit poll"""
  15. self.logout_user()
  16. response = self.put(self.api_link)
  17. self.assertEqual(response.status_code, 403)
  18. self.assertEqual(response.json(), {
  19. 'detail': "This action is not available to guests.",
  20. })
  21. def test_invalid_thread_id(self):
  22. """api validates that thread id is integer"""
  23. api_link = reverse(
  24. 'misago:api:thread-poll-detail',
  25. kwargs={
  26. 'thread_pk': 'kjha6dsa687sa',
  27. 'pk': self.poll.pk,
  28. }
  29. )
  30. response = self.put(api_link)
  31. self.assertEqual(response.status_code, 404)
  32. self.assertEqual(response.json(), {
  33. 'detail': "NOT FOUND",
  34. })
  35. def test_nonexistant_thread_id(self):
  36. """api validates that thread exists"""
  37. api_link = reverse(
  38. 'misago:api:thread-poll-detail',
  39. kwargs={
  40. 'thread_pk': self.thread.pk + 1,
  41. 'pk': self.poll.pk,
  42. }
  43. )
  44. response = self.put(api_link)
  45. self.assertEqual(response.status_code, 404)
  46. self.assertEqual(response.json(), {
  47. 'detail': "No Thread matches the given query.",
  48. })
  49. def test_invalid_poll_id(self):
  50. """api validates that poll id is integer"""
  51. api_link = reverse(
  52. 'misago:api:thread-poll-detail',
  53. kwargs={
  54. 'thread_pk': self.thread.pk,
  55. 'pk': 'sad98as7d97sa98',
  56. }
  57. )
  58. response = self.put(api_link)
  59. self.assertEqual(response.status_code, 404)
  60. self.assertEqual(response.json(), {
  61. 'detail': "NOT FOUND",
  62. })
  63. def test_nonexistant_poll_id(self):
  64. """api validates that poll exists"""
  65. api_link = reverse(
  66. 'misago:api:thread-poll-detail',
  67. kwargs={
  68. 'thread_pk': self.thread.pk,
  69. 'pk': self.poll.pk + 123,
  70. }
  71. )
  72. response = self.put(api_link)
  73. self.assertEqual(response.status_code, 404)
  74. self.assertEqual(response.json(), {
  75. 'detail': "NOT FOUND",
  76. })
  77. def test_no_permission(self):
  78. """api validates that user has permission to edit poll in thread"""
  79. self.override_acl({'can_edit_polls': 0})
  80. response = self.put(self.api_link)
  81. self.assertEqual(response.status_code, 403)
  82. self.assertEqual(response.json(), {
  83. 'detail': "You can't edit polls.",
  84. })
  85. def test_no_permission_timeout(self):
  86. """api validates that user's window to edit poll in thread has closed"""
  87. self.override_acl({'can_edit_polls': 1, 'poll_edit_time': 5})
  88. self.poll.posted_on = timezone.now() - timedelta(minutes=15)
  89. self.poll.save()
  90. response = self.put(self.api_link)
  91. self.assertEqual(response.status_code, 403)
  92. self.assertEqual(response.json(), {
  93. 'detail': "You can't edit polls that are older than 5 minutes.",
  94. })
  95. def test_no_permission_poll_closed(self):
  96. """api validates that user's window to edit poll in thread has closed"""
  97. self.override_acl({'can_edit_polls': 1})
  98. self.poll.posted_on = timezone.now() - timedelta(days=15)
  99. self.poll.length = 5
  100. self.poll.save()
  101. response = self.put(self.api_link)
  102. self.assertEqual(response.status_code, 403)
  103. self.assertEqual(response.json(), {
  104. 'detail': "This poll is over. You can't edit it.",
  105. })
  106. def test_no_permission_other_user_poll(self):
  107. """api validates that user has permission to edit other user poll in thread"""
  108. self.override_acl({'can_edit_polls': 1})
  109. self.poll.poster = None
  110. self.poll.save()
  111. response = self.put(self.api_link)
  112. self.assertEqual(response.status_code, 403)
  113. self.assertEqual(response.json(), {
  114. 'detail': "You can't edit other users polls in this category.",
  115. })
  116. def test_no_permission_closed_thread(self):
  117. """api validates that user has permission to edit poll in closed thread"""
  118. self.override_acl(category={'can_close_threads': 0})
  119. self.thread.is_closed = True
  120. self.thread.save()
  121. response = self.put(self.api_link)
  122. self.assertEqual(response.status_code, 403)
  123. self.assertEqual(response.json(), {
  124. 'detail': "This thread is closed. You can't edit polls in it.",
  125. })
  126. self.override_acl(category={'can_close_threads': 1})
  127. response = self.put(self.api_link)
  128. self.assertEqual(response.status_code, 400)
  129. def test_no_permission_closed_category(self):
  130. """api validates that user has permission to edit poll in closed category"""
  131. self.override_acl(category={'can_close_threads': 0})
  132. self.category.is_closed = True
  133. self.category.save()
  134. response = self.put(self.api_link)
  135. self.assertEqual(response.status_code, 403)
  136. self.assertEqual(response.json(), {
  137. 'detail': "This category is closed. You can't edit polls in it.",
  138. })
  139. self.override_acl(category={'can_close_threads': 1})
  140. response = self.put(self.api_link)
  141. self.assertEqual(response.status_code, 400)
  142. def test_empty_data(self):
  143. """api handles empty request data"""
  144. response = self.put(self.api_link)
  145. self.assertEqual(response.status_code, 400)
  146. self.assertEqual(response.json(), {
  147. 'question': ["This field is required."],
  148. 'choices': ["This field is required."],
  149. 'length': ["This field is required."],
  150. 'allowed_choices': ["This field is required."],
  151. })
  152. def test_length_validation(self):
  153. """api validates poll's length"""
  154. response = self.put(
  155. self.api_link, data={
  156. 'length': -1,
  157. }
  158. )
  159. self.assertEqual(response.status_code, 400)
  160. self.assertEqual(response.json(), {
  161. 'question': ["This field is required."],
  162. 'choices': ["This field is required."],
  163. 'length': ["Ensure this value is greater than or equal to 0."],
  164. 'allowed_choices': ["This field is required."],
  165. })
  166. response = self.put(
  167. self.api_link, data={
  168. 'length': 200,
  169. }
  170. )
  171. self.assertEqual(response.status_code, 400)
  172. self.assertEqual(response.json(), {
  173. 'question': ["This field is required."],
  174. 'choices': ["This field is required."],
  175. 'length': ["Ensure this value is less than or equal to 180."],
  176. 'allowed_choices': ["This field is required."],
  177. })
  178. def test_question_validation(self):
  179. """api validates question length"""
  180. response = self.put(
  181. self.api_link, data={
  182. 'question': 'abcd' * 255,
  183. }
  184. )
  185. self.assertEqual(response.status_code, 400)
  186. self.assertEqual(response.json(), {
  187. 'question': ["Ensure this field has no more than 255 characters."],
  188. 'choices': ["This field is required."],
  189. 'length': ["This field is required."],
  190. 'allowed_choices': ["This field is required."],
  191. })
  192. def test_validate_choice_length(self):
  193. """api validates single choice length"""
  194. response = self.put(
  195. self.api_link, data={
  196. 'choices': [
  197. {
  198. 'hash': 'qwertyuiopas',
  199. 'label': '',
  200. },
  201. ],
  202. }
  203. )
  204. self.assertEqual(response.status_code, 400)
  205. self.assertEqual(response.json(), {
  206. 'question': ["This field is required."],
  207. 'choices': ["One or more poll choices are invalid."],
  208. 'length': ["This field is required."],
  209. 'allowed_choices': ["This field is required."],
  210. })
  211. response = self.put(
  212. self.api_link,
  213. data={
  214. 'choices': [
  215. {
  216. 'hash': 'qwertyuiopas',
  217. 'label': 'abcd' * 255,
  218. },
  219. ],
  220. }
  221. )
  222. self.assertEqual(response.status_code, 400)
  223. self.assertEqual(response.json(), {
  224. 'question': ["This field is required."],
  225. 'choices': ["One or more poll choices are invalid."],
  226. 'length': ["This field is required."],
  227. 'allowed_choices': ["This field is required."],
  228. })
  229. def test_validate_two_choices(self):
  230. """api validates that there are at least two choices in poll"""
  231. response = self.put(self.api_link, data={'choices': [{'label': 'Choice'}]})
  232. self.assertEqual(response.status_code, 400)
  233. self.assertEqual(response.json(), {
  234. 'question': ["This field is required."],
  235. 'choices': ["You need to add at least two choices to a poll."],
  236. 'length': ["This field is required."],
  237. 'allowed_choices': ["This field is required."],
  238. })
  239. def test_validate_max_choices(self):
  240. """api validates that there are no more choices in poll than allowed number"""
  241. response = self.put(
  242. self.api_link, data={
  243. 'choices': [
  244. {
  245. 'label': 'Choice',
  246. },
  247. ] * (MAX_POLL_OPTIONS + 1),
  248. }
  249. )
  250. self.assertEqual(response.status_code, 400)
  251. error_formats = (MAX_POLL_OPTIONS, MAX_POLL_OPTIONS + 1)
  252. self.assertEqual(response.json(), {
  253. 'question': ["This field is required."],
  254. 'choices': [
  255. "You can't add more than %s options to a single poll (added %s)." % error_formats
  256. ],
  257. 'length': ["This field is required."],
  258. 'allowed_choices': ["This field is required."],
  259. })
  260. def test_allowed_choices_validation(self):
  261. """api validates allowed choices number"""
  262. response = self.put(self.api_link, data={'allowed_choices': 0})
  263. self.assertEqual(response.status_code, 400)
  264. self.assertEqual(response.json(), {
  265. 'question': ["This field is required."],
  266. 'choices': ["This field is required."],
  267. 'length': ["This field is required."],
  268. 'allowed_choices': ["Ensure this value is greater than or equal to 1."],
  269. })
  270. response = self.put(
  271. self.api_link,
  272. data={
  273. 'length': 0,
  274. 'question': "Lorem ipsum",
  275. 'allowed_choices': 3,
  276. 'choices': [
  277. {
  278. 'label': 'Choice',
  279. },
  280. {
  281. 'label': 'Choice',
  282. },
  283. ],
  284. }
  285. )
  286. self.assertEqual(response.status_code, 400)
  287. self.assertEqual(response.json(), {
  288. 'non_field_errors': [
  289. "Number of allowed choices can't be greater than number of all choices."
  290. ],
  291. })
  292. def test_poll_all_choices_replaced(self):
  293. """api edits all poll choices out"""
  294. response = self.put(
  295. self.api_link,
  296. data={
  297. 'length': 40,
  298. 'question': "Select two best colors",
  299. 'allowed_choices': 2,
  300. 'allow_revotes': True,
  301. 'is_public': True,
  302. 'choices': [
  303. {
  304. 'label': '\nRed ',
  305. },
  306. {
  307. 'label': 'Green',
  308. },
  309. {
  310. 'label': 'Blue',
  311. },
  312. ],
  313. }
  314. )
  315. self.assertEqual(response.status_code, 200)
  316. poll = Poll.objects.all()[0]
  317. add_acl(self.user, poll)
  318. response_json = response.json()
  319. self.assertEqual(response_json['poster_name'], self.user.username)
  320. self.assertEqual(response_json['length'], 40)
  321. self.assertEqual(response_json['question'], "Select two best colors")
  322. self.assertEqual(response_json['allowed_choices'], 2)
  323. self.assertTrue(response_json['allow_revotes'])
  324. # you can't change poll's type after its posted
  325. self.assertFalse(response_json['is_public'])
  326. # choices were updated
  327. self.assertEqual(len(response_json['choices']), 3)
  328. self.assertEqual(len(set([c['hash'] for c in response_json['choices']])), 3)
  329. self.assertEqual([c['label'] for c in response_json['choices']], ['Red', 'Green', 'Blue'])
  330. self.assertEqual([c['votes'] for c in response_json['choices']], [0, 0, 0])
  331. self.assertEqual([c['selected'] for c in response_json['choices']], [False, False, False])
  332. # votes were removed
  333. self.assertEqual(response_json['votes'], 0)
  334. self.assertEqual(self.poll.pollvote_set.count(), 0)
  335. def test_poll_current_choices_edited(self):
  336. """api edits current poll choices"""
  337. response = self.put(
  338. self.api_link,
  339. data={
  340. 'length': 40,
  341. 'question': "Select two best colors",
  342. 'allowed_choices': 2,
  343. 'allow_revotes': True,
  344. 'is_public': True,
  345. 'choices': [
  346. {
  347. 'hash': 'aaaaaaaaaaaa',
  348. 'label': '\nFirst ',
  349. 'votes': 5555,
  350. },
  351. {
  352. 'hash': 'bbbbbbbbbbbb',
  353. 'label': 'Second',
  354. 'votes': 5555,
  355. },
  356. {
  357. 'hash': 'gggggggggggg',
  358. 'label': 'Third',
  359. 'votes': 5555,
  360. },
  361. {
  362. 'hash': 'dddddddddddd',
  363. 'label': 'Fourth',
  364. 'votes': 5555,
  365. },
  366. ],
  367. }
  368. )
  369. self.assertEqual(response.status_code, 200)
  370. poll = Poll.objects.all()[0]
  371. add_acl(self.user, poll)
  372. self.assertEqual(response.json(), {
  373. 'id': poll.id,
  374. 'poster_name': self.user.username,
  375. 'posted_on': serialize_datetime(poll.posted_on),
  376. 'length': 40,
  377. 'question': "Select two best colors",
  378. 'allowed_choices': 2,
  379. 'allow_revotes': True,
  380. 'votes': 4,
  381. 'is_public': False,
  382. 'acl': poll.acl,
  383. 'choices': [
  384. {
  385. 'hash': 'aaaaaaaaaaaa',
  386. 'label': 'First',
  387. 'votes': 1,
  388. 'selected': False,
  389. },
  390. {
  391. 'hash': 'bbbbbbbbbbbb',
  392. 'label': 'Second',
  393. 'votes': 0,
  394. 'selected': False,
  395. },
  396. {
  397. 'hash': 'gggggggggggg',
  398. 'label': 'Third',
  399. 'votes': 2,
  400. 'selected': True,
  401. },
  402. {
  403. 'hash': 'dddddddddddd',
  404. 'label': 'Fourth',
  405. 'votes': 1,
  406. 'selected': True,
  407. },
  408. ],
  409. 'api': {
  410. 'index': poll.get_api_url(),
  411. 'votes': poll.get_votes_api_url(),
  412. },
  413. 'url': {
  414. 'poster': self.user.get_absolute_url(),
  415. },
  416. })
  417. # no votes were removed
  418. self.assertEqual(self.poll.pollvote_set.count(), 4)
  419. def test_poll_some_choices_edited(self):
  420. """api edits some poll choices"""
  421. response = self.put(
  422. self.api_link,
  423. data={
  424. 'length': 40,
  425. 'question': "Select two best colors",
  426. 'allowed_choices': 2,
  427. 'allow_revotes': True,
  428. 'is_public': True,
  429. 'choices': [
  430. {
  431. 'hash': 'aaaaaaaaaaaa',
  432. 'label': '\nFirst ',
  433. 'votes': 5555,
  434. },
  435. {
  436. 'hash': 'bbbbbbbbbbbb',
  437. 'label': 'Second',
  438. 'votes': 5555,
  439. },
  440. {
  441. 'hash': 'dsadsadsa788',
  442. 'label': 'New Option',
  443. 'votes': 5555,
  444. },
  445. ],
  446. }
  447. )
  448. self.assertEqual(response.status_code, 200)
  449. poll = Poll.objects.all()[0]
  450. add_acl(self.user, poll)
  451. self.assertEqual(response.json(), {
  452. 'id': poll.id,
  453. 'poster_name': self.user.username,
  454. 'posted_on': serialize_datetime(poll.posted_on),
  455. 'length': 40,
  456. 'question': "Select two best colors",
  457. 'allowed_choices': 2,
  458. 'allow_revotes': True,
  459. 'votes': 1,
  460. 'is_public': False,
  461. 'acl': poll.acl,
  462. 'choices': [
  463. {
  464. 'hash': 'aaaaaaaaaaaa',
  465. 'label': 'First',
  466. 'votes': 1,
  467. 'selected': False,
  468. },
  469. {
  470. 'hash': 'bbbbbbbbbbbb',
  471. 'label': 'Second',
  472. 'votes': 0,
  473. 'selected': False,
  474. },
  475. {
  476. 'hash': poll.choices[2]['hash'],
  477. 'label': 'New Option',
  478. 'votes': 0,
  479. 'selected': False,
  480. },
  481. ],
  482. 'api': {
  483. 'index': poll.get_api_url(),
  484. 'votes': poll.get_votes_api_url(),
  485. },
  486. 'url': {
  487. 'poster': self.user.get_absolute_url(),
  488. },
  489. })
  490. # no votes were removed
  491. self.assertEqual(self.poll.pollvote_set.count(), 1)
  492. def test_moderate_user_poll(self):
  493. """api edits all poll choices out in other users poll, even if its over"""
  494. self.override_acl({'can_edit_polls': 2, 'poll_edit_time': 5})
  495. self.poll.poster = None
  496. self.poll.posted_on = timezone.now() - timedelta(days=15)
  497. self.poll.length = 5
  498. self.poll.save()
  499. response = self.put(
  500. self.api_link,
  501. data={
  502. 'length': 40,
  503. 'question': "Select two best colors",
  504. 'allowed_choices': 2,
  505. 'allow_revotes': True,
  506. 'is_public': True,
  507. 'choices': [
  508. {
  509. 'label': '\nRed ',
  510. },
  511. {
  512. 'label': 'Green',
  513. },
  514. {
  515. 'label': 'Blue',
  516. },
  517. ],
  518. }
  519. )
  520. self.assertEqual(response.status_code, 200)
  521. poll = Poll.objects.all()[0]
  522. add_acl(self.user, poll)
  523. expected_choices = []
  524. for choice in poll.choices:
  525. self.assertIn(choice['label'], ["Red", "Green", "Blue"])
  526. expected_choices.append(choice.copy())
  527. expected_choices[-1]['selected'] = False
  528. self.assertEqual(response.json(), {
  529. 'id': poll.id,
  530. 'poster_name': self.user.username,
  531. 'posted_on': serialize_datetime(poll.posted_on),
  532. 'length': 40,
  533. 'question': "Select two best colors",
  534. 'allowed_choices': 2,
  535. 'allow_revotes': True,
  536. 'votes': 0,
  537. 'is_public': False,
  538. 'acl': poll.acl,
  539. 'choices': expected_choices,
  540. 'api': {
  541. 'index': poll.get_api_url(),
  542. 'votes': poll.get_votes_api_url(),
  543. },
  544. 'url': {
  545. 'poster': None,
  546. },
  547. })
  548. # votes were removed
  549. self.assertEqual(self.poll.pollvote_set.count(), 0)