AuthorizationManagerTests.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath =
  4. '../../../../app/src/Features/Authorization/AuthorizationManager.js'
  5. const SandboxedModule = require('sandboxed-module')
  6. const Errors = require('../../../../app/src/Features/Errors/Errors')
  7. const PrivilegeLevels = require('../../../../app/src/Features/Authorization/PrivilegeLevels')
  8. const PublicAccessLevels = require('../../../../app/src/Features/Authorization/PublicAccessLevels')
  9. const { ObjectId } = require('mongodb-legacy')
  10. describe('AuthorizationManager', function () {
  11. beforeEach(function () {
  12. this.user = { _id: new ObjectId() }
  13. this.project = { _id: new ObjectId() }
  14. this.doc = { _id: new ObjectId() }
  15. this.thread = { _id: new ObjectId() }
  16. this.token = 'some-token'
  17. this.ProjectGetter = {
  18. promises: {
  19. getProject: sinon.stub().resolves(null),
  20. },
  21. }
  22. this.ProjectGetter.promises.getProject
  23. .withArgs(this.project._id)
  24. .resolves(this.project)
  25. this.CollaboratorsGetter = {
  26. promises: {
  27. getProjectAccess: sinon.stub().resolves({
  28. publicAccessLevel: sinon.stub().returns(PublicAccessLevels.PRIVATE),
  29. privilegeLevelForUser: sinon.stub().returns(PrivilegeLevels.NONE),
  30. }),
  31. },
  32. }
  33. this.CollaboratorsHandler = {}
  34. this.User = {
  35. findOne: sinon.stub().returns({ exec: sinon.stub().resolves(null) }),
  36. }
  37. this.User.findOne
  38. .withArgs({ _id: this.user._id })
  39. .returns({ exec: sinon.stub().resolves(this.user) })
  40. this.TokenAccessHandler = {
  41. promises: {
  42. validateTokenForAnonymousAccess: sinon
  43. .stub()
  44. .resolves({ isValidReadAndWrite: false, isValidReadOnly: false }),
  45. },
  46. }
  47. this.DocumentUpdaterHandler = {
  48. promises: {
  49. getComment: sinon
  50. .stub()
  51. .resolves({ metadata: { user_id: new ObjectId() } }),
  52. },
  53. }
  54. this.AuthorizationManager = SandboxedModule.require(modulePath, {
  55. requires: {
  56. 'mongodb-legacy': { ObjectId },
  57. '../Collaborators/CollaboratorsGetter': this.CollaboratorsGetter,
  58. '../Collaborators/CollaboratorsHandler': this.CollaboratorsHandler,
  59. '../Project/ProjectGetter': this.ProjectGetter,
  60. '../../models/User': { User: this.User },
  61. '../TokenAccess/TokenAccessHandler': this.TokenAccessHandler,
  62. '../DocumentUpdater/DocumentUpdaterHandler':
  63. this.DocumentUpdaterHandler,
  64. '@overleaf/settings': {
  65. passwordStrengthOptions: {},
  66. adminPrivilegeAvailable: true,
  67. },
  68. },
  69. })
  70. })
  71. describe('isRestrictedUser', function () {
  72. it('should produce the correct values', function () {
  73. const notRestrictedScenarios = [
  74. [null, 'readAndWrite', false, false],
  75. ['id', 'readAndWrite', true, false],
  76. ['id', 'readAndWrite', true, true],
  77. ['id', 'readOnly', false, false],
  78. ['id', 'readOnly', false, true],
  79. ['id', 'review', false, true],
  80. ]
  81. const restrictedScenarios = [
  82. [null, 'readOnly', false, false],
  83. ['id', 'readOnly', true, false],
  84. [null, false, true, false],
  85. [null, false, false, false],
  86. ['id', false, true, false],
  87. ['id', false, false, false],
  88. ]
  89. for (const notRestrictedArgs of notRestrictedScenarios) {
  90. expect(
  91. this.AuthorizationManager.isRestrictedUser(...notRestrictedArgs)
  92. ).to.equal(false)
  93. }
  94. for (const restrictedArgs of restrictedScenarios) {
  95. expect(
  96. this.AuthorizationManager.isRestrictedUser(...restrictedArgs)
  97. ).to.equal(true)
  98. }
  99. })
  100. })
  101. describe('getPrivilegeLevelForProject', function () {
  102. describe('with a token-based project', function () {
  103. beforeEach(function () {
  104. this.project.publicAccesLevel = 'tokenBased'
  105. })
  106. describe('with a user id with a privilege level', function () {
  107. beforeEach(async function () {
  108. this.CollaboratorsGetter.promises.getProjectAccess
  109. .withArgs(this.project._id)
  110. .resolves({
  111. publicAccessLevel: sinon
  112. .stub()
  113. .returns(PublicAccessLevels.PRIVATE),
  114. privilegeLevelForUser: sinon
  115. .stub()
  116. .withArgs(this.user._id)
  117. .returns(PrivilegeLevels.READ_ONLY),
  118. })
  119. this.result =
  120. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  121. this.user._id,
  122. this.project._id,
  123. this.token
  124. )
  125. })
  126. it("should return the user's privilege level", function () {
  127. expect(this.result).to.equal('readOnly')
  128. })
  129. })
  130. describe('with a user id with no privilege level', function () {
  131. beforeEach(async function () {
  132. this.result =
  133. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  134. this.user._id,
  135. this.project._id,
  136. this.token
  137. )
  138. })
  139. it('should return false', function () {
  140. expect(this.result).to.equal(false)
  141. })
  142. })
  143. describe('with a user id who is an admin', function () {
  144. beforeEach(async function () {
  145. this.user.isAdmin = true
  146. this.result =
  147. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  148. this.user._id,
  149. this.project._id,
  150. this.token
  151. )
  152. })
  153. it('should return the user as an owner', function () {
  154. expect(this.result).to.equal('owner')
  155. })
  156. })
  157. describe('with no user (anonymous)', function () {
  158. describe('when the token is not valid', function () {
  159. beforeEach(async function () {
  160. this.result =
  161. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  162. null,
  163. this.project._id,
  164. this.token
  165. )
  166. })
  167. it('should not call CollaboratorsGetter.getProjectAccess', function () {
  168. this.CollaboratorsGetter.promises.getProjectAccess.called.should.equal(
  169. false
  170. )
  171. })
  172. it('should check if the token is valid', function () {
  173. this.TokenAccessHandler.promises.validateTokenForAnonymousAccess.should.have.been.calledWith(
  174. this.project._id,
  175. this.token
  176. )
  177. })
  178. it('should return false', function () {
  179. expect(this.result).to.equal(false)
  180. })
  181. })
  182. describe('when the token is valid for read-and-write', function () {
  183. beforeEach(async function () {
  184. this.TokenAccessHandler.promises.validateTokenForAnonymousAccess =
  185. sinon
  186. .stub()
  187. .withArgs(this.project._id, this.token)
  188. .resolves({ isValidReadAndWrite: true, isValidReadOnly: false })
  189. this.result =
  190. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  191. null,
  192. this.project._id,
  193. this.token
  194. )
  195. })
  196. it('should not call CollaboratorsGetter.getProjectAccess', function () {
  197. this.CollaboratorsGetter.promises.getProjectAccess.called.should.equal(
  198. false
  199. )
  200. })
  201. it('should check if the token is valid', function () {
  202. this.TokenAccessHandler.promises.validateTokenForAnonymousAccess.should.have.been.calledWith(
  203. this.project._id,
  204. this.token
  205. )
  206. })
  207. it('should give read-write access', function () {
  208. expect(this.result).to.equal('readAndWrite')
  209. })
  210. })
  211. describe('when the token is valid for read-only', function () {
  212. beforeEach(async function () {
  213. this.TokenAccessHandler.promises.validateTokenForAnonymousAccess =
  214. sinon
  215. .stub()
  216. .withArgs(this.project._id, this.token)
  217. .resolves({ isValidReadAndWrite: false, isValidReadOnly: true })
  218. this.result =
  219. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  220. null,
  221. this.project._id,
  222. this.token
  223. )
  224. })
  225. it('should not call CollaboratorsGetter.getProjectAccess', function () {
  226. this.CollaboratorsGetter.promises.getProjectAccess.called.should.equal(
  227. false
  228. )
  229. })
  230. it('should check if the token is valid', function () {
  231. this.TokenAccessHandler.promises.validateTokenForAnonymousAccess.should.have.been.calledWith(
  232. this.project._id,
  233. this.token
  234. )
  235. })
  236. it('should give read-only access', function () {
  237. expect(this.result).to.equal('readOnly')
  238. })
  239. })
  240. })
  241. })
  242. describe('with a private project', function () {
  243. beforeEach(function () {
  244. this.project.publicAccesLevel = 'private'
  245. })
  246. describe('with a user id with a privilege level', function () {
  247. beforeEach(async function () {
  248. this.CollaboratorsGetter.promises.getProjectAccess
  249. .withArgs(this.project._id)
  250. .resolves({
  251. publicAccessLevel: sinon
  252. .stub()
  253. .returns(PublicAccessLevels.PRIVATE),
  254. privilegeLevelForUser: sinon
  255. .stub()
  256. .withArgs(this.user._id)
  257. .returns(PrivilegeLevels.READ_ONLY),
  258. })
  259. this.result =
  260. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  261. this.user._id,
  262. this.project._id,
  263. this.token
  264. )
  265. })
  266. it("should return the user's privilege level", function () {
  267. expect(this.result).to.equal('readOnly')
  268. })
  269. })
  270. describe('with a user id with no privilege level', function () {
  271. beforeEach(async function () {
  272. this.result =
  273. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  274. this.user._id,
  275. this.project._id,
  276. this.token
  277. )
  278. })
  279. it('should return false', function () {
  280. expect(this.result).to.equal(false)
  281. })
  282. })
  283. describe('with a user id who is an admin', function () {
  284. beforeEach(async function () {
  285. this.user.isAdmin = true
  286. this.result =
  287. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  288. this.user._id,
  289. this.project._id,
  290. this.token
  291. )
  292. })
  293. it('should return the user as an owner', function () {
  294. expect(this.result).to.equal('owner')
  295. })
  296. })
  297. describe('with no user (anonymous)', function () {
  298. beforeEach(async function () {
  299. this.result =
  300. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  301. null,
  302. this.project._id,
  303. this.token
  304. )
  305. })
  306. it('should not call CollaboratorsGetter.getProjectAccess', function () {
  307. this.CollaboratorsGetter.promises.getProjectAccess.called.should.equal(
  308. false
  309. )
  310. })
  311. it('should return false', function () {
  312. expect(this.result).to.equal(false)
  313. })
  314. })
  315. })
  316. describe('with a public project', function () {
  317. beforeEach(function () {
  318. this.project.publicAccesLevel = 'readAndWrite'
  319. this.CollaboratorsGetter.promises.getProjectAccess
  320. .withArgs(this.project._id)
  321. .resolves({
  322. publicAccessLevel: sinon
  323. .stub()
  324. .returns(this.project.publicAccesLevel),
  325. privilegeLevelForUser: sinon
  326. .stub()
  327. .withArgs(this.user._id)
  328. .returns(PrivilegeLevels.NONE),
  329. })
  330. })
  331. describe('with a user id with a privilege level', function () {
  332. beforeEach(async function () {
  333. this.CollaboratorsGetter.promises.getProjectAccess
  334. .withArgs(this.project._id)
  335. .resolves({
  336. publicAccessLevel: sinon
  337. .stub()
  338. .returns(this.project.publicAccesLevel),
  339. privilegeLevelForUser: sinon
  340. .stub()
  341. .withArgs(this.user._id)
  342. .returns(PrivilegeLevels.READ_ONLY),
  343. })
  344. this.result =
  345. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  346. this.user._id,
  347. this.project._id,
  348. this.token
  349. )
  350. })
  351. it("should return the user's privilege level", function () {
  352. expect(this.result).to.equal('readOnly')
  353. })
  354. })
  355. describe('with a user id with no privilege level', function () {
  356. beforeEach(async function () {
  357. this.result =
  358. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  359. this.user._id,
  360. this.project._id,
  361. this.token
  362. )
  363. })
  364. it('should return the public privilege level', function () {
  365. expect(this.result).to.equal('readAndWrite')
  366. })
  367. })
  368. describe('with a user id who is an admin', function () {
  369. beforeEach(async function () {
  370. this.user.isAdmin = true
  371. this.result =
  372. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  373. this.user._id,
  374. this.project._id,
  375. this.token
  376. )
  377. })
  378. it('should return the user as an owner', function () {
  379. expect(this.result).to.equal('owner')
  380. })
  381. })
  382. describe('with no user (anonymous)', function () {
  383. beforeEach(async function () {
  384. this.result =
  385. await this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  386. null,
  387. this.project._id,
  388. this.token
  389. )
  390. })
  391. it('should not call CollaboratorsGetter.getProjectAccess', function () {
  392. this.CollaboratorsGetter.promises.getProjectAccess.called.should.equal(
  393. false
  394. )
  395. })
  396. it('should return the public privilege level', function () {
  397. expect(this.result).to.equal('readAndWrite')
  398. })
  399. })
  400. })
  401. describe("when the project doesn't exist", function () {
  402. beforeEach(function () {
  403. this.CollaboratorsGetter.promises.getProjectAccess.rejects(
  404. new Errors.NotFoundError()
  405. )
  406. })
  407. it('should return a NotFoundError', async function () {
  408. const someOtherId = new ObjectId()
  409. await expect(
  410. this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  411. this.user._id,
  412. someOtherId,
  413. this.token
  414. )
  415. ).to.be.rejectedWith(Errors.NotFoundError)
  416. })
  417. })
  418. describe('when the project id is not valid', function () {
  419. beforeEach(function () {
  420. this.CollaboratorsGetter.promises.getProjectAccess
  421. .withArgs(this.project._id)
  422. .resolves({
  423. publicAccessLevel: sinon.stub().returns(PublicAccessLevels.PRIVATE),
  424. privilegeLevelForUser: sinon
  425. .stub()
  426. .withArgs(this.user._id)
  427. .returns(PrivilegeLevels.READ_ONLY),
  428. })
  429. })
  430. it('should return a error', async function () {
  431. await expect(
  432. this.AuthorizationManager.promises.getPrivilegeLevelForProject(
  433. undefined,
  434. 'not project id',
  435. this.token
  436. )
  437. ).to.be.rejected
  438. })
  439. })
  440. })
  441. testPermission('canUserReadProject', {
  442. siteAdmin: true,
  443. owner: true,
  444. readAndWrite: true,
  445. review: true,
  446. readOnly: true,
  447. publicReadAndWrite: true,
  448. publicReadOnly: true,
  449. tokenReadAndWrite: true,
  450. tokenReadOnly: true,
  451. })
  452. testPermission('canUserWriteOrReviewProjectContent', {
  453. siteAdmin: true,
  454. owner: true,
  455. readAndWrite: true,
  456. review: true,
  457. publicReadAndWrite: true,
  458. tokenReadAndWrite: true,
  459. })
  460. testPermission('canUserWriteProjectContent', {
  461. siteAdmin: true,
  462. owner: true,
  463. readAndWrite: true,
  464. publicReadAndWrite: true,
  465. tokenReadAndWrite: true,
  466. })
  467. testPermission('canUserWriteProjectSettings', {
  468. siteAdmin: true,
  469. owner: true,
  470. readAndWrite: true,
  471. tokenReadAndWrite: true,
  472. })
  473. testPermission('canUserRenameProject', {
  474. siteAdmin: true,
  475. owner: true,
  476. })
  477. testPermission('canUserAdminProject', { siteAdmin: true, owner: true })
  478. describe('isUserSiteAdmin', function () {
  479. describe('when user is admin', function () {
  480. beforeEach(function () {
  481. this.user.isAdmin = true
  482. })
  483. it('should return true', async function () {
  484. const isAdmin =
  485. await this.AuthorizationManager.promises.isUserSiteAdmin(
  486. this.user._id
  487. )
  488. expect(isAdmin).to.equal(true)
  489. })
  490. })
  491. describe('when user is not admin', function () {
  492. it('should return false', async function () {
  493. const isAdmin =
  494. await this.AuthorizationManager.promises.isUserSiteAdmin(
  495. this.user._id
  496. )
  497. expect(isAdmin).to.equal(false)
  498. })
  499. })
  500. describe('when user is not found', function () {
  501. it('should return false', async function () {
  502. const someOtherId = new ObjectId()
  503. const isAdmin =
  504. await this.AuthorizationManager.promises.isUserSiteAdmin(someOtherId)
  505. expect(isAdmin).to.equal(false)
  506. })
  507. })
  508. describe('when no user is passed', function () {
  509. it('should return false', async function () {
  510. const isAdmin =
  511. await this.AuthorizationManager.promises.isUserSiteAdmin(null)
  512. expect(isAdmin).to.equal(false)
  513. })
  514. })
  515. })
  516. describe('canUserDeleteOrResolveThread', function () {
  517. it('should return true when user has write permissions', async function () {
  518. this.CollaboratorsGetter.promises.getProjectAccess
  519. .withArgs(this.project._id)
  520. .resolves({
  521. publicAccessLevel: sinon.stub().returns(PublicAccessLevels.PRIVATE),
  522. privilegeLevelForUser: sinon
  523. .stub()
  524. .withArgs(this.user._id)
  525. .returns(PrivilegeLevels.READ_AND_WRITE),
  526. })
  527. const canResolve =
  528. await this.AuthorizationManager.promises.canUserDeleteOrResolveThread(
  529. this.user._id,
  530. this.project._id,
  531. this.doc._id,
  532. this.thread._id,
  533. this.token
  534. )
  535. expect(canResolve).to.equal(true)
  536. })
  537. it('should return false when user has read permission', async function () {
  538. this.CollaboratorsGetter.promises.getProjectAccess
  539. .withArgs(this.project._id)
  540. .resolves({
  541. publicAccessLevel: sinon.stub().returns(PublicAccessLevels.PRIVATE),
  542. privilegeLevelForUser: sinon
  543. .stub()
  544. .withArgs(this.user._id)
  545. .returns(PrivilegeLevels.READ_ONLY),
  546. })
  547. const canResolve =
  548. await this.AuthorizationManager.promises.canUserDeleteOrResolveThread(
  549. this.user._id,
  550. this.project._id,
  551. this.doc._id,
  552. this.thread._id,
  553. this.token
  554. )
  555. expect(canResolve).to.equal(false)
  556. })
  557. describe('when user has review permission', function () {
  558. beforeEach(function () {
  559. this.CollaboratorsGetter.promises.getProjectAccess
  560. .withArgs(this.project._id)
  561. .resolves({
  562. publicAccessLevel: sinon.stub().returns(PublicAccessLevels.PRIVATE),
  563. privilegeLevelForUser: sinon
  564. .stub()
  565. .withArgs(this.user._id)
  566. .returns(PrivilegeLevels.REVIEW),
  567. })
  568. })
  569. it('should return false when user is not the comment author', async function () {
  570. const canResolve =
  571. await this.AuthorizationManager.promises.canUserDeleteOrResolveThread(
  572. this.user._id,
  573. this.project._id,
  574. this.doc._id,
  575. this.thread._id,
  576. this.token
  577. )
  578. expect(canResolve).to.equal(false)
  579. })
  580. it('should return true when user is the comment author', async function () {
  581. this.DocumentUpdaterHandler.promises.getComment
  582. .withArgs(this.project._id, this.doc._id, this.thread._id)
  583. .resolves({ metadata: { user_id: this.user._id } })
  584. const canResolve =
  585. await this.AuthorizationManager.promises.canUserDeleteOrResolveThread(
  586. this.user._id,
  587. this.project._id,
  588. this.doc._id,
  589. this.thread._id,
  590. this.token
  591. )
  592. expect(canResolve).to.equal(true)
  593. })
  594. })
  595. })
  596. })
  597. function testPermission(permission, privilegeLevels) {
  598. describe(permission, function () {
  599. describe('when authenticated', function () {
  600. describe('when user is site admin', function () {
  601. beforeEach('set user as site admin', function () {
  602. this.user.isAdmin = true
  603. })
  604. expectPermission(permission, privilegeLevels.siteAdmin || false)
  605. })
  606. describe('when user is owner', function () {
  607. setupUserPrivilegeLevel(PrivilegeLevels.OWNER)
  608. expectPermission(permission, privilegeLevels.owner || false)
  609. })
  610. describe('when user has read-write access', function () {
  611. setupUserPrivilegeLevel(PrivilegeLevels.READ_AND_WRITE)
  612. expectPermission(permission, privilegeLevels.readAndWrite || false)
  613. })
  614. describe('when user has review access', function () {
  615. setupUserPrivilegeLevel(PrivilegeLevels.REVIEW)
  616. expectPermission(permission, privilegeLevels.review || false)
  617. })
  618. describe('when user has read-only access', function () {
  619. setupUserPrivilegeLevel(PrivilegeLevels.READ_ONLY)
  620. expectPermission(permission, privilegeLevels.readOnly || false)
  621. })
  622. describe('when user has read-write access as the public', function () {
  623. setupPublicAccessLevel(PublicAccessLevels.READ_AND_WRITE)
  624. expectPermission(
  625. permission,
  626. privilegeLevels.publicReadAndWrite || false
  627. )
  628. })
  629. describe('when user has read-only access as the public', function () {
  630. setupPublicAccessLevel(PublicAccessLevels.READ_ONLY)
  631. expectPermission(permission, privilegeLevels.publicReadOnly || false)
  632. })
  633. describe('when user is not found', function () {
  634. it('should return false', async function () {
  635. const otherUserId = new ObjectId()
  636. const value = await this.AuthorizationManager.promises[permission](
  637. otherUserId,
  638. this.project._id,
  639. this.token
  640. )
  641. expect(value).to.equal(false)
  642. })
  643. })
  644. })
  645. describe('when anonymous', function () {
  646. beforeEach(function () {
  647. this.user = null
  648. })
  649. describe('with read-write access through a token', function () {
  650. setupTokenAccessLevel('readAndWrite')
  651. expectPermission(permission, privilegeLevels.tokenReadAndWrite || false)
  652. })
  653. describe('with read-only access through a token', function () {
  654. setupTokenAccessLevel('readOnly')
  655. expectPermission(permission, privilegeLevels.tokenReadOnly || false)
  656. })
  657. describe('with public read-write access', function () {
  658. setupPublicAccessLevel(PublicAccessLevels.READ_AND_WRITE)
  659. expectPermission(
  660. permission,
  661. privilegeLevels.publicReadAndWrite || false
  662. )
  663. })
  664. describe('with public read-only access', function () {
  665. setupPublicAccessLevel(PublicAccessLevels.READ_ONLY)
  666. expectPermission(permission, privilegeLevels.publicReadOnly || false)
  667. })
  668. })
  669. })
  670. }
  671. function setupUserPrivilegeLevel(privilegeLevel) {
  672. beforeEach(`set user privilege level to ${privilegeLevel}`, function () {
  673. this.CollaboratorsGetter.promises.getProjectAccess
  674. .withArgs(this.project._id)
  675. .resolves({
  676. publicAccessLevel: sinon.stub().returns(PublicAccessLevels.PRIVATE),
  677. privilegeLevelForUser: sinon
  678. .stub()
  679. .withArgs(this.user._id)
  680. .returns(privilegeLevel),
  681. })
  682. })
  683. }
  684. function setupPublicAccessLevel(level) {
  685. beforeEach(`set public access level to ${level}`, function () {
  686. this.project.publicAccesLevel = level
  687. this.CollaboratorsGetter.promises.getProjectAccess
  688. .withArgs(this.project._id)
  689. .resolves({
  690. publicAccessLevel: sinon.stub().returns(this.project.publicAccesLevel),
  691. privilegeLevelForUser: sinon.stub().returns(PrivilegeLevels.NONE),
  692. })
  693. })
  694. }
  695. function setupTokenAccessLevel(level) {
  696. beforeEach(`set token access level to ${level}`, function () {
  697. this.project.publicAccesLevel = PublicAccessLevels.TOKEN_BASED
  698. this.TokenAccessHandler.promises.validateTokenForAnonymousAccess
  699. .withArgs(this.project._id, this.token)
  700. .resolves({
  701. isValidReadAndWrite: level === 'readAndWrite',
  702. isValidReadOnly: level === 'readOnly',
  703. })
  704. })
  705. }
  706. function expectPermission(permission, expectedValue) {
  707. it(`should return ${expectedValue}`, async function () {
  708. const value = await this.AuthorizationManager.promises[permission](
  709. this.user && this.user._id,
  710. this.project._id,
  711. this.token
  712. )
  713. expect(value).to.equal(expectedValue)
  714. })
  715. }