AuthorizationManagerTests.js 27 KB

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