AuthorizationManagerTests.js 30 KB

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