test_thread_polledit_api.py 17 KB

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