test_thread_pollcreate_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. length=30,
  72. question='Test',
  73. choices=[
  74. {
  75. 'hash': 't3st'
  76. },
  77. ],
  78. allowed_choices=1,
  79. )
  80. response = self.post(self.api_link)
  81. self.assertContains(response, "There's already a poll in this thread.", status_code=403)
  82. def test_empty_data(self):
  83. """api handles empty request data"""
  84. response = self.post(self.api_link)
  85. self.assertEqual(response.status_code, 400)
  86. response_json = response.json()
  87. self.assertEqual(len(response_json), 4)
  88. def test_length_validation(self):
  89. """api validates poll's length"""
  90. response = self.post(
  91. self.api_link, data={
  92. 'length': -1,
  93. }
  94. )
  95. self.assertEqual(response.status_code, 400)
  96. response_json = response.json()
  97. self.assertEqual(
  98. response_json['length'], ["Ensure this value is greater than or equal to 0."]
  99. )
  100. response = self.post(
  101. self.api_link, data={
  102. 'length': 200,
  103. }
  104. )
  105. self.assertEqual(response.status_code, 400)
  106. response_json = response.json()
  107. self.assertEqual(
  108. response_json['length'], ["Ensure this value is less than or equal to 180."]
  109. )
  110. def test_question_validation(self):
  111. """api validates question length"""
  112. response = self.post(
  113. self.api_link, data={
  114. 'question': 'abcd' * 255,
  115. }
  116. )
  117. self.assertEqual(response.status_code, 400)
  118. response_json = response.json()
  119. self.assertEqual(
  120. response_json['question'], ["Ensure this field has no more than 255 characters."]
  121. )
  122. def test_validate_choice_length(self):
  123. """api validates single choice length"""
  124. response = self.post(
  125. self.api_link, data={
  126. 'choices': [
  127. {
  128. 'hash': 'qwertyuiopas',
  129. 'label': '',
  130. },
  131. ],
  132. }
  133. )
  134. self.assertEqual(response.status_code, 400)
  135. response_json = response.json()
  136. self.assertEqual(response_json['choices'], ["One or more poll choices are invalid."])
  137. response = self.post(
  138. self.api_link,
  139. data={
  140. 'choices': [
  141. {
  142. 'hash': 'qwertyuiopas',
  143. 'label': 'abcd' * 255,
  144. },
  145. ],
  146. }
  147. )
  148. self.assertEqual(response.status_code, 400)
  149. response_json = response.json()
  150. self.assertEqual(response_json['choices'], ["One or more poll choices are invalid."])
  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={'choices': [{'label': 'Choice'}]})
  154. self.assertEqual(response.status_code, 400)
  155. response_json = response.json()
  156. self.assertEqual(
  157. response_json['choices'], ["You need to add at least two choices to a poll."]
  158. )
  159. def test_validate_max_choices(self):
  160. """api validates that there are no more choices in poll than allowed number"""
  161. response = self.post(
  162. self.api_link, data={
  163. 'choices': [
  164. {
  165. 'label': 'Choice',
  166. },
  167. ] * (MAX_POLL_OPTIONS + 1),
  168. }
  169. )
  170. self.assertEqual(response.status_code, 400)
  171. error_formats = (MAX_POLL_OPTIONS, MAX_POLL_OPTIONS + 1)
  172. response_json = response.json()
  173. self.assertEqual(
  174. response_json['choices'],
  175. ["You can't add more than %s options to a single poll (added %s)." % error_formats]
  176. )
  177. def test_allowed_choices_validation(self):
  178. """api validates allowed choices number"""
  179. response = self.post(self.api_link, data={'allowed_choices': 0})
  180. self.assertEqual(response.status_code, 400)
  181. response_json = response.json()
  182. self.assertEqual(
  183. response_json['allowed_choices'], ["Ensure this value is greater than or equal to 1."]
  184. )
  185. response = self.post(
  186. self.api_link,
  187. data={
  188. 'length': 0,
  189. 'question': "Lorem ipsum",
  190. 'allowed_choices': 3,
  191. 'choices': [
  192. {
  193. 'label': 'Choice',
  194. },
  195. {
  196. 'label': 'Choice',
  197. },
  198. ],
  199. }
  200. )
  201. self.assertEqual(response.status_code, 400)
  202. response_json = response.json()
  203. self.assertEqual(
  204. response_json['non_field_errors'],
  205. ["Number of allowed choices can't be greater than number of all choices."]
  206. )
  207. def test_poll_created(self):
  208. """api creates public poll if provided with valid data"""
  209. response = self.post(
  210. self.api_link,
  211. data={
  212. 'length': 40,
  213. 'question': "Select two best colors",
  214. 'allowed_choices': 2,
  215. 'allow_revotes': True,
  216. 'is_public': True,
  217. 'choices': [
  218. {
  219. 'label': '\nRed ',
  220. },
  221. {
  222. 'label': 'Green',
  223. },
  224. {
  225. 'label': 'Blue',
  226. },
  227. ],
  228. }
  229. )
  230. self.assertEqual(response.status_code, 200)
  231. response_json = response.json()
  232. self.assertEqual(response_json['poster_name'], self.user.username)
  233. self.assertEqual(response_json['length'], 40)
  234. self.assertEqual(response_json['question'], "Select two best colors")
  235. self.assertEqual(response_json['allowed_choices'], 2)
  236. self.assertTrue(response_json['allow_revotes'])
  237. self.assertEqual(response_json['votes'], 0)
  238. self.assertTrue(response_json['is_public'])
  239. self.assertEqual(len(response_json['choices']), 3)
  240. self.assertEqual(len(set([c['hash'] for c in response_json['choices']])), 3)
  241. self.assertEqual([c['label'] for c in response_json['choices']], ['Red', 'Green', 'Blue'])
  242. thread = Thread.objects.get(pk=self.thread.pk)
  243. self.assertTrue(thread.has_poll)
  244. poll = thread.poll
  245. self.assertEqual(poll.category_id, self.category.id)
  246. self.assertEqual(poll.thread_id, self.thread.id)
  247. self.assertEqual(poll.poster_id, self.user.id)
  248. self.assertEqual(poll.poster_name, self.user.username)
  249. self.assertEqual(poll.poster_slug, self.user.slug)
  250. self.assertEqual(poll.length, 40)
  251. self.assertEqual(poll.question, "Select two best colors")
  252. self.assertEqual(poll.allowed_choices, 2)
  253. self.assertTrue(poll.allow_revotes)
  254. self.assertEqual(poll.votes, 0)
  255. self.assertTrue(poll.is_public)
  256. self.assertEqual(len(poll.choices), 3)
  257. self.assertEqual(len(set([c['hash'] for c in poll.choices])), 3)
  258. self.assertEqual(self.user.audittrail_set.count(), 1)