test_thread_polledit_api.py 20 KB

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