test_thread_patch_api.py 57 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855
  1. import json
  2. from datetime import timedelta
  3. from django.utils import six, timezone
  4. from misago.acl.testutils import override_acl
  5. from misago.categories.models import Category
  6. from misago.readtracker import poststracker
  7. from misago.threads import testutils
  8. from misago.threads.models import Thread
  9. from .test_threads_api import ThreadsApiTestCase
  10. class ThreadPatchApiTestCase(ThreadsApiTestCase):
  11. def patch(self, api_link, ops):
  12. return self.client.patch(api_link, json.dumps(ops), content_type="application/json")
  13. class ThreadAddAclApiTests(ThreadPatchApiTestCase):
  14. def test_add_acl_true(self):
  15. """api adds current thread's acl to response"""
  16. response = self.patch(self.api_link, [
  17. {
  18. 'op': 'add',
  19. 'path': 'acl',
  20. 'value': True,
  21. },
  22. ])
  23. self.assertEqual(response.status_code, 200)
  24. response_json = response.json()
  25. self.assertTrue(response_json['acl'])
  26. def test_add_acl_false(self):
  27. """if value is false, api won't add acl to the response, but will set empty key"""
  28. response = self.patch(self.api_link, [
  29. {
  30. 'op': 'add',
  31. 'path': 'acl',
  32. 'value': False,
  33. },
  34. ])
  35. self.assertEqual(response.status_code, 200)
  36. response_json = response.json()
  37. self.assertIsNone(response_json['acl'])
  38. class ThreadChangeTitleApiTests(ThreadPatchApiTestCase):
  39. def test_change_thread_title(self):
  40. """api makes it possible to change thread title"""
  41. self.override_acl({'can_edit_threads': 2})
  42. response = self.patch(
  43. self.api_link, [
  44. {
  45. 'op': 'replace',
  46. 'path': 'title',
  47. 'value': "Lorem ipsum change!",
  48. },
  49. ]
  50. )
  51. self.assertEqual(response.status_code, 200)
  52. response_json = response.json()
  53. self.assertEqual(response_json['title'], "Lorem ipsum change!")
  54. thread_json = self.get_thread_json()
  55. self.assertEqual(thread_json['title'], "Lorem ipsum change!")
  56. def test_change_thread_title_no_permission(self):
  57. """api validates permission to change title"""
  58. self.override_acl({'can_edit_threads': 0})
  59. response = self.patch(
  60. self.api_link, [
  61. {
  62. 'op': 'replace',
  63. 'path': 'title',
  64. 'value': "Lorem ipsum change!",
  65. },
  66. ]
  67. )
  68. self.assertEqual(response.status_code, 400)
  69. response_json = response.json()
  70. self.assertEqual(response_json['detail'][0], "You can't edit threads in this category.")
  71. def test_change_thread_title_closed_category_no_permission(self):
  72. """api test permission to edit thread title in closed category"""
  73. self.override_acl({
  74. 'can_edit_threads': 2,
  75. 'can_close_threads': 0
  76. })
  77. self.category.is_closed = True
  78. self.category.save()
  79. response = self.patch(
  80. self.api_link, [
  81. {
  82. 'op': 'replace',
  83. 'path': 'title',
  84. 'value': "Lorem ipsum change!",
  85. },
  86. ]
  87. )
  88. self.assertEqual(response.status_code, 400)
  89. response_json = response.json()
  90. self.assertEqual(
  91. response_json['detail'][0], "This category is closed. You can't edit threads in it."
  92. )
  93. def test_change_thread_title_closed_thread_no_permission(self):
  94. """api test permission to edit closed thread title"""
  95. self.override_acl({
  96. 'can_edit_threads': 2,
  97. 'can_close_threads': 0
  98. })
  99. self.thread.is_closed = True
  100. self.thread.save()
  101. response = self.patch(
  102. self.api_link, [
  103. {
  104. 'op': 'replace',
  105. 'path': 'title',
  106. 'value': "Lorem ipsum change!",
  107. },
  108. ]
  109. )
  110. self.assertEqual(response.status_code, 400)
  111. response_json = response.json()
  112. self.assertEqual(
  113. response_json['detail'][0], "This thread is closed. You can't edit it."
  114. )
  115. def test_change_thread_title_after_edit_time(self):
  116. """api cleans, validates and rejects too short title"""
  117. self.override_acl({'thread_edit_time': 1, 'can_edit_threads': 1})
  118. self.thread.starter = self.user
  119. self.thread.started_on = timezone.now() - timedelta(minutes=10)
  120. self.thread.save()
  121. response = self.patch(
  122. self.api_link, [
  123. {
  124. 'op': 'replace',
  125. 'path': 'title',
  126. 'value': "Lorem ipsum change!",
  127. },
  128. ]
  129. )
  130. self.assertEqual(response.status_code, 400)
  131. response_json = response.json()
  132. self.assertEqual(
  133. response_json['detail'][0], "You can't edit threads that are older than 1 minute."
  134. )
  135. def test_change_thread_title_invalid(self):
  136. """api cleans, validates and rejects too short title"""
  137. self.override_acl({'can_edit_threads': 2})
  138. response = self.patch(
  139. self.api_link, [
  140. {
  141. 'op': 'replace',
  142. 'path': 'title',
  143. 'value': 12,
  144. },
  145. ]
  146. )
  147. self.assertEqual(response.status_code, 400)
  148. response_json = response.json()
  149. self.assertEqual(
  150. response_json['detail'][0],
  151. "Thread title should be at least 5 characters long (it has 2)."
  152. )
  153. class ThreadPinGloballyApiTests(ThreadPatchApiTestCase):
  154. def test_pin_thread(self):
  155. """api makes it possible to pin globally thread"""
  156. self.override_acl({'can_pin_threads': 2})
  157. response = self.patch(
  158. self.api_link, [
  159. {
  160. 'op': 'replace',
  161. 'path': 'weight',
  162. 'value': 2,
  163. },
  164. ]
  165. )
  166. self.assertEqual(response.status_code, 200)
  167. response_json = response.json()
  168. self.assertEqual(response_json['weight'], 2)
  169. thread_json = self.get_thread_json()
  170. self.assertEqual(thread_json['weight'], 2)
  171. def test_pin_thread_closed_category_no_permission(self):
  172. """api checks if category is closed"""
  173. self.override_acl({
  174. 'can_pin_threads': 2,
  175. 'can_close_threads': 0,
  176. })
  177. self.category.is_closed = True
  178. self.category.save()
  179. response = self.patch(
  180. self.api_link, [
  181. {
  182. 'op': 'replace',
  183. 'path': 'weight',
  184. 'value': 2,
  185. },
  186. ]
  187. )
  188. self.assertEqual(response.status_code, 400)
  189. response_json = response.json()
  190. self.assertEqual(
  191. response_json['detail'][0], "This category is closed. You can't change threads weights in it."
  192. )
  193. def test_pin_thread_closed_no_permission(self):
  194. """api checks if thread is closed"""
  195. self.override_acl({
  196. 'can_pin_threads': 2,
  197. 'can_close_threads': 0,
  198. })
  199. self.thread.is_closed = True
  200. self.thread.save()
  201. response = self.patch(
  202. self.api_link, [
  203. {
  204. 'op': 'replace',
  205. 'path': 'weight',
  206. 'value': 2,
  207. },
  208. ]
  209. )
  210. self.assertEqual(response.status_code, 400)
  211. response_json = response.json()
  212. self.assertEqual(
  213. response_json['detail'][0], "This thread is closed. You can't change its weight."
  214. )
  215. def test_unpin_thread(self):
  216. """api makes it possible to unpin thread"""
  217. self.thread.weight = 2
  218. self.thread.save()
  219. thread_json = self.get_thread_json()
  220. self.assertEqual(thread_json['weight'], 2)
  221. self.override_acl({'can_pin_threads': 2})
  222. response = self.patch(
  223. self.api_link, [
  224. {
  225. 'op': 'replace',
  226. 'path': 'weight',
  227. 'value': 0,
  228. },
  229. ]
  230. )
  231. self.assertEqual(response.status_code, 200)
  232. response_json = response.json()
  233. self.assertEqual(response_json['weight'], 0)
  234. thread_json = self.get_thread_json()
  235. self.assertEqual(thread_json['weight'], 0)
  236. def test_pin_thread_no_permission(self):
  237. """api pin thread globally with no permission fails"""
  238. self.override_acl({'can_pin_threads': 1})
  239. response = self.patch(
  240. self.api_link, [
  241. {
  242. 'op': 'replace',
  243. 'path': 'weight',
  244. 'value': 2,
  245. },
  246. ]
  247. )
  248. self.assertEqual(response.status_code, 400)
  249. response_json = response.json()
  250. self.assertEqual(
  251. response_json['detail'][0], "You can't pin threads globally in this category."
  252. )
  253. thread_json = self.get_thread_json()
  254. self.assertEqual(thread_json['weight'], 0)
  255. def test_unpin_thread_no_permission(self):
  256. """api unpin thread with no permission fails"""
  257. self.thread.weight = 2
  258. self.thread.save()
  259. thread_json = self.get_thread_json()
  260. self.assertEqual(thread_json['weight'], 2)
  261. self.override_acl({'can_pin_threads': 1})
  262. response = self.patch(
  263. self.api_link, [
  264. {
  265. 'op': 'replace',
  266. 'path': 'weight',
  267. 'value': 1,
  268. },
  269. ]
  270. )
  271. self.assertEqual(response.status_code, 400)
  272. response_json = response.json()
  273. self.assertEqual(
  274. response_json['detail'][0], "You can't change globally pinned threads weights in this category."
  275. )
  276. thread_json = self.get_thread_json()
  277. self.assertEqual(thread_json['weight'], 2)
  278. class ThreadPinLocallyApiTests(ThreadPatchApiTestCase):
  279. def test_pin_thread(self):
  280. """api makes it possible to pin locally thread"""
  281. self.override_acl({'can_pin_threads': 1})
  282. response = self.patch(
  283. self.api_link, [
  284. {
  285. 'op': 'replace',
  286. 'path': 'weight',
  287. 'value': 1,
  288. },
  289. ]
  290. )
  291. self.assertEqual(response.status_code, 200)
  292. response_json = response.json()
  293. self.assertEqual(response_json['weight'], 1)
  294. thread_json = self.get_thread_json()
  295. self.assertEqual(thread_json['weight'], 1)
  296. def test_unpin_thread(self):
  297. """api makes it possible to unpin thread"""
  298. self.thread.weight = 1
  299. self.thread.save()
  300. thread_json = self.get_thread_json()
  301. self.assertEqual(thread_json['weight'], 1)
  302. self.override_acl({'can_pin_threads': 1})
  303. response = self.patch(
  304. self.api_link, [
  305. {
  306. 'op': 'replace',
  307. 'path': 'weight',
  308. 'value': 0,
  309. },
  310. ]
  311. )
  312. self.assertEqual(response.status_code, 200)
  313. response_json = response.json()
  314. self.assertEqual(response_json['weight'], 0)
  315. thread_json = self.get_thread_json()
  316. self.assertEqual(thread_json['weight'], 0)
  317. def test_pin_thread_no_permission(self):
  318. """api pin thread locally with no permission fails"""
  319. self.override_acl({'can_pin_threads': 0})
  320. response = self.patch(
  321. self.api_link, [
  322. {
  323. 'op': 'replace',
  324. 'path': 'weight',
  325. 'value': 1,
  326. },
  327. ]
  328. )
  329. self.assertEqual(response.status_code, 400)
  330. response_json = response.json()
  331. self.assertEqual(
  332. response_json['detail'][0], "You can't change threads weights in this category."
  333. )
  334. thread_json = self.get_thread_json()
  335. self.assertEqual(thread_json['weight'], 0)
  336. def test_unpin_thread_no_permission(self):
  337. """api unpin thread with no permission fails"""
  338. self.thread.weight = 1
  339. self.thread.save()
  340. thread_json = self.get_thread_json()
  341. self.assertEqual(thread_json['weight'], 1)
  342. self.override_acl({'can_pin_threads': 0})
  343. response = self.patch(
  344. self.api_link, [
  345. {
  346. 'op': 'replace',
  347. 'path': 'weight',
  348. 'value': 0,
  349. },
  350. ]
  351. )
  352. self.assertEqual(response.status_code, 400)
  353. response_json = response.json()
  354. self.assertEqual(
  355. response_json['detail'][0], "You can't change threads weights in this category."
  356. )
  357. thread_json = self.get_thread_json()
  358. self.assertEqual(thread_json['weight'], 1)
  359. class ThreadMoveApiTests(ThreadPatchApiTestCase):
  360. def setUp(self):
  361. super(ThreadMoveApiTests, self).setUp()
  362. Category(
  363. name='Category B',
  364. slug='category-b',
  365. ).insert_at(
  366. self.category,
  367. position='last-child',
  368. save=True,
  369. )
  370. self.category_b = Category.objects.get(slug='category-b')
  371. def override_other_acl(self, acl):
  372. other_category_acl = self.user.acl_cache['categories'][self.category.pk].copy()
  373. other_category_acl.update({
  374. 'can_see': 1,
  375. 'can_browse': 1,
  376. 'can_see_all_threads': 1,
  377. 'can_see_own_threads': 0,
  378. 'can_hide_threads': 0,
  379. 'can_approve_content': 0,
  380. })
  381. other_category_acl.update(acl)
  382. categories_acl = self.user.acl_cache['categories']
  383. categories_acl[self.category_b.pk] = other_category_acl
  384. visible_categories = [self.category.pk]
  385. if other_category_acl['can_see']:
  386. visible_categories.append(self.category_b.pk)
  387. override_acl(
  388. self.user, {
  389. 'visible_categories': visible_categories,
  390. 'categories': categories_acl,
  391. }
  392. )
  393. def test_move_thread_no_top(self):
  394. """api moves thread to other category, sets no top category"""
  395. self.override_acl({'can_move_threads': True})
  396. self.override_other_acl({'can_start_threads': 2})
  397. response = self.patch(
  398. self.api_link, [
  399. {
  400. 'op': 'replace',
  401. 'path': 'category',
  402. 'value': self.category_b.pk,
  403. },
  404. {
  405. 'op': 'add',
  406. 'path': 'top-category',
  407. 'value': self.category_b.pk,
  408. },
  409. {
  410. 'op': 'replace',
  411. 'path': 'flatten-categories',
  412. 'value': None,
  413. },
  414. ]
  415. )
  416. self.assertEqual(response.status_code, 200)
  417. reponse_json = response.json()
  418. self.assertEqual(reponse_json['category'], self.category_b.pk)
  419. self.override_other_acl({})
  420. thread_json = self.get_thread_json()
  421. self.assertEqual(thread_json['category']['id'], self.category_b.pk)
  422. def test_move_thread_with_top(self):
  423. """api moves thread to other category, sets top"""
  424. self.override_acl({'can_move_threads': True})
  425. self.override_other_acl({'can_start_threads': 2})
  426. response = self.patch(
  427. self.api_link, [
  428. {
  429. 'op': 'replace',
  430. 'path': 'category',
  431. 'value': self.category_b.pk,
  432. },
  433. {
  434. 'op': 'add',
  435. 'path': 'top-category',
  436. 'value': Category.objects.root_category().pk,
  437. },
  438. {
  439. 'op': 'replace',
  440. 'path': 'flatten-categories',
  441. 'value': None,
  442. },
  443. ]
  444. )
  445. self.assertEqual(response.status_code, 200)
  446. reponse_json = response.json()
  447. self.assertEqual(reponse_json['category'], self.category_b.pk)
  448. self.override_other_acl({})
  449. thread_json = self.get_thread_json()
  450. self.assertEqual(thread_json['category']['id'], self.category_b.pk)
  451. def test_move_thread_reads(self):
  452. """api moves thread reads together with thread"""
  453. self.override_acl({'can_move_threads': True})
  454. self.override_other_acl({'can_start_threads': 2})
  455. poststracker.save_read(self.user, self.thread.first_post)
  456. self.assertEqual(self.user.postread_set.count(), 1)
  457. self.user.postread_set.get(category=self.category)
  458. response = self.patch(
  459. self.api_link, [
  460. {
  461. 'op': 'replace',
  462. 'path': 'category',
  463. 'value': self.category_b.pk,
  464. },
  465. {
  466. 'op': 'add',
  467. 'path': 'top-category',
  468. 'value': self.category_b.pk,
  469. },
  470. {
  471. 'op': 'replace',
  472. 'path': 'flatten-categories',
  473. 'value': None,
  474. },
  475. ]
  476. )
  477. self.assertEqual(response.status_code, 200)
  478. # thread read was moved to new category
  479. postreads = self.user.postread_set.filter(post__is_event=False).order_by('id')
  480. self.assertEqual(postreads.count(), 1)
  481. postreads.get(category=self.category_b)
  482. def test_move_thread_subscriptions(self):
  483. """api moves thread subscriptions together with thread"""
  484. self.override_acl({'can_move_threads': True})
  485. self.override_other_acl({'can_start_threads': 2})
  486. self.user.subscription_set.create(
  487. thread=self.thread,
  488. category=self.thread.category,
  489. last_read_on=self.thread.last_post_on,
  490. send_email=False,
  491. )
  492. self.assertEqual(self.user.subscription_set.count(), 1)
  493. self.user.subscription_set.get(category=self.category)
  494. response = self.patch(
  495. self.api_link, [
  496. {
  497. 'op': 'replace',
  498. 'path': 'category',
  499. 'value': self.category_b.pk,
  500. },
  501. {
  502. 'op': 'add',
  503. 'path': 'top-category',
  504. 'value': self.category_b.pk,
  505. },
  506. {
  507. 'op': 'replace',
  508. 'path': 'flatten-categories',
  509. 'value': None,
  510. },
  511. ]
  512. )
  513. self.assertEqual(response.status_code, 200)
  514. # thread read was moved to new category
  515. self.assertEqual(self.user.subscription_set.count(), 1)
  516. self.user.subscription_set.get(category=self.category_b)
  517. def test_move_thread_no_permission(self):
  518. """api move thread to other category with no permission fails"""
  519. self.override_acl({'can_move_threads': False})
  520. self.override_other_acl({})
  521. response = self.patch(
  522. self.api_link, [
  523. {
  524. 'op': 'replace',
  525. 'path': 'category',
  526. 'value': self.category_b.pk,
  527. },
  528. ]
  529. )
  530. self.assertEqual(response.status_code, 400)
  531. response_json = response.json()
  532. self.assertEqual(
  533. response_json['detail'][0], "You can't move threads in this category."
  534. )
  535. self.override_other_acl({})
  536. thread_json = self.get_thread_json()
  537. self.assertEqual(thread_json['category']['id'], self.category.pk)
  538. def test_move_thread_closed_category_no_permission(self):
  539. """api move thread from closed category with no permission fails"""
  540. self.override_acl({
  541. 'can_move_threads': True,
  542. 'can_close_threads': False,
  543. })
  544. self.override_other_acl({})
  545. self.category.is_closed = True
  546. self.category.save()
  547. response = self.patch(
  548. self.api_link, [
  549. {
  550. 'op': 'replace',
  551. 'path': 'category',
  552. 'value': self.category_b.pk,
  553. },
  554. ]
  555. )
  556. self.assertEqual(response.status_code, 400)
  557. response_json = response.json()
  558. self.assertEqual(
  559. response_json['detail'][0], "This category is closed. You can't move it's threads."
  560. )
  561. def test_move_closed_thread_no_permission(self):
  562. """api move closed thread with no permission fails"""
  563. self.override_acl({
  564. 'can_move_threads': True,
  565. 'can_close_threads': False,
  566. })
  567. self.override_other_acl({})
  568. self.thread.is_closed = True
  569. self.thread.save()
  570. response = self.patch(
  571. self.api_link, [
  572. {
  573. 'op': 'replace',
  574. 'path': 'category',
  575. 'value': self.category_b.pk,
  576. },
  577. ]
  578. )
  579. self.assertEqual(response.status_code, 400)
  580. response_json = response.json()
  581. self.assertEqual(
  582. response_json['detail'][0], "This thread is closed. You can't move it."
  583. )
  584. def test_move_thread_no_category_access(self):
  585. """api move thread to category with no access fails"""
  586. self.override_acl({'can_move_threads': True})
  587. self.override_other_acl({'can_see': False})
  588. response = self.patch(
  589. self.api_link, [
  590. {
  591. 'op': 'replace',
  592. 'path': 'category',
  593. 'value': self.category_b.pk,
  594. },
  595. ]
  596. )
  597. self.assertEqual(response.status_code, 400)
  598. response_json = response.json()
  599. self.assertEqual(response_json['detail'][0], 'NOT FOUND')
  600. self.override_other_acl({})
  601. thread_json = self.get_thread_json()
  602. self.assertEqual(thread_json['category']['id'], self.category.pk)
  603. def test_move_thread_no_category_browse(self):
  604. """api move thread to category with no browsing access fails"""
  605. self.override_acl({'can_move_threads': True})
  606. self.override_other_acl({'can_browse': False})
  607. response = self.patch(
  608. self.api_link, [
  609. {
  610. 'op': 'replace',
  611. 'path': 'category',
  612. 'value': self.category_b.pk,
  613. },
  614. ]
  615. )
  616. self.assertEqual(response.status_code, 400)
  617. response_json = response.json()
  618. self.assertEqual(
  619. response_json['detail'][0],
  620. 'You don\'t have permission to browse "Category B" contents.'
  621. )
  622. self.override_other_acl({})
  623. thread_json = self.get_thread_json()
  624. self.assertEqual(thread_json['category']['id'], self.category.pk)
  625. def test_move_thread_no_category_start_threads(self):
  626. """api move thread to category with no posting access fails"""
  627. self.override_acl({'can_move_threads': True})
  628. self.override_other_acl({'can_start_threads': False})
  629. response = self.patch(
  630. self.api_link, [
  631. {
  632. 'op': 'replace',
  633. 'path': 'category',
  634. 'value': self.category_b.pk,
  635. },
  636. ]
  637. )
  638. self.assertEqual(response.status_code, 400)
  639. response_json = response.json()
  640. self.assertEqual(
  641. response_json['detail'][0],
  642. "You don't have permission to start new threads in this category."
  643. )
  644. self.override_other_acl({})
  645. thread_json = self.get_thread_json()
  646. self.assertEqual(thread_json['category']['id'], self.category.pk)
  647. def test_move_thread_same_category(self):
  648. """api move thread to category it's already in fails"""
  649. self.override_acl({'can_move_threads': True})
  650. self.override_other_acl({'can_start_threads': 2})
  651. response = self.patch(
  652. self.api_link, [
  653. {
  654. 'op': 'replace',
  655. 'path': 'category',
  656. 'value': self.thread.category_id,
  657. },
  658. ]
  659. )
  660. self.assertEqual(response.status_code, 400)
  661. response_json = response.json()
  662. self.assertEqual(
  663. response_json['detail'][0], "You can't move thread to the category it's already in."
  664. )
  665. self.override_other_acl({})
  666. thread_json = self.get_thread_json()
  667. self.assertEqual(thread_json['category']['id'], self.category.pk)
  668. def test_thread_flatten_categories(self):
  669. """api flatten thread categories"""
  670. response = self.patch(
  671. self.api_link, [
  672. {
  673. 'op': 'replace',
  674. 'path': 'flatten-categories',
  675. 'value': None,
  676. },
  677. ]
  678. )
  679. self.assertEqual(response.status_code, 200)
  680. response_json = response.json()
  681. self.assertEqual(response_json['category'], self.category.pk)
  682. class ThreadCloseApiTests(ThreadPatchApiTestCase):
  683. def test_close_thread(self):
  684. """api makes it possible to close thread"""
  685. self.override_acl({'can_close_threads': True})
  686. response = self.patch(
  687. self.api_link, [
  688. {
  689. 'op': 'replace',
  690. 'path': 'is-closed',
  691. 'value': True,
  692. },
  693. ]
  694. )
  695. self.assertEqual(response.status_code, 200)
  696. response_json = response.json()
  697. self.assertTrue(response_json['is_closed'])
  698. thread_json = self.get_thread_json()
  699. self.assertTrue(thread_json['is_closed'])
  700. def test_open_thread(self):
  701. """api makes it possible to open thread"""
  702. self.thread.is_closed = True
  703. self.thread.save()
  704. thread_json = self.get_thread_json()
  705. self.assertTrue(thread_json['is_closed'])
  706. self.override_acl({'can_close_threads': True})
  707. response = self.patch(
  708. self.api_link, [
  709. {
  710. 'op': 'replace',
  711. 'path': 'is-closed',
  712. 'value': False,
  713. },
  714. ]
  715. )
  716. self.assertEqual(response.status_code, 200)
  717. response_json = response.json()
  718. self.assertFalse(response_json['is_closed'])
  719. thread_json = self.get_thread_json()
  720. self.assertFalse(thread_json['is_closed'])
  721. def test_close_thread_no_permission(self):
  722. """api close thread with no permission fails"""
  723. self.override_acl({'can_close_threads': False})
  724. response = self.patch(
  725. self.api_link, [
  726. {
  727. 'op': 'replace',
  728. 'path': 'is-closed',
  729. 'value': True,
  730. },
  731. ]
  732. )
  733. self.assertEqual(response.status_code, 400)
  734. response_json = response.json()
  735. self.assertEqual(
  736. response_json['detail'][0], "You don't have permission to close this thread."
  737. )
  738. thread_json = self.get_thread_json()
  739. self.assertFalse(thread_json['is_closed'])
  740. def test_open_thread_no_permission(self):
  741. """api open thread with no permission fails"""
  742. self.thread.is_closed = True
  743. self.thread.save()
  744. thread_json = self.get_thread_json()
  745. self.assertTrue(thread_json['is_closed'])
  746. self.override_acl({'can_close_threads': False})
  747. response = self.patch(
  748. self.api_link, [
  749. {
  750. 'op': 'replace',
  751. 'path': 'is-closed',
  752. 'value': False,
  753. },
  754. ]
  755. )
  756. self.assertEqual(response.status_code, 400)
  757. response_json = response.json()
  758. self.assertEqual(
  759. response_json['detail'][0], "You don't have permission to open this thread."
  760. )
  761. thread_json = self.get_thread_json()
  762. self.assertTrue(thread_json['is_closed'])
  763. class ThreadApproveApiTests(ThreadPatchApiTestCase):
  764. def test_approve_thread(self):
  765. """api makes it possible to approve thread"""
  766. self.thread.first_post.is_unapproved = True
  767. self.thread.first_post.save()
  768. self.thread.synchronize()
  769. self.thread.save()
  770. self.assertTrue(self.thread.is_unapproved)
  771. self.assertTrue(self.thread.has_unapproved_posts)
  772. self.override_acl({'can_approve_content': 1})
  773. response = self.patch(
  774. self.api_link, [
  775. {
  776. 'op': 'replace',
  777. 'path': 'is-unapproved',
  778. 'value': False,
  779. },
  780. ]
  781. )
  782. self.assertEqual(response.status_code, 200)
  783. response_json = response.json()
  784. self.assertFalse(response_json['is_unapproved'])
  785. self.assertFalse(response_json['has_unapproved_posts'])
  786. thread_json = self.get_thread_json()
  787. self.assertFalse(thread_json['is_unapproved'])
  788. self.assertFalse(thread_json['has_unapproved_posts'])
  789. thread = Thread.objects.get(pk=self.thread.pk)
  790. self.assertFalse(thread.is_unapproved)
  791. self.assertFalse(thread.has_unapproved_posts)
  792. def test_approve_thread_category_closed_no_permission(self):
  793. """api checks permission for approving threads in closed categories"""
  794. self.thread.first_post.is_unapproved = True
  795. self.thread.first_post.save()
  796. self.thread.synchronize()
  797. self.thread.save()
  798. self.assertTrue(self.thread.is_unapproved)
  799. self.assertTrue(self.thread.has_unapproved_posts)
  800. self.category.is_closed = True
  801. self.category.save()
  802. self.override_acl({
  803. 'can_approve_content': 1,
  804. 'can_close_threads': 0,
  805. })
  806. response = self.patch(
  807. self.api_link, [
  808. {
  809. 'op': 'replace',
  810. 'path': 'is-unapproved',
  811. 'value': False,
  812. },
  813. ]
  814. )
  815. self.assertEqual(response.status_code, 400)
  816. response_json = response.json()
  817. self.assertEqual(response_json['detail'][0], "This category is closed. You can't approve threads in it.")
  818. def test_approve_thread_closed_no_permission(self):
  819. """api checks permission for approving posts in closed categories"""
  820. self.thread.first_post.is_unapproved = True
  821. self.thread.first_post.save()
  822. self.thread.synchronize()
  823. self.thread.save()
  824. self.assertTrue(self.thread.is_unapproved)
  825. self.assertTrue(self.thread.has_unapproved_posts)
  826. self.thread.is_closed = True
  827. self.thread.save()
  828. self.override_acl({
  829. 'can_approve_content': 1,
  830. 'can_close_threads': 0,
  831. })
  832. response = self.patch(
  833. self.api_link, [
  834. {
  835. 'op': 'replace',
  836. 'path': 'is-unapproved',
  837. 'value': False,
  838. },
  839. ]
  840. )
  841. self.assertEqual(response.status_code, 400)
  842. response_json = response.json()
  843. self.assertEqual(response_json['detail'][0], "This thread is closed. You can't approve it.")
  844. def test_unapprove_thread(self):
  845. """api returns permission error on approval removal"""
  846. self.override_acl({'can_approve_content': 1})
  847. response = self.patch(
  848. self.api_link, [
  849. {
  850. 'op': 'replace',
  851. 'path': 'is-unapproved',
  852. 'value': True,
  853. },
  854. ]
  855. )
  856. self.assertEqual(response.status_code, 400)
  857. response_json = response.json()
  858. self.assertEqual(response_json['detail'][0], "Content approval can't be reversed.")
  859. class ThreadHideApiTests(ThreadPatchApiTestCase):
  860. def test_hide_thread(self):
  861. """api makes it possible to hide thread"""
  862. self.override_acl({'can_hide_threads': 1})
  863. response = self.patch(
  864. self.api_link, [
  865. {
  866. 'op': 'replace',
  867. 'path': 'is-hidden',
  868. 'value': True,
  869. },
  870. ]
  871. )
  872. self.assertEqual(response.status_code, 200)
  873. reponse_json = response.json()
  874. self.assertTrue(reponse_json['is_hidden'])
  875. self.override_acl({'can_hide_threads': 1})
  876. thread_json = self.get_thread_json()
  877. self.assertTrue(thread_json['is_hidden'])
  878. def test_hide_thread_no_permission(self):
  879. """api hide thread with no permission fails"""
  880. self.override_acl({'can_hide_threads': 0})
  881. response = self.patch(
  882. self.api_link, [
  883. {
  884. 'op': 'replace',
  885. 'path': 'is-hidden',
  886. 'value': True,
  887. },
  888. ]
  889. )
  890. self.assertEqual(response.status_code, 400)
  891. response_json = response.json()
  892. self.assertEqual(
  893. response_json['detail'][0], "You can't hide threads in this category."
  894. )
  895. thread_json = self.get_thread_json()
  896. self.assertFalse(thread_json['is_hidden'])
  897. def test_hide_non_owned_thread(self):
  898. """api forbids non-moderator from hiding other users threads"""
  899. self.override_acl({
  900. 'can_hide_own_threads': 1,
  901. 'can_hide_threads': 0
  902. })
  903. response = self.patch(
  904. self.api_link, [
  905. {
  906. 'op': 'replace',
  907. 'path': 'is-hidden',
  908. 'value': True,
  909. },
  910. ]
  911. )
  912. self.assertEqual(response.status_code, 400)
  913. response_json = response.json()
  914. self.assertEqual(
  915. response_json['detail'][0], "You can't hide other users theads in this category."
  916. )
  917. def test_hide_owned_thread_no_time(self):
  918. """api forbids non-moderator from hiding other users threads"""
  919. self.override_acl({
  920. 'can_hide_own_threads': 1,
  921. 'can_hide_threads': 0,
  922. 'thread_edit_time': 1,
  923. })
  924. self.thread.starter = self.user
  925. self.thread.started_on = timezone.now() - timedelta(minutes=5)
  926. self.thread.save()
  927. response = self.patch(
  928. self.api_link, [
  929. {
  930. 'op': 'replace',
  931. 'path': 'is-hidden',
  932. 'value': True,
  933. },
  934. ]
  935. )
  936. self.assertEqual(response.status_code, 400)
  937. response_json = response.json()
  938. self.assertEqual(
  939. response_json['detail'][0], "You can't hide threads that are older than 1 minute."
  940. )
  941. def test_hide_closed_category_no_permission(self):
  942. """api test permission to hide thread in closed category"""
  943. self.override_acl({
  944. 'can_hide_threads': 1,
  945. 'can_close_threads': 0
  946. })
  947. self.category.is_closed = True
  948. self.category.save()
  949. response = self.patch(
  950. self.api_link, [
  951. {
  952. 'op': 'replace',
  953. 'path': 'is-hidden',
  954. 'value': True,
  955. },
  956. ]
  957. )
  958. self.assertEqual(response.status_code, 400)
  959. response_json = response.json()
  960. self.assertEqual(
  961. response_json['detail'][0], "This category is closed. You can't hide threads in it."
  962. )
  963. def test_hide_closed_thread_no_permission(self):
  964. """api test permission to hide closed thread"""
  965. self.override_acl({
  966. 'can_hide_threads': 1,
  967. 'can_close_threads': 0
  968. })
  969. self.thread.is_closed = True
  970. self.thread.save()
  971. response = self.patch(
  972. self.api_link, [
  973. {
  974. 'op': 'replace',
  975. 'path': 'is-hidden',
  976. 'value': True,
  977. },
  978. ]
  979. )
  980. self.assertEqual(response.status_code, 400)
  981. response_json = response.json()
  982. self.assertEqual(
  983. response_json['detail'][0], "This thread is closed. You can't hide it."
  984. )
  985. class ThreadUnhideApiTests(ThreadPatchApiTestCase):
  986. def setUp(self):
  987. super(ThreadUnhideApiTests, self).setUp()
  988. self.thread.is_hidden = True
  989. self.thread.save()
  990. def test_unhide_thread(self):
  991. """api makes it possible to unhide thread"""
  992. self.override_acl({'can_hide_threads': 1})
  993. response = self.patch(
  994. self.api_link, [
  995. {
  996. 'op': 'replace',
  997. 'path': 'is-hidden',
  998. 'value': False,
  999. },
  1000. ]
  1001. )
  1002. self.assertEqual(response.status_code, 200)
  1003. reponse_json = response.json()
  1004. self.assertFalse(reponse_json['is_hidden'])
  1005. self.override_acl({'can_hide_threads': 1})
  1006. thread_json = self.get_thread_json()
  1007. self.assertFalse(thread_json['is_hidden'])
  1008. def test_unhide_thread_no_permission(self):
  1009. """api unhide thread with no permission fails as thread is invisible"""
  1010. self.override_acl({'can_hide_threads': 0})
  1011. response = self.patch(
  1012. self.api_link, [
  1013. {
  1014. 'op': 'replace',
  1015. 'path': 'is-hidden',
  1016. 'value': True,
  1017. },
  1018. ]
  1019. )
  1020. self.assertEqual(response.status_code, 404)
  1021. def test_unhide_closed_category_no_permission(self):
  1022. """api test permission to unhide thread in closed category"""
  1023. self.override_acl({
  1024. 'can_hide_threads': 1,
  1025. 'can_close_threads': 0
  1026. })
  1027. self.category.is_closed = True
  1028. self.category.save()
  1029. response = self.patch(
  1030. self.api_link, [
  1031. {
  1032. 'op': 'replace',
  1033. 'path': 'is-hidden',
  1034. 'value': False,
  1035. },
  1036. ]
  1037. )
  1038. self.assertEqual(response.status_code, 400)
  1039. response_json = response.json()
  1040. self.assertEqual(
  1041. response_json['detail'][0], "This category is closed. You can't reveal threads in it."
  1042. )
  1043. def test_unhide_closed_thread_no_permission(self):
  1044. """api test permission to unhide closed thread"""
  1045. self.override_acl({
  1046. 'can_hide_threads': 1,
  1047. 'can_close_threads': 0
  1048. })
  1049. self.thread.is_closed = True
  1050. self.thread.save()
  1051. response = self.patch(
  1052. self.api_link, [
  1053. {
  1054. 'op': 'replace',
  1055. 'path': 'is-hidden',
  1056. 'value': False,
  1057. },
  1058. ]
  1059. )
  1060. self.assertEqual(response.status_code, 400)
  1061. response_json = response.json()
  1062. self.assertEqual(
  1063. response_json['detail'][0], "This thread is closed. You can't reveal it."
  1064. )
  1065. class ThreadSubscribeApiTests(ThreadPatchApiTestCase):
  1066. def test_subscribe_thread(self):
  1067. """api makes it possible to subscribe thread"""
  1068. response = self.patch(
  1069. self.api_link, [
  1070. {
  1071. 'op': 'replace',
  1072. 'path': 'subscription',
  1073. 'value': 'notify',
  1074. },
  1075. ]
  1076. )
  1077. self.assertEqual(response.status_code, 200)
  1078. reponse_json = response.json()
  1079. self.assertFalse(reponse_json['subscription'])
  1080. thread_json = self.get_thread_json()
  1081. self.assertFalse(thread_json['subscription'])
  1082. subscription = self.user.subscription_set.get(thread=self.thread)
  1083. self.assertFalse(subscription.send_email)
  1084. def test_subscribe_thread_with_email(self):
  1085. """api makes it possible to subscribe thread with emails"""
  1086. response = self.patch(
  1087. self.api_link, [
  1088. {
  1089. 'op': 'replace',
  1090. 'path': 'subscription',
  1091. 'value': 'email',
  1092. },
  1093. ]
  1094. )
  1095. self.assertEqual(response.status_code, 200)
  1096. reponse_json = response.json()
  1097. self.assertTrue(reponse_json['subscription'])
  1098. thread_json = self.get_thread_json()
  1099. self.assertTrue(thread_json['subscription'])
  1100. subscription = self.user.subscription_set.get(thread=self.thread)
  1101. self.assertTrue(subscription.send_email)
  1102. def test_unsubscribe_thread(self):
  1103. """api makes it possible to unsubscribe thread"""
  1104. response = self.patch(
  1105. self.api_link, [
  1106. {
  1107. 'op': 'replace',
  1108. 'path': 'subscription',
  1109. 'value': 'remove',
  1110. },
  1111. ]
  1112. )
  1113. self.assertEqual(response.status_code, 200)
  1114. reponse_json = response.json()
  1115. self.assertIsNone(reponse_json['subscription'])
  1116. thread_json = self.get_thread_json()
  1117. self.assertIsNone(thread_json['subscription'])
  1118. self.assertEqual(self.user.subscription_set.count(), 0)
  1119. def test_subscribe_as_guest(self):
  1120. """api makes it impossible to subscribe thread"""
  1121. self.logout_user()
  1122. response = self.patch(
  1123. self.api_link, [
  1124. {
  1125. 'op': 'replace',
  1126. 'path': 'subscription',
  1127. 'value': 'email',
  1128. },
  1129. ]
  1130. )
  1131. self.assertEqual(response.status_code, 403)
  1132. def test_subscribe_nonexistant_thread(self):
  1133. """api makes it impossible to subscribe nonexistant thread"""
  1134. bad_api_link = self.api_link.replace(
  1135. six.text_type(self.thread.pk), six.text_type(self.thread.pk + 9)
  1136. )
  1137. response = self.patch(
  1138. bad_api_link, [
  1139. {
  1140. 'op': 'replace',
  1141. 'path': 'subscription',
  1142. 'value': 'email',
  1143. },
  1144. ]
  1145. )
  1146. self.assertEqual(response.status_code, 404)
  1147. class ThreadMarkBestAnswerApiTests(ThreadPatchApiTestCase):
  1148. def test_mark_best_answer(self):
  1149. """api makes it possible to mark best answer"""
  1150. self.override_acl({'can_mark_best_answers': 2})
  1151. best_answer = testutils.reply_thread(self.thread)
  1152. response = self.patch(
  1153. self.api_link, [
  1154. {
  1155. 'op': 'replace',
  1156. 'path': 'best-answer',
  1157. 'value': best_answer.id,
  1158. },
  1159. ]
  1160. )
  1161. self.assertEqual(response.status_code, 200)
  1162. self.assertEqual(response.json(), {
  1163. 'id': self.thread.id,
  1164. 'detail': ['ok'],
  1165. 'best_answer': best_answer.id,
  1166. 'best_answer_is_protected': False,
  1167. 'best_answer_marked_on': response.json()['best_answer_marked_on'],
  1168. 'best_answer_marked_by': self.user.id,
  1169. 'best_answer_marked_by_name': self.user.username,
  1170. 'best_answer_marked_by_slug': self.user.slug,
  1171. })
  1172. thread_json = self.get_thread_json()
  1173. self.assertEqual(thread_json['best_answer'], best_answer.id)
  1174. self.assertEqual(thread_json['best_answer_is_protected'], False)
  1175. self.assertEqual(
  1176. thread_json['best_answer_marked_on'], response.json()['best_answer_marked_on'])
  1177. self.assertEqual(thread_json['best_answer_marked_by'], self.user.id)
  1178. self.assertEqual(thread_json['best_answer_marked_by_name'], self.user.username)
  1179. self.assertEqual(thread_json['best_answer_marked_by_slug'], self.user.slug)
  1180. def test_mark_best_answer_anonymous(self):
  1181. """api validates that user is authenticated before marking best answer"""
  1182. self.logout_user()
  1183. self.override_acl({'can_mark_best_answers': 2})
  1184. best_answer = testutils.reply_thread(self.thread)
  1185. response = self.patch(
  1186. self.api_link, [
  1187. {
  1188. 'op': 'replace',
  1189. 'path': 'best-answer',
  1190. 'value': best_answer.id,
  1191. },
  1192. ]
  1193. )
  1194. self.assertEqual(response.status_code, 403)
  1195. self.assertEqual(response.json(), {
  1196. 'detail': "This action is not available to guests.",
  1197. })
  1198. thread_json = self.get_thread_json()
  1199. self.assertIsNone(thread_json['best_answer'])
  1200. def test_mark_best_answer_no_permission(self):
  1201. """api validates permission to mark best answers"""
  1202. self.override_acl({'can_mark_best_answers': 0})
  1203. best_answer = testutils.reply_thread(self.thread)
  1204. response = self.patch(
  1205. self.api_link, [
  1206. {
  1207. 'op': 'replace',
  1208. 'path': 'best-answer',
  1209. 'value': best_answer.id,
  1210. },
  1211. ]
  1212. )
  1213. self.assertEqual(response.status_code, 400)
  1214. self.assertEqual(response.json(), {
  1215. 'id': self.thread.id,
  1216. 'detail': [
  1217. 'You don\'t have permission to mark best answers in the "First category" category.'
  1218. ],
  1219. })
  1220. thread_json = self.get_thread_json()
  1221. self.assertIsNone(thread_json['best_answer'])
  1222. def test_mark_best_answer_not_thread_starter(self):
  1223. """api validates permission to mark best answers in owned thread"""
  1224. self.override_acl({'can_mark_best_answers': 1})
  1225. best_answer = testutils.reply_thread(self.thread)
  1226. response = self.patch(
  1227. self.api_link, [
  1228. {
  1229. 'op': 'replace',
  1230. 'path': 'best-answer',
  1231. 'value': best_answer.id,
  1232. },
  1233. ]
  1234. )
  1235. self.assertEqual(response.status_code, 400)
  1236. self.assertEqual(response.json(), {
  1237. 'id': self.thread.id,
  1238. 'detail': [
  1239. "You don't have permission to mark best answer in this thread because you didn't "
  1240. "start it."
  1241. ],
  1242. })
  1243. thread_json = self.get_thread_json()
  1244. self.assertIsNone(thread_json['best_answer'])
  1245. # passing scenario is possible
  1246. self.thread.starter = self.user
  1247. self.thread.save()
  1248. self.override_acl({'can_mark_best_answers': 1})
  1249. response = self.patch(
  1250. self.api_link, [
  1251. {
  1252. 'op': 'replace',
  1253. 'path': 'best-answer',
  1254. 'value': best_answer.id,
  1255. },
  1256. ]
  1257. )
  1258. self.assertEqual(response.status_code, 200)
  1259. def test_mark_best_answer_category_closed(self):
  1260. """api validates permission to mark best answers in closed category"""
  1261. self.override_acl({'can_mark_best_answers': 2, 'can_close_threads': 0})
  1262. best_answer = testutils.reply_thread(self.thread)
  1263. self.category.is_closed = True
  1264. self.category.save()
  1265. response = self.patch(
  1266. self.api_link, [
  1267. {
  1268. 'op': 'replace',
  1269. 'path': 'best-answer',
  1270. 'value': best_answer.id,
  1271. },
  1272. ]
  1273. )
  1274. self.assertEqual(response.status_code, 400)
  1275. self.assertEqual(response.json(), {
  1276. 'id': self.thread.id,
  1277. 'detail': [
  1278. 'You don\'t have permission to mark best answer in this thread because its '
  1279. 'category "First category" is closed.'
  1280. ],
  1281. })
  1282. thread_json = self.get_thread_json()
  1283. self.assertIsNone(thread_json['best_answer'])
  1284. # passing scenario is possible
  1285. self.override_acl({'can_mark_best_answers': 2, 'can_close_threads': 1})
  1286. response = self.patch(
  1287. self.api_link, [
  1288. {
  1289. 'op': 'replace',
  1290. 'path': 'best-answer',
  1291. 'value': best_answer.id,
  1292. },
  1293. ]
  1294. )
  1295. self.assertEqual(response.status_code, 200)
  1296. def test_mark_best_answer_thread_closed(self):
  1297. """api validates permission to mark best answers in closed thread"""
  1298. self.override_acl({'can_mark_best_answers': 2, 'can_close_threads': 0})
  1299. best_answer = testutils.reply_thread(self.thread)
  1300. self.thread.is_closed = True
  1301. self.thread.save()
  1302. response = self.patch(
  1303. self.api_link, [
  1304. {
  1305. 'op': 'replace',
  1306. 'path': 'best-answer',
  1307. 'value': best_answer.id,
  1308. },
  1309. ]
  1310. )
  1311. self.assertEqual(response.status_code, 400)
  1312. self.assertEqual(response.json(), {
  1313. 'id': self.thread.id,
  1314. 'detail': [
  1315. "You can't mark best answer in this thread because it's closed and you don't have "
  1316. "permission to open it."
  1317. ],
  1318. })
  1319. thread_json = self.get_thread_json()
  1320. self.assertIsNone(thread_json['best_answer'])
  1321. # passing scenario is possible
  1322. self.override_acl({'can_mark_best_answers': 2, 'can_close_threads': 1})
  1323. response = self.patch(
  1324. self.api_link, [
  1325. {
  1326. 'op': 'replace',
  1327. 'path': 'best-answer',
  1328. 'value': best_answer.id,
  1329. },
  1330. ]
  1331. )
  1332. self.assertEqual(response.status_code, 200)
  1333. def test_mark_best_answer_invalid_post_id(self):
  1334. """api validates that post id is int"""
  1335. self.override_acl({'can_mark_best_answers': 2})
  1336. response = self.patch(
  1337. self.api_link, [
  1338. {
  1339. 'op': 'replace',
  1340. 'path': 'best-answer',
  1341. 'value': 'd7sd89a7d98sa',
  1342. },
  1343. ]
  1344. )
  1345. self.assertEqual(response.status_code, 400)
  1346. self.assertEqual(response.json(), {
  1347. 'id': self.thread.id,
  1348. 'detail': ["A valid integer is required."],
  1349. })
  1350. thread_json = self.get_thread_json()
  1351. self.assertIsNone(thread_json['best_answer'])
  1352. def test_mark_best_answer_post_not_found(self):
  1353. """api validates that post exists"""
  1354. self.override_acl({'can_mark_best_answers': 2})
  1355. response = self.patch(
  1356. self.api_link, [
  1357. {
  1358. 'op': 'replace',
  1359. 'path': 'best-answer',
  1360. 'value': self.thread.last_post_id + 1,
  1361. },
  1362. ]
  1363. )
  1364. self.assertEqual(response.status_code, 400)
  1365. self.assertEqual(response.json(), {
  1366. 'id': self.thread.id,
  1367. 'detail': ["NOT FOUND"],
  1368. })
  1369. thread_json = self.get_thread_json()
  1370. self.assertIsNone(thread_json['best_answer'])
  1371. def test_mark_best_answer_post_invisible(self):
  1372. """api validates post visibility to action author"""
  1373. self.override_acl({'can_mark_best_answers': 2})
  1374. unapproved_post = testutils.reply_thread(self.thread, is_unapproved=True)
  1375. response = self.patch(
  1376. self.api_link, [
  1377. {
  1378. 'op': 'replace',
  1379. 'path': 'best-answer',
  1380. 'value': unapproved_post.id,
  1381. },
  1382. ]
  1383. )
  1384. self.assertEqual(response.status_code, 400)
  1385. self.assertEqual(response.json(), {
  1386. 'id': self.thread.id,
  1387. 'detail': ["NOT FOUND"],
  1388. })
  1389. thread_json = self.get_thread_json()
  1390. self.assertIsNone(thread_json['best_answer'])
  1391. def test_mark_best_answer_post_other_thread(self):
  1392. """api validates post belongs to same thread"""
  1393. self.override_acl({'can_mark_best_answers': 2})
  1394. other_thread = testutils.post_thread(self.category)
  1395. response = self.patch(
  1396. self.api_link, [
  1397. {
  1398. 'op': 'replace',
  1399. 'path': 'best-answer',
  1400. 'value': other_thread.first_post_id,
  1401. },
  1402. ]
  1403. )
  1404. self.assertEqual(response.status_code, 400)
  1405. self.assertEqual(response.json(), {
  1406. 'id': self.thread.id,
  1407. 'detail': ["NOT FOUND"],
  1408. })
  1409. thread_json = self.get_thread_json()
  1410. self.assertIsNone(thread_json['best_answer'])
  1411. def test_mark_best_answer_event_id(self):
  1412. """api validates that post is not an event"""
  1413. self.override_acl({'can_mark_best_answers': 2})
  1414. best_answer = testutils.reply_thread(self.thread)
  1415. best_answer.is_event = True
  1416. best_answer.save()
  1417. response = self.patch(
  1418. self.api_link, [
  1419. {
  1420. 'op': 'replace',
  1421. 'path': 'best-answer',
  1422. 'value': best_answer.id,
  1423. },
  1424. ]
  1425. )
  1426. self.assertEqual(response.status_code, 400)
  1427. self.assertEqual(response.json(), {
  1428. 'id': self.thread.id,
  1429. 'detail': ["Events can't be marked as best answers."],
  1430. })
  1431. thread_json = self.get_thread_json()
  1432. self.assertIsNone(thread_json['best_answer'])
  1433. def test_mark_best_answer_first_post(self):
  1434. """api validates that post is not a first post in thread"""
  1435. self.override_acl({'can_mark_best_answers': 2})
  1436. response = self.patch(
  1437. self.api_link, [
  1438. {
  1439. 'op': 'replace',
  1440. 'path': 'best-answer',
  1441. 'value': self.thread.first_post_id,
  1442. },
  1443. ]
  1444. )
  1445. self.assertEqual(response.status_code, 400)
  1446. self.assertEqual(response.json(), {
  1447. 'id': self.thread.id,
  1448. 'detail': ["First post in a thread can't be marked as best answer."],
  1449. })
  1450. thread_json = self.get_thread_json()
  1451. self.assertIsNone(thread_json['best_answer'])
  1452. def test_mark_best_answer_hidden_post(self):
  1453. """api validates that post is not hidden"""
  1454. self.override_acl({'can_mark_best_answers': 2})
  1455. best_answer = testutils.reply_thread(self.thread, is_hidden=True)
  1456. response = self.patch(
  1457. self.api_link, [
  1458. {
  1459. 'op': 'replace',
  1460. 'path': 'best-answer',
  1461. 'value': best_answer.id,
  1462. },
  1463. ]
  1464. )
  1465. self.assertEqual(response.status_code, 400)
  1466. self.assertEqual(response.json(), {
  1467. 'id': self.thread.id,
  1468. 'detail': ["Hidden posts can't be marked as best answers."],
  1469. })
  1470. thread_json = self.get_thread_json()
  1471. self.assertIsNone(thread_json['best_answer'])
  1472. def test_mark_best_answer_unapproved_post(self):
  1473. """api validates that post is not unapproved"""
  1474. self.override_acl({'can_mark_best_answers': 2})
  1475. best_answer = testutils.reply_thread(self.thread, poster=self.user, is_unapproved=True)
  1476. response = self.patch(
  1477. self.api_link, [
  1478. {
  1479. 'op': 'replace',
  1480. 'path': 'best-answer',
  1481. 'value': best_answer.id,
  1482. },
  1483. ]
  1484. )
  1485. self.assertEqual(response.status_code, 400)
  1486. self.assertEqual(response.json(), {
  1487. 'id': self.thread.id,
  1488. 'detail': ["Unapproved posts can't be marked as best answers."],
  1489. })
  1490. thread_json = self.get_thread_json()
  1491. self.assertIsNone(thread_json['best_answer'])
  1492. def test_mark_best_answer_protected_post(self):
  1493. """api respects post protection"""
  1494. self.override_acl({'can_mark_best_answers': 2, 'can_protect_posts': 0})
  1495. best_answer = testutils.reply_thread(self.thread, is_protected=True)
  1496. response = self.patch(
  1497. self.api_link, [
  1498. {
  1499. 'op': 'replace',
  1500. 'path': 'best-answer',
  1501. 'value': best_answer.id,
  1502. },
  1503. ]
  1504. )
  1505. self.assertEqual(response.status_code, 400)
  1506. self.assertEqual(response.json(), {
  1507. 'id': self.thread.id,
  1508. 'detail': [
  1509. "You don't have permission to mark this post as best answer because a moderator "
  1510. "has protected it."
  1511. ],
  1512. })
  1513. thread_json = self.get_thread_json()
  1514. self.assertIsNone(thread_json['best_answer'])
  1515. # passing scenario is possible
  1516. self.override_acl({'can_mark_best_answers': 2, 'can_protect_posts': 1})
  1517. response = self.patch(
  1518. self.api_link, [
  1519. {
  1520. 'op': 'replace',
  1521. 'path': 'best-answer',
  1522. 'value': best_answer.id,
  1523. },
  1524. ]
  1525. )
  1526. self.assertEqual(response.status_code, 200)