test_thread_postmove_api.py 12 KB

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