test_thread_postmerge_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import json
  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.threads.api.postendpoints.merge import MERGE_LIMIT
  9. from misago.threads.models import Post, Thread
  10. from misago.users.testutils import AuthenticatedUserTestCase
  11. class ThreadPostMergeApiTestCase(AuthenticatedUserTestCase):
  12. def setUp(self):
  13. super(ThreadPostMergeApiTestCase, self).setUp()
  14. self.category = Category.objects.get(slug='first-category')
  15. self.thread = testutils.post_thread(category=self.category)
  16. self.post = testutils.reply_thread(self.thread, poster=self.user)
  17. self.api_link = reverse(
  18. 'misago:api:thread-post-merge', kwargs={'thread_pk': self.thread.pk}
  19. )
  20. self.override_acl()
  21. def refresh_thread(self):
  22. self.thread = Thread.objects.get(pk=self.thread.pk)
  23. def override_acl(self, extra_acl=None):
  24. new_acl = self.user.acl_cache
  25. new_acl['categories'][self.category.pk].update({
  26. 'can_see': 1,
  27. 'can_browse': 1,
  28. 'can_start_threads': 0,
  29. 'can_reply_threads': 0,
  30. 'can_edit_posts': 1,
  31. 'can_approve_content': 0,
  32. 'can_merge_posts': 1
  33. })
  34. if extra_acl:
  35. new_acl['categories'][self.category.pk].update(extra_acl)
  36. override_acl(self.user, new_acl)
  37. def test_anonymous_user(self):
  38. """you need to authenticate to merge posts"""
  39. self.logout_user()
  40. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  41. self.assertEqual(response.status_code, 403)
  42. def test_no_permission(self):
  43. """api validates permission to merge"""
  44. self.override_acl({'can_merge_posts': 0})
  45. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  46. self.assertContains(response, "You can't merge posts in this thread.", status_code=403)
  47. def test_closed_thread(self):
  48. """api validates permission to merge in closed thread"""
  49. self.thread.is_closed = True
  50. self.thread.save()
  51. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  52. self.assertContains(response, "You can't merge posts in this thread.", status_code=403)
  53. # allow closing threads
  54. self.override_acl({'can_close_threads': 1})
  55. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  56. self.assertContains(
  57. response, "You have to select at least two posts to merge.", status_code=400
  58. )
  59. def test_closed_category(self):
  60. """api validates permission to merge in closed category"""
  61. self.category.is_closed = True
  62. self.category.save()
  63. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  64. self.assertContains(response, "You can't merge posts in this thread.", status_code=403)
  65. # allow closing threads
  66. self.override_acl({'can_close_threads': 1})
  67. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  68. self.assertContains(
  69. response, "You have to select at least two posts to merge.", status_code=400
  70. )
  71. def test_empty_data(self):
  72. """api handles empty data"""
  73. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  74. self.assertContains(
  75. response, "You have to select at least two posts to merge.", status_code=400
  76. )
  77. def test_no_posts_ids(self):
  78. """api rejects no posts ids"""
  79. response = self.client.post(
  80. self.api_link, json.dumps({
  81. 'posts': []
  82. }), content_type="application/json"
  83. )
  84. self.assertContains(
  85. response, "You have to select at least two posts to merge.", status_code=400
  86. )
  87. def test_invalid_posts_data(self):
  88. """api handles invalid data"""
  89. response = self.client.post(
  90. self.api_link, json.dumps({
  91. 'posts': 'string'
  92. }), content_type="application/json"
  93. )
  94. self.assertContains(
  95. response, "One or more post ids received were invalid.", status_code=400
  96. )
  97. def test_invalid_posts_ids(self):
  98. """api handles invalid post id"""
  99. response = self.client.post(
  100. self.api_link, json.dumps({
  101. 'posts': [1, 2, 'string']
  102. }), content_type="application/json"
  103. )
  104. self.assertContains(
  105. response, "One or more post ids received were invalid.", status_code=400
  106. )
  107. def test_one_post_id(self):
  108. """api rejects one post id"""
  109. response = self.client.post(
  110. self.api_link, json.dumps({
  111. 'posts': [1]
  112. }), content_type="application/json"
  113. )
  114. self.assertContains(
  115. response, "You have to select at least two posts to merge.", status_code=400
  116. )
  117. def test_merge_limit(self):
  118. """api rejects more posts than merge limit"""
  119. response = self.client.post(
  120. self.api_link,
  121. json.dumps({
  122. 'posts': list(range(MERGE_LIMIT + 1))
  123. }),
  124. content_type="application/json"
  125. )
  126. self.assertContains(
  127. response, "No more than {} posts can be merged".format(MERGE_LIMIT), status_code=400
  128. )
  129. def test_merge_event(self):
  130. """api recjects events"""
  131. event = testutils.reply_thread(self.thread, is_event=True, poster=self.user)
  132. response = self.client.post(
  133. self.api_link,
  134. json.dumps({
  135. 'posts': [self.post.pk, event.pk]
  136. }),
  137. content_type="application/json"
  138. )
  139. self.assertContains(response, "Events can't be merged.", status_code=400)
  140. def test_merge_notfound_pk(self):
  141. """api recjects nonexistant pk's"""
  142. response = self.client.post(
  143. self.api_link,
  144. json.dumps({
  145. 'posts': [self.post.pk, self.post.pk * 1000]
  146. }),
  147. content_type="application/json"
  148. )
  149. self.assertContains(
  150. response, "One or more posts to merge could not be found.", status_code=400
  151. )
  152. def test_merge_cross_threads(self):
  153. """api recjects attempt to merge with post made in other thread"""
  154. other_thread = testutils.post_thread(category=self.category)
  155. other_post = testutils.reply_thread(other_thread, poster=self.user)
  156. response = self.client.post(
  157. self.api_link,
  158. json.dumps({
  159. 'posts': [self.post.pk, other_post.pk]
  160. }),
  161. content_type="application/json"
  162. )
  163. self.assertContains(
  164. response, "One or more posts to merge could not be found.", status_code=400
  165. )
  166. def test_merge_authenticated_with_guest_post(self):
  167. """api recjects attempt to merge with post made by deleted user"""
  168. other_post = testutils.reply_thread(self.thread)
  169. response = self.client.post(
  170. self.api_link,
  171. json.dumps({
  172. 'posts': [self.post.pk, other_post.pk]
  173. }),
  174. content_type="application/json"
  175. )
  176. self.assertContains(
  177. response, "Posts made by different users can't be merged.", status_code=400
  178. )
  179. def test_merge_guest_with_authenticated_post(self):
  180. """api recjects attempt to merge with post made by deleted user"""
  181. other_post = testutils.reply_thread(self.thread)
  182. response = self.client.post(
  183. self.api_link,
  184. json.dumps({
  185. 'posts': [other_post.pk, self.post.pk]
  186. }),
  187. content_type="application/json"
  188. )
  189. self.assertContains(
  190. response, "Posts made by different users can't be merged.", status_code=400
  191. )
  192. def test_merge_guest_posts_different_usernames(self):
  193. """api recjects attempt to merge posts made by different guests"""
  194. response = self.client.post(
  195. self.api_link,
  196. json.dumps({
  197. 'posts': [
  198. testutils.reply_thread(self.thread, poster="Bob").pk,
  199. testutils.reply_thread(self.thread, poster="Miku").pk
  200. ]
  201. }),
  202. content_type="application/json"
  203. )
  204. self.assertContains(
  205. response, "Posts made by different users can't be merged.", status_code=400
  206. )
  207. def test_merge_different_visibility(self):
  208. """api recjects attempt to merge posts with different visibility"""
  209. self.override_acl({'can_hide_posts': 1})
  210. response = self.client.post(
  211. self.api_link,
  212. json.dumps({
  213. 'posts': [
  214. testutils.reply_thread(self.thread, poster="Bob", is_hidden=True).pk,
  215. testutils.reply_thread(self.thread, poster="Bob", is_hidden=False).pk
  216. ]
  217. }),
  218. content_type="application/json"
  219. )
  220. self.assertContains(
  221. response, "Posts with different visibility can't be merged.", status_code=400
  222. )
  223. def test_merge_different_approval(self):
  224. """api recjects attempt to merge posts with different approval"""
  225. self.override_acl({'can_approve_content': 1})
  226. response = self.client.post(
  227. self.api_link,
  228. json.dumps({
  229. 'posts': [
  230. testutils.reply_thread(self.thread, poster="Bob", is_unapproved=True).pk,
  231. testutils.reply_thread(self.thread, poster="Bob", is_unapproved=False).pk
  232. ]
  233. }),
  234. content_type="application/json"
  235. )
  236. self.assertContains(
  237. response, "Posts with different visibility can't be merged.", status_code=400
  238. )
  239. def test_merge_posts(self):
  240. """api merges two posts"""
  241. post_a = testutils.reply_thread(self.thread, poster=self.user, message="Battęry")
  242. post_b = testutils.reply_thread(self.thread, poster=self.user, message="Hórse")
  243. thread_replies = self.thread.replies
  244. response = self.client.post(
  245. self.api_link,
  246. json.dumps({
  247. 'posts': [post_a.pk, post_b.pk]
  248. }),
  249. content_type="application/json"
  250. )
  251. self.assertEqual(response.status_code, 200)
  252. self.refresh_thread()
  253. self.assertEqual(self.thread.replies, thread_replies - 1)
  254. with self.assertRaises(Post.DoesNotExist):
  255. Post.objects.get(pk=post_b.pk)
  256. merged_post = Post.objects.get(pk=post_a.pk)
  257. self.assertEqual(merged_post.parsed, '{}\n{}'.format(post_a.parsed, post_b.parsed))
  258. def test_merge_hidden_posts(self):
  259. """api merges two hidden posts"""
  260. self.override_acl({'can_hide_posts': 1})
  261. response = self.client.post(
  262. self.api_link,
  263. json.dumps({
  264. 'posts': [
  265. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True).pk,
  266. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True).pk
  267. ]
  268. }),
  269. content_type="application/json"
  270. )
  271. self.assertEqual(response.status_code, 200)
  272. def test_merge_unapproved_posts(self):
  273. """api merges two unapproved posts"""
  274. self.override_acl({'can_approve_content': 1})
  275. response = self.client.post(
  276. self.api_link,
  277. json.dumps({
  278. 'posts': [
  279. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True).pk,
  280. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True).pk
  281. ]
  282. }),
  283. content_type="application/json"
  284. )
  285. self.assertEqual(response.status_code, 200)
  286. def test_merge_with_hidden_thread(self):
  287. """api recjects attempt to merge posts with different visibility"""
  288. self.thread.first_post.is_hidden = True
  289. self.thread.first_post.poster = self.user
  290. self.thread.first_post.save()
  291. post_visible = testutils.reply_thread(self.thread, poster=self.user, is_hidden=False)
  292. self.override_acl({'can_hide_threads': 1})
  293. response = self.client.post(
  294. self.api_link,
  295. json.dumps({
  296. 'posts': [self.thread.first_post.pk, post_visible.pk]
  297. }),
  298. content_type="application/json"
  299. )
  300. self.assertEqual(response.status_code, 200)