test_thread_postmove_api.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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.move import MOVE_LIMIT
  9. from misago.threads.models import Thread
  10. from misago.users.testutils import AuthenticatedUserTestCase
  11. class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
  12. def setUp(self):
  13. super(ThreadPostMoveApiTestCase, self).setUp()
  14. self.category = Category.objects.get(slug='first-category')
  15. self.thread = testutils.post_thread(category=self.category)
  16. self.api_link = reverse('misago:api:thread-post-move', kwargs={
  17. 'thread_pk': self.thread.pk
  18. })
  19. Category(
  20. name='Category B',
  21. slug='category-b',
  22. ).insert_at(self.category, position='last-child', save=True)
  23. self.category_b = Category.objects.get(slug='category-b')
  24. self.override_acl()
  25. self.override_other_acl()
  26. def refresh_thread(self):
  27. self.thread = Thread.objects.get(pk=self.thread.pk)
  28. def override_acl(self, extra_acl=None):
  29. new_acl = self.user.acl_cache
  30. new_acl['categories'][self.category.pk].update({
  31. 'can_see': 1,
  32. 'can_browse': 1,
  33. 'can_start_threads': 1,
  34. 'can_reply_threads': 1,
  35. 'can_edit_posts': 1,
  36. 'can_approve_content': 0,
  37. 'can_move_posts': 1
  38. })
  39. if extra_acl:
  40. new_acl['categories'][self.category.pk].update(extra_acl)
  41. override_acl(self.user, new_acl)
  42. def override_other_acl(self, acl=None):
  43. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  44. other_category_acl.update({
  45. 'can_see': 1,
  46. 'can_browse': 1,
  47. 'can_start_threads': 0,
  48. 'can_reply_threads': 0,
  49. 'can_edit_posts': 1,
  50. 'can_approve_content': 0,
  51. 'can_move_posts': 1
  52. })
  53. if acl:
  54. other_category_acl.update(acl)
  55. categories_acl = self.user.acl_cache['categories']
  56. categories_acl[self.category_b.pk] = other_category_acl
  57. visible_categories = [self.category.pk]
  58. if other_category_acl['can_see']:
  59. visible_categories.append(self.category_b.pk)
  60. override_acl(self.user, {
  61. 'visible_categories': visible_categories,
  62. 'categories': categories_acl,
  63. })
  64. def test_anonymous_user(self):
  65. """you need to authenticate to move posts"""
  66. self.logout_user()
  67. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  68. self.assertEqual(response.status_code, 403)
  69. def test_no_permission(self):
  70. """api validates permission to move"""
  71. self.override_acl({
  72. 'can_move_posts': 0
  73. })
  74. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  75. self.assertContains(response, "You can't move posts in this thread.", status_code=403)
  76. def test_move_no_url(self):
  77. """api validates if thread url was given"""
  78. response = self.client.post(self.api_link)
  79. self.assertContains(response, "This is not a valid thread link.", status_code=400)
  80. def test_invalid_url(self):
  81. """api validates thread url"""
  82. response = self.client.post(self.api_link, {
  83. 'thread_url': self.user.get_absolute_url()
  84. })
  85. self.assertContains(response, "This is not a valid thread link.", status_code=400)
  86. def test_current_thread_url(self):
  87. """api validates if thread url given is to current thread"""
  88. response = self.client.post(self.api_link, {
  89. 'thread_url': self.thread.get_absolute_url()
  90. })
  91. self.assertContains(response, "Thread to move posts to is same as current one.", status_code=400)
  92. def test_other_thread_exists(self):
  93. """api validates if other thread exists"""
  94. self.override_other_acl()
  95. other_thread = testutils.post_thread(self.category_b)
  96. other_thread_url = other_thread.get_absolute_url()
  97. other_thread.delete()
  98. response = self.client.post(self.api_link, {
  99. 'thread_url': other_thread_url
  100. })
  101. self.assertContains(response, "The thread you have entered link to doesn't exist", status_code=400)
  102. def test_other_thread_is_invisible(self):
  103. """api validates if other thread is visible"""
  104. self.override_other_acl({
  105. 'can_see': 0
  106. })
  107. other_thread = testutils.post_thread(self.category_b)
  108. response = self.client.post(self.api_link, {
  109. 'thread_url': other_thread.get_absolute_url()
  110. })
  111. self.assertContains(response, "The thread you have entered link to doesn't exist", status_code=400)
  112. def test_other_thread_isnt_replyable(self):
  113. """api validates if other thread can be replied"""
  114. self.override_other_acl({
  115. 'can_reply_threads': 0
  116. })
  117. other_thread = testutils.post_thread(self.category_b)
  118. response = self.client.post(self.api_link, {
  119. 'thread_url': other_thread.get_absolute_url()
  120. })
  121. self.assertContains(response, "You can't move posts to threads you can't reply.", status_code=400)
  122. def test_empty_data(self):
  123. """api handles empty data"""
  124. other_thread = testutils.post_thread(self.category)
  125. response = self.client.post(self.api_link, {
  126. 'thread_url': other_thread.get_absolute_url()
  127. })
  128. self.assertContains(response, "You have to specify at least one post to move.", status_code=400)
  129. def test_no_posts_ids(self):
  130. """api rejects no posts ids"""
  131. other_thread = testutils.post_thread(self.category)
  132. response = self.client.post(self.api_link, json.dumps({
  133. 'thread_url': other_thread.get_absolute_url(),
  134. 'posts': []
  135. }), content_type="application/json")
  136. self.assertContains(response, "You have to specify at least one post to move.", status_code=400)
  137. def test_invalid_posts_data(self):
  138. """api handles invalid data"""
  139. other_thread = testutils.post_thread(self.category)
  140. response = self.client.post(self.api_link, json.dumps({
  141. 'thread_url': other_thread.get_absolute_url(),
  142. 'posts': 'string'
  143. }), content_type="application/json")
  144. self.assertContains(response, "One or more post ids received were invalid.", status_code=400)
  145. def test_invalid_posts_ids(self):
  146. """api handles invalid post id"""
  147. other_thread = testutils.post_thread(self.category)
  148. response = self.client.post(self.api_link, json.dumps({
  149. 'thread_url': other_thread.get_absolute_url(),
  150. 'posts': [1, 2, 'string']
  151. }), content_type="application/json")
  152. self.assertContains(response, "One or more post ids received were invalid.", status_code=400)
  153. def test_move_limit(self):
  154. """api rejects more posts than move limit"""
  155. other_thread = testutils.post_thread(self.category)
  156. response = self.client.post(self.api_link, json.dumps({
  157. 'thread_url': other_thread.get_absolute_url(),
  158. 'posts': list(range(MOVE_LIMIT + 1))
  159. }), content_type="application/json")
  160. self.assertContains(response, "No more than {} posts can be moved".format(MOVE_LIMIT), status_code=400)
  161. def test_move_invisible(self):
  162. """api validates posts visibility"""
  163. other_thread = testutils.post_thread(self.category)
  164. response = self.client.post(self.api_link, json.dumps({
  165. 'thread_url': other_thread.get_absolute_url(),
  166. 'posts': [
  167. testutils.reply_thread(self.thread, is_unapproved=True).pk
  168. ]
  169. }), content_type="application/json")
  170. self.assertContains(response, "One or more posts to move could not be found.", status_code=400)
  171. def test_move_other_thread_posts(self):
  172. """api recjects attempt to move other thread's post"""
  173. other_thread = testutils.post_thread(self.category)
  174. response = self.client.post(self.api_link, json.dumps({
  175. 'thread_url': other_thread.get_absolute_url(),
  176. 'posts': [
  177. testutils.reply_thread(other_thread, is_hidden=True).pk
  178. ]
  179. }), content_type="application/json")
  180. self.assertContains(response, "One or more posts to move could not be found.", status_code=400)
  181. def test_move_event(self):
  182. """api rejects events move"""
  183. other_thread = testutils.post_thread(self.category)
  184. response = self.client.post(self.api_link, json.dumps({
  185. 'thread_url': other_thread.get_absolute_url(),
  186. 'posts': [
  187. testutils.reply_thread(self.thread, is_event=True).pk
  188. ]
  189. }), content_type="application/json")
  190. self.assertContains(response, "Events can't be moved.", status_code=400)
  191. def test_move_first_post(self):
  192. """api rejects first post move"""
  193. other_thread = testutils.post_thread(self.category)
  194. response = self.client.post(self.api_link, json.dumps({
  195. 'thread_url': other_thread.get_absolute_url(),
  196. 'posts': [
  197. self.thread.first_post_id
  198. ]
  199. }), content_type="application/json")
  200. self.assertContains(response, "You can't move thread's first post.", status_code=400)
  201. def test_move_hidden_posts(self):
  202. """api recjects attempt to move urneadable hidden post"""
  203. other_thread = testutils.post_thread(self.category)
  204. response = self.client.post(self.api_link, json.dumps({
  205. 'thread_url': other_thread.get_absolute_url(),
  206. 'posts': [
  207. testutils.reply_thread(self.thread, is_hidden=True).pk
  208. ]
  209. }), content_type="application/json")
  210. self.assertContains(response, "You can't move posts the content you can't see.", status_code=400)
  211. def test_move_posts(self):
  212. """api moves posts to other thread"""
  213. self.override_other_acl({
  214. 'can_reply_threads': 1
  215. })
  216. other_thread = testutils.post_thread(self.category_b)
  217. posts = (
  218. testutils.reply_thread(self.thread).pk,
  219. testutils.reply_thread(self.thread).pk,
  220. testutils.reply_thread(self.thread).pk,
  221. testutils.reply_thread(self.thread).pk,
  222. )
  223. self.refresh_thread()
  224. self.assertEqual(self.thread.replies, 4)
  225. response = self.client.post(self.api_link, json.dumps({
  226. 'thread_url': other_thread.get_absolute_url(),
  227. 'posts': posts
  228. }), content_type="application/json")
  229. self.assertEqual(response.status_code, 200)
  230. # replies were moved
  231. self.refresh_thread()
  232. self.assertEqual(self.thread.replies, 0)
  233. other_thread = Thread.objects.get(pk=other_thread.pk)
  234. self.assertEqual(other_thread.post_set.filter(pk__in=posts).count(), 4)
  235. self.assertEqual(other_thread.replies, 4)