test_thread_pollcreate_api.py 12 KB

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