AuthorizationManagerTests.js 13 KB

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