test_thread_postmerge_api.py 12 KB

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