test_thread_pollcreate_api.py 10 KB

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