test_thread_editreply_api.py 9.6 KB

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