test_thread_postmove_api.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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.readtracker import poststracker
  8. from misago.threads import testutils
  9. from misago.threads.models import Thread
  10. from misago.threads.serializers.moderation import POSTS_LIMIT
  11. from misago.users.testutils import AuthenticatedUserTestCase
  12. class ThreadPostMoveApiTestCase(AuthenticatedUserTestCase):
  13. def setUp(self):
  14. super(ThreadPostMoveApiTestCase, self).setUp()
  15. self.category = Category.objects.get(slug='first-category')
  16. self.thread = testutils.post_thread(category=self.category)
  17. self.api_link = reverse(
  18. 'misago:api:thread-post-move', kwargs={
  19. 'thread_pk': self.thread.pk,
  20. }
  21. )
  22. Category(
  23. name='Category B',
  24. slug='category-b',
  25. ).insert_at(
  26. self.category,
  27. position='last-child',
  28. save=True,
  29. )
  30. self.category_b = Category.objects.get(slug='category-b')
  31. self.override_acl()
  32. self.override_other_acl()
  33. def refresh_thread(self):
  34. self.thread = Thread.objects.get(pk=self.thread.pk)
  35. def override_acl(self, extra_acl=None):
  36. new_acl = self.user.acl_cache
  37. new_acl['categories'][self.category.pk].update({
  38. 'can_see': 1,
  39. 'can_browse': 1,
  40. 'can_start_threads': 1,
  41. 'can_reply_threads': 1,
  42. 'can_edit_posts': 1,
  43. 'can_approve_content': 0,
  44. 'can_move_posts': 1,
  45. })
  46. if extra_acl:
  47. new_acl['categories'][self.category.pk].update(extra_acl)
  48. override_acl(self.user, new_acl)
  49. def override_other_acl(self, extra_acl=None):
  50. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  51. other_category_acl.update({
  52. 'can_see': 1,
  53. 'can_browse': 1,
  54. 'can_start_threads': 0,
  55. 'can_reply_threads': 0,
  56. 'can_edit_posts': 1,
  57. 'can_approve_content': 0,
  58. 'can_move_posts': 1,
  59. })
  60. if extra_acl:
  61. other_category_acl.update(extra_acl)
  62. categories_acl = self.user.acl_cache['categories']
  63. categories_acl[self.category_b.pk] = other_category_acl
  64. visible_categories = [self.category.pk]
  65. if other_category_acl['can_see']:
  66. visible_categories.append(self.category_b.pk)
  67. override_acl(
  68. self.user, {
  69. 'visible_categories': visible_categories,
  70. 'categories': categories_acl,
  71. }
  72. )
  73. def test_anonymous_user(self):
  74. """you need to authenticate to move posts"""
  75. self.logout_user()
  76. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  77. self.assertEqual(response.status_code, 403)
  78. def test_invalid_data(self):
  79. """api handles post that is invalid type"""
  80. self.override_acl()
  81. response = self.client.post(self.api_link, '[]', content_type="application/json")
  82. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  83. self.override_acl()
  84. response = self.client.post(self.api_link, '123', content_type="application/json")
  85. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  86. self.override_acl()
  87. response = self.client.post(self.api_link, '"string"', content_type="application/json")
  88. self.assertContains(response, "Invalid data. Expected a dictionary", status_code=400)
  89. self.override_acl()
  90. response = self.client.post(self.api_link, 'malformed', content_type="application/json")
  91. self.assertContains(response, "JSON parse error", status_code=400)
  92. def test_no_permission(self):
  93. """api validates permission to move"""
  94. self.override_acl({'can_move_posts': 0})
  95. response = self.client.post(self.api_link, json.dumps({}), content_type="application/json")
  96. self.assertContains(response, "You can't move posts in this thread.", status_code=403)
  97. def test_move_no_new_thread_url(self):
  98. """api validates if new thread url was given"""
  99. response = self.client.post(self.api_link)
  100. self.assertContains(response, "Enter link to new thread.", status_code=400)
  101. def test_invalid_new_thread_url(self):
  102. """api validates new thread url"""
  103. response = self.client.post(self.api_link, {
  104. 'new_thread': self.user.get_absolute_url(),
  105. })
  106. self.assertContains(response, "This is not a valid thread link.", status_code=400)
  107. def test_current_new_thread_url(self):
  108. """api validates if new thread url points to current thread"""
  109. response = self.client.post(
  110. self.api_link, {
  111. 'new_thread': self.thread.get_absolute_url(),
  112. }
  113. )
  114. self.assertContains(
  115. response, "Thread to move posts to is same as current one.", status_code=400
  116. )
  117. def test_other_thread_exists(self):
  118. """api validates if other thread exists"""
  119. self.override_other_acl()
  120. other_thread = testutils.post_thread(self.category_b)
  121. other_new_thread = other_thread.get_absolute_url()
  122. other_thread.delete()
  123. response = self.client.post(self.api_link, {
  124. 'new_thread': other_new_thread,
  125. })
  126. self.assertContains(
  127. response, "The thread you have entered link to doesn't exist", status_code=400
  128. )
  129. def test_other_thread_is_invisible(self):
  130. """api validates if other thread is visible"""
  131. self.override_other_acl({'can_see': 0})
  132. other_thread = testutils.post_thread(self.category_b)
  133. response = self.client.post(
  134. self.api_link, {
  135. 'new_thread': other_thread.get_absolute_url(),
  136. }
  137. )
  138. self.assertContains(
  139. response, "The thread you have entered link to doesn't exist", status_code=400
  140. )
  141. def test_other_thread_isnt_replyable(self):
  142. """api validates if other thread can be replied"""
  143. self.override_other_acl({'can_reply_threads': 0})
  144. other_thread = testutils.post_thread(self.category_b)
  145. response = self.client.post(
  146. self.api_link, {
  147. 'new_thread': other_thread.get_absolute_url(),
  148. }
  149. )
  150. self.assertContains(
  151. response, "You can't move posts to threads you can't reply.", status_code=400
  152. )
  153. def test_empty_data(self):
  154. """api handles empty data"""
  155. other_thread = testutils.post_thread(self.category)
  156. response = self.client.post(self.api_link)
  157. self.assertContains(
  158. response, "Enter link to new thread.", status_code=400
  159. )
  160. def test_empty_posts_data_json(self):
  161. """api handles empty json data"""
  162. other_thread = testutils.post_thread(self.category)
  163. response = self.client.post(
  164. self.api_link,
  165. json.dumps({
  166. 'new_thread': other_thread.get_absolute_url(),
  167. }),
  168. content_type="application/json",
  169. )
  170. self.assertContains(
  171. response, "You have to specify at least one post to move.", status_code=400
  172. )
  173. def test_empty_posts_data_form(self):
  174. """api handles empty form data"""
  175. other_thread = testutils.post_thread(self.category)
  176. response = self.client.post(
  177. self.api_link,
  178. {
  179. 'new_thread': other_thread.get_absolute_url(),
  180. },
  181. )
  182. self.assertContains(
  183. response, "You have to specify at least one post to move.", status_code=400
  184. )
  185. def test_no_posts_ids(self):
  186. """api rejects no posts ids"""
  187. other_thread = testutils.post_thread(self.category)
  188. response = self.client.post(
  189. self.api_link,
  190. json.dumps({
  191. 'new_thread': other_thread.get_absolute_url(),
  192. 'posts': [],
  193. }),
  194. content_type="application/json",
  195. )
  196. self.assertContains(
  197. response, "You have to specify at least one post to move.", status_code=400
  198. )
  199. def test_invalid_posts_data(self):
  200. """api handles invalid data"""
  201. other_thread = testutils.post_thread(self.category)
  202. response = self.client.post(
  203. self.api_link,
  204. json.dumps({
  205. 'new_thread': other_thread.get_absolute_url(),
  206. 'posts': 'string',
  207. }),
  208. content_type="application/json",
  209. )
  210. self.assertContains(
  211. response, "Expected a list of items", status_code=400
  212. )
  213. def test_invalid_posts_ids(self):
  214. """api handles invalid post id"""
  215. other_thread = testutils.post_thread(self.category)
  216. response = self.client.post(
  217. self.api_link,
  218. json.dumps({
  219. 'new_thread': other_thread.get_absolute_url(),
  220. 'posts': [1, 2, 'string'],
  221. }),
  222. content_type="application/json",
  223. )
  224. self.assertContains(
  225. response, "One or more post ids received were invalid.", status_code=400
  226. )
  227. def test_move_limit(self):
  228. """api rejects more posts than move limit"""
  229. other_thread = testutils.post_thread(self.category)
  230. response = self.client.post(
  231. self.api_link,
  232. json.dumps({
  233. 'new_thread': other_thread.get_absolute_url(),
  234. 'posts': list(range(POSTS_LIMIT + 1)),
  235. }),
  236. content_type="application/json",
  237. )
  238. self.assertContains(
  239. response, "No more than {} posts can be moved".format(POSTS_LIMIT), status_code=400
  240. )
  241. def test_move_invisible(self):
  242. """api validates posts visibility"""
  243. other_thread = testutils.post_thread(self.category)
  244. response = self.client.post(
  245. self.api_link,
  246. json.dumps({
  247. 'new_thread': other_thread.get_absolute_url(),
  248. 'posts': [testutils.reply_thread(self.thread, is_unapproved=True).pk],
  249. }),
  250. content_type="application/json",
  251. )
  252. self.assertContains(
  253. response, "One or more posts to move could not be found.", status_code=400
  254. )
  255. def test_move_other_thread_posts(self):
  256. """api recjects attempt to move other thread's post"""
  257. other_thread = testutils.post_thread(self.category)
  258. response = self.client.post(
  259. self.api_link,
  260. json.dumps({
  261. 'new_thread': other_thread.get_absolute_url(),
  262. 'posts': [testutils.reply_thread(other_thread, is_hidden=True).pk],
  263. }),
  264. content_type="application/json",
  265. )
  266. self.assertContains(
  267. response, "One or more posts to move could not be found.", status_code=400
  268. )
  269. def test_move_event(self):
  270. """api rejects events move"""
  271. other_thread = testutils.post_thread(self.category)
  272. response = self.client.post(
  273. self.api_link,
  274. json.dumps({
  275. 'new_thread': other_thread.get_absolute_url(),
  276. 'posts': [testutils.reply_thread(self.thread, is_event=True).pk],
  277. }),
  278. content_type="application/json",
  279. )
  280. self.assertContains(response, "Events can't be moved.", status_code=400)
  281. def test_move_first_post(self):
  282. """api rejects first post move"""
  283. other_thread = testutils.post_thread(self.category)
  284. response = self.client.post(
  285. self.api_link,
  286. json.dumps({
  287. 'new_thread': other_thread.get_absolute_url(),
  288. 'posts': [self.thread.first_post_id],
  289. }),
  290. content_type="application/json",
  291. )
  292. self.assertContains(response, "You can't move thread's first post.", status_code=400)
  293. def test_move_hidden_posts(self):
  294. """api recjects attempt to move urneadable hidden post"""
  295. other_thread = testutils.post_thread(self.category)
  296. response = self.client.post(
  297. self.api_link,
  298. json.dumps({
  299. 'new_thread': other_thread.get_absolute_url(),
  300. 'posts': [testutils.reply_thread(self.thread, is_hidden=True).pk],
  301. }),
  302. content_type="application/json",
  303. )
  304. self.assertContains(
  305. response, "You can't move posts the content you can't see.", status_code=400
  306. )
  307. def test_move_posts_closed_thread_no_permission(self):
  308. """api recjects attempt to move posts from closed thread"""
  309. other_thread = testutils.post_thread(self.category)
  310. self.thread.is_closed = True
  311. self.thread.save()
  312. self.override_acl({'can_close_threads': 0})
  313. response = self.client.post(
  314. self.api_link,
  315. json.dumps({
  316. 'new_thread': other_thread.get_absolute_url(),
  317. 'posts': [testutils.reply_thread(self.thread).pk],
  318. }),
  319. content_type="application/json",
  320. )
  321. self.assertContains(
  322. response, "This thread is closed. You can't move posts in it.", status_code=400
  323. )
  324. def test_move_posts_closed_category_no_permission(self):
  325. """api recjects attempt to move posts from closed thread"""
  326. other_thread = testutils.post_thread(self.category_b)
  327. self.category.is_closed = True
  328. self.category.save()
  329. self.override_acl({'can_close_threads': 0})
  330. self.override_other_acl({'can_reply_threads': 1})
  331. response = self.client.post(
  332. self.api_link,
  333. json.dumps({
  334. 'new_thread': other_thread.get_absolute_url(),
  335. 'posts': [testutils.reply_thread(self.thread).pk],
  336. }),
  337. content_type="application/json",
  338. )
  339. self.assertContains(
  340. response, "This category is closed. You can't move posts in it.", status_code=400
  341. )
  342. def test_move_posts(self):
  343. """api moves posts to other thread"""
  344. self.override_other_acl({'can_reply_threads': 1})
  345. other_thread = testutils.post_thread(self.category_b)
  346. posts = (
  347. testutils.reply_thread(self.thread).pk, testutils.reply_thread(self.thread).pk,
  348. testutils.reply_thread(self.thread).pk, testutils.reply_thread(self.thread).pk,
  349. )
  350. self.refresh_thread()
  351. self.assertEqual(self.thread.replies, 4)
  352. response = self.client.post(
  353. self.api_link,
  354. json.dumps({
  355. 'new_thread': other_thread.get_absolute_url(),
  356. 'posts': posts,
  357. }),
  358. content_type="application/json",
  359. )
  360. self.assertEqual(response.status_code, 200)
  361. # replies were moved
  362. self.refresh_thread()
  363. self.assertEqual(self.thread.replies, 0)
  364. other_thread = Thread.objects.get(pk=other_thread.pk)
  365. self.assertEqual(other_thread.post_set.filter(pk__in=posts).count(), 4)
  366. self.assertEqual(other_thread.replies, 4)
  367. def test_move_posts_reads(self):
  368. """api moves posts reads together with posts"""
  369. self.override_other_acl({'can_reply_threads': 1})
  370. other_thread = testutils.post_thread(self.category_b)
  371. posts = (
  372. testutils.reply_thread(self.thread),
  373. testutils.reply_thread(self.thread),
  374. )
  375. self.refresh_thread()
  376. self.assertEqual(self.thread.replies, 2)
  377. poststracker.save_read(self.user, self.thread.first_post)
  378. for post in posts:
  379. poststracker.save_read(self.user, post)
  380. response = self.client.post(
  381. self.api_link,
  382. json.dumps({
  383. 'new_thread': other_thread.get_absolute_url(),
  384. 'posts': [p.pk for p in posts],
  385. }),
  386. content_type="application/json",
  387. )
  388. self.assertEqual(response.status_code, 200)
  389. other_thread = Thread.objects.get(pk=other_thread.pk)
  390. # postreads were removed
  391. postreads = self.user.postread_set.order_by('id')
  392. postreads_threads = list(postreads.values_list('thread_id', flat=True))
  393. self.assertEqual(postreads_threads, [self.thread.pk])
  394. postreads_categories = list(postreads.values_list('category_id', flat=True))
  395. self.assertEqual(postreads_categories, [self.category.pk])