test_thread_polledit_api.py 17 KB

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