AuthorizationManager.test.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  1. import { beforeEach, describe, chai, expect, it } from 'vitest'
  2. import sinon from 'sinon'
  3. chai.should()
  4. const modulePath = '../../../app/js/AuthorizationManager'
  5. describe('AuthorizationManager', () => {
  6. beforeEach(async ctx => {
  7. ctx.client = { ol_context: {} }
  8. ctx.AuthorizationManager = (await import(modulePath)).default
  9. })
  10. describe('assertClientCanViewProject', () => {
  11. it('should allow the readOnly privilegeLevel', async ctx => {
  12. await new Promise((resolve, reject) => {
  13. ctx.client.ol_context.privilege_level = 'readOnly'
  14. ctx.AuthorizationManager.assertClientCanViewProject(
  15. ctx.client,
  16. error => {
  17. expect(error).to.be.null
  18. resolve()
  19. }
  20. )
  21. })
  22. })
  23. it('should allow the readAndWrite privilegeLevel', async ctx => {
  24. await new Promise((resolve, reject) => {
  25. ctx.client.ol_context.privilege_level = 'readAndWrite'
  26. ctx.AuthorizationManager.assertClientCanViewProject(
  27. ctx.client,
  28. error => {
  29. expect(error).to.be.null
  30. resolve()
  31. }
  32. )
  33. })
  34. })
  35. it('should allow the review privilegeLevel', async ctx => {
  36. await new Promise((resolve, reject) => {
  37. ctx.client.ol_context.privilege_level = 'review'
  38. ctx.AuthorizationManager.assertClientCanViewProject(
  39. ctx.client,
  40. error => {
  41. expect(error).to.be.null
  42. resolve()
  43. }
  44. )
  45. })
  46. })
  47. it('should allow the owner privilegeLevel', async ctx => {
  48. await new Promise((resolve, reject) => {
  49. ctx.client.ol_context.privilege_level = 'owner'
  50. ctx.AuthorizationManager.assertClientCanViewProject(
  51. ctx.client,
  52. error => {
  53. expect(error).to.be.null
  54. resolve()
  55. }
  56. )
  57. })
  58. })
  59. it('should return an error with any other privilegeLevel', async ctx => {
  60. await new Promise((resolve, reject) => {
  61. ctx.client.ol_context.privilege_level = 'unknown'
  62. ctx.AuthorizationManager.assertClientCanViewProject(
  63. ctx.client,
  64. error => {
  65. error.message.should.equal('not authorized')
  66. resolve()
  67. }
  68. )
  69. })
  70. })
  71. })
  72. describe('assertClientCanEditProject', () => {
  73. it('should not allow the readOnly privilegeLevel', async ctx => {
  74. await new Promise((resolve, reject) => {
  75. ctx.client.ol_context.privilege_level = 'readOnly'
  76. ctx.AuthorizationManager.assertClientCanEditProject(
  77. ctx.client,
  78. error => {
  79. error.message.should.equal('not authorized')
  80. resolve()
  81. }
  82. )
  83. })
  84. })
  85. it('should allow the readAndWrite privilegeLevel', async ctx => {
  86. await new Promise((resolve, reject) => {
  87. ctx.client.ol_context.privilege_level = 'readAndWrite'
  88. ctx.AuthorizationManager.assertClientCanEditProject(
  89. ctx.client,
  90. error => {
  91. expect(error).to.be.null
  92. resolve()
  93. }
  94. )
  95. })
  96. })
  97. it('should allow the owner privilegeLevel', async ctx => {
  98. await new Promise((resolve, reject) => {
  99. ctx.client.ol_context.privilege_level = 'owner'
  100. ctx.AuthorizationManager.assertClientCanEditProject(
  101. ctx.client,
  102. error => {
  103. expect(error).to.be.null
  104. resolve()
  105. }
  106. )
  107. })
  108. })
  109. it('should return an error with any other privilegeLevel', async ctx => {
  110. await new Promise((resolve, reject) => {
  111. ctx.client.ol_context.privilege_level = 'unknown'
  112. ctx.AuthorizationManager.assertClientCanEditProject(
  113. ctx.client,
  114. error => {
  115. error.message.should.equal('not authorized')
  116. resolve()
  117. }
  118. )
  119. })
  120. })
  121. })
  122. // check doc access for project
  123. describe('assertClientCanViewProjectAndDoc', () => {
  124. beforeEach(ctx => {
  125. ctx.doc_id = '12345'
  126. ctx.callback = sinon.stub()
  127. ctx.client.ol_context = {}
  128. })
  129. describe('when not authorised at the project level', () => {
  130. beforeEach(ctx => {
  131. ctx.client.ol_context.privilege_level = 'unknown'
  132. })
  133. it('should not allow access', ctx => {
  134. ctx.AuthorizationManager.assertClientCanViewProjectAndDoc(
  135. ctx.client,
  136. ctx.doc_id,
  137. err => err.message.should.equal('not authorized')
  138. )
  139. })
  140. describe('even when authorised at the doc level', () => {
  141. beforeEach(async ctx => {
  142. await new Promise((resolve, reject) => {
  143. ctx.AuthorizationManager.addAccessToDoc(
  144. ctx.client,
  145. ctx.doc_id,
  146. err => {
  147. if (err) return reject(err)
  148. resolve()
  149. }
  150. )
  151. })
  152. })
  153. it('should not allow access', ctx => {
  154. ctx.AuthorizationManager.assertClientCanViewProjectAndDoc(
  155. ctx.client,
  156. ctx.doc_id,
  157. err => err.message.should.equal('not authorized')
  158. )
  159. })
  160. })
  161. })
  162. describe('when authorised at the project level', () => {
  163. beforeEach(ctx => {
  164. ctx.client.ol_context.privilege_level = 'readOnly'
  165. })
  166. describe('and not authorised at the document level', () => {
  167. it('should not allow access', ctx => {
  168. ctx.AuthorizationManager.assertClientCanViewProjectAndDoc(
  169. ctx.client,
  170. ctx.doc_id,
  171. err => err.message.should.equal('not authorized')
  172. )
  173. })
  174. })
  175. describe('and authorised at the document level', () => {
  176. beforeEach(async ctx => {
  177. await new Promise((resolve, reject) => {
  178. ctx.AuthorizationManager.addAccessToDoc(
  179. ctx.client,
  180. ctx.doc_id,
  181. err => {
  182. if (err) return reject(err)
  183. resolve()
  184. }
  185. )
  186. })
  187. })
  188. it('should allow access', ctx => {
  189. ctx.AuthorizationManager.assertClientCanViewProjectAndDoc(
  190. ctx.client,
  191. ctx.doc_id,
  192. ctx.callback
  193. )
  194. ctx.callback.calledWith(null).should.equal(true)
  195. })
  196. })
  197. describe('when document authorisation is added and then removed', () => {
  198. beforeEach(async ctx => {
  199. await new Promise((resolve, reject) => {
  200. ctx.AuthorizationManager.addAccessToDoc(
  201. ctx.client,
  202. ctx.doc_id,
  203. () => {
  204. ctx.AuthorizationManager.removeAccessToDoc(
  205. ctx.client,
  206. ctx.doc_id,
  207. err => {
  208. if (err) return reject(err)
  209. resolve()
  210. }
  211. )
  212. }
  213. )
  214. })
  215. })
  216. it('should deny access', ctx => {
  217. ctx.AuthorizationManager.assertClientCanViewProjectAndDoc(
  218. ctx.client,
  219. ctx.doc_id,
  220. err => err.message.should.equal('not authorized')
  221. )
  222. })
  223. })
  224. })
  225. })
  226. describe('assertClientCanEditProjectAndDoc', () => {
  227. beforeEach(ctx => {
  228. ctx.doc_id = '12345'
  229. ctx.callback = sinon.stub()
  230. ctx.client.ol_context = {}
  231. })
  232. describe('when not authorised at the project level', () => {
  233. beforeEach(ctx => {
  234. ctx.client.ol_context.privilege_level = 'readOnly'
  235. })
  236. it('should not allow access', ctx => {
  237. ctx.AuthorizationManager.assertClientCanEditProjectAndDoc(
  238. ctx.client,
  239. ctx.doc_id,
  240. err => err.message.should.equal('not authorized')
  241. )
  242. })
  243. describe('even when authorised at the doc level', () => {
  244. beforeEach(async ctx => {
  245. await new Promise((resolve, reject) => {
  246. ctx.AuthorizationManager.addAccessToDoc(
  247. ctx.client,
  248. ctx.doc_id,
  249. err => {
  250. if (err) return reject(err)
  251. resolve()
  252. }
  253. )
  254. })
  255. })
  256. it('should not allow access', ctx => {
  257. ctx.AuthorizationManager.assertClientCanEditProjectAndDoc(
  258. ctx.client,
  259. ctx.doc_id,
  260. err => err.message.should.equal('not authorized')
  261. )
  262. })
  263. })
  264. })
  265. describe('when authorised at the project level', () => {
  266. beforeEach(ctx => {
  267. ctx.client.ol_context.privilege_level = 'readAndWrite'
  268. })
  269. describe('and not authorised at the document level', () => {
  270. it('should not allow access', ctx => {
  271. ctx.AuthorizationManager.assertClientCanEditProjectAndDoc(
  272. ctx.client,
  273. ctx.doc_id,
  274. err => err.message.should.equal('not authorized')
  275. )
  276. })
  277. })
  278. describe('and authorised at the document level', () => {
  279. beforeEach(async ctx => {
  280. await new Promise((resolve, reject) => {
  281. ctx.AuthorizationManager.addAccessToDoc(
  282. ctx.client,
  283. ctx.doc_id,
  284. err => {
  285. if (err) return reject(err)
  286. resolve()
  287. }
  288. )
  289. })
  290. })
  291. it('should allow access', ctx => {
  292. ctx.AuthorizationManager.assertClientCanEditProjectAndDoc(
  293. ctx.client,
  294. ctx.doc_id,
  295. ctx.callback
  296. )
  297. ctx.callback.calledWith(null).should.equal(true)
  298. })
  299. })
  300. describe('when document authorisation is added and then removed', () => {
  301. beforeEach(async ctx => {
  302. await new Promise((resolve, reject) => {
  303. ctx.AuthorizationManager.addAccessToDoc(
  304. ctx.client,
  305. ctx.doc_id,
  306. () => {
  307. ctx.AuthorizationManager.removeAccessToDoc(
  308. ctx.client,
  309. ctx.doc_id,
  310. err => {
  311. if (err) return reject(err)
  312. resolve()
  313. }
  314. )
  315. }
  316. )
  317. })
  318. })
  319. it('should deny access', ctx => {
  320. ctx.AuthorizationManager.assertClientCanEditProjectAndDoc(
  321. ctx.client,
  322. ctx.doc_id,
  323. err => err.message.should.equal('not authorized')
  324. )
  325. })
  326. })
  327. })
  328. })
  329. describe('assertClientCanReviewProjectAndDoc', () => {
  330. beforeEach(ctx => {
  331. ctx.doc_id = '12345'
  332. ctx.callback = sinon.stub()
  333. ctx.client.ol_context = {}
  334. })
  335. describe('when not authorised at the project level', () => {
  336. beforeEach(ctx => {
  337. ctx.client.ol_context.privilege_level = 'readOnly'
  338. })
  339. it('should not allow access', ctx => {
  340. ctx.AuthorizationManager.assertClientCanReviewProjectAndDoc(
  341. ctx.client,
  342. ctx.doc_id,
  343. err => err.message.should.equal('not authorized')
  344. )
  345. })
  346. describe('even when authorised at the doc level', () => {
  347. beforeEach(async ctx => {
  348. await new Promise((resolve, reject) => {
  349. ctx.AuthorizationManager.addAccessToDoc(
  350. ctx.client,
  351. ctx.doc_id,
  352. err => {
  353. if (err) return reject(err)
  354. resolve()
  355. }
  356. )
  357. })
  358. })
  359. it('should not allow access', ctx => {
  360. ctx.AuthorizationManager.assertClientCanReviewProjectAndDoc(
  361. ctx.client,
  362. ctx.doc_id,
  363. err => err.message.should.equal('not authorized')
  364. )
  365. })
  366. })
  367. })
  368. describe('when authorised at the project level', () => {
  369. beforeEach(ctx => {
  370. ctx.client.ol_context.privilege_level = 'review'
  371. })
  372. describe('and not authorised at the document level', () => {
  373. it('should not allow access', ctx => {
  374. ctx.AuthorizationManager.assertClientCanReviewProjectAndDoc(
  375. ctx.client,
  376. ctx.doc_id,
  377. err => err.message.should.equal('not authorized')
  378. )
  379. })
  380. })
  381. describe('and authorised at the document level', () => {
  382. beforeEach(async ctx => {
  383. await new Promise((resolve, reject) => {
  384. ctx.AuthorizationManager.addAccessToDoc(
  385. ctx.client,
  386. ctx.doc_id,
  387. err => {
  388. if (err) return reject(err)
  389. resolve()
  390. }
  391. )
  392. })
  393. })
  394. it('should allow access', ctx => {
  395. ctx.AuthorizationManager.assertClientCanReviewProjectAndDoc(
  396. ctx.client,
  397. ctx.doc_id,
  398. ctx.callback
  399. )
  400. ctx.callback.calledWith(null).should.equal(true)
  401. })
  402. })
  403. describe('when document authorisation is added and then removed', () => {
  404. beforeEach(async ctx => {
  405. await new Promise((resolve, reject) => {
  406. ctx.AuthorizationManager.addAccessToDoc(
  407. ctx.client,
  408. ctx.doc_id,
  409. () => {
  410. ctx.AuthorizationManager.removeAccessToDoc(
  411. ctx.client,
  412. ctx.doc_id,
  413. err => {
  414. if (err) return reject(err)
  415. resolve()
  416. }
  417. )
  418. }
  419. )
  420. })
  421. })
  422. it('should deny access', ctx => {
  423. ctx.AuthorizationManager.assertClientCanReviewProjectAndDoc(
  424. ctx.client,
  425. ctx.doc_id,
  426. err => err.message.should.equal('not authorized')
  427. )
  428. })
  429. })
  430. })
  431. })
  432. })