test_thread_pollcreate_api.py 10 KB

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