test_thread_pollcreate_api.py 11 KB

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