test_thread_editreply_api.py 9.8 KB

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