test_thread_polledit_api.py 18 KB

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