test_thread_pollcreate_api.py 11 KB

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