test_thread_pollcreate_api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. def test_length_validation(self):
  120. """api validates poll's length"""
  121. response = self.post(
  122. self.api_link, data={
  123. 'length': -1,
  124. }
  125. )
  126. self.assertEqual(response.status_code, 400)
  127. self.assertEqual(response.json(), {
  128. 'question': ["This field is required."],
  129. 'choices': ["This field is required."],
  130. 'length': ["Ensure this value is greater than or equal to 0."],
  131. 'allowed_choices': ["This field is required."],
  132. })
  133. response = self.post(
  134. self.api_link, data={
  135. 'length': 200,
  136. }
  137. )
  138. self.assertEqual(response.status_code, 400)
  139. self.assertEqual(response.json(), {
  140. 'question': ["This field is required."],
  141. 'choices': ["This field is required."],
  142. 'length': ["Ensure this value is less than or equal to 180."],
  143. 'allowed_choices': ["This field is required."],
  144. })
  145. def test_question_validation(self):
  146. """api validates question length"""
  147. response = self.post(
  148. self.api_link, data={
  149. 'question': 'abcd' * 255,
  150. }
  151. )
  152. self.assertEqual(response.status_code, 400)
  153. self.assertEqual(response.json(), {
  154. 'question': ["Ensure this field has no more than 255 characters."],
  155. 'choices': ["This field is required."],
  156. 'length': ["This field is required."],
  157. 'allowed_choices': ["This field is required."],
  158. })
  159. def test_validate_choice_length(self):
  160. """api validates single choice length"""
  161. response = self.post(
  162. self.api_link, data={
  163. 'choices': [
  164. {
  165. 'hash': 'qwertyuiopas',
  166. 'label': '',
  167. },
  168. ],
  169. }
  170. )
  171. self.assertEqual(response.status_code, 400)
  172. self.assertEqual(response.json(), {
  173. 'question': ["This field is required."],
  174. 'choices': ["One or more poll choices are invalid."],
  175. 'length': ["This field is required."],
  176. 'allowed_choices': ["This field is required."],
  177. })
  178. response = self.post(
  179. self.api_link,
  180. data={
  181. 'choices': [
  182. {
  183. 'hash': 'qwertyuiopas',
  184. 'label': 'abcd' * 255,
  185. },
  186. ],
  187. }
  188. )
  189. self.assertEqual(response.status_code, 400)
  190. self.assertEqual(response.json(), {
  191. 'question': ["This field is required."],
  192. 'choices': ["One or more poll choices are invalid."],
  193. 'length': ["This field is required."],
  194. 'allowed_choices': ["This field is required."],
  195. })
  196. def test_validate_two_choices(self):
  197. """api validates that there are at least two choices in poll"""
  198. response = self.post(self.api_link, data={'choices': [{'label': 'Choice'}]})
  199. self.assertEqual(response.status_code, 400)
  200. self.assertEqual(response.json(), {
  201. 'question': ["This field is required."],
  202. 'choices': ["You need to add at least two choices to a poll."],
  203. 'length': ["This field is required."],
  204. 'allowed_choices': ["This field is required."],
  205. })
  206. def test_validate_max_choices(self):
  207. """api validates that there are no more choices in poll than allowed number"""
  208. response = self.post(
  209. self.api_link, data={
  210. 'choices': [
  211. {
  212. 'label': 'Choice',
  213. },
  214. ] * (MAX_POLL_OPTIONS + 1),
  215. }
  216. )
  217. self.assertEqual(response.status_code, 400)
  218. error_formats = (MAX_POLL_OPTIONS, MAX_POLL_OPTIONS + 1)
  219. self.assertEqual(response.json(), {
  220. 'question': ["This field is required."],
  221. 'choices': [
  222. "You can't add more than %s options to a single poll (added %s)." % error_formats
  223. ],
  224. 'length': ["This field is required."],
  225. 'allowed_choices': ["This field is required."],
  226. })
  227. def test_allowed_choices_validation(self):
  228. """api validates allowed choices number"""
  229. response = self.post(self.api_link, data={'allowed_choices': 0})
  230. self.assertEqual(response.status_code, 400)
  231. self.assertEqual(response.json(), {
  232. 'question': ["This field is required."],
  233. 'choices': ["This field is required."],
  234. 'length': ["This field is required."],
  235. 'allowed_choices': ["Ensure this value is greater than or equal to 1."],
  236. })
  237. response = self.post(
  238. self.api_link,
  239. data={
  240. 'length': 0,
  241. 'question': "Lorem ipsum",
  242. 'allowed_choices': 3,
  243. 'choices': [
  244. {
  245. 'label': 'Choice',
  246. },
  247. {
  248. 'label': 'Choice',
  249. },
  250. ],
  251. }
  252. )
  253. self.assertEqual(response.status_code, 400)
  254. self.assertEqual(response.json(), {
  255. 'non_field_errors': [
  256. "Number of allowed choices can't be greater than number of all choices."
  257. ],
  258. })
  259. def test_poll_created(self):
  260. """api creates public poll if provided with valid data"""
  261. response = self.post(
  262. self.api_link,
  263. data={
  264. 'length': 40,
  265. 'question': "Select two best colors",
  266. 'allowed_choices': 2,
  267. 'allow_revotes': True,
  268. 'is_public': True,
  269. 'choices': [
  270. {
  271. 'label': '\nRed ',
  272. },
  273. {
  274. 'label': 'Green',
  275. },
  276. {
  277. 'label': 'Blue',
  278. },
  279. ],
  280. }
  281. )
  282. self.assertEqual(response.status_code, 200)
  283. self.maxDiff = None
  284. poll = Poll.objects.all()[0]
  285. add_acl(self.user, poll)
  286. expected_choices = []
  287. for choice in poll.choices:
  288. expected_choices.append(choice.copy())
  289. expected_choices[-1]['selected'] = False
  290. self.assertEqual(response.json(), {
  291. 'id': poll.id,
  292. 'poster_name': self.user.username,
  293. 'posted_on': serialize_datetime(poll.posted_on),
  294. 'length': 40,
  295. 'question': "Select two best colors",
  296. 'allowed_choices': 2,
  297. 'allow_revotes': True,
  298. 'votes': 0,
  299. 'is_public': True,
  300. 'acl': poll.acl,
  301. 'choices': expected_choices,
  302. 'api': {
  303. 'index': poll.get_api_url(),
  304. 'votes': poll.get_votes_api_url(),
  305. },
  306. 'url': {
  307. 'poster': self.user.get_absolute_url(),
  308. },
  309. })
  310. self.assertEqual(len(poll.choices), 3)
  311. self.assertEqual(len(set([c['hash'] for c in poll.choices])), 3)
  312. self.assertEqual([c['label'] for c in poll.choices], ['Red', 'Green', 'Blue'])
  313. thread = Thread.objects.get(pk=self.thread.pk)
  314. self.assertTrue(thread.has_poll)
  315. self.assertEqual(thread.poll, poll)