test_thread_postmerge_api.py 12 KB

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