AuthorizationManagerTests.js 32 KB

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