test_thread_reply_api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. from django.urls import reverse
  2. from misago.acl.test import patch_user_acl
  3. from misago.categories.models import Category
  4. from misago.threads import testutils
  5. from misago.threads.models import Thread
  6. from misago.threads.test import patch_category_acl
  7. from misago.users.testutils import AuthenticatedUserTestCase
  8. class ReplyThreadTests(AuthenticatedUserTestCase):
  9. def setUp(self):
  10. super().setUp()
  11. self.category = Category.objects.get(slug='first-category')
  12. self.thread = testutils.post_thread(category=self.category)
  13. self.api_link = reverse(
  14. 'misago:api:thread-post-list', kwargs={
  15. 'thread_pk': self.thread.pk,
  16. }
  17. )
  18. def test_cant_reply_thread_as_guest(self):
  19. """user has to be authenticated to be able to post reply"""
  20. self.logout_user()
  21. response = self.client.post(self.api_link)
  22. self.assertEqual(response.status_code, 403)
  23. def test_thread_visibility(self):
  24. """thread's visibility is validated"""
  25. with patch_category_acl({'can_see': 0}):
  26. response = self.client.post(self.api_link)
  27. self.assertEqual(response.status_code, 404)
  28. with patch_category_acl({'can_browse': 0}):
  29. response = self.client.post(self.api_link)
  30. self.assertEqual(response.status_code, 404)
  31. with patch_category_acl({'can_see_all_threads': 0}):
  32. response = self.client.post(self.api_link)
  33. self.assertEqual(response.status_code, 404)
  34. @patch_category_acl({"can_reply_threads": False})
  35. def test_cant_reply_thread(self):
  36. """permission to reply thread is validated"""
  37. response = self.client.post(self.api_link)
  38. self.assertEqual(response.status_code, 403)
  39. self.assertEqual(response.json(), {
  40. "detail": "You can't reply to threads in this category.",
  41. })
  42. @patch_category_acl({"can_reply_threads": True, "can_close_threads": False})
  43. def test_closed_category_no_permission(self):
  44. """permssion to reply in closed category is validated"""
  45. self.category.is_closed = True
  46. self.category.save()
  47. response = self.client.post(self.api_link)
  48. self.assertEqual(response.status_code, 403)
  49. self.assertEqual(response.json(), {
  50. "detail": "This category is closed. You can't reply to threads in it.",
  51. })
  52. @patch_category_acl({"can_reply_threads": True, "can_close_threads": True})
  53. def test_closed_category(self):
  54. """permssion to reply in closed category is validated"""
  55. self.category.is_closed = True
  56. self.category.save()
  57. response = self.client.post(self.api_link)
  58. self.assertEqual(response.status_code, 400)
  59. @patch_category_acl({"can_reply_threads": True, "can_close_threads": False})
  60. def test_closed_thread_no_permission(self):
  61. """permssion to reply in closed thread is validated"""
  62. self.thread.is_closed = True
  63. self.thread.save()
  64. response = self.client.post(self.api_link)
  65. self.assertEqual(response.status_code, 403)
  66. self.assertEqual(response.json(), {
  67. "detail": "You can't reply to closed threads in this category.",
  68. })
  69. @patch_category_acl({"can_reply_threads": True, "can_close_threads": True})
  70. def test_closed_thread(self):
  71. """permssion to reply in closed thread is validated"""
  72. self.thread.is_closed = True
  73. self.thread.save()
  74. response = self.client.post(self.api_link)
  75. self.assertEqual(response.status_code, 400)
  76. @patch_category_acl({"can_reply_threads": True})
  77. def test_empty_data(self):
  78. """no data sent handling has no showstoppers"""
  79. response = self.client.post(self.api_link, data={})
  80. self.assertEqual(response.status_code, 400)
  81. self.assertEqual(response.json(), {
  82. "post": ["You have to enter a message."],
  83. })
  84. @patch_category_acl({"can_reply_threads": True})
  85. def test_invalid_data(self):
  86. """api errors for invalid request data"""
  87. response = self.client.post(
  88. self.api_link,
  89. 'false',
  90. content_type="application/json",
  91. )
  92. self.assertEqual(response.status_code, 400)
  93. self.assertEqual(response.json(), {
  94. 'non_field_errors': ['Invalid data. Expected a dictionary, but got bool.']
  95. })
  96. @patch_category_acl({"can_reply_threads": True})
  97. def test_post_is_validated(self):
  98. """post is validated"""
  99. response = self.client.post(
  100. self.api_link, data={
  101. 'post': "a",
  102. }
  103. )
  104. self.assertEqual(response.status_code, 400)
  105. self.assertEqual(
  106. response.json(), {
  107. 'post': ["Posted message should be at least 5 characters long (it has 1)."],
  108. }
  109. )
  110. @patch_category_acl({"can_reply_threads": True})
  111. def test_can_reply_thread(self):
  112. """endpoint creates new reply"""
  113. response = self.client.post(
  114. self.api_link, data={
  115. 'post': "This is test response!",
  116. }
  117. )
  118. self.assertEqual(response.status_code, 200)
  119. thread = Thread.objects.get(pk=self.thread.pk)
  120. response = self.client.get(self.thread.get_absolute_url())
  121. self.assertContains(response, "<p>This is test response!</p>")
  122. # api increased user's posts counts
  123. self.reload_user()
  124. self.assertEqual(self.user.threads, 0)
  125. self.assertEqual(self.user.posts, 1)
  126. self.assertEqual(self.user.audittrail_set.count(), 1)
  127. post = self.user.post_set.all()[:1][0]
  128. self.assertEqual(post.category_id, self.category.pk)
  129. self.assertEqual(post.original, "This is test response!")
  130. self.assertEqual(post.poster_id, self.user.id)
  131. self.assertEqual(post.poster_name, self.user.username)
  132. self.assertEqual(thread.last_post_id, post.id)
  133. self.assertEqual(thread.last_poster_id, self.user.id)
  134. self.assertEqual(thread.last_poster_name, self.user.username)
  135. self.assertEqual(thread.last_poster_slug, self.user.slug)
  136. category = Category.objects.get(pk=self.category.pk)
  137. self.assertEqual(category.last_thread_id, thread.id)
  138. self.assertEqual(category.last_thread_title, thread.title)
  139. self.assertEqual(category.last_thread_slug, thread.slug)
  140. self.assertEqual(category.last_poster_id, self.user.id)
  141. self.assertEqual(category.last_poster_name, self.user.username)
  142. self.assertEqual(category.last_poster_slug, self.user.slug)
  143. @patch_category_acl({"can_reply_threads": True})
  144. def test_post_unicode(self):
  145. """unicode characters can be posted"""
  146. response = self.client.post(
  147. self.api_link, data={
  148. 'post': "Chrzążczyżewoszyce, powiat Łękółody.",
  149. }
  150. )
  151. self.assertEqual(response.status_code, 200)
  152. @patch_category_acl({"can_reply_threads": True})
  153. def test_category_moderation_queue(self):
  154. """reply thread in category that requires approval"""
  155. self.category.require_replies_approval = True
  156. self.category.save()
  157. response = self.client.post(
  158. self.api_link, data={
  159. 'post': "Lorem ipsum dolor met!",
  160. }
  161. )
  162. self.assertEqual(response.status_code, 200)
  163. thread = Thread.objects.get(pk=self.thread.pk)
  164. self.assertFalse(thread.is_unapproved)
  165. self.assertTrue(thread.has_unapproved_posts)
  166. self.assertEqual(thread.replies, self.thread.replies)
  167. post = self.user.post_set.all()[:1][0]
  168. self.assertTrue(post.is_unapproved)
  169. category = Category.objects.get(slug='first-category')
  170. self.assertEqual(category.threads, self.category.threads)
  171. self.assertEqual(category.posts, self.category.posts)
  172. @patch_category_acl({"can_reply_threads": True})
  173. @patch_user_acl({"can_approve_content": True})
  174. def test_category_moderation_queue_bypass(self):
  175. """bypass moderation queue due to user's acl"""
  176. self.category.require_replies_approval = True
  177. self.category.save()
  178. response = self.client.post(
  179. self.api_link, data={
  180. 'post': "Lorem ipsum dolor met!",
  181. }
  182. )
  183. self.assertEqual(response.status_code, 200)
  184. thread = Thread.objects.get(pk=self.thread.pk)
  185. self.assertFalse(thread.is_unapproved)
  186. self.assertFalse(thread.has_unapproved_posts)
  187. self.assertEqual(thread.replies, self.thread.replies + 1)
  188. post = self.user.post_set.all()[:1][0]
  189. self.assertFalse(post.is_unapproved)
  190. category = Category.objects.get(slug='first-category')
  191. self.assertEqual(category.threads, self.category.threads)
  192. self.assertEqual(category.posts, self.category.posts + 1)
  193. @patch_category_acl({"can_reply_threads": True, "require_replies_approval": True})
  194. def test_user_moderation_queue(self):
  195. """reply thread by user that requires approval"""
  196. response = self.client.post(
  197. self.api_link, data={
  198. 'post': "Lorem ipsum dolor met!",
  199. }
  200. )
  201. self.assertEqual(response.status_code, 200)
  202. thread = Thread.objects.get(pk=self.thread.pk)
  203. self.assertFalse(thread.is_unapproved)
  204. self.assertTrue(thread.has_unapproved_posts)
  205. self.assertEqual(thread.replies, self.thread.replies)
  206. post = self.user.post_set.all()[:1][0]
  207. self.assertTrue(post.is_unapproved)
  208. category = Category.objects.get(slug='first-category')
  209. self.assertEqual(category.threads, self.category.threads)
  210. self.assertEqual(category.posts, self.category.posts)
  211. @patch_category_acl({"can_reply_threads": True, "require_replies_approval": True})
  212. @patch_user_acl({"can_approve_content": True})
  213. def test_user_moderation_queue_bypass(self):
  214. """bypass moderation queue due to user's acl"""
  215. response = self.client.post(
  216. self.api_link, data={
  217. 'post': "Lorem ipsum dolor met!",
  218. }
  219. )
  220. self.assertEqual(response.status_code, 200)
  221. thread = Thread.objects.get(pk=self.thread.pk)
  222. self.assertFalse(thread.is_unapproved)
  223. self.assertFalse(thread.has_unapproved_posts)
  224. self.assertEqual(thread.replies, self.thread.replies + 1)
  225. post = self.user.post_set.all()[:1][0]
  226. self.assertFalse(post.is_unapproved)
  227. category = Category.objects.get(slug='first-category')
  228. self.assertEqual(category.threads, self.category.threads)
  229. self.assertEqual(category.posts, self.category.posts + 1)
  230. @patch_category_acl({
  231. "can_reply_threads": True,
  232. "require_threads_approval": True,
  233. "require_edits_approval": True,
  234. })
  235. def test_omit_other_moderation_queues(self):
  236. """other queues are omitted"""
  237. self.category.require_threads_approval = True
  238. self.category.require_edits_approval = True
  239. self.category.save()
  240. response = self.client.post(
  241. self.api_link, data={
  242. 'post': "Lorem ipsum dolor met!",
  243. }
  244. )
  245. self.assertEqual(response.status_code, 200)
  246. thread = Thread.objects.get(pk=self.thread.pk)
  247. self.assertFalse(thread.is_unapproved)
  248. self.assertFalse(thread.has_unapproved_posts)
  249. self.assertEqual(thread.replies, self.thread.replies + 1)
  250. post = self.user.post_set.all()[:1][0]
  251. self.assertFalse(post.is_unapproved)
  252. category = Category.objects.get(slug='first-category')
  253. self.assertEqual(category.threads, self.category.threads)
  254. self.assertEqual(category.posts, self.category.posts + 1)