test_threads_editor_api.py 21 KB

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