AuthorizationManagerTests.js 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968
  1. /* eslint-disable
  2. node/handle-callback-err,
  3. max-len,
  4. no-return-assign,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. const sinon = require('sinon')
  15. const { expect } = require('chai')
  16. const modulePath =
  17. '../../../../app/src/Features/Authorization/AuthorizationManager.js'
  18. const SandboxedModule = require('sandboxed-module')
  19. const Errors = require('../../../../app/src/Features/Errors/Errors.js')
  20. const { ObjectId } = require('mongodb')
  21. describe('AuthorizationManager', function () {
  22. beforeEach(function () {
  23. this.AuthorizationManager = SandboxedModule.require(modulePath, {
  24. requires: {
  25. mongodb: { ObjectId },
  26. '../Collaborators/CollaboratorsGetter': (this.CollaboratorsGetter = {}),
  27. '../Collaborators/CollaboratorsHandler': (this.CollaboratorsHandler = {}),
  28. '../Project/ProjectGetter': (this.ProjectGetter = {}),
  29. '../../models/User': {
  30. User: (this.User = {}),
  31. },
  32. '../TokenAccess/TokenAccessHandler': (this.TokenAccessHandler = {
  33. validateTokenForAnonymousAccess: sinon
  34. .stub()
  35. .callsArgWith(2, null, false, false),
  36. }),
  37. '@overleaf/settings': { passwordStrengthOptions: {} },
  38. },
  39. })
  40. this.user_id = 'user-id-1'
  41. this.project_id = 'project-id-1'
  42. this.token = 'some-token'
  43. return (this.callback = sinon.stub())
  44. })
  45. describe('isRestrictedUser', function () {
  46. it('should produce the correct values', function () {
  47. const notRestrictedScenarios = [
  48. [null, 'readAndWrite', false],
  49. ['id', 'readAndWrite', true],
  50. ['id', 'readOnly', false],
  51. ]
  52. const restrictedScenarios = [
  53. [null, 'readOnly', false],
  54. ['id', 'readOnly', true],
  55. [null, false, true],
  56. [null, false, false],
  57. ['id', false, true],
  58. ['id', false, false],
  59. ]
  60. for (var notRestrictedArgs of notRestrictedScenarios) {
  61. expect(
  62. this.AuthorizationManager.isRestrictedUser(...notRestrictedArgs)
  63. ).to.equal(false)
  64. }
  65. for (var restrictedArgs of restrictedScenarios) {
  66. expect(
  67. this.AuthorizationManager.isRestrictedUser(...restrictedArgs)
  68. ).to.equal(true)
  69. }
  70. })
  71. })
  72. describe('getPrivilegeLevelForProject', function () {
  73. beforeEach(function () {
  74. this.ProjectGetter.getProject = sinon.stub()
  75. this.AuthorizationManager.isUserSiteAdmin = sinon.stub()
  76. return (this.CollaboratorsGetter.getMemberIdPrivilegeLevel = sinon.stub())
  77. })
  78. describe('with a token-based project', function () {
  79. beforeEach(function () {
  80. return this.ProjectGetter.getProject
  81. .withArgs(this.project_id, { publicAccesLevel: 1 })
  82. .yields(null, { publicAccesLevel: 'tokenBased' })
  83. })
  84. describe('with a user_id with a privilege level', function () {
  85. beforeEach(function () {
  86. this.AuthorizationManager.isUserSiteAdmin
  87. .withArgs(this.user_id)
  88. .yields(null, false)
  89. this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  90. .withArgs(this.user_id, this.project_id)
  91. .yields(null, 'readOnly')
  92. return this.AuthorizationManager.getPrivilegeLevelForProject(
  93. this.user_id,
  94. this.project_id,
  95. this.token,
  96. this.callback
  97. )
  98. })
  99. it("should return the user's privilege level", function () {
  100. return this.callback
  101. .calledWith(null, 'readOnly', false, false)
  102. .should.equal(true)
  103. })
  104. })
  105. describe('with a user_id with no privilege level', function () {
  106. beforeEach(function () {
  107. this.AuthorizationManager.isUserSiteAdmin
  108. .withArgs(this.user_id)
  109. .yields(null, false)
  110. this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  111. .withArgs(this.user_id, this.project_id)
  112. .yields(null, false)
  113. return this.AuthorizationManager.getPrivilegeLevelForProject(
  114. this.user_id,
  115. this.project_id,
  116. this.token,
  117. this.callback
  118. )
  119. })
  120. it('should return false', function () {
  121. return this.callback
  122. .calledWith(null, false, false, false)
  123. .should.equal(true)
  124. })
  125. })
  126. describe('with a user_id who is an admin', function () {
  127. beforeEach(function () {
  128. this.AuthorizationManager.isUserSiteAdmin
  129. .withArgs(this.user_id)
  130. .yields(null, true)
  131. this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  132. .withArgs(this.user_id, this.project_id)
  133. .yields(null, false)
  134. return this.AuthorizationManager.getPrivilegeLevelForProject(
  135. this.user_id,
  136. this.project_id,
  137. this.token,
  138. this.callback
  139. )
  140. })
  141. it('should return the user as an owner', function () {
  142. return this.callback
  143. .calledWith(null, 'owner', false, true)
  144. .should.equal(true)
  145. })
  146. })
  147. describe('with no user (anonymous)', function () {
  148. describe('when the token is not valid', function () {
  149. beforeEach(function () {
  150. this.TokenAccessHandler.validateTokenForAnonymousAccess = sinon
  151. .stub()
  152. .withArgs(this.project_id, this.token)
  153. .yields(null, false, false)
  154. return this.AuthorizationManager.getPrivilegeLevelForProject(
  155. null,
  156. this.project_id,
  157. this.token,
  158. this.callback
  159. )
  160. })
  161. it('should not call CollaboratorsGetter.getMemberIdPrivilegeLevel', function () {
  162. return this.CollaboratorsGetter.getMemberIdPrivilegeLevel.called.should.equal(
  163. false
  164. )
  165. })
  166. it('should not call AuthorizationManager.isUserSiteAdmin', function () {
  167. return this.AuthorizationManager.isUserSiteAdmin.called.should.equal(
  168. false
  169. )
  170. })
  171. it('should check if the token is valid', function () {
  172. return this.TokenAccessHandler.validateTokenForAnonymousAccess
  173. .calledWith(this.project_id, this.token)
  174. .should.equal(true)
  175. })
  176. it('should return false', function () {
  177. return this.callback
  178. .calledWith(null, false, false, false)
  179. .should.equal(true)
  180. })
  181. })
  182. describe('when the token is valid for read-and-write', function () {
  183. beforeEach(function () {
  184. this.TokenAccessHandler.validateTokenForAnonymousAccess = sinon
  185. .stub()
  186. .withArgs(this.project_id, this.token)
  187. .yields(null, true, false)
  188. return this.AuthorizationManager.getPrivilegeLevelForProject(
  189. null,
  190. this.project_id,
  191. this.token,
  192. this.callback
  193. )
  194. })
  195. it('should not call CollaboratorsGetter.getMemberIdPrivilegeLevel', function () {
  196. return this.CollaboratorsGetter.getMemberIdPrivilegeLevel.called.should.equal(
  197. false
  198. )
  199. })
  200. it('should not call AuthorizationManager.isUserSiteAdmin', function () {
  201. return this.AuthorizationManager.isUserSiteAdmin.called.should.equal(
  202. false
  203. )
  204. })
  205. it('should check if the token is valid', function () {
  206. return this.TokenAccessHandler.validateTokenForAnonymousAccess
  207. .calledWith(this.project_id, this.token)
  208. .should.equal(true)
  209. })
  210. it('should give read-write access', function () {
  211. return this.callback
  212. .calledWith(null, 'readAndWrite', false)
  213. .should.equal(true)
  214. })
  215. })
  216. describe('when the token is valid for read-only', function () {
  217. beforeEach(function () {
  218. this.TokenAccessHandler.validateTokenForAnonymousAccess = sinon
  219. .stub()
  220. .withArgs(this.project_id, this.token)
  221. .yields(null, false, true)
  222. return this.AuthorizationManager.getPrivilegeLevelForProject(
  223. null,
  224. this.project_id,
  225. this.token,
  226. this.callback
  227. )
  228. })
  229. it('should not call CollaboratorsGetter.getMemberIdPrivilegeLevel', function () {
  230. return this.CollaboratorsGetter.getMemberIdPrivilegeLevel.called.should.equal(
  231. false
  232. )
  233. })
  234. it('should not call AuthorizationManager.isUserSiteAdmin', function () {
  235. return this.AuthorizationManager.isUserSiteAdmin.called.should.equal(
  236. false
  237. )
  238. })
  239. it('should check if the token is valid', function () {
  240. return this.TokenAccessHandler.validateTokenForAnonymousAccess
  241. .calledWith(this.project_id, this.token)
  242. .should.equal(true)
  243. })
  244. it('should give read-only access', function () {
  245. return this.callback
  246. .calledWith(null, 'readOnly', false)
  247. .should.equal(true)
  248. })
  249. })
  250. })
  251. })
  252. describe('with a private project', function () {
  253. beforeEach(function () {
  254. return this.ProjectGetter.getProject
  255. .withArgs(this.project_id, { publicAccesLevel: 1 })
  256. .yields(null, { publicAccesLevel: 'private' })
  257. })
  258. describe('with a user_id with a privilege level', function () {
  259. beforeEach(function () {
  260. this.AuthorizationManager.isUserSiteAdmin
  261. .withArgs(this.user_id)
  262. .yields(null, false)
  263. this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  264. .withArgs(this.user_id, this.project_id)
  265. .yields(null, 'readOnly')
  266. return this.AuthorizationManager.getPrivilegeLevelForProject(
  267. this.user_id,
  268. this.project_id,
  269. this.token,
  270. this.callback
  271. )
  272. })
  273. it("should return the user's privilege level", function () {
  274. return this.callback
  275. .calledWith(null, 'readOnly', false, false)
  276. .should.equal(true)
  277. })
  278. })
  279. describe('with a user_id with no privilege level', function () {
  280. beforeEach(function () {
  281. this.AuthorizationManager.isUserSiteAdmin
  282. .withArgs(this.user_id)
  283. .yields(null, false)
  284. this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  285. .withArgs(this.user_id, this.project_id)
  286. .yields(null, false)
  287. return this.AuthorizationManager.getPrivilegeLevelForProject(
  288. this.user_id,
  289. this.project_id,
  290. this.token,
  291. this.callback
  292. )
  293. })
  294. it('should return false', function () {
  295. return this.callback
  296. .calledWith(null, false, false, false)
  297. .should.equal(true)
  298. })
  299. })
  300. describe('with a user_id who is an admin', function () {
  301. beforeEach(function () {
  302. this.AuthorizationManager.isUserSiteAdmin
  303. .withArgs(this.user_id)
  304. .yields(null, true)
  305. this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  306. .withArgs(this.user_id, this.project_id)
  307. .yields(null, false)
  308. return this.AuthorizationManager.getPrivilegeLevelForProject(
  309. this.user_id,
  310. this.project_id,
  311. this.token,
  312. this.callback
  313. )
  314. })
  315. it('should return the user as an owner', function () {
  316. return this.callback
  317. .calledWith(null, 'owner', false, true)
  318. .should.equal(true)
  319. })
  320. })
  321. describe('with no user (anonymous)', function () {
  322. beforeEach(function () {
  323. return this.AuthorizationManager.getPrivilegeLevelForProject(
  324. null,
  325. this.project_id,
  326. this.token,
  327. this.callback
  328. )
  329. })
  330. it('should not call CollaboratorsGetter.getMemberIdPrivilegeLevel', function () {
  331. return this.CollaboratorsGetter.getMemberIdPrivilegeLevel.called.should.equal(
  332. false
  333. )
  334. })
  335. it('should not call AuthorizationManager.isUserSiteAdmin', function () {
  336. return this.AuthorizationManager.isUserSiteAdmin.called.should.equal(
  337. false
  338. )
  339. })
  340. it('should return false', function () {
  341. return this.callback
  342. .calledWith(null, false, false, false)
  343. .should.equal(true)
  344. })
  345. })
  346. })
  347. describe('with a public project', function () {
  348. beforeEach(function () {
  349. return this.ProjectGetter.getProject
  350. .withArgs(this.project_id, { publicAccesLevel: 1 })
  351. .yields(null, { publicAccesLevel: 'readAndWrite' })
  352. })
  353. describe('with a user_id with a privilege level', function () {
  354. beforeEach(function () {
  355. this.AuthorizationManager.isUserSiteAdmin
  356. .withArgs(this.user_id)
  357. .yields(null, false)
  358. this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  359. .withArgs(this.user_id, this.project_id)
  360. .yields(null, 'readOnly')
  361. return this.AuthorizationManager.getPrivilegeLevelForProject(
  362. this.user_id,
  363. this.project_id,
  364. this.token,
  365. this.callback
  366. )
  367. })
  368. it("should return the user's privilege level", function () {
  369. return this.callback
  370. .calledWith(null, 'readOnly', false)
  371. .should.equal(true)
  372. })
  373. })
  374. describe('with a user_id with no privilege level', function () {
  375. beforeEach(function () {
  376. this.AuthorizationManager.isUserSiteAdmin
  377. .withArgs(this.user_id)
  378. .yields(null, false)
  379. this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  380. .withArgs(this.user_id, this.project_id)
  381. .yields(null, false)
  382. return this.AuthorizationManager.getPrivilegeLevelForProject(
  383. this.user_id,
  384. this.project_id,
  385. this.token,
  386. this.callback
  387. )
  388. })
  389. it('should return the public privilege level', function () {
  390. return this.callback
  391. .calledWith(null, 'readAndWrite', true)
  392. .should.equal(true)
  393. })
  394. })
  395. describe('with a user_id who is an admin', function () {
  396. beforeEach(function () {
  397. this.AuthorizationManager.isUserSiteAdmin
  398. .withArgs(this.user_id)
  399. .yields(null, true)
  400. this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  401. .withArgs(this.user_id, this.project_id)
  402. .yields(null, false)
  403. return this.AuthorizationManager.getPrivilegeLevelForProject(
  404. this.user_id,
  405. this.project_id,
  406. this.token,
  407. this.callback
  408. )
  409. })
  410. it('should return the user as an owner', function () {
  411. return this.callback
  412. .calledWith(null, 'owner', false)
  413. .should.equal(true)
  414. })
  415. })
  416. describe('with no user (anonymous)', function () {
  417. beforeEach(function () {
  418. return this.AuthorizationManager.getPrivilegeLevelForProject(
  419. null,
  420. this.project_id,
  421. this.token,
  422. this.callback
  423. )
  424. })
  425. it('should not call CollaboratorsGetter.getMemberIdPrivilegeLevel', function () {
  426. return this.CollaboratorsGetter.getMemberIdPrivilegeLevel.called.should.equal(
  427. false
  428. )
  429. })
  430. it('should not call AuthorizationManager.isUserSiteAdmin', function () {
  431. return this.AuthorizationManager.isUserSiteAdmin.called.should.equal(
  432. false
  433. )
  434. })
  435. it('should return the public privilege level', function () {
  436. return this.callback
  437. .calledWith(null, 'readAndWrite', true)
  438. .should.equal(true)
  439. })
  440. })
  441. })
  442. describe("when the project doesn't exist", function () {
  443. beforeEach(function () {
  444. return this.ProjectGetter.getProject
  445. .withArgs(this.project_id, { publicAccesLevel: 1 })
  446. .yields(null, null)
  447. })
  448. it('should return a NotFoundError', function () {
  449. return this.AuthorizationManager.getPrivilegeLevelForProject(
  450. this.user_id,
  451. this.project_id,
  452. this.token,
  453. error => error.should.be.instanceof(Errors.NotFoundError)
  454. )
  455. })
  456. })
  457. describe('when the project id is not valid', function () {
  458. beforeEach(function () {
  459. this.AuthorizationManager.isUserSiteAdmin
  460. .withArgs(this.user_id)
  461. .yields(null, false)
  462. return this.CollaboratorsGetter.getMemberIdPrivilegeLevel
  463. .withArgs(this.user_id, this.project_id)
  464. .yields(null, 'readOnly')
  465. })
  466. it('should return a error', function (done) {
  467. return this.AuthorizationManager.getPrivilegeLevelForProject(
  468. undefined,
  469. 'not project id',
  470. this.token,
  471. err => {
  472. this.ProjectGetter.getProject.called.should.equal(false)
  473. expect(err).to.exist
  474. return done()
  475. }
  476. )
  477. })
  478. })
  479. })
  480. describe('canUserReadProject', function () {
  481. beforeEach(function () {
  482. return (this.AuthorizationManager.getPrivilegeLevelForProject = sinon.stub())
  483. })
  484. describe('when user is owner', function () {
  485. beforeEach(function () {
  486. return this.AuthorizationManager.getPrivilegeLevelForProject
  487. .withArgs(this.user_id, this.project_id, this.token)
  488. .yields(null, 'owner', false)
  489. })
  490. it('should return true', function (done) {
  491. return this.AuthorizationManager.canUserReadProject(
  492. this.user_id,
  493. this.project_id,
  494. this.token,
  495. (error, canRead) => {
  496. expect(canRead).to.equal(true)
  497. return done()
  498. }
  499. )
  500. })
  501. })
  502. describe('when user has read-write access', function () {
  503. beforeEach(function () {
  504. return this.AuthorizationManager.getPrivilegeLevelForProject
  505. .withArgs(this.user_id, this.project_id, this.token)
  506. .yields(null, 'readAndWrite', false)
  507. })
  508. it('should return true', function (done) {
  509. return this.AuthorizationManager.canUserReadProject(
  510. this.user_id,
  511. this.project_id,
  512. this.token,
  513. (error, canRead) => {
  514. expect(canRead).to.equal(true)
  515. return done()
  516. }
  517. )
  518. })
  519. })
  520. describe('when user has read-only access', function () {
  521. beforeEach(function () {
  522. return this.AuthorizationManager.getPrivilegeLevelForProject
  523. .withArgs(this.user_id, this.project_id, this.token)
  524. .yields(null, 'readOnly', false)
  525. })
  526. it('should return true', function (done) {
  527. return this.AuthorizationManager.canUserReadProject(
  528. this.user_id,
  529. this.project_id,
  530. this.token,
  531. (error, canRead) => {
  532. expect(canRead).to.equal(true)
  533. return done()
  534. }
  535. )
  536. })
  537. })
  538. describe('when user has no access', function () {
  539. beforeEach(function () {
  540. return this.AuthorizationManager.getPrivilegeLevelForProject
  541. .withArgs(this.user_id, this.project_id, this.token)
  542. .yields(null, false, false)
  543. })
  544. it('should return false', function (done) {
  545. return this.AuthorizationManager.canUserReadProject(
  546. this.user_id,
  547. this.project_id,
  548. this.token,
  549. (error, canRead) => {
  550. expect(canRead).to.equal(false)
  551. return done()
  552. }
  553. )
  554. })
  555. })
  556. })
  557. describe('canUserWriteProjectContent', function () {
  558. beforeEach(function () {
  559. return (this.AuthorizationManager.getPrivilegeLevelForProject = sinon.stub())
  560. })
  561. describe('when user is owner', function () {
  562. beforeEach(function () {
  563. return this.AuthorizationManager.getPrivilegeLevelForProject
  564. .withArgs(this.user_id, this.project_id, this.token)
  565. .yields(null, 'owner', false)
  566. })
  567. it('should return true', function (done) {
  568. return this.AuthorizationManager.canUserWriteProjectContent(
  569. this.user_id,
  570. this.project_id,
  571. this.token,
  572. (error, canWrite) => {
  573. expect(canWrite).to.equal(true)
  574. return done()
  575. }
  576. )
  577. })
  578. })
  579. describe('when user has read-write access', function () {
  580. beforeEach(function () {
  581. return this.AuthorizationManager.getPrivilegeLevelForProject
  582. .withArgs(this.user_id, this.project_id, this.token)
  583. .yields(null, 'readAndWrite', false)
  584. })
  585. it('should return true', function (done) {
  586. return this.AuthorizationManager.canUserWriteProjectContent(
  587. this.user_id,
  588. this.project_id,
  589. this.token,
  590. (error, canWrite) => {
  591. expect(canWrite).to.equal(true)
  592. return done()
  593. }
  594. )
  595. })
  596. })
  597. describe('when user has read-only access', function () {
  598. beforeEach(function () {
  599. return this.AuthorizationManager.getPrivilegeLevelForProject
  600. .withArgs(this.user_id, this.project_id, this.token)
  601. .yields(null, 'readOnly', false)
  602. })
  603. it('should return false', function (done) {
  604. return this.AuthorizationManager.canUserWriteProjectContent(
  605. this.user_id,
  606. this.project_id,
  607. this.token,
  608. (error, canWrite) => {
  609. expect(canWrite).to.equal(false)
  610. return done()
  611. }
  612. )
  613. })
  614. })
  615. describe('when user has no access', function () {
  616. beforeEach(function () {
  617. return this.AuthorizationManager.getPrivilegeLevelForProject
  618. .withArgs(this.user_id, this.project_id, this.token)
  619. .yields(null, false, false)
  620. })
  621. it('should return false', function (done) {
  622. return this.AuthorizationManager.canUserWriteProjectContent(
  623. this.user_id,
  624. this.project_id,
  625. this.token,
  626. (error, canWrite) => {
  627. expect(canWrite).to.equal(false)
  628. return done()
  629. }
  630. )
  631. })
  632. })
  633. })
  634. describe('canUserWriteProjectSettings', function () {
  635. beforeEach(function () {
  636. return (this.AuthorizationManager.getPrivilegeLevelForProject = sinon.stub())
  637. })
  638. describe('when user is owner', function () {
  639. beforeEach(function () {
  640. return this.AuthorizationManager.getPrivilegeLevelForProject
  641. .withArgs(this.user_id, this.project_id, this.token)
  642. .yields(null, 'owner', false)
  643. })
  644. it('should return true', function (done) {
  645. return this.AuthorizationManager.canUserWriteProjectSettings(
  646. this.user_id,
  647. this.project_id,
  648. this.token,
  649. (error, canWrite) => {
  650. expect(canWrite).to.equal(true)
  651. return done()
  652. }
  653. )
  654. })
  655. })
  656. describe('when user has read-write access as a collaborator', function () {
  657. beforeEach(function () {
  658. return this.AuthorizationManager.getPrivilegeLevelForProject
  659. .withArgs(this.user_id, this.project_id, this.token)
  660. .yields(null, 'readAndWrite', false)
  661. })
  662. it('should return true', function (done) {
  663. return this.AuthorizationManager.canUserWriteProjectSettings(
  664. this.user_id,
  665. this.project_id,
  666. this.token,
  667. (error, canWrite) => {
  668. expect(canWrite).to.equal(true)
  669. return done()
  670. }
  671. )
  672. })
  673. })
  674. describe('when user has read-write access as the public', function () {
  675. beforeEach(function () {
  676. return this.AuthorizationManager.getPrivilegeLevelForProject
  677. .withArgs(this.user_id, this.project_id, this.token)
  678. .yields(null, 'readAndWrite', true)
  679. })
  680. it('should return false', function (done) {
  681. return this.AuthorizationManager.canUserWriteProjectSettings(
  682. this.user_id,
  683. this.project_id,
  684. this.token,
  685. (error, canWrite) => {
  686. expect(canWrite).to.equal(false)
  687. return done()
  688. }
  689. )
  690. })
  691. })
  692. describe('when user has read-only access', function () {
  693. beforeEach(function () {
  694. return this.AuthorizationManager.getPrivilegeLevelForProject
  695. .withArgs(this.user_id, this.project_id, this.token)
  696. .yields(null, 'readOnly', false)
  697. })
  698. it('should return false', function (done) {
  699. return this.AuthorizationManager.canUserWriteProjectSettings(
  700. this.user_id,
  701. this.project_id,
  702. this.token,
  703. (error, canWrite) => {
  704. expect(canWrite).to.equal(false)
  705. return done()
  706. }
  707. )
  708. })
  709. })
  710. describe('when user has no access', function () {
  711. beforeEach(function () {
  712. return this.AuthorizationManager.getPrivilegeLevelForProject
  713. .withArgs(this.user_id, this.project_id, this.token)
  714. .yields(null, false, false)
  715. })
  716. it('should return false', function (done) {
  717. return this.AuthorizationManager.canUserWriteProjectSettings(
  718. this.user_id,
  719. this.project_id,
  720. this.token,
  721. (error, canWrite) => {
  722. expect(canWrite).to.equal(false)
  723. return done()
  724. }
  725. )
  726. })
  727. })
  728. })
  729. describe('canUserAdminProject', function () {
  730. beforeEach(function () {
  731. return (this.AuthorizationManager.getPrivilegeLevelForProject = sinon.stub())
  732. })
  733. describe('when user is owner', function () {
  734. beforeEach(function () {
  735. return this.AuthorizationManager.getPrivilegeLevelForProject
  736. .withArgs(this.user_id, this.project_id, this.token)
  737. .yields(null, 'owner', false)
  738. })
  739. it('should return true', function (done) {
  740. return this.AuthorizationManager.canUserAdminProject(
  741. this.user_id,
  742. this.project_id,
  743. this.token,
  744. (error, canAdmin) => {
  745. expect(canAdmin).to.equal(true)
  746. return done()
  747. }
  748. )
  749. })
  750. })
  751. describe('when user has read-write access', function () {
  752. beforeEach(function () {
  753. return this.AuthorizationManager.getPrivilegeLevelForProject
  754. .withArgs(this.user_id, this.project_id, this.token)
  755. .yields(null, 'readAndWrite', false)
  756. })
  757. it('should return false', function (done) {
  758. return this.AuthorizationManager.canUserAdminProject(
  759. this.user_id,
  760. this.project_id,
  761. this.token,
  762. (error, canAdmin) => {
  763. expect(canAdmin).to.equal(false)
  764. return done()
  765. }
  766. )
  767. })
  768. })
  769. describe('when user has read-only access', function () {
  770. beforeEach(function () {
  771. return this.AuthorizationManager.getPrivilegeLevelForProject
  772. .withArgs(this.user_id, this.project_id, this.token)
  773. .yields(null, 'readOnly', false)
  774. })
  775. it('should return false', function (done) {
  776. return this.AuthorizationManager.canUserAdminProject(
  777. this.user_id,
  778. this.project_id,
  779. this.token,
  780. (error, canAdmin) => {
  781. expect(canAdmin).to.equal(false)
  782. return done()
  783. }
  784. )
  785. })
  786. })
  787. describe('when user has no access', function () {
  788. beforeEach(function () {
  789. return this.AuthorizationManager.getPrivilegeLevelForProject
  790. .withArgs(this.user_id, this.project_id, this.token)
  791. .yields(null, false, false)
  792. })
  793. it('should return false', function (done) {
  794. return this.AuthorizationManager.canUserAdminProject(
  795. this.user_id,
  796. this.project_id,
  797. this.token,
  798. (error, canAdmin) => {
  799. expect(canAdmin).to.equal(false)
  800. return done()
  801. }
  802. )
  803. })
  804. })
  805. })
  806. describe('isUserSiteAdmin', function () {
  807. beforeEach(function () {
  808. return (this.User.findOne = sinon.stub())
  809. })
  810. describe('when user is admin', function () {
  811. beforeEach(function () {
  812. return this.User.findOne
  813. .withArgs({ _id: this.user_id }, { isAdmin: 1 })
  814. .yields(null, { isAdmin: true })
  815. })
  816. it('should return true', function (done) {
  817. return this.AuthorizationManager.isUserSiteAdmin(
  818. this.user_id,
  819. (error, isAdmin) => {
  820. expect(isAdmin).to.equal(true)
  821. return done()
  822. }
  823. )
  824. })
  825. })
  826. describe('when user is not admin', function () {
  827. beforeEach(function () {
  828. return this.User.findOne
  829. .withArgs({ _id: this.user_id }, { isAdmin: 1 })
  830. .yields(null, { isAdmin: false })
  831. })
  832. it('should return false', function (done) {
  833. return this.AuthorizationManager.isUserSiteAdmin(
  834. this.user_id,
  835. (error, isAdmin) => {
  836. expect(isAdmin).to.equal(false)
  837. return done()
  838. }
  839. )
  840. })
  841. })
  842. describe('when user is not found', function () {
  843. beforeEach(function () {
  844. return this.User.findOne
  845. .withArgs({ _id: this.user_id }, { isAdmin: 1 })
  846. .yields(null, null)
  847. })
  848. it('should return false', function (done) {
  849. return this.AuthorizationManager.isUserSiteAdmin(
  850. this.user_id,
  851. (error, isAdmin) => {
  852. expect(isAdmin).to.equal(false)
  853. return done()
  854. }
  855. )
  856. })
  857. })
  858. describe('when no user is passed', function () {
  859. it('should return false', function (done) {
  860. return this.AuthorizationManager.isUserSiteAdmin(
  861. null,
  862. (error, isAdmin) => {
  863. this.User.findOne.called.should.equal(false)
  864. expect(isAdmin).to.equal(false)
  865. return done()
  866. }
  867. )
  868. })
  869. })
  870. })
  871. })