test_thread_polledit_api.py 19 KB

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