test_thread_editreply_api.py 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. from django.test.client import BOUNDARY, MULTIPART_CONTENT, encode_multipart
  4. from django.urls import reverse
  5. from misago.acl.testutils import override_acl
  6. from misago.categories.models import Category
  7. from misago.threads import testutils
  8. from misago.users.testutils import AuthenticatedUserTestCase
  9. class EditReplyTests(AuthenticatedUserTestCase):
  10. def setUp(self):
  11. super(EditReplyTests, self).setUp()
  12. self.category = Category.objects.get(slug='first-category')
  13. self.thread = testutils.post_thread(category=self.category)
  14. self.post = testutils.reply_thread(self.thread, poster=self.user)
  15. self.api_link = reverse(
  16. 'misago:api:thread-post-detail',
  17. kwargs={
  18. 'thread_pk': self.thread.pk,
  19. 'pk': self.post.pk,
  20. }
  21. )
  22. def override_acl(self, extra_acl=None):
  23. new_acl = self.user.acl_cache
  24. new_acl['categories'][self.category.pk].update({
  25. 'can_see': 1,
  26. 'can_browse': 1,
  27. 'can_start_threads': 0,
  28. 'can_reply_threads': 0,
  29. 'can_edit_posts': 1,
  30. })
  31. if extra_acl:
  32. new_acl['categories'][self.category.pk].update(extra_acl)
  33. override_acl(self.user, new_acl)
  34. def put(self, url, data=None):
  35. content = encode_multipart(BOUNDARY, data or {})
  36. return self.client.put(url, content, content_type=MULTIPART_CONTENT)
  37. def test_cant_edit_reply_as_guest(self):
  38. """user has to be authenticated to be able to edit reply"""
  39. self.logout_user()
  40. response = self.put(self.api_link)
  41. self.assertEqual(response.status_code, 403)
  42. def test_thread_visibility(self):
  43. """thread's visibility is validated"""
  44. self.override_acl({'can_see': 0})
  45. response = self.put(self.api_link)
  46. self.assertEqual(response.status_code, 404)
  47. self.override_acl({'can_browse': 0})
  48. response = self.put(self.api_link)
  49. self.assertEqual(response.status_code, 404)
  50. self.override_acl({'can_see_all_threads': 0})
  51. response = self.put(self.api_link)
  52. self.assertEqual(response.status_code, 404)
  53. def test_cant_edit_reply(self):
  54. """permission to edit reply is validated"""
  55. self.override_acl({'can_edit_posts': 0})
  56. response = self.put(self.api_link)
  57. self.assertContains(response, "You can't edit posts in this category.", status_code=403)
  58. def test_cant_edit_other_user_reply(self):
  59. """permission to edit reply by other users is validated"""
  60. self.override_acl({'can_edit_posts': 1})
  61. self.post.poster = None
  62. self.post.save()
  63. response = self.put(self.api_link)
  64. self.assertContains(
  65. response, "You can't edit other users posts in this category.", status_code=403
  66. )
  67. def test_closed_category(self):
  68. """permssion to edit reply in closed category is validated"""
  69. self.override_acl({'can_close_threads': 0})
  70. self.category.is_closed = True
  71. self.category.save()
  72. response = self.put(self.api_link)
  73. self.assertContains(
  74. response, "This category is closed. You can't edit posts in it.", status_code=403
  75. )
  76. # allow to post in closed category
  77. self.override_acl({'can_close_threads': 1})
  78. response = self.put(self.api_link)
  79. self.assertEqual(response.status_code, 400)
  80. def test_closed_thread(self):
  81. """permssion to edit reply in closed thread is validated"""
  82. self.override_acl({'can_close_threads': 0})
  83. self.thread.is_closed = True
  84. self.thread.save()
  85. response = self.put(self.api_link)
  86. self.assertContains(
  87. response, "This thread is closed. You can't edit posts in it.", status_code=403
  88. )
  89. # allow to post in closed thread
  90. self.override_acl({'can_close_threads': 1})
  91. response = self.put(self.api_link)
  92. self.assertEqual(response.status_code, 400)
  93. def test_protected_post(self):
  94. """permssion to edit protected post is validated"""
  95. self.override_acl({'can_protect_posts': 0})
  96. self.post.is_protected = True
  97. self.post.save()
  98. response = self.put(self.api_link)
  99. self.assertContains(
  100. response, "This post is protected. You can't edit it.", status_code=403
  101. )
  102. # allow to post in closed thread
  103. self.override_acl({'can_protect_posts': 1})
  104. response = self.put(self.api_link)
  105. self.assertEqual(response.status_code, 400)
  106. def test_empty_data(self):
  107. """no data sent handling has no showstoppers"""
  108. self.override_acl()
  109. response = self.put(self.api_link, data={})
  110. self.assertEqual(response.status_code, 400)
  111. self.assertEqual(response.json(), {
  112. 'post': ["You have to enter a message."],
  113. })
  114. def test_edit_event(self):
  115. """events can't be edited"""
  116. self.override_acl()
  117. self.post.is_event = True
  118. self.post.save()
  119. response = self.put(self.api_link, data={})
  120. self.assertContains(response, "Events can't be edited.", status_code=403)
  121. def test_post_is_validated(self):
  122. """post is validated"""
  123. self.override_acl()
  124. response = self.put(
  125. self.api_link, data={
  126. 'post': "a",
  127. }
  128. )
  129. self.assertEqual(response.status_code, 400)
  130. self.assertEqual(
  131. response.json(), {
  132. 'post': ["Posted message should be at least 5 characters long (it has 1)."],
  133. }
  134. )
  135. def test_edit_reply_no_change(self):
  136. """endpoint isn't bumping edits count if no change was made to post's body"""
  137. self.override_acl()
  138. self.assertEqual(self.post.edits_record.count(), 0)
  139. response = self.put(self.api_link, data={'post': self.post.original})
  140. self.assertEqual(response.status_code, 200)
  141. self.override_acl()
  142. response = self.client.get(self.thread.get_absolute_url())
  143. self.assertContains(response, self.post.parsed)
  144. post = self.thread.post_set.order_by('id').last()
  145. self.assertEqual(post.edits, 0)
  146. self.assertEqual(post.original, self.post.original)
  147. self.assertIsNone(post.last_editor_id, self.user.id)
  148. self.assertIsNone(post.last_editor_name, self.user.username)
  149. self.assertIsNone(post.last_editor_slug, self.user.slug)
  150. self.assertEqual(self.post.edits_record.count(), 0)
  151. def test_edit_reply(self):
  152. """endpoint updates reply"""
  153. self.override_acl()
  154. self.assertEqual(self.post.edits_record.count(), 0)
  155. response = self.put(self.api_link, data={'post': "This is test edit!"})
  156. self.assertEqual(response.status_code, 200)
  157. self.override_acl()
  158. response = self.client.get(self.thread.get_absolute_url())
  159. self.assertContains(response, "<p>This is test edit!</p>")
  160. post = self.thread.post_set.order_by('id').last()
  161. self.assertEqual(post.edits, 1)
  162. self.assertEqual(post.original, "This is test edit!")
  163. self.assertEqual(post.last_editor_id, self.user.id)
  164. self.assertEqual(post.last_editor_name, self.user.username)
  165. self.assertEqual(post.last_editor_slug, self.user.slug)
  166. self.assertEqual(self.post.edits_record.count(), 1)
  167. post_edit = post.edits_record.last()
  168. self.assertEqual(post_edit.edited_from, self.post.original)
  169. self.assertEqual(post_edit.edited_to, post.original)
  170. self.assertEqual(post_edit.editor_id, self.user.id)
  171. self.assertEqual(post_edit.editor_name, self.user.username)
  172. self.assertEqual(post_edit.editor_slug, self.user.slug)
  173. def test_edit_first_post_hidden(self):
  174. """endpoint updates hidden thread's first post"""
  175. self.override_acl({'can_hide_threads': 1, 'can_edit_posts': 2})
  176. self.thread.is_hidden = True
  177. self.thread.save()
  178. self.thread.first_post.is_hidden = True
  179. self.thread.first_post.save()
  180. api_link = reverse(
  181. 'misago:api:thread-post-detail',
  182. kwargs={
  183. 'thread_pk': self.thread.pk,
  184. 'pk': self.thread.first_post.pk,
  185. }
  186. )
  187. response = self.put(api_link, data={'post': "This is test edit!"})
  188. self.assertEqual(response.status_code, 200)
  189. def test_protect_post(self):
  190. """can protect post"""
  191. self.override_acl({'can_protect_posts': 1})
  192. response = self.put(
  193. self.api_link, data={
  194. 'post': "Lorem ipsum dolor met!",
  195. 'protect': 1,
  196. }
  197. )
  198. self.assertEqual(response.status_code, 200)
  199. post = self.user.post_set.order_by('id').last()
  200. self.assertTrue(post.is_protected)
  201. def test_protect_post_no_permission(self):
  202. """cant protect post without permission"""
  203. self.override_acl({'can_protect_posts': 0})
  204. response = self.put(
  205. self.api_link, data={
  206. 'post': "Lorem ipsum dolor met!",
  207. 'protect': 1,
  208. }
  209. )
  210. self.assertEqual(response.status_code, 200)
  211. post = self.user.post_set.order_by('id').last()
  212. self.assertFalse(post.is_protected)
  213. def test_post_unicode(self):
  214. """unicode characters can be posted"""
  215. self.override_acl()
  216. response = self.put(
  217. self.api_link, data={
  218. 'post': "Chrzążczyżewoszyce, powiat Łękółody.",
  219. }
  220. )
  221. self.assertEqual(response.status_code, 200)