test_poll_api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import json
  2. from django.core.urlresolvers import reverse
  3. from misago.acl.testutils import override_acl
  4. from misago.categories.models import Category
  5. from misago.users.testutils import AuthenticatedUserTestCase
  6. from .. import testutils
  7. from ..models import Poll
  8. from ..serializers.poll import MAX_POLL_OPTIONS
  9. class ThreadPollApiTestCase(AuthenticatedUserTestCase):
  10. def setUp(self):
  11. super(ThreadPollApiTestCase, self).setUp()
  12. self.category = Category.objects.get(slug='first-category')
  13. self.thread = testutils.post_thread(self.category, poster=self.user)
  14. self.override_acl()
  15. self.api_link = reverse('misago:api:thread-poll-list', kwargs={
  16. 'thread_pk': self.thread.pk
  17. })
  18. def post(self, url, data=None):
  19. return self.client.post(url, json.dumps(data or {}), content_type='application/json')
  20. def override_acl(self, user=None, category=None):
  21. new_acl = self.user.acl
  22. new_acl['categories'][self.category.pk].update({
  23. 'can_see': 1,
  24. 'can_browse': 1,
  25. 'can_close_threads': 0,
  26. })
  27. new_acl.update({
  28. 'can_start_polls': 1,
  29. 'can_edit_polls': 1,
  30. 'can_delete_polls': 1,
  31. 'poll_edit_time': 0,
  32. 'can_always_see_poll_voters': 0
  33. })
  34. if user:
  35. new_acl.update(user)
  36. if category:
  37. new_acl['categories'][self.category.pk].update(category)
  38. override_acl(self.user, new_acl)
  39. class ThreadPollCreateTests(ThreadPollApiTestCase):
  40. def test_anonymous(self):
  41. """api requires you to sign in to create poll"""
  42. self.logout_user()
  43. response = self.post(self.api_link)
  44. self.assertEqual(response.status_code, 403)
  45. def test_invalid_thread_id(self):
  46. """api validates that thread id is integer"""
  47. api_link = reverse('misago:api:thread-poll-list', kwargs={
  48. 'thread_pk': 'kjha6dsa687sa'
  49. })
  50. response = self.post(api_link)
  51. self.assertEqual(response.status_code, 404)
  52. def test_nonexistant_thread_id(self):
  53. """api validates that thread exists"""
  54. api_link = reverse('misago:api:thread-poll-list', kwargs={
  55. 'thread_pk': self.thread.pk + 1
  56. })
  57. response = self.post(api_link)
  58. self.assertEqual(response.status_code, 404)
  59. def test_no_permission(self):
  60. """api validates that user has permission to start poll in thread"""
  61. self.override_acl({
  62. 'can_start_polls': 0
  63. })
  64. response = self.post(self.api_link)
  65. self.assertContains(response, "can't start polls", status_code=403)
  66. def test_no_permission_closed_thread(self):
  67. """api validates that user has permission to start poll in closed thread"""
  68. self.override_acl(category={
  69. 'can_close_threads': 0
  70. })
  71. self.thread.is_closed = True
  72. self.thread.save()
  73. response = self.post(self.api_link)
  74. self.assertContains(response, "thread is closed", status_code=403)
  75. self.override_acl(category={
  76. 'can_close_threads': 1
  77. })
  78. response = self.post(self.api_link)
  79. self.assertEqual(response.status_code, 400)
  80. def test_no_permission_closed_thread(self):
  81. """api validates that user has permission to start poll in closed category"""
  82. self.override_acl(category={
  83. 'can_close_threads': 0
  84. })
  85. self.category.is_closed = True
  86. self.category.save()
  87. response = self.post(self.api_link)
  88. self.assertContains(response, "category is closed", status_code=403)
  89. self.override_acl(category={
  90. 'can_close_threads': 1
  91. })
  92. response = self.post(self.api_link)
  93. self.assertEqual(response.status_code, 400)
  94. def test_no_permission_other_user_thread(self):
  95. """api validates that user has permission to start poll in other user's thread"""
  96. self.override_acl({
  97. 'can_start_polls': 1
  98. })
  99. self.thread.starter = None
  100. self.thread.save()
  101. response = self.post(self.api_link)
  102. self.assertContains(response, "can't start polls in other users threads", status_code=403)
  103. self.override_acl({
  104. 'can_start_polls': 2
  105. })
  106. response = self.post(self.api_link)
  107. self.assertEqual(response.status_code, 400)
  108. def test_no_permission_poll_exists(self):
  109. """api validates that user can't start second poll in thread"""
  110. self.thread.poll = Poll.objects.create(
  111. thread=self.thread,
  112. category=self.category,
  113. poster_name='Test',
  114. poster_slug='test',
  115. poster_ip='127.0.0.1',
  116. length=30,
  117. question='Test',
  118. choices=[{'hash': 't3st'}],
  119. allowed_choices=1
  120. )
  121. response = self.post(self.api_link)
  122. self.assertContains(response, "There's already a poll in this thread.", status_code=403)
  123. def test_empty_data(self):
  124. """api handles empty request data"""
  125. response = self.post(self.api_link)
  126. self.assertEqual(response.status_code, 400)
  127. response_json = response.json()
  128. self.assertEqual(len(response_json), 4)
  129. def test_length_validation(self):
  130. """api validates poll's length"""
  131. response = self.post(self.api_link, data={
  132. 'length': -1
  133. })
  134. self.assertEqual(response.status_code, 400)
  135. response_json = response.json()
  136. self.assertEqual(response_json['length'], [
  137. "Ensure this value is greater than or equal to 0."
  138. ])
  139. response = self.post(self.api_link, data={
  140. 'length': 200
  141. })
  142. self.assertEqual(response.status_code, 400)
  143. response_json = response.json()
  144. self.assertEqual(response_json['length'], [
  145. "Ensure this value is less than or equal to 180."
  146. ])
  147. def test_question_validation(self):
  148. """api validates question length"""
  149. response = self.post(self.api_link, data={
  150. 'question': 'abcd' * 255
  151. })
  152. self.assertEqual(response.status_code, 400)
  153. response_json = response.json()
  154. self.assertEqual(response_json['question'], [
  155. "Ensure this field has no more than 255 characters."
  156. ])
  157. def test_validate_choice_length(self):
  158. """api validates single choice length"""
  159. response = self.post(self.api_link, data={
  160. 'choices': [
  161. {
  162. 'hash': 'qwertyuiopas',
  163. 'label': ''
  164. }
  165. ]
  166. })
  167. self.assertEqual(response.status_code, 400)
  168. response_json = response.json()
  169. self.assertEqual(response_json['choices'], [
  170. "One or more poll choices are invalid."
  171. ])
  172. response = self.post(self.api_link, data={
  173. 'choices': [
  174. {
  175. 'hash': 'qwertyuiopas',
  176. 'label': 'abcd' * 255
  177. }
  178. ]
  179. })
  180. self.assertEqual(response.status_code, 400)
  181. response_json = response.json()
  182. self.assertEqual(response_json['choices'], [
  183. "One or more poll choices are invalid."
  184. ])
  185. def test_validate_two_choices(self):
  186. """api validates that there are at least two choices in poll"""
  187. response = self.post(self.api_link, data={
  188. 'choices': [
  189. {
  190. 'label': 'Choice'
  191. }
  192. ]
  193. })
  194. self.assertEqual(response.status_code, 400)
  195. response_json = response.json()
  196. self.assertEqual(response_json['choices'], [
  197. "You need to add at least two choices to a poll."
  198. ])
  199. def test_validate_max_choices(self):
  200. """api validates that there are no more choices in poll than allowed number"""
  201. response = self.post(self.api_link, data={
  202. 'choices': [
  203. {
  204. 'label': 'Choice'
  205. }
  206. ] * (MAX_POLL_OPTIONS + 1)
  207. })
  208. self.assertEqual(response.status_code, 400)
  209. error_formats = (MAX_POLL_OPTIONS, MAX_POLL_OPTIONS + 1)
  210. response_json = response.json()
  211. self.assertEqual(response_json['choices'], [
  212. "You can't add more than %s options to a single poll (added %s)." % error_formats
  213. ])
  214. def test_allowed_choices_validation(self):
  215. """api validates allowed choices number"""
  216. response = self.post(self.api_link, data={
  217. 'allowed_choices': 0
  218. })
  219. self.assertEqual(response.status_code, 400)
  220. response_json = response.json()
  221. self.assertEqual(response_json['allowed_choices'], [
  222. "Ensure this value is greater than or equal to 1."
  223. ])
  224. response = self.post(self.api_link, data={
  225. 'length': 0,
  226. 'question': "Lorem ipsum",
  227. 'allowed_choices': 3,
  228. 'choices': [
  229. {
  230. 'label': 'Choice'
  231. },
  232. {
  233. 'label': 'Choice'
  234. }
  235. ]
  236. })
  237. self.assertEqual(response.status_code, 400)
  238. response_json = response.json()
  239. self.assertEqual(response_json['non_field_errors'], [
  240. "Number of allowed choices can't be greater than number of all choices."
  241. ])
  242. def test_poll_created(self):
  243. """api creates public poll if provided with valid data"""
  244. response = self.post(self.api_link, data={
  245. 'length': 40,
  246. 'question': "Select two best colors",
  247. 'allowed_choices': 2,
  248. 'allow_revotes': True,
  249. 'is_public': True,
  250. 'choices': [
  251. {
  252. 'label': 'Red'
  253. },
  254. {
  255. 'label': 'Green'
  256. },
  257. {
  258. 'label': 'Blue'
  259. }
  260. ]
  261. })
  262. self.assertEqual(response.status_code, 200)
  263. response_json = response.json()
  264. self.assertEqual(response_json['poster_name'], self.user.username)
  265. self.assertEqual(response_json['poster_slug'], self.user.slug)
  266. self.assertEqual(response_json['length'], 40)
  267. self.assertEqual(response_json['question'], "Select two best colors")
  268. self.assertEqual(response_json['allowed_choices'], 2)
  269. self.assertTrue(response_json['allow_revotes'])
  270. self.assertEqual(response_json['votes'], 0)
  271. self.assertTrue(response_json['is_public'])
  272. self.assertEqual(len(response_json['choices']), 3)
  273. self.assertEqual(len(set([c['hash'] for c in response_json['choices']])), 3)
  274. poll = self.thread.poll
  275. self.assertEqual(poll.category_id, self.category.id)
  276. self.assertEqual(poll.thread_id, self.thread.id)
  277. self.assertEqual(poll.poster_id, self.user.id)
  278. self.assertEqual(poll.poster_name, self.user.username)
  279. self.assertEqual(poll.poster_slug, self.user.slug)
  280. self.assertEqual(poll.length, 40)
  281. self.assertEqual(poll.question, "Select two best colors")
  282. self.assertEqual(poll.allowed_choices, 2)
  283. self.assertTrue(poll.allow_revotes)
  284. self.assertEqual(poll.votes, 0)
  285. self.assertTrue(poll.is_public)
  286. self.assertEqual(len(poll.choices), 3)
  287. self.assertEqual(len(set([c['hash'] for c in poll.choices])), 3)
  288. class ThreadPollEditTests(ThreadPollApiTestCase):
  289. pass
  290. class ThreadPollDeleteTests(ThreadPollApiTestCase):
  291. pass
  292. class ThreadPollVoteTests(ThreadPollApiTestCase):
  293. pass
  294. class ThreadPollVotersTests(ThreadPollApiTestCase):
  295. pass