test_threads_editor_api.py 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. import os
  2. from django.urls import reverse
  3. from misago.acl import useracl
  4. from misago.acl.objectacl import add_acl_to_obj
  5. from misago.categories.models import Category
  6. from misago.threads import testutils
  7. from misago.threads.models import Attachment
  8. from misago.threads.serializers import AttachmentSerializer
  9. from misago.threads.test import patch_category_acl
  10. from misago.users.testutils import AuthenticatedUserTestCase
  11. TESTFILES_DIR = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'testfiles')
  12. TEST_DOCUMENT_PATH = os.path.join(TESTFILES_DIR, 'document.pdf')
  13. cache_versions = {"acl": "abcdefgh"}
  14. class EditorApiTestCase(AuthenticatedUserTestCase):
  15. def setUp(self):
  16. super().setUp()
  17. self.category = Category.objects.get(slug='first-category')
  18. class ThreadPostEditorApiTests(EditorApiTestCase):
  19. def setUp(self):
  20. super().setUp()
  21. self.api_link = reverse('misago:api:thread-editor')
  22. def test_anonymous_user_request(self):
  23. """endpoint validates if user is authenticated"""
  24. self.logout_user()
  25. response = self.client.get(self.api_link)
  26. self.assertEqual(response.status_code, 403)
  27. self.assertEqual(response.json(), {
  28. "detail": "You need to be signed in to start threads.",
  29. })
  30. @patch_category_acl({'can_browse': False})
  31. def test_category_visibility_validation(self):
  32. """endpoint omits non-browseable categories"""
  33. response = self.client.get(self.api_link)
  34. self.assertEqual(response.status_code, 403)
  35. self.assertEqual(response.json(), {
  36. "detail": "No categories that allow new threads are available to you at the moment.",
  37. })
  38. @patch_category_acl({'can_start_threads': False})
  39. def test_category_disallowing_new_threads(self):
  40. """endpoint omits category disallowing starting threads"""
  41. response = self.client.get(self.api_link)
  42. self.assertEqual(response.status_code, 403)
  43. self.assertEqual(response.json(), {
  44. "detail": "No categories that allow new threads are available to you at the moment.",
  45. })
  46. @patch_category_acl({'can_close_threads': False, 'can_start_threads': True})
  47. def test_category_closed_disallowing_new_threads(self):
  48. """endpoint omits closed category"""
  49. self.category.is_closed = True
  50. self.category.save()
  51. response = self.client.get(self.api_link)
  52. self.assertEqual(response.status_code, 403)
  53. self.assertEqual(response.json(), {
  54. "detail": "No categories that allow new threads are available to you at the moment.",
  55. })
  56. @patch_category_acl({'can_close_threads': True, 'can_start_threads': True})
  57. def test_category_closed_allowing_new_threads(self):
  58. """endpoint adds closed category that allows new threads"""
  59. self.category.is_closed = True
  60. self.category.save()
  61. response = self.client.get(self.api_link)
  62. self.assertEqual(response.status_code, 200)
  63. response_json = response.json()
  64. self.assertEqual(
  65. response_json[0], {
  66. 'id': self.category.pk,
  67. 'name': self.category.name,
  68. 'level': 0,
  69. 'post': {
  70. 'close': True,
  71. 'hide': False,
  72. 'pin': 0,
  73. },
  74. }
  75. )
  76. @patch_category_acl({'can_start_threads': True})
  77. def test_category_allowing_new_threads(self):
  78. """endpoint adds category that allows new threads"""
  79. response = self.client.get(self.api_link)
  80. self.assertEqual(response.status_code, 200)
  81. response_json = response.json()
  82. self.assertEqual(
  83. response_json[0], {
  84. 'id': self.category.pk,
  85. 'name': self.category.name,
  86. 'level': 0,
  87. 'post': {
  88. 'close': False,
  89. 'hide': False,
  90. 'pin': 0,
  91. },
  92. }
  93. )
  94. @patch_category_acl({'can_close_threads': True, 'can_start_threads': True})
  95. def test_category_allowing_closing_threads(self):
  96. """endpoint adds category that allows new closed threads"""
  97. response = self.client.get(self.api_link)
  98. self.assertEqual(response.status_code, 200)
  99. response_json = response.json()
  100. self.assertEqual(
  101. response_json[0], {
  102. 'id': self.category.pk,
  103. 'name': self.category.name,
  104. 'level': 0,
  105. 'post': {
  106. 'close': True,
  107. 'hide': False,
  108. 'pin': 0,
  109. },
  110. }
  111. )
  112. @patch_category_acl({'can_start_threads': True, 'can_pin_threads': 1})
  113. def test_category_allowing_locally_pinned_threads(self):
  114. """endpoint adds category that allows locally pinned threads"""
  115. response = self.client.get(self.api_link)
  116. self.assertEqual(response.status_code, 200)
  117. response_json = response.json()
  118. self.assertEqual(
  119. response_json[0], {
  120. 'id': self.category.pk,
  121. 'name': self.category.name,
  122. 'level': 0,
  123. 'post': {
  124. 'close': False,
  125. 'hide': False,
  126. 'pin': 1,
  127. },
  128. }
  129. )
  130. @patch_category_acl({'can_start_threads': True, 'can_pin_threads': 2})
  131. def test_category_allowing_globally_pinned_threads(self):
  132. """endpoint adds category that allows globally pinned threads"""
  133. response = self.client.get(self.api_link)
  134. self.assertEqual(response.status_code, 200)
  135. response_json = response.json()
  136. self.assertEqual(
  137. response_json[0], {
  138. 'id': self.category.pk,
  139. 'name': self.category.name,
  140. 'level': 0,
  141. 'post': {
  142. 'close': False,
  143. 'hide': False,
  144. 'pin': 2,
  145. },
  146. }
  147. )
  148. @patch_category_acl({'can_start_threads': True, 'can_hide_threads': 1})
  149. def test_category_allowing_hidding_threads(self):
  150. """endpoint adds category that allows hiding threads"""
  151. response = self.client.get(self.api_link)
  152. self.assertEqual(response.status_code, 200)
  153. response_json = response.json()
  154. self.assertEqual(
  155. response_json[0], {
  156. 'id': self.category.pk,
  157. 'name': self.category.name,
  158. 'level': 0,
  159. 'post': {
  160. 'close': 0,
  161. 'hide': 1,
  162. 'pin': 0,
  163. },
  164. }
  165. )
  166. @patch_category_acl({'can_start_threads': True, 'can_hide_threads': 2})
  167. def test_category_allowing_hidding_and_deleting_threads(self):
  168. """endpoint adds category that allows hiding and deleting threads"""
  169. response = self.client.get(self.api_link)
  170. self.assertEqual(response.status_code, 200)
  171. response_json = response.json()
  172. self.assertEqual(
  173. response_json[0], {
  174. 'id': self.category.pk,
  175. 'name': self.category.name,
  176. 'level': 0,
  177. 'post': {
  178. 'close': False,
  179. 'hide': 1,
  180. 'pin': 0,
  181. },
  182. }
  183. )
  184. class ThreadReplyEditorApiTests(EditorApiTestCase):
  185. def setUp(self):
  186. super().setUp()
  187. self.thread = testutils.post_thread(category=self.category)
  188. self.api_link = reverse(
  189. 'misago:api:thread-post-editor', kwargs={
  190. 'thread_pk': self.thread.pk,
  191. }
  192. )
  193. def test_anonymous_user_request(self):
  194. """endpoint validates if user is authenticated"""
  195. self.logout_user()
  196. response = self.client.get(self.api_link)
  197. self.assertEqual(response.status_code, 403)
  198. self.assertEqual(response.json(), {
  199. "detail": "You have to sign in to reply threads.",
  200. })
  201. def test_thread_visibility(self):
  202. """thread's visibility is validated"""
  203. with patch_category_acl({'can_see': False}):
  204. response = self.client.get(self.api_link)
  205. self.assertEqual(response.status_code, 404)
  206. with patch_category_acl({'can_browse': False}):
  207. response = self.client.get(self.api_link)
  208. self.assertEqual(response.status_code, 404)
  209. with patch_category_acl({'can_see_all_threads': False}):
  210. response = self.client.get(self.api_link)
  211. self.assertEqual(response.status_code, 404)
  212. @patch_category_acl({'can_reply_threads': False})
  213. def test_no_reply_permission(self):
  214. """permssion to reply is validated"""
  215. response = self.client.get(self.api_link)
  216. self.assertEqual(response.status_code, 403)
  217. self.assertEqual(response.json(), {
  218. "detail": "You can't reply to threads in this category.",
  219. })
  220. def test_closed_category(self):
  221. """permssion to reply in closed category is validated"""
  222. self.category.is_closed = True
  223. self.category.save()
  224. with patch_category_acl({'can_reply_threads': True, 'can_close_threads': False}):
  225. response = self.client.get(self.api_link)
  226. self.assertEqual(response.status_code, 403)
  227. self.assertEqual(response.json(), {
  228. "detail": "This category is closed. You can't reply to threads in it.",
  229. })
  230. # allow to post in closed category
  231. with patch_category_acl({'can_reply_threads': True, 'can_close_threads': True}):
  232. response = self.client.get(self.api_link)
  233. self.assertEqual(response.status_code, 200)
  234. def test_closed_thread(self):
  235. """permssion to reply in closed thread is validated"""
  236. self.thread.is_closed = True
  237. self.thread.save()
  238. with patch_category_acl({'can_reply_threads': True, 'can_close_threads': False}):
  239. response = self.client.get(self.api_link)
  240. self.assertEqual(response.status_code, 403)
  241. self.assertEqual(response.json(), {
  242. "detail": "You can't reply to closed threads in this category.",
  243. })
  244. # allow to post in closed thread
  245. with patch_category_acl({'can_reply_threads': True, 'can_close_threads': True}):
  246. response = self.client.get(self.api_link)
  247. self.assertEqual(response.status_code, 200)
  248. @patch_category_acl({'can_reply_threads': True})
  249. def test_allow_reply_thread(self):
  250. """api returns 200 code if thread reply is allowed"""
  251. response = self.client.get(self.api_link)
  252. self.assertEqual(response.status_code, 200)
  253. def test_reply_to_visibility(self):
  254. """api validates replied post visibility"""
  255. # unapproved reply can't be replied to
  256. unapproved_reply = testutils.reply_thread(self.thread, is_unapproved=True)
  257. with patch_category_acl({'can_reply_threads': True}):
  258. response = self.client.get('%s?reply=%s' % (self.api_link, unapproved_reply.pk))
  259. self.assertEqual(response.status_code, 404)
  260. # hidden reply can't be replied to
  261. hidden_reply = testutils.reply_thread(self.thread, is_hidden=True)
  262. with patch_category_acl({'can_reply_threads': True}):
  263. response = self.client.get('%s?reply=%s' % (self.api_link, hidden_reply.pk))
  264. self.assertEqual(response.status_code, 403)
  265. self.assertEqual(response.json(), {
  266. "detail": "You can't reply to hidden posts.",
  267. })
  268. def test_reply_to_other_thread_post(self):
  269. """api validates is replied post belongs to same thread"""
  270. other_thread = testutils.post_thread(category=self.category)
  271. reply_to = testutils.reply_thread(other_thread)
  272. response = self.client.get('%s?reply=%s' % (self.api_link, reply_to.pk))
  273. self.assertEqual(response.status_code, 404)
  274. @patch_category_acl({'can_reply_threads': True})
  275. def test_reply_to_event(self):
  276. """events can't be replied to"""
  277. reply_to = testutils.reply_thread(self.thread, is_event=True)
  278. response = self.client.get('%s?reply=%s' % (self.api_link, reply_to.pk))
  279. self.assertEqual(response.status_code, 403)
  280. self.assertEqual(response.json(), {
  281. "detail": "You can't reply to events.",
  282. })
  283. @patch_category_acl({'can_reply_threads': True})
  284. def test_reply_to(self):
  285. """api includes replied to post details in response"""
  286. reply_to = testutils.reply_thread(self.thread)
  287. response = self.client.get('%s?reply=%s' % (self.api_link, reply_to.pk))
  288. self.assertEqual(response.status_code, 200)
  289. self.assertEqual(
  290. response.json(), {
  291. 'id': reply_to.pk,
  292. 'post': reply_to.original,
  293. 'poster': reply_to.poster_name,
  294. }
  295. )
  296. class EditReplyEditorApiTests(EditorApiTestCase):
  297. def setUp(self):
  298. super().setUp()
  299. self.thread = testutils.post_thread(category=self.category)
  300. self.post = testutils.reply_thread(self.thread, poster=self.user)
  301. self.api_link = reverse(
  302. 'misago:api:thread-post-editor',
  303. kwargs={
  304. 'thread_pk': self.thread.pk,
  305. 'pk': self.post.pk,
  306. }
  307. )
  308. def test_anonymous_user_request(self):
  309. """endpoint validates if user is authenticated"""
  310. self.logout_user()
  311. response = self.client.get(self.api_link)
  312. self.assertEqual(response.status_code, 403)
  313. self.assertEqual(response.json(), {
  314. "detail": "You have to sign in to edit posts.",
  315. })
  316. def test_thread_visibility(self):
  317. """thread's visibility is validated"""
  318. with patch_category_acl({'can_see': False}):
  319. response = self.client.get(self.api_link)
  320. self.assertEqual(response.status_code, 404)
  321. with patch_category_acl({'can_browse': False}):
  322. response = self.client.get(self.api_link)
  323. self.assertEqual(response.status_code, 404)
  324. with patch_category_acl({'can_see_all_threads': False}):
  325. response = self.client.get(self.api_link)
  326. self.assertEqual(response.status_code, 404)
  327. @patch_category_acl({'can_edit_posts': 0})
  328. def test_no_edit_permission(self):
  329. """permssion to edit is validated"""
  330. response = self.client.get(self.api_link)
  331. self.assertEqual(response.status_code, 403)
  332. self.assertEqual(response.json(), {
  333. "detail": "You can't edit posts in this category.",
  334. })
  335. def test_closed_category(self):
  336. """permssion to edit in closed category is validated"""
  337. self.category.is_closed = True
  338. self.category.save()
  339. with patch_category_acl({'can_edit_posts': 1, 'can_close_threads': False}):
  340. response = self.client.get(self.api_link)
  341. self.assertEqual(response.status_code, 403)
  342. self.assertEqual(response.json(), {
  343. "detail": "This category is closed. You can't edit posts in it.",
  344. })
  345. # allow to edit in closed category
  346. with patch_category_acl({'can_edit_posts': 1, 'can_close_threads': True}):
  347. response = self.client.get(self.api_link)
  348. self.assertEqual(response.status_code, 200)
  349. def test_closed_thread(self):
  350. """permssion to edit in closed thread is validated"""
  351. self.thread.is_closed = True
  352. self.thread.save()
  353. with patch_category_acl({'can_edit_posts': 1, 'can_close_threads': False}):
  354. response = self.client.get(self.api_link)
  355. self.assertEqual(response.status_code, 403)
  356. self.assertEqual(response.json(), {
  357. "detail": "This thread is closed. You can't edit posts in it.",
  358. })
  359. # allow to edit in closed thread
  360. with patch_category_acl({'can_edit_posts': 1, 'can_close_threads': True}):
  361. response = self.client.get(self.api_link)
  362. self.assertEqual(response.status_code, 200)
  363. def test_protected_post(self):
  364. """permssion to edit protected post is validated"""
  365. self.post.is_protected = True
  366. self.post.save()
  367. with patch_category_acl({'can_edit_posts': 1, 'can_protect_posts': False}):
  368. response = self.client.get(self.api_link)
  369. self.assertEqual(response.status_code, 403)
  370. self.assertEqual(response.json(), {
  371. "detail": "This post is protected. You can't edit it.",
  372. })
  373. # allow to post in closed thread
  374. with patch_category_acl({'can_edit_posts': 1, 'can_protect_posts': True}):
  375. response = self.client.get(self.api_link)
  376. self.assertEqual(response.status_code, 200)
  377. def test_post_visibility(self):
  378. """edited posts visibility is validated"""
  379. self.post.is_hidden = True
  380. self.post.save()
  381. with patch_category_acl({'can_edit_posts': 1}):
  382. response = self.client.get(self.api_link)
  383. self.assertEqual(response.status_code, 403)
  384. self.assertEqual(response.json(), {
  385. "detail": "This post is hidden, you can't edit it.",
  386. })
  387. # allow hidden edition
  388. with patch_category_acl({'can_edit_posts': 1, 'can_hide_posts': 1}):
  389. response = self.client.get(self.api_link)
  390. self.assertEqual(response.status_code, 200)
  391. # test unapproved post
  392. self.post.is_unapproved = True
  393. self.post.is_hidden = False
  394. self.post.poster = None
  395. self.post.save()
  396. with patch_category_acl({'can_edit_posts': 2, 'can_approve_content': 0}):
  397. response = self.client.get(self.api_link)
  398. self.assertEqual(response.status_code, 404)
  399. # allow unapproved edition
  400. with patch_category_acl({'can_edit_posts': 2, 'can_approve_content': 1}):
  401. response = self.client.get(self.api_link)
  402. self.assertEqual(response.status_code, 200)
  403. @patch_category_acl({'can_edit_posts': 2})
  404. def test_post_is_event(self):
  405. """events can't be edited"""
  406. self.post.is_event = True
  407. self.post.save()
  408. response = self.client.get(self.api_link)
  409. self.assertEqual(response.status_code, 403)
  410. self.assertEqual(response.json(), {
  411. "detail": "Events can't be edited.",
  412. })
  413. def test_other_user_post(self):
  414. """api validates if other user's post can be edited"""
  415. self.post.poster = None
  416. self.post.save()
  417. with patch_category_acl({'can_edit_posts': 1}):
  418. response = self.client.get(self.api_link)
  419. self.assertEqual(response.status_code, 403)
  420. self.assertEqual(response.json(), {
  421. "detail": "You can't edit other users posts in this category.",
  422. })
  423. # allow other users post edition
  424. with patch_category_acl({'can_edit_posts': 2}):
  425. response = self.client.get(self.api_link)
  426. self.assertEqual(response.status_code, 200)
  427. @patch_category_acl({'can_hide_threads': 1, 'can_edit_posts': 2})
  428. def test_edit_first_post_hidden(self):
  429. """endpoint returns valid configuration for editor of hidden thread's first post"""
  430. self.thread.is_hidden = True
  431. self.thread.save()
  432. self.thread.first_post.is_hidden = True
  433. self.thread.first_post.save()
  434. api_link = reverse(
  435. 'misago:api:thread-post-editor',
  436. kwargs={
  437. 'thread_pk': self.thread.pk,
  438. 'pk': self.thread.first_post.pk,
  439. }
  440. )
  441. response = self.client.get(api_link)
  442. self.assertEqual(response.status_code, 200)
  443. @patch_category_acl({'can_edit_posts': 1})
  444. def test_edit(self):
  445. """endpoint returns valid configuration for editor"""
  446. with patch_category_acl({'max_attachment_size': 1000}):
  447. for _ in range(3):
  448. with open(TEST_DOCUMENT_PATH, 'rb') as upload:
  449. response = self.client.post(
  450. reverse('misago:api:attachment-list'), data={
  451. 'upload': upload,
  452. }
  453. )
  454. self.assertEqual(response.status_code, 200)
  455. attachments = list(Attachment.objects.order_by('id'))
  456. attachments[0].uploader = None
  457. attachments[0].save()
  458. for attachment in attachments[:2]:
  459. attachment.post = self.post
  460. attachment.save()
  461. response = self.client.get(self.api_link)
  462. user_acl = useracl.get_user_acl(self.user, cache_versions)
  463. for attachment in attachments:
  464. add_acl_to_obj(user_acl, attachment)
  465. self.assertEqual(response.status_code, 200)
  466. self.assertEqual(
  467. response.json(), {
  468. 'id': self.post.pk,
  469. 'api': self.post.get_api_url(),
  470. 'post': self.post.original,
  471. 'can_protect': False,
  472. 'is_protected': self.post.is_protected,
  473. 'poster': self.post.poster_name,
  474. 'attachments': [
  475. AttachmentSerializer(attachments[1], context={'user': self.user}).data,
  476. AttachmentSerializer(attachments[0], context={'user': self.user}).data,
  477. ],
  478. }
  479. )