test_thread_postmerge_api.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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('misago:api:thread-post-merge', kwargs={
  18. '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({
  45. 'can_merge_posts': 0
  46. })
  47. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  48. self.assertContains(response, "You can't merge posts in this thread.", status_code=403)
  49. def test_closed_thread(self):
  50. """api validates permission to merge in closed thread"""
  51. self.thread.is_closed = True
  52. self.thread.save()
  53. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  54. self.assertContains(response, "You can't merge posts in this thread.", status_code=403)
  55. # allow closing threads
  56. self.override_acl({
  57. 'can_close_threads': 1
  58. })
  59. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  60. self.assertContains(response, "You have to select at least two posts to merge.", status_code=400)
  61. def test_closed_category(self):
  62. """api validates permission to merge in closed category"""
  63. self.category.is_closed = True
  64. self.category.save()
  65. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  66. self.assertContains(response, "You can't merge posts in this thread.", status_code=403)
  67. # allow closing threads
  68. self.override_acl({
  69. 'can_close_threads': 1
  70. })
  71. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  72. self.assertContains(response, "You have to select at least two posts to merge.", status_code=400)
  73. def test_empty_data(self):
  74. """api handles empty data"""
  75. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  76. self.assertContains(response, "You have to select at least two posts to merge.", status_code=400)
  77. def test_no_posts_ids(self):
  78. """api rejects no posts ids"""
  79. response = self.client.post(self.api_link, json.dumps({
  80. 'posts': []
  81. }), content_type="application/json")
  82. self.assertContains(response, "You have to select at least two posts to merge.", status_code=400)
  83. def test_invalid_posts_data(self):
  84. """api handles invalid data"""
  85. response = self.client.post(self.api_link, json.dumps({
  86. 'posts': 'string'
  87. }), content_type="application/json")
  88. self.assertContains(response, "One or more post ids received were invalid.", status_code=400)
  89. def test_invalid_posts_ids(self):
  90. """api handles invalid post id"""
  91. response = self.client.post(self.api_link, json.dumps({
  92. 'posts': [1, 2, 'string']
  93. }), content_type="application/json")
  94. self.assertContains(response, "One or more post ids received were invalid.", status_code=400)
  95. def test_one_post_id(self):
  96. """api rejects one post id"""
  97. response = self.client.post(self.api_link, json.dumps({
  98. 'posts': [1]
  99. }), content_type="application/json")
  100. self.assertContains(response, "You have to select at least two posts to merge.", status_code=400)
  101. def test_merge_limit(self):
  102. """api rejects more posts than merge limit"""
  103. response = self.client.post(self.api_link, json.dumps({
  104. 'posts': list(range(MERGE_LIMIT + 1))
  105. }), content_type="application/json")
  106. self.assertContains(response, "No more than {} posts can be merged".format(MERGE_LIMIT), status_code=400)
  107. def test_merge_event(self):
  108. """api recjects events"""
  109. event = testutils.reply_thread(self.thread, is_event=True, poster=self.user)
  110. response = self.client.post(self.api_link, json.dumps({
  111. 'posts': [self.post.pk, event.pk]
  112. }), content_type="application/json")
  113. self.assertContains(response, "Events can't be merged.", status_code=400)
  114. def test_merge_notfound_pk(self):
  115. """api recjects nonexistant pk's"""
  116. response = self.client.post(self.api_link, json.dumps({
  117. 'posts': [self.post.pk, self.post.pk * 1000]
  118. }), content_type="application/json")
  119. self.assertContains(response, "One or more posts to merge could not be found.", status_code=400)
  120. def test_merge_cross_threads(self):
  121. """api recjects attempt to merge with post made in other thread"""
  122. other_thread = testutils.post_thread(category=self.category)
  123. other_post = testutils.reply_thread(other_thread, poster=self.user)
  124. response = self.client.post(self.api_link, json.dumps({
  125. 'posts': [self.post.pk, other_post.pk]
  126. }), content_type="application/json")
  127. self.assertContains(response, "One or more posts to merge could not be found.", status_code=400)
  128. def test_merge_authenticated_with_guest_post(self):
  129. """api recjects attempt to merge with post made by deleted user"""
  130. other_post = testutils.reply_thread(self.thread)
  131. response = self.client.post(self.api_link, json.dumps({
  132. 'posts': [self.post.pk, other_post.pk]
  133. }), content_type="application/json")
  134. self.assertContains(response, "Posts made by different users can't be merged.", status_code=400)
  135. def test_merge_guest_with_authenticated_post(self):
  136. """api recjects attempt to merge with post made by deleted user"""
  137. other_post = testutils.reply_thread(self.thread)
  138. response = self.client.post(self.api_link, json.dumps({
  139. 'posts': [other_post.pk, self.post.pk]
  140. }), content_type="application/json")
  141. self.assertContains(response, "Posts made by different users can't be merged.", status_code=400)
  142. def test_merge_guest_posts_different_usernames(self):
  143. """api recjects attempt to merge posts made by different guests"""
  144. response = self.client.post(self.api_link, json.dumps({
  145. 'posts': [
  146. testutils.reply_thread(self.thread, poster="Bob").pk,
  147. testutils.reply_thread(self.thread, poster="Miku").pk
  148. ]
  149. }), content_type="application/json")
  150. self.assertContains(response, "Posts made by different users can't be merged.", status_code=400)
  151. def test_merge_different_visibility(self):
  152. """api recjects attempt to merge posts with different visibility"""
  153. self.override_acl({
  154. 'can_hide_posts': 1
  155. })
  156. response = self.client.post(self.api_link, json.dumps({
  157. 'posts': [
  158. testutils.reply_thread(self.thread, poster="Bob", is_hidden=True).pk,
  159. testutils.reply_thread(self.thread, poster="Bob", is_hidden=False).pk
  160. ]
  161. }), content_type="application/json")
  162. self.assertContains(response, "Posts with different visibility can't be merged.", status_code=400)
  163. def test_merge_different_approval(self):
  164. """api recjects attempt to merge posts with different approval"""
  165. self.override_acl({
  166. 'can_approve_content': 1
  167. })
  168. response = self.client.post(self.api_link, json.dumps({
  169. 'posts': [
  170. testutils.reply_thread(self.thread, poster="Bob", is_unapproved=True).pk,
  171. testutils.reply_thread(self.thread, poster="Bob", is_unapproved=False).pk
  172. ]
  173. }), content_type="application/json")
  174. self.assertContains(response, "Posts with different visibility can't be merged.", status_code=400)
  175. def test_merge_posts(self):
  176. """api merges two posts"""
  177. post_a = testutils.reply_thread(self.thread, poster=self.user, message="Battęry")
  178. post_b = testutils.reply_thread(self.thread, poster=self.user, message="Hórse")
  179. thread_replies = self.thread.replies
  180. response = self.client.post(self.api_link, json.dumps({
  181. 'posts': [post_a.pk, post_b.pk]
  182. }), content_type="application/json")
  183. self.assertEqual(response.status_code, 200)
  184. self.refresh_thread()
  185. self.assertEqual(self.thread.replies, thread_replies - 1)
  186. with self.assertRaises(Post.DoesNotExist):
  187. Post.objects.get(pk=post_b.pk)
  188. merged_post = Post.objects.get(pk=post_a.pk)
  189. self.assertEqual(merged_post.parsed, '{}\n{}'.format(post_a.parsed, post_b.parsed))
  190. def test_merge_hidden_posts(self):
  191. """api merges two hidden posts"""
  192. self.override_acl({
  193. 'can_hide_posts': 1
  194. })
  195. response = self.client.post(self.api_link, json.dumps({
  196. 'posts': [
  197. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True).pk,
  198. testutils.reply_thread(self.thread, poster=self.user, is_hidden=True).pk
  199. ]
  200. }), content_type="application/json")
  201. self.assertEqual(response.status_code, 200)
  202. def test_merge_unapproved_posts(self):
  203. """api merges two unapproved posts"""
  204. self.override_acl({
  205. 'can_approve_content': 1
  206. })
  207. response = self.client.post(self.api_link, json.dumps({
  208. 'posts': [
  209. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True).pk,
  210. testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True).pk
  211. ]
  212. }), content_type="application/json")
  213. self.assertEqual(response.status_code, 200)
  214. def test_merge_with_hidden_thread(self):
  215. """api recjects attempt to merge posts with different visibility"""
  216. self.thread.first_post.is_hidden = True
  217. self.thread.first_post.poster = self.user
  218. self.thread.first_post.save()
  219. post_visible = testutils.reply_thread(self.thread, poster=self.user, is_hidden=False)
  220. self.override_acl({
  221. 'can_hide_threads': 1
  222. })
  223. response = self.client.post(self.api_link, json.dumps({
  224. 'posts': [self.thread.first_post.pk, post_visible.pk]
  225. }), content_type="application/json")
  226. self.assertEqual(response.status_code, 200)