test_thread_polledit_api.py 18 KB

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