test_thread_postmove_api.py 13 KB

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