AuthorizationManagerTests.js 31 KB

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