| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244 |
- sinon = require('sinon')
- chai = require('chai')
- should = chai.should()
- expect = chai.expect
- modulePath = "../../../../app/js/Features/Authorization/AuthorizationMiddlewear.js"
- SandboxedModule = require('sandboxed-module')
- Errors = require "../../../../app/js/Features/Errors/Errors.js"
- describe "AuthorizationMiddlewear", ->
- beforeEach ->
- @user_id = "user-id-123"
- @project_id = "project-id-123"
- @AuthenticationController =
- getLoggedInUserId: sinon.stub().returns(@user_id)
- isUserLoggedIn: sinon.stub().returns(true)
- @AuthorizationMiddlewear = SandboxedModule.require modulePath, requires:
- "./AuthorizationManager": @AuthorizationManager = {}
- "logger-sharelatex": {log: () ->}
- "mongojs": ObjectId: @ObjectId = {}
- "../Errors/Errors": Errors
- '../Authentication/AuthenticationController': @AuthenticationController
- @req = {}
- @res = {}
- @ObjectId.isValid = sinon.stub()
- @ObjectId.isValid.withArgs(@project_id).returns true
- @next = sinon.stub()
- METHODS_TO_TEST = {
- "ensureUserCanReadProject": "canUserReadProject"
- "ensureUserCanWriteProjectSettings": "canUserWriteProjectSettings"
- "ensureUserCanWriteProjectContent": "canUserWriteProjectContent"
- "ensureUserCanAdminProject": "canUserAdminProject"
- }
- for middlewearMethod, managerMethod of METHODS_TO_TEST
- do (middlewearMethod, managerMethod) ->
- describe middlewearMethod, ->
- beforeEach ->
- @req.params =
- project_id: @project_id
- @AuthorizationManager[managerMethod] = sinon.stub()
- @AuthorizationMiddlewear.redirectToRestricted = sinon.stub()
- describe "with missing project_id", ->
- beforeEach ->
- @req.params = {}
- it "should return an error to next", ->
- @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
- @next.calledWith(new Error()).should.equal true
- describe "with logged in user", ->
- beforeEach ->
- @AuthenticationController.getLoggedInUserId.returns(@user_id)
- describe "when user has permission", ->
- beforeEach ->
- @AuthorizationManager[managerMethod]
- .withArgs(@user_id, @project_id)
- .yields(null, true)
- it "should return next", ->
- @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
- @next.called.should.equal true
- describe "when user doesn't have permission", ->
- beforeEach ->
- @AuthorizationManager[managerMethod]
- .withArgs(@user_id, @project_id)
- .yields(null, false)
- it "should redirect to redirectToRestricted", ->
- @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
- @next.called.should.equal false
- @AuthorizationMiddlewear.redirectToRestricted
- .calledWith(@req, @res, @next)
- .should.equal true
- describe "with anonymous user", ->
- describe "when user has permission", ->
- beforeEach ->
- @AuthenticationController.getLoggedInUserId.returns(null)
- @AuthorizationManager[managerMethod]
- .withArgs(null, @project_id)
- .yields(null, true)
- it "should return next", ->
- @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
- @next.called.should.equal true
- describe "when user doesn't have permission", ->
- beforeEach ->
- @AuthenticationController.getLoggedInUserId.returns(null)
- @AuthorizationManager[managerMethod]
- .withArgs(null, @project_id)
- .yields(null, false)
- it "should redirect to redirectToRestricted", ->
- @AuthorizationMiddlewear[middlewearMethod] @req, @res, @next
- @next.called.should.equal false
- @AuthorizationMiddlewear.redirectToRestricted
- .calledWith(@req, @res, @next)
- .should.equal true
- describe "with malformed project id", ->
- beforeEach ->
- @req.params =
- project_id: "blah"
- @ObjectId.isValid = sinon.stub().returns false
- it "should return a not found error", (done) ->
- @AuthorizationMiddlewear[middlewearMethod] @req, @res, (error) ->
- error.should.be.instanceof Errors.NotFoundError
- done()
- describe "ensureUserIsSiteAdmin", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin = sinon.stub()
- @AuthorizationMiddlewear.redirectToRestricted = sinon.stub()
- describe "with logged in user", ->
- beforeEach ->
- @AuthenticationController.getLoggedInUserId.returns(@user_id)
- describe "when user has permission", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin
- .withArgs(@user_id)
- .yields(null, true)
- it "should return next", ->
- @AuthorizationMiddlewear.ensureUserIsSiteAdmin @req, @res, @next
- @next.called.should.equal true
- describe "when user doesn't have permission", ->
- beforeEach ->
- @AuthorizationManager.isUserSiteAdmin
- .withArgs(@user_id)
- .yields(null, false)
- it "should redirect to redirectToRestricted", ->
- @AuthorizationMiddlewear.ensureUserIsSiteAdmin @req, @res, @next
- @next.called.should.equal false
- @AuthorizationMiddlewear.redirectToRestricted
- .calledWith(@req, @res, @next)
- .should.equal true
- describe "with anonymous user", ->
- describe "when user has permission", ->
- beforeEach ->
- @AuthenticationController.getLoggedInUserId.returns(null)
- @AuthorizationManager.isUserSiteAdmin
- .withArgs(null)
- .yields(null, true)
- it "should return next", ->
- @AuthorizationMiddlewear.ensureUserIsSiteAdmin @req, @res, @next
- @next.called.should.equal true
- describe "when user doesn't have permission", ->
- beforeEach ->
- @AuthenticationController.getLoggedInUserId.returns(null)
- @AuthorizationManager.isUserSiteAdmin
- .withArgs(null)
- .yields(null, false)
- it "should redirect to redirectToRestricted", ->
- @AuthorizationMiddlewear.ensureUserIsSiteAdmin @req, @res, @next
- @next.called.should.equal false
- @AuthorizationMiddlewear.redirectToRestricted
- .calledWith(@req, @res, @next)
- .should.equal true
- describe "ensureUserCanReadMultipleProjects", ->
- beforeEach ->
- @AuthorizationManager.canUserReadProject = sinon.stub()
- @AuthorizationMiddlewear.redirectToRestricted = sinon.stub()
- @req.query =
- project_ids: "project1,project2"
- describe "with logged in user", ->
- beforeEach ->
- @AuthenticationController.getLoggedInUserId.returns(@user_id)
- describe "when user has permission to access all projects", ->
- beforeEach ->
- @AuthorizationManager.canUserReadProject
- .withArgs(@user_id, "project1")
- .yields(null, true)
- @AuthorizationManager.canUserReadProject
- .withArgs(@user_id, "project2")
- .yields(null, true)
- it "should return next", ->
- @AuthorizationMiddlewear.ensureUserCanReadMultipleProjects @req, @res, @next
- @next.called.should.equal true
- describe "when user doesn't have permission to access one of the projects", ->
- beforeEach ->
- @AuthorizationManager.canUserReadProject
- .withArgs(@user_id, "project1")
- .yields(null, true)
- @AuthorizationManager.canUserReadProject
- .withArgs(@user_id, "project2")
- .yields(null, false)
- it "should redirect to redirectToRestricted", ->
- @AuthorizationMiddlewear.ensureUserCanReadMultipleProjects @req, @res, @next
- @next.called.should.equal false
- @AuthorizationMiddlewear.redirectToRestricted
- .calledWith(@req, @res, @next)
- .should.equal true
- describe "with anonymous user", ->
- describe "when user has permission", ->
- describe "when user has permission to access all projects", ->
- beforeEach ->
- @AuthenticationController.getLoggedInUserId.returns(null)
- @AuthorizationManager.canUserReadProject
- .withArgs(null, "project1")
- .yields(null, true)
- @AuthorizationManager.canUserReadProject
- .withArgs(null, "project2")
- .yields(null, true)
- it "should return next", ->
- @AuthorizationMiddlewear.ensureUserCanReadMultipleProjects @req, @res, @next
- @next.called.should.equal true
- describe "when user doesn't have permission to access one of the projects", ->
- beforeEach ->
- @AuthenticationController.getLoggedInUserId.returns(null)
- @AuthorizationManager.canUserReadProject
- .withArgs(null, "project1")
- .yields(null, true)
- @AuthorizationManager.canUserReadProject
- .withArgs(null, "project2")
- .yields(null, false)
- it "should redirect to redirectToRestricted", ->
- @AuthorizationMiddlewear.ensureUserCanReadMultipleProjects @req, @res, @next
- @next.called.should.equal false
- @AuthorizationMiddlewear.redirectToRestricted
- .calledWith(@req, @res, @next)
- .should.equal true
|