AuthorizationManagerTests.js 31 KB

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