test_thread_postmerge_api.py 13 KB

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