test_thread_postmerge_api.py 12 KB

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