test_thread_pollcreate_api.py 9.7 KB

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