| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440 |
- /* eslint-disable
- camelcase,
- max-len,
- no-return-assign,
- no-unused-vars,
- */
- // TODO: This file was created by bulk-decaffeinate.
- // Fix any style issues and re-enable lint.
- /*
- * decaffeinate suggestions:
- * DS102: Remove unnecessary code created because of implicit returns
- * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
- */
- const sinon = require('sinon')
- const chai = require('chai')
- const should = chai.should()
- const { expect } = chai
- const modulePath =
- '../../../../app/src/Features/Authorization/AuthorizationMiddleware.js'
- const SandboxedModule = require('sandboxed-module')
- const Errors = require('../../../../app/src/Features/Errors/Errors.js')
- describe('AuthorizationMiddleware', function() {
- beforeEach(function() {
- this.user_id = 'user-id-123'
- this.project_id = 'project-id-123'
- this.token = 'some-token'
- this.AuthenticationController = {
- getLoggedInUserId: sinon.stub().returns(this.user_id),
- isUserLoggedIn: sinon.stub().returns(true)
- }
- this.AuthorizationMiddleware = SandboxedModule.require(modulePath, {
- requires: {
- './AuthorizationManager': (this.AuthorizationManager = {}),
- 'logger-sharelatex': { log() {} },
- mongojs: {
- ObjectId: (this.ObjectId = {})
- },
- '../Errors/Errors': Errors,
- '../Authentication/AuthenticationController': this
- .AuthenticationController,
- '../TokenAccess/TokenAccessHandler': (this.TokenAccessHandler = {
- getRequestToken: sinon.stub().returns(this.token)
- })
- }
- })
- this.req = {}
- this.res = {}
- this.ObjectId.isValid = sinon.stub()
- this.ObjectId.isValid.withArgs(this.project_id).returns(true)
- return (this.next = sinon.stub())
- })
- describe('_getUserId', function() {
- beforeEach(function() {
- return (this.req = {})
- })
- it('should get the user from session', function(done) {
- this.AuthenticationController.getLoggedInUserId = sinon
- .stub()
- .returns('1234')
- return this.AuthorizationMiddleware._getUserId(
- this.req,
- (err, user_id) => {
- expect(err).to.not.exist
- expect(user_id).to.equal('1234')
- return done()
- }
- )
- })
- it('should get oauth_user from request', function(done) {
- this.AuthenticationController.getLoggedInUserId = sinon
- .stub()
- .returns(null)
- this.req.oauth_user = { _id: '5678' }
- return this.AuthorizationMiddleware._getUserId(
- this.req,
- (err, user_id) => {
- expect(err).to.not.exist
- expect(user_id).to.equal('5678')
- return done()
- }
- )
- })
- it('should fall back to null', function(done) {
- this.AuthenticationController.getLoggedInUserId = sinon
- .stub()
- .returns(null)
- this.req.oauth_user = undefined
- return this.AuthorizationMiddleware._getUserId(
- this.req,
- (err, user_id) => {
- expect(err).to.not.exist
- expect(user_id).to.equal(null)
- return done()
- }
- )
- })
- })
- const METHODS_TO_TEST = {
- ensureUserCanReadProject: 'canUserReadProject',
- ensureUserCanWriteProjectSettings: 'canUserWriteProjectSettings',
- ensureUserCanWriteProjectContent: 'canUserWriteProjectContent',
- ensureUserCanAdminProject: 'canUserAdminProject'
- }
- for (let middlewareMethod in METHODS_TO_TEST) {
- const managerMethod = METHODS_TO_TEST[middlewareMethod]
- ;((middlewareMethod, managerMethod) =>
- describe(middlewareMethod, function() {
- beforeEach(function() {
- this.req.params = { project_id: this.project_id }
- this.AuthorizationManager[managerMethod] = sinon.stub()
- return (this.AuthorizationMiddleware.redirectToRestricted = sinon.stub())
- })
- describe('with missing project_id', function() {
- beforeEach(function() {
- return (this.req.params = {})
- })
- it('should return an error to next', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- return this.next.calledWith(new Error()).should.equal(true)
- })
- })
- describe('with logged in user', function() {
- beforeEach(function() {
- return this.AuthenticationController.getLoggedInUserId.returns(
- this.user_id
- )
- })
- describe('when user has permission', function() {
- beforeEach(function() {
- return this.AuthorizationManager[managerMethod]
- .withArgs(this.user_id, this.project_id, this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- return this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- return this.AuthorizationManager[managerMethod]
- .withArgs(this.user_id, this.project_id, this.token)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- return this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- describe('with anonymous user', function() {
- describe('when user has permission', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- return this.AuthorizationManager[managerMethod]
- .withArgs(null, this.project_id, this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- return this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- return this.AuthorizationManager[managerMethod]
- .withArgs(null, this.project_id, this.token)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- return this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- describe('with malformed project id', function() {
- beforeEach(function() {
- this.req.params = { project_id: 'blah' }
- return (this.ObjectId.isValid = sinon.stub().returns(false))
- })
- it('should return a not found error', function(done) {
- return this.AuthorizationMiddleware[middlewareMethod](
- this.req,
- this.res,
- function(error) {
- error.should.be.instanceof(Errors.NotFoundError)
- return done()
- }
- )
- })
- })
- }))(middlewareMethod, managerMethod)
- }
- describe('ensureUserIsSiteAdmin', function() {
- beforeEach(function() {
- this.AuthorizationManager.isUserSiteAdmin = sinon.stub()
- return (this.AuthorizationMiddleware.redirectToRestricted = sinon.stub())
- })
- describe('with logged in user', function() {
- beforeEach(function() {
- return this.AuthenticationController.getLoggedInUserId.returns(
- this.user_id
- )
- })
- describe('when user has permission', function() {
- beforeEach(function() {
- return this.AuthorizationManager.isUserSiteAdmin
- .withArgs(this.user_id)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
- this.req,
- this.res,
- this.next
- )
- return this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- return this.AuthorizationManager.isUserSiteAdmin
- .withArgs(this.user_id)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- return this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- describe('with anonymous user', function() {
- describe('when user has permission', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- return this.AuthorizationManager.isUserSiteAdmin
- .withArgs(null)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
- this.req,
- this.res,
- this.next
- )
- return this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission", function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- return this.AuthorizationManager.isUserSiteAdmin
- .withArgs(null)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- return this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- })
- describe('ensureUserCanReadMultipleProjects', function() {
- beforeEach(function() {
- this.AuthorizationManager.canUserReadProject = sinon.stub()
- this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
- return (this.req.query = { project_ids: 'project1,project2' })
- })
- describe('with logged in user', function() {
- beforeEach(function() {
- return this.AuthenticationController.getLoggedInUserId.returns(
- this.user_id
- )
- })
- describe('when user has permission to access all projects', function() {
- beforeEach(function() {
- this.AuthorizationManager.canUserReadProject
- .withArgs(this.user_id, 'project1', this.token)
- .yields(null, true)
- return this.AuthorizationManager.canUserReadProject
- .withArgs(this.user_id, 'project2', this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
- this.req,
- this.res,
- this.next
- )
- return this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission to access one of the projects", function() {
- beforeEach(function() {
- this.AuthorizationManager.canUserReadProject
- .withArgs(this.user_id, 'project1', this.token)
- .yields(null, true)
- return this.AuthorizationManager.canUserReadProject
- .withArgs(this.user_id, 'project2', this.token)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- return this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- })
- describe('with anonymous user', () =>
- describe('when user has permission', function() {
- describe('when user has permission to access all projects', function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager.canUserReadProject
- .withArgs(null, 'project1', this.token)
- .yields(null, true)
- return this.AuthorizationManager.canUserReadProject
- .withArgs(null, 'project2', this.token)
- .yields(null, true)
- })
- it('should return next', function() {
- this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
- this.req,
- this.res,
- this.next
- )
- return this.next.called.should.equal(true)
- })
- })
- describe("when user doesn't have permission to access one of the projects", function() {
- beforeEach(function() {
- this.AuthenticationController.getLoggedInUserId.returns(null)
- this.AuthorizationManager.canUserReadProject
- .withArgs(null, 'project1', this.token)
- .yields(null, true)
- return this.AuthorizationManager.canUserReadProject
- .withArgs(null, 'project2', this.token)
- .yields(null, false)
- })
- it('should redirect to redirectToRestricted', function() {
- this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
- this.req,
- this.res,
- this.next
- )
- this.next.called.should.equal(false)
- return this.AuthorizationMiddleware.redirectToRestricted
- .calledWith(this.req, this.res, this.next)
- .should.equal(true)
- })
- })
- }))
- })
- })
|