test_thread_postmove_api.py 12 KB

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