mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-06-06 07:39:02 +02:00
Merge pull request #3495 from overleaf/ae-prettier-2
Upgrade Prettier to v2 GitOrigin-RevId: 85aa3fa1acb6332c4f58c46165a43d1a51471f33
This commit is contained in:
@@ -6,8 +6,8 @@ const Errors = require('../../../../app/src/Features/Errors/Errors.js')
|
||||
const MODULE_PATH =
|
||||
'../../../../app/src/Features/Authorization/AuthorizationMiddleware.js'
|
||||
|
||||
describe('AuthorizationMiddleware', function() {
|
||||
beforeEach(function() {
|
||||
describe('AuthorizationMiddleware', function () {
|
||||
beforeEach(function () {
|
||||
this.userId = 'user-id-123'
|
||||
this.project_id = 'project-id-123'
|
||||
this.token = 'some-token'
|
||||
@@ -23,10 +23,7 @@ describe('AuthorizationMiddleware', function() {
|
||||
getRequestToken: sinon.stub().returns(this.token)
|
||||
}
|
||||
this.ObjectId = {
|
||||
isValid: sinon
|
||||
.stub()
|
||||
.withArgs(this.project_id)
|
||||
.returns(true)
|
||||
isValid: sinon.stub().withArgs(this.project_id).returns(true)
|
||||
}
|
||||
this.AuthorizationManager = {}
|
||||
this.AuthorizationMiddleware = SandboxedModule.require(MODULE_PATH, {
|
||||
@@ -46,12 +43,12 @@ describe('AuthorizationMiddleware', function() {
|
||||
this.next = sinon.stub()
|
||||
})
|
||||
|
||||
describe('_getUserId', function() {
|
||||
beforeEach(function() {
|
||||
describe('_getUserId', function () {
|
||||
beforeEach(function () {
|
||||
this.req = {}
|
||||
})
|
||||
|
||||
it('should get the user from session', function(done) {
|
||||
it('should get the user from session', function (done) {
|
||||
this.AuthenticationController.getLoggedInUserId = sinon
|
||||
.stub()
|
||||
.returns('1234')
|
||||
@@ -62,7 +59,7 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
it('should get oauth_user from request', function(done) {
|
||||
it('should get oauth_user from request', function (done) {
|
||||
this.AuthenticationController.getLoggedInUserId = sinon
|
||||
.stub()
|
||||
.returns(null)
|
||||
@@ -74,7 +71,7 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
it('should fall back to null', function(done) {
|
||||
it('should fall back to null', function (done) {
|
||||
this.AuthenticationController.getLoggedInUserId = sinon
|
||||
.stub()
|
||||
.returns(null)
|
||||
@@ -94,19 +91,19 @@ describe('AuthorizationMiddleware', function() {
|
||||
}
|
||||
Object.entries(METHODS_TO_TEST).forEach(
|
||||
([middlewareMethod, managerMethod]) => {
|
||||
describe(middlewareMethod, function() {
|
||||
beforeEach(function() {
|
||||
describe(middlewareMethod, function () {
|
||||
beforeEach(function () {
|
||||
this.req.params = { project_id: this.project_id }
|
||||
this.AuthorizationManager[managerMethod] = sinon.stub()
|
||||
this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
|
||||
})
|
||||
|
||||
describe('with missing project_id', function() {
|
||||
beforeEach(function() {
|
||||
describe('with missing project_id', function () {
|
||||
beforeEach(function () {
|
||||
this.req.params = {}
|
||||
})
|
||||
|
||||
it('should return an error to next', function() {
|
||||
it('should return an error to next', function () {
|
||||
this.AuthorizationMiddleware[middlewareMethod](
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -118,19 +115,19 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('with logged in user', function() {
|
||||
beforeEach(function() {
|
||||
describe('with logged in user', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(this.userId)
|
||||
})
|
||||
|
||||
describe('when user has permission', function() {
|
||||
beforeEach(function() {
|
||||
describe('when user has permission', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager[managerMethod]
|
||||
.withArgs(this.userId, this.project_id, this.token)
|
||||
.yields(null, true)
|
||||
})
|
||||
|
||||
it('should return next', function() {
|
||||
it('should return next', function () {
|
||||
this.AuthorizationMiddleware[middlewareMethod](
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -140,14 +137,14 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe("when user doesn't have permission", function() {
|
||||
beforeEach(function() {
|
||||
describe("when user doesn't have permission", function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager[managerMethod]
|
||||
.withArgs(this.userId, this.project_id, this.token)
|
||||
.yields(null, false)
|
||||
})
|
||||
|
||||
it('should raise a 403', function() {
|
||||
it('should raise a 403', function () {
|
||||
this.AuthorizationMiddleware[middlewareMethod](
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -161,16 +158,16 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('with anonymous user', function() {
|
||||
describe('when user has permission', function() {
|
||||
beforeEach(function() {
|
||||
describe('with anonymous user', function () {
|
||||
describe('when user has permission', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(null)
|
||||
this.AuthorizationManager[managerMethod]
|
||||
.withArgs(null, this.project_id, this.token)
|
||||
.yields(null, true)
|
||||
})
|
||||
|
||||
it('should return next', function() {
|
||||
it('should return next', function () {
|
||||
this.AuthorizationMiddleware[middlewareMethod](
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -180,15 +177,15 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe("when user doesn't have permission", function() {
|
||||
beforeEach(function() {
|
||||
describe("when user doesn't have permission", function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(null)
|
||||
this.AuthorizationManager[managerMethod]
|
||||
.withArgs(null, this.project_id, this.token)
|
||||
.yields(null, false)
|
||||
})
|
||||
|
||||
it('should redirect to redirectToRestricted', function() {
|
||||
it('should redirect to redirectToRestricted', function () {
|
||||
this.AuthorizationMiddleware[middlewareMethod](
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -202,13 +199,13 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('with malformed project id', function() {
|
||||
beforeEach(function() {
|
||||
describe('with malformed project id', function () {
|
||||
beforeEach(function () {
|
||||
this.req.params = { project_id: 'blah' }
|
||||
this.ObjectId.isValid = sinon.stub().returns(false)
|
||||
})
|
||||
|
||||
it('should return a not found error', function(done) {
|
||||
it('should return a not found error', function (done) {
|
||||
this.AuthorizationMiddleware[middlewareMethod](
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -223,19 +220,19 @@ describe('AuthorizationMiddleware', function() {
|
||||
}
|
||||
)
|
||||
|
||||
describe('ensureUserCanAdminProject', function() {
|
||||
beforeEach(function() {
|
||||
describe('ensureUserCanAdminProject', function () {
|
||||
beforeEach(function () {
|
||||
this.req.params = { project_id: this.project_id }
|
||||
this.AuthorizationManager.canUserAdminProject = sinon.stub()
|
||||
this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
|
||||
})
|
||||
|
||||
describe('with missing project_id', function() {
|
||||
beforeEach(function() {
|
||||
describe('with missing project_id', function () {
|
||||
beforeEach(function () {
|
||||
this.req.params = {}
|
||||
})
|
||||
|
||||
it('should return an error to next', function() {
|
||||
it('should return an error to next', function () {
|
||||
this.AuthorizationMiddleware.ensureUserCanAdminProject(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -245,19 +242,19 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('with logged in user', function() {
|
||||
beforeEach(function() {
|
||||
describe('with logged in user', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(this.userId)
|
||||
})
|
||||
|
||||
describe('when user has permission', function() {
|
||||
beforeEach(function() {
|
||||
describe('when user has permission', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager.canUserAdminProject
|
||||
.withArgs(this.userId, this.project_id, this.token)
|
||||
.yields(null, true)
|
||||
})
|
||||
|
||||
it('should return next', function() {
|
||||
it('should return next', function () {
|
||||
this.AuthorizationMiddleware.ensureUserCanAdminProject(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -267,14 +264,14 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe("when user doesn't have permission", function() {
|
||||
beforeEach(function() {
|
||||
describe("when user doesn't have permission", function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager.canUserAdminProject
|
||||
.withArgs(this.userId, this.project_id, this.token)
|
||||
.yields(null, false)
|
||||
})
|
||||
|
||||
it('should invoke HTTP forbidden error handler', function(done) {
|
||||
it('should invoke HTTP forbidden error handler', function (done) {
|
||||
this.HttpErrorHandler.forbidden = sinon.spy(() => done())
|
||||
this.AuthorizationMiddleware.ensureUserCanAdminProject(
|
||||
this.req,
|
||||
@@ -284,16 +281,16 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('with anonymous user', function() {
|
||||
describe('when user has permission', function() {
|
||||
beforeEach(function() {
|
||||
describe('with anonymous user', function () {
|
||||
describe('when user has permission', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(null)
|
||||
this.AuthorizationManager.canUserAdminProject
|
||||
.withArgs(null, this.project_id, this.token)
|
||||
.yields(null, true)
|
||||
})
|
||||
|
||||
it('should return next', function() {
|
||||
it('should return next', function () {
|
||||
this.AuthorizationMiddleware.ensureUserCanAdminProject(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -303,15 +300,15 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe("when user doesn't have permission", function() {
|
||||
beforeEach(function() {
|
||||
describe("when user doesn't have permission", function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(null)
|
||||
this.AuthorizationManager.canUserAdminProject
|
||||
.withArgs(null, this.project_id, this.token)
|
||||
.yields(null, false)
|
||||
})
|
||||
|
||||
it('should invoke HTTP forbidden error handler', function(done) {
|
||||
it('should invoke HTTP forbidden error handler', function (done) {
|
||||
this.HttpErrorHandler.forbidden = sinon.spy(() => done())
|
||||
this.AuthorizationMiddleware.ensureUserCanAdminProject(
|
||||
this.req,
|
||||
@@ -321,13 +318,13 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('with malformed project id', function() {
|
||||
beforeEach(function() {
|
||||
describe('with malformed project id', function () {
|
||||
beforeEach(function () {
|
||||
this.req.params = { project_id: 'blah' }
|
||||
this.ObjectId.isValid = sinon.stub().returns(false)
|
||||
})
|
||||
|
||||
it('should return a not found error', function(done) {
|
||||
it('should return a not found error', function (done) {
|
||||
this.AuthorizationMiddleware.ensureUserCanAdminProject(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -340,25 +337,25 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('ensureUserIsSiteAdmin', function() {
|
||||
beforeEach(function() {
|
||||
describe('ensureUserIsSiteAdmin', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager.isUserSiteAdmin = sinon.stub()
|
||||
this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
|
||||
})
|
||||
|
||||
describe('with logged in user', function() {
|
||||
beforeEach(function() {
|
||||
describe('with logged in user', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(this.userId)
|
||||
})
|
||||
|
||||
describe('when user has permission', function() {
|
||||
beforeEach(function() {
|
||||
describe('when user has permission', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager.isUserSiteAdmin
|
||||
.withArgs(this.userId)
|
||||
.yields(null, true)
|
||||
})
|
||||
|
||||
it('should return next', function() {
|
||||
it('should return next', function () {
|
||||
this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -368,14 +365,14 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe("when user doesn't have permission", function() {
|
||||
beforeEach(function() {
|
||||
describe("when user doesn't have permission", function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager.isUserSiteAdmin
|
||||
.withArgs(this.userId)
|
||||
.yields(null, false)
|
||||
})
|
||||
|
||||
it('should redirect to redirectToRestricted', function() {
|
||||
it('should redirect to redirectToRestricted', function () {
|
||||
this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -389,16 +386,16 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('with anonymous user', function() {
|
||||
describe('when user has permission', function() {
|
||||
beforeEach(function() {
|
||||
describe('with anonymous user', function () {
|
||||
describe('when user has permission', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(null)
|
||||
this.AuthorizationManager.isUserSiteAdmin
|
||||
.withArgs(null)
|
||||
.yields(null, true)
|
||||
})
|
||||
|
||||
it('should return next', function() {
|
||||
it('should return next', function () {
|
||||
this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -408,15 +405,15 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe("when user doesn't have permission", function() {
|
||||
beforeEach(function() {
|
||||
describe("when user doesn't have permission", function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(null)
|
||||
this.AuthorizationManager.isUserSiteAdmin
|
||||
.withArgs(null)
|
||||
.yields(null, false)
|
||||
})
|
||||
|
||||
it('should redirect to redirectToRestricted', function() {
|
||||
it('should redirect to redirectToRestricted', function () {
|
||||
this.AuthorizationMiddleware.ensureUserIsSiteAdmin(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -431,14 +428,14 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('blockRestrictedUserFromProject', function() {
|
||||
beforeEach(function() {
|
||||
describe('blockRestrictedUserFromProject', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationMiddleware._getUserAndProjectId = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, this.userId, this.project_id)
|
||||
})
|
||||
|
||||
it('should issue a 401 response for a restricted user', function(done) {
|
||||
it('should issue a 401 response for a restricted user', function (done) {
|
||||
this.AuthorizationManager.isRestrictedUserForProject = sinon
|
||||
.stub()
|
||||
.callsArgWith(3, null, true)
|
||||
@@ -459,7 +456,7 @@ describe('AuthorizationMiddleware', function() {
|
||||
)
|
||||
})
|
||||
|
||||
it('should pass through for a regular user', function(done) {
|
||||
it('should pass through for a regular user', function (done) {
|
||||
this.AuthorizationManager.isRestrictedUserForProject = sinon
|
||||
.stub()
|
||||
.callsArgWith(3, null, false)
|
||||
@@ -480,20 +477,20 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('ensureUserCanReadMultipleProjects', function() {
|
||||
beforeEach(function() {
|
||||
describe('ensureUserCanReadMultipleProjects', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager.canUserReadProject = sinon.stub()
|
||||
this.AuthorizationMiddleware.redirectToRestricted = sinon.stub()
|
||||
this.req.query = { project_ids: 'project1,project2' }
|
||||
})
|
||||
|
||||
describe('with logged in user', function() {
|
||||
beforeEach(function() {
|
||||
describe('with logged in user', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthenticationController.getLoggedInUserId.returns(this.userId)
|
||||
})
|
||||
|
||||
describe('when user has permission to access all projects', function() {
|
||||
beforeEach(function() {
|
||||
describe('when user has permission to access all projects', function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager.canUserReadProject
|
||||
.withArgs(this.userId, 'project1', this.token)
|
||||
.yields(null, true)
|
||||
@@ -502,7 +499,7 @@ describe('AuthorizationMiddleware', function() {
|
||||
.yields(null, true)
|
||||
})
|
||||
|
||||
it('should return next', function() {
|
||||
it('should return next', function () {
|
||||
this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -512,8 +509,8 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe("when user doesn't have permission to access one of the projects", function() {
|
||||
beforeEach(function() {
|
||||
describe("when user doesn't have permission to access one of the projects", function () {
|
||||
beforeEach(function () {
|
||||
this.AuthorizationManager.canUserReadProject
|
||||
.withArgs(this.userId, 'project1', this.token)
|
||||
.yields(null, true)
|
||||
@@ -522,7 +519,7 @@ describe('AuthorizationMiddleware', function() {
|
||||
.yields(null, false)
|
||||
})
|
||||
|
||||
it('should redirect to redirectToRestricted', function() {
|
||||
it('should redirect to redirectToRestricted', function () {
|
||||
this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -536,10 +533,10 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe('with anonymous user', function() {
|
||||
describe('when user has permission', function() {
|
||||
describe('when user has permission to access all projects', function() {
|
||||
beforeEach(function() {
|
||||
describe('with anonymous user', function () {
|
||||
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)
|
||||
@@ -549,7 +546,7 @@ describe('AuthorizationMiddleware', function() {
|
||||
.yields(null, true)
|
||||
})
|
||||
|
||||
it('should return next', function() {
|
||||
it('should return next', function () {
|
||||
this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
|
||||
this.req,
|
||||
this.res,
|
||||
@@ -559,8 +556,8 @@ describe('AuthorizationMiddleware', function() {
|
||||
})
|
||||
})
|
||||
|
||||
describe("when user doesn't have permission to access one of the projects", function() {
|
||||
beforeEach(function() {
|
||||
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)
|
||||
@@ -570,7 +567,7 @@ describe('AuthorizationMiddleware', function() {
|
||||
.yields(null, false)
|
||||
})
|
||||
|
||||
it('should redirect to redirectToRestricted', function() {
|
||||
it('should redirect to redirectToRestricted', function () {
|
||||
this.AuthorizationMiddleware.ensureUserCanReadMultipleProjects(
|
||||
this.req,
|
||||
this.res,
|
||||
|
||||
Reference in New Issue
Block a user