AuthorizationManager.test.mjs 28 KB

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