test_thread_postmove_api.py 19 KB

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