test_thread_polledit_api.py 17 KB

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