test_thread_polledit_api.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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(ThreadPollEditTests, self).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. def test_poll_current_choices_edited(self):
  275. """api edits current poll choices"""
  276. response = self.put(
  277. self.api_link,
  278. data={
  279. 'length': 40,
  280. 'question': "Select two best colors",
  281. 'allowed_choices': 2,
  282. 'allow_revotes': True,
  283. 'is_public': True,
  284. 'choices': [
  285. {
  286. 'hash': 'aaaaaaaaaaaa',
  287. 'label': '\nFirst ',
  288. 'votes': 5555,
  289. },
  290. {
  291. 'hash': 'bbbbbbbbbbbb',
  292. 'label': 'Second',
  293. 'votes': 5555,
  294. },
  295. {
  296. 'hash': 'gggggggggggg',
  297. 'label': 'Third',
  298. 'votes': 5555,
  299. },
  300. {
  301. 'hash': 'dddddddddddd',
  302. 'label': 'Fourth',
  303. 'votes': 5555,
  304. },
  305. ],
  306. }
  307. )
  308. self.assertEqual(response.status_code, 200)
  309. response_json = response.json()
  310. self.assertEqual(response_json['poster_name'], self.user.username)
  311. self.assertEqual(response_json['length'], 40)
  312. self.assertEqual(response_json['question'], "Select two best colors")
  313. self.assertEqual(response_json['allowed_choices'], 2)
  314. self.assertTrue(response_json['allow_revotes'])
  315. # you can't change poll's type after its posted
  316. self.assertFalse(response_json['is_public'])
  317. # choices were updated
  318. self.assertEqual(len(response_json['choices']), 4)
  319. self.assertEqual(
  320. response_json['choices'],
  321. [
  322. {
  323. 'hash': 'aaaaaaaaaaaa',
  324. 'label': 'First',
  325. 'votes': 1,
  326. 'selected': False,
  327. },
  328. {
  329. 'hash': 'bbbbbbbbbbbb',
  330. 'label': 'Second',
  331. 'votes': 0,
  332. 'selected': False,
  333. },
  334. {
  335. 'hash': 'gggggggggggg',
  336. 'label': 'Third',
  337. 'votes': 2,
  338. 'selected': True,
  339. },
  340. {
  341. 'hash': 'dddddddddddd',
  342. 'label': 'Fourth',
  343. 'votes': 1,
  344. 'selected': True,
  345. },
  346. ],
  347. )
  348. # no votes were removed
  349. self.assertEqual(response_json['votes'], 4)
  350. self.assertEqual(self.poll.pollvote_set.count(), 4)
  351. def test_poll_some_choices_edited(self):
  352. """api edits some poll choices"""
  353. response = self.put(
  354. self.api_link,
  355. data={
  356. 'length': 40,
  357. 'question': "Select two best colors",
  358. 'allowed_choices': 2,
  359. 'allow_revotes': True,
  360. 'is_public': True,
  361. 'choices': [
  362. {
  363. 'hash': 'aaaaaaaaaaaa',
  364. 'label': '\nFirst ',
  365. 'votes': 5555,
  366. },
  367. {
  368. 'hash': 'bbbbbbbbbbbb',
  369. 'label': 'Second',
  370. 'votes': 5555,
  371. },
  372. {
  373. 'hash': 'dsadsadsa788',
  374. 'label': 'New Option',
  375. 'votes': 5555,
  376. },
  377. ],
  378. }
  379. )
  380. self.assertEqual(response.status_code, 200)
  381. response_json = response.json()
  382. self.assertEqual(response_json['poster_name'], self.user.username)
  383. self.assertEqual(response_json['length'], 40)
  384. self.assertEqual(response_json['question'], "Select two best colors")
  385. self.assertEqual(response_json['allowed_choices'], 2)
  386. self.assertTrue(response_json['allow_revotes'])
  387. # you can't change poll's type after its posted
  388. self.assertFalse(response_json['is_public'])
  389. # choices were updated
  390. self.assertEqual(len(response_json['choices']), 3)
  391. self.assertEqual(
  392. response_json['choices'],
  393. [
  394. {
  395. 'hash': 'aaaaaaaaaaaa',
  396. 'label': 'First',
  397. 'votes': 1,
  398. 'selected': False,
  399. },
  400. {
  401. 'hash': 'bbbbbbbbbbbb',
  402. 'label': 'Second',
  403. 'votes': 0,
  404. 'selected': False,
  405. },
  406. {
  407. 'hash': response_json['choices'][2]['hash'],
  408. 'label': 'New Option',
  409. 'votes': 0,
  410. 'selected': False,
  411. },
  412. ],
  413. )
  414. # no votes were removed
  415. self.assertEqual(response_json['votes'], 1)
  416. self.assertEqual(self.poll.pollvote_set.count(), 1)
  417. def test_moderate_user_poll(self):
  418. """api edits all poll choices out in other users poll, even if its over"""
  419. self.override_acl({'can_edit_polls': 2, 'poll_edit_time': 5})
  420. self.poll.poster = None
  421. self.poll.posted_on = timezone.now() - timedelta(days=15)
  422. self.poll.length = 5
  423. self.poll.save()
  424. response = self.put(
  425. self.api_link,
  426. data={
  427. 'length': 40,
  428. 'question': "Select two best colors",
  429. 'allowed_choices': 2,
  430. 'allow_revotes': True,
  431. 'is_public': True,
  432. 'choices': [
  433. {
  434. 'label': '\nRed ',
  435. },
  436. {
  437. 'label': 'Green',
  438. },
  439. {
  440. 'label': 'Blue',
  441. },
  442. ],
  443. }
  444. )
  445. self.assertEqual(response.status_code, 200)
  446. response_json = response.json()
  447. self.assertEqual(response_json['poster_name'], self.user.username)
  448. self.assertEqual(response_json['length'], 40)
  449. self.assertEqual(response_json['question'], "Select two best colors")
  450. self.assertEqual(response_json['allowed_choices'], 2)
  451. self.assertTrue(response_json['allow_revotes'])
  452. # you can't change poll's type after its posted
  453. self.assertFalse(response_json['is_public'])
  454. # choices were updated
  455. self.assertEqual(len(response_json['choices']), 3)
  456. self.assertEqual(len(set([c['hash'] for c in response_json['choices']])), 3)
  457. self.assertEqual([c['label'] for c in response_json['choices']], ['Red', 'Green', 'Blue'])
  458. self.assertEqual([c['votes'] for c in response_json['choices']], [0, 0, 0])
  459. self.assertEqual([c['selected'] for c in response_json['choices']], [False, False, False])
  460. # votes were removed
  461. self.assertEqual(response_json['votes'], 0)
  462. self.assertEqual(self.poll.pollvote_set.count(), 0)