test_thread_polledit_api.py 18 KB

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