test_thread_postmerge_api.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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,
  101. json.dumps({
  102. 'posts': [1, 2, 'string']
  103. }),
  104. content_type="application/json"
  105. )
  106. self.assertContains(
  107. response, "One or more post ids received were invalid.", status_code=400
  108. )
  109. def test_one_post_id(self):
  110. """api rejects one post id"""
  111. response = self.client.post(
  112. self.api_link, json.dumps({
  113. 'posts': [1]
  114. }), content_type="application/json"
  115. )
  116. self.assertContains(
  117. response, "You have to select at least two posts to merge.", status_code=400
  118. )
  119. def test_merge_limit(self):
  120. """api rejects more posts than merge limit"""
  121. response = self.client.post(
  122. self.api_link,
  123. json.dumps({
  124. 'posts': list(range(MERGE_LIMIT + 1))
  125. }),
  126. content_type="application/json"
  127. )
  128. self.assertContains(
  129. response, "No more than {} posts can be merged".format(MERGE_LIMIT), status_code=400
  130. )
  131. def test_merge_event(self):
  132. """api recjects events"""
  133. event = testutils.reply_thread(self.thread, is_event=True, poster=self.user)
  134. response = self.client.post(
  135. self.api_link,
  136. json.dumps({
  137. 'posts': [self.post.pk, event.pk]
  138. }),
  139. content_type="application/json"
  140. )
  141. self.assertContains(response, "Events can't be merged.", status_code=400)
  142. def test_merge_notfound_pk(self):
  143. """api recjects nonexistant pk's"""
  144. response = self.client.post(
  145. self.api_link,
  146. json.dumps({
  147. 'posts': [self.post.pk, self.post.pk * 1000]
  148. }),
  149. content_type="application/json"
  150. )
  151. self.assertContains(
  152. response, "One or more posts to merge could not be found.", status_code=400
  153. )
  154. def test_merge_cross_threads(self):
  155. """api recjects attempt to merge with post made in other thread"""
  156. other_thread = testutils.post_thread(category=self.category)
  157. other_post = testutils.reply_thread(other_thread, poster=self.user)
  158. response = self.client.post(
  159. self.api_link,
  160. json.dumps({
  161. 'posts': [self.post.pk, other_post.pk]
  162. }),
  163. content_type="application/json"
  164. )
  165. self.assertContains(
  166. response, "One or more posts to merge could not be found.", status_code=400
  167. )
  168. def test_merge_authenticated_with_guest_post(self):
  169. """api recjects attempt to merge with post made by deleted user"""
  170. other_post = testutils.reply_thread(self.thread)
  171. response = self.client.post(
  172. self.api_link,
  173. json.dumps({
  174. 'posts': [self.post.pk, other_post.pk]
  175. }),
  176. content_type="application/json"
  177. )
  178. self.assertContains(
  179. response, "Posts made by different users can't be merged.", status_code=400
  180. )
  181. def test_merge_guest_with_authenticated_post(self):
  182. """api recjects attempt to merge with post made by deleted user"""
  183. other_post = testutils.reply_thread(self.thread)
  184. response = self.client.post(
  185. self.api_link,
  186. json.dumps({
  187. 'posts': [other_post.pk, self.post.pk]
  188. }),
  189. content_type="application/json"
  190. )
  191. self.assertContains(
  192. response, "Posts made by different users can't be merged.", status_code=400
  193. )
  194. def test_merge_guest_posts_different_usernames(self):
  195. """api recjects attempt to merge posts made by different guests"""
  196. response = self.client.post(
  197. self.api_link,
  198. json.dumps({
  199. 'posts': [
  200. testutils.reply_thread(self.thread, poster="Bob").pk,
  201. testutils.reply_thread(self.thread, poster="Miku").pk
  202. ]
  203. }),
  204. content_type="application/json"
  205. )
  206. self.assertContains(
  207. response, "Posts made by different users can't be merged.", status_code=400
  208. )
  209. def test_merge_different_visibility(self):
  210. """api recjects attempt to merge posts with different visibility"""
  211. self.override_acl({'can_hide_posts': 1})
  212. response = self.client.post(
  213. self.api_link,
  214. json.dumps({
  215. 'posts': [
  216. testutils.reply_thread(self.thread, poster="Bob", is_hidden=True).pk,
  217. testutils.reply_thread(self.thread, poster="Bob", is_hidden=False).pk
  218. ]
  219. }),
  220. content_type="application/json"
  221. )
  222. self.assertContains(
  223. response, "Posts with different visibility can't be merged.", status_code=400
  224. )
  225. def test_merge_different_approval(self):
  226. """api recjects attempt to merge posts with different approval"""
  227. self.override_acl({'can_approve_content': 1})
  228. response = self.client.post(
  229. self.api_link,
  230. json.dumps({
  231. 'posts': [
  232. testutils.reply_thread(self.thread, poster="Bob", is_unapproved=True).pk,
  233. testutils.reply_thread(self.thread, poster="Bob", is_unapproved=False).pk
  234. ]
  235. }),
  236. content_type="application/json"
  237. )
  238. self.assertContains(
  239. response, "Posts with different visibility can't be merged.", status_code=400
  240. )
  241. def test_merge_posts(self):
  242. """api merges two posts"""
  243. post_a = testutils.reply_thread(self.thread, poster=self.user, message="Battęry")
  244. post_b = testutils.reply_thread(self.thread, poster=self.user, message="Hórse")
  245. thread_replies = self.thread.replies
  246. response = self.client.post(
  247. self.api_link,
  248. json.dumps({
  249. 'posts': [post_a.pk, post_b.pk]
  250. }),
  251. content_type="application/json"
  252. )
  253. self.assertEqual(response.status_code, 200)
  254. self.refresh_thread()
  255. self.assertEqual(self.thread.replies, thread_replies - 1)
  256. with self.assertRaises(Post.DoesNotExist):
  257. Post.objects.get(pk=post_b.pk)
  258. merged_post = Post.objects.get(pk=post_a.pk)
  259. self.assertEqual(merged_post.parsed, '{}\n{}'.format(post_a.parsed, post_b.parsed))
  260. def test_merge_hidden_posts(self):
  261. """api merges two hidden posts"""
  262. self.override_acl({'can_hide_posts': 1})
  263. response = self.client.post(
  264. self.api_link,
  265. json.dumps({
  266. 'posts': [
  267. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True).pk,
  268. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True).pk
  269. ]
  270. }),
  271. content_type="application/json"
  272. )
  273. self.assertEqual(response.status_code, 200)
  274. def test_merge_unapproved_posts(self):
  275. """api merges two unapproved posts"""
  276. self.override_acl({'can_approve_content': 1})
  277. response = self.client.post(
  278. self.api_link,
  279. json.dumps({
  280. 'posts': [
  281. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True).pk,
  282. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True).pk
  283. ]
  284. }),
  285. content_type="application/json"
  286. )
  287. self.assertEqual(response.status_code, 200)
  288. def test_merge_with_hidden_thread(self):
  289. """api recjects attempt to merge posts with different visibility"""
  290. self.thread.first_post.is_hidden = True
  291. self.thread.first_post.poster = self.user
  292. self.thread.first_post.save()
  293. post_visible = testutils.reply_thread(self.thread, poster=self.user, is_hidden=False)
  294. self.override_acl({'can_hide_threads': 1})
  295. response = self.client.post(
  296. self.api_link,
  297. json.dumps({
  298. 'posts': [self.thread.first_post.pk, post_visible.pk]
  299. }),
  300. content_type="application/json"
  301. )
  302. self.assertEqual(response.status_code, 200)