test_thread_pollcreate_api.py 12 KB

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