test_thread_pollcreate_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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. """api validates that user has permission to start poll in other user's thread"""
  76. self.thread.starter = None
  77. self.thread.save()
  78. response = self.post(self.api_link)
  79. self.assertEqual(response.status_code, 403)
  80. self.assertEqual(
  81. response.json(), {"detail": "You can't start polls in other users threads."}
  82. )
  83. @patch_user_acl({"can_start_polls": 2})
  84. def test_other_user_thread(self):
  85. """api validates that user has permission to start poll in other user's thread"""
  86. self.thread.starter = None
  87. self.thread.save()
  88. response = self.post(self.api_link)
  89. self.assertEqual(response.status_code, 400)
  90. def test_no_permission_poll_exists(self):
  91. """api validates that user can't start second poll in thread"""
  92. self.thread.poll = Poll.objects.create(
  93. thread=self.thread,
  94. category=self.category,
  95. poster_name="Test",
  96. poster_slug="test",
  97. length=30,
  98. question="Test",
  99. choices=[{"hash": "t3st"}],
  100. allowed_choices=1,
  101. )
  102. response = self.post(self.api_link)
  103. self.assertEqual(response.status_code, 403)
  104. self.assertEqual(
  105. response.json(), {"detail": "There's already a poll in this thread."}
  106. )
  107. def test_empty_data(self):
  108. """api handles empty request data"""
  109. response = self.post(self.api_link)
  110. self.assertEqual(response.status_code, 400)
  111. response_json = response.json()
  112. self.assertEqual(len(response_json), 4)
  113. def test_length_validation(self):
  114. """api validates poll's length"""
  115. response = self.post(self.api_link, data={"length": -1})
  116. self.assertEqual(response.status_code, 400)
  117. response_json = response.json()
  118. self.assertEqual(
  119. response_json["length"],
  120. ["Ensure this value is greater than or equal to 0."],
  121. )
  122. response = self.post(self.api_link, data={"length": 200})
  123. self.assertEqual(response.status_code, 400)
  124. response_json = response.json()
  125. self.assertEqual(
  126. response_json["length"], ["Ensure this value is less than or equal to 180."]
  127. )
  128. def test_question_validation(self):
  129. """api validates question length"""
  130. response = self.post(self.api_link, data={"question": "abcd" * 255})
  131. self.assertEqual(response.status_code, 400)
  132. response_json = response.json()
  133. self.assertEqual(
  134. response_json["question"],
  135. ["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={"choices": [{"hash": "qwertyuiopas", "label": ""}]}
  141. )
  142. self.assertEqual(response.status_code, 400)
  143. response_json = response.json()
  144. self.assertEqual(
  145. response_json["choices"], ["One or more poll choices are invalid."]
  146. )
  147. response = self.post(
  148. self.api_link,
  149. data={"choices": [{"hash": "qwertyuiopas", "label": "abcd" * 255}]},
  150. )
  151. self.assertEqual(response.status_code, 400)
  152. response_json = response.json()
  153. self.assertEqual(
  154. response_json["choices"], ["One or more poll choices are invalid."]
  155. )
  156. def test_validate_two_choices(self):
  157. """api validates that there are at least two choices in poll"""
  158. response = self.post(self.api_link, data={"choices": [{"label": "Choice"}]})
  159. self.assertEqual(response.status_code, 400)
  160. response_json = response.json()
  161. self.assertEqual(
  162. response_json["choices"],
  163. ["You need to add at least two choices to a poll."],
  164. )
  165. def test_validate_max_choices(self):
  166. """api validates that there are no more choices in poll than allowed number"""
  167. response = self.post(
  168. self.api_link,
  169. data={"choices": [{"label": "Choice"}] * (MAX_POLL_OPTIONS + 1)},
  170. )
  171. self.assertEqual(response.status_code, 400)
  172. error_formats = (MAX_POLL_OPTIONS, MAX_POLL_OPTIONS + 1)
  173. response_json = response.json()
  174. self.assertEqual(
  175. response_json["choices"],
  176. [
  177. "You can't add more than %s options to a single poll (added %s)."
  178. % error_formats
  179. ],
  180. )
  181. def test_allowed_choices_validation(self):
  182. """api validates allowed choices number"""
  183. response = self.post(self.api_link, data={"allowed_choices": 0})
  184. self.assertEqual(response.status_code, 400)
  185. response_json = response.json()
  186. self.assertEqual(
  187. response_json["allowed_choices"],
  188. ["Ensure this value is greater than or equal to 1."],
  189. )
  190. response = self.post(
  191. self.api_link,
  192. data={
  193. "length": 0,
  194. "question": "Lorem ipsum",
  195. "allowed_choices": 3,
  196. "choices": [{"label": "Choice"}, {"label": "Choice"}],
  197. },
  198. )
  199. self.assertEqual(response.status_code, 400)
  200. response_json = response.json()
  201. self.assertEqual(
  202. response_json["non_field_errors"],
  203. ["Number of allowed choices can't be greater than number of all choices."],
  204. )
  205. def test_poll_created(self):
  206. """api creates public poll if provided with valid data"""
  207. response = self.post(
  208. self.api_link,
  209. data={
  210. "length": 40,
  211. "question": "Select two best colors",
  212. "allowed_choices": 2,
  213. "allow_revotes": True,
  214. "is_public": True,
  215. "choices": [{"label": "\nRed "}, {"label": "Green"}, {"label": "Blue"}],
  216. },
  217. )
  218. self.assertEqual(response.status_code, 200)
  219. response_json = response.json()
  220. self.assertEqual(response_json["poster_name"], self.user.username)
  221. self.assertEqual(response_json["length"], 40)
  222. self.assertEqual(response_json["question"], "Select two best colors")
  223. self.assertEqual(response_json["allowed_choices"], 2)
  224. self.assertTrue(response_json["allow_revotes"])
  225. self.assertEqual(response_json["votes"], 0)
  226. self.assertTrue(response_json["is_public"])
  227. self.assertEqual(len(response_json["choices"]), 3)
  228. self.assertEqual(len(set([c["hash"] for c in response_json["choices"]])), 3)
  229. self.assertEqual(
  230. [c["label"] for c in response_json["choices"]], ["Red", "Green", "Blue"]
  231. )
  232. thread = Thread.objects.get(pk=self.thread.pk)
  233. self.assertTrue(thread.has_poll)
  234. poll = thread.poll
  235. self.assertEqual(poll.category_id, self.category.id)
  236. self.assertEqual(poll.thread_id, self.thread.id)
  237. self.assertEqual(poll.poster_id, self.user.id)
  238. self.assertEqual(poll.poster_name, self.user.username)
  239. self.assertEqual(poll.poster_slug, self.user.slug)
  240. self.assertEqual(poll.length, 40)
  241. self.assertEqual(poll.question, "Select two best colors")
  242. self.assertEqual(poll.allowed_choices, 2)
  243. self.assertTrue(poll.allow_revotes)
  244. self.assertEqual(poll.votes, 0)
  245. self.assertTrue(poll.is_public)
  246. self.assertEqual(len(poll.choices), 3)
  247. self.assertEqual(len(set([c["hash"] for c in poll.choices])), 3)
  248. self.assertEqual(self.user.audittrail_set.count(), 1)