test_thread_polledit_api.py 19 KB

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