Merge pull request #3179 from overleaf/jel-remove-sudo-mode
Remove SudoMode GitOrigin-RevId: 9419f9b28e5051a1c5abd29f498f72448d1afd33
This commit is contained in:
@@ -67,9 +67,6 @@ describe('AuthenticationController', function() {
|
||||
'../../infrastructure/Modules': (this.Modules = {
|
||||
hooks: { fire: sinon.stub().yields(null, []) }
|
||||
}),
|
||||
'../SudoMode/SudoModeHandler': (this.SudoModeHandler = {
|
||||
activateSudoMode: sinon.stub().callsArgWith(1, null)
|
||||
}),
|
||||
'../Notifications/NotificationsBuilder': (this.NotificationsBuilder = {
|
||||
ipMatcherAffiliation: sinon.stub()
|
||||
}),
|
||||
|
||||
@@ -1,457 +0,0 @@
|
||||
/* eslint-disable
|
||||
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 SandboxedModule = require('sandboxed-module')
|
||||
const sinon = require('sinon')
|
||||
const should = require('chai').should()
|
||||
const { expect } = require('chai')
|
||||
const MockRequest = require('../helpers/MockRequest')
|
||||
const MockResponse = require('../helpers/MockResponse')
|
||||
const modulePath = '../../../../app/src/Features/SudoMode/SudoModeController'
|
||||
|
||||
describe('SudoModeController', function() {
|
||||
beforeEach(function() {
|
||||
this.user = {
|
||||
_id: 'abcd',
|
||||
email: 'user@example.com'
|
||||
}
|
||||
this.UserGetter = { getUser: sinon.stub().callsArgWith(2, null, this.user) }
|
||||
this.SudoModeHandler = {
|
||||
authenticate: sinon.stub(),
|
||||
isSudoModeActive: sinon.stub(),
|
||||
activateSudoMode: sinon.stub()
|
||||
}
|
||||
this.AuthenticationController = {
|
||||
getLoggedInUserId: sinon.stub().returns(this.user._id),
|
||||
_getRediretFromSession: sinon.stub()
|
||||
}
|
||||
this.UserGetter = { getUser: sinon.stub() }
|
||||
return (this.SudoModeController = SandboxedModule.require(modulePath, {
|
||||
globals: {
|
||||
console: console
|
||||
},
|
||||
requires: {
|
||||
'logger-sharelatex': {
|
||||
log: sinon.stub(),
|
||||
warn: sinon.stub(),
|
||||
err: sinon.stub()
|
||||
},
|
||||
'./SudoModeHandler': this.SudoModeHandler,
|
||||
'../Authentication/AuthenticationController': this
|
||||
.AuthenticationController,
|
||||
mongodb: {
|
||||
ObjectId() {
|
||||
return 'some_object_id'
|
||||
}
|
||||
},
|
||||
'../User/UserGetter': this.UserGetter,
|
||||
'settings-sharelatex': (this.Settings = {})
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
describe('sudoModePrompt', function() {
|
||||
beforeEach(function() {
|
||||
this.SudoModeHandler.isSudoModeActive = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, false)
|
||||
this.req = {
|
||||
externalAuthenticationSystemUsed: sinon.stub().returns(false)
|
||||
}
|
||||
this.res = { redirect: sinon.stub(), render: sinon.stub() }
|
||||
return (this.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should get the logged in user id', function() {
|
||||
this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
|
||||
this.AuthenticationController.getLoggedInUserId.callCount.should.equal(1)
|
||||
return this.AuthenticationController.getLoggedInUserId
|
||||
.calledWith(this.req)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should check if sudo-mode is active', function() {
|
||||
this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
|
||||
this.SudoModeHandler.isSudoModeActive.callCount.should.equal(1)
|
||||
return this.SudoModeHandler.isSudoModeActive
|
||||
.calledWith(this.user._id)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should redirect when sudo-mode is active', function() {
|
||||
this.SudoModeHandler.isSudoModeActive = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, true)
|
||||
this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
|
||||
this.res.redirect.callCount.should.equal(1)
|
||||
return this.res.redirect.calledWith('/project').should.equal(true)
|
||||
})
|
||||
|
||||
it('should render the sudo_mode_prompt page when sudo mode is not active', function() {
|
||||
this.SudoModeHandler.isSudoModeActive = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, false)
|
||||
this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
|
||||
this.res.render.callCount.should.equal(1)
|
||||
return this.res.render
|
||||
.calledWith('sudo_mode/sudo_mode_prompt')
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
describe('when isSudoModeActive produces an error', function() {
|
||||
beforeEach(function() {
|
||||
this.SudoModeHandler.isSudoModeActive = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, new Error('woops'))
|
||||
return (this.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should call next with an error', function() {
|
||||
this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
|
||||
this.next.callCount.should.equal(1)
|
||||
return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
|
||||
})
|
||||
|
||||
it('should not render page', function() {
|
||||
this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
|
||||
return this.res.render.callCount.should.equal(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when external auth system is used', function() {
|
||||
beforeEach(function() {
|
||||
return (this.req.externalAuthenticationSystemUsed = sinon
|
||||
.stub()
|
||||
.returns(true))
|
||||
})
|
||||
|
||||
it('should redirect', function() {
|
||||
this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
|
||||
this.res.redirect.callCount.should.equal(1)
|
||||
return this.res.redirect.calledWith('/project').should.equal(true)
|
||||
})
|
||||
|
||||
it('should not check if sudo mode is active', function() {
|
||||
this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
|
||||
return this.SudoModeHandler.isSudoModeActive.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
it('should not render page', function() {
|
||||
this.SudoModeController.sudoModePrompt(this.req, this.res, this.next)
|
||||
return this.res.render.callCount.should.equal(0)
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('submitPassword', function() {
|
||||
beforeEach(function() {
|
||||
this.AuthenticationController._getRedirectFromSession = sinon
|
||||
.stub()
|
||||
.returns('/somewhere')
|
||||
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, this.user)
|
||||
this.SudoModeHandler.authenticate = sinon
|
||||
.stub()
|
||||
.callsArgWith(2, null, this.user)
|
||||
this.SudoModeHandler.activateSudoMode = sinon.stub().callsArgWith(1, null)
|
||||
this.password = 'a_terrible_secret'
|
||||
this.req = { body: { password: this.password } }
|
||||
this.res = { json: sinon.stub() }
|
||||
return (this.next = sinon.stub())
|
||||
})
|
||||
|
||||
describe('when all goes well', function() {
|
||||
beforeEach(function() {})
|
||||
|
||||
it('should get the logged in user id', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
|
||||
1
|
||||
)
|
||||
return this.AuthenticationController.getLoggedInUserId
|
||||
.calledWith(this.req)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should get redirect from session', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.AuthenticationController._getRedirectFromSession.callCount.should.equal(
|
||||
1
|
||||
)
|
||||
return this.AuthenticationController._getRedirectFromSession
|
||||
.calledWith(this.req)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should get the user from storage', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.UserGetter.getUser.callCount.should.equal(1)
|
||||
return this.UserGetter.getUser
|
||||
.calledWith('some_object_id', { email: 1 })
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should try to authenticate the user with the password', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.SudoModeHandler.authenticate.callCount.should.equal(1)
|
||||
return this.SudoModeHandler.authenticate
|
||||
.calledWith(this.user.email, this.password)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should activate sudo mode', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.SudoModeHandler.activateSudoMode.callCount.should.equal(1)
|
||||
return this.SudoModeHandler.activateSudoMode
|
||||
.calledWith(this.user._id)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should send back a json response', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.res.json.callCount.should.equal(1)
|
||||
return this.res.json
|
||||
.calledWith({ redir: '/somewhere' })
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should not call next', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.next.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
describe('when no password is supplied', function() {
|
||||
beforeEach(function() {
|
||||
this.req.body.password = ''
|
||||
return (this.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should return next with an error', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.next.callCount.should.equal(1)
|
||||
return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
|
||||
})
|
||||
|
||||
it('should not get the user from storage', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.UserGetter.getUser.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
it('should not try to authenticate the user with the password', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.SudoModeHandler.authenticate.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
it('should not activate sudo mode', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
it('should not send back a json response', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.res.json.callCount.should.equal(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when getUser produces an error', function() {
|
||||
beforeEach(function() {
|
||||
this.UserGetter.getUser = sinon
|
||||
.stub()
|
||||
.callsArgWith(2, new Error('woops'))
|
||||
return (this.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should return next with an error', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.next.callCount.should.equal(1)
|
||||
return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
|
||||
})
|
||||
|
||||
it('should get the user from storage', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.UserGetter.getUser.callCount.should.equal(1)
|
||||
return this.UserGetter.getUser
|
||||
.calledWith('some_object_id', { email: 1 })
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should not try to authenticate the user with the password', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.SudoModeHandler.authenticate.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
it('should not activate sudo mode', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
it('should not send back a json response', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.res.json.callCount.should.equal(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when getUser does not find a user', function() {
|
||||
beforeEach(function() {
|
||||
this.UserGetter.getUser = sinon.stub().callsArgWith(2, null, null)
|
||||
return (this.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should return next with an error', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.next.callCount.should.equal(1)
|
||||
return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
|
||||
})
|
||||
|
||||
it('should get the user from storage', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.UserGetter.getUser.callCount.should.equal(1)
|
||||
return this.UserGetter.getUser
|
||||
.calledWith('some_object_id', { email: 1 })
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should not try to authenticate the user with the password', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.SudoModeHandler.authenticate.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
it('should not activate sudo mode', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
|
||||
})
|
||||
|
||||
it('should not send back a json response', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.res.json.callCount.should.equal(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when authentication fails', function() {
|
||||
beforeEach(function() {
|
||||
this.SudoModeHandler.authenticate = sinon
|
||||
.stub()
|
||||
.callsArgWith(2, null, null)
|
||||
this.res.json = sinon.stub()
|
||||
return (this.req.i18n = { translate: sinon.stub() })
|
||||
})
|
||||
|
||||
it('should send back a failure message', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.res.json.callCount.should.equal(1)
|
||||
expect(this.res.json.lastCall.args[0]).to.have.keys(['message'])
|
||||
expect(this.res.json.lastCall.args[0].message).to.have.keys([
|
||||
'text',
|
||||
'type'
|
||||
])
|
||||
this.req.i18n.translate.callCount.should.equal(1)
|
||||
return this.req.i18n.translate.calledWith('invalid_password')
|
||||
})
|
||||
|
||||
it('should get the user from storage', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.UserGetter.getUser.callCount.should.equal(1)
|
||||
return this.UserGetter.getUser
|
||||
.calledWith('some_object_id', { email: 1 })
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should try to authenticate the user with the password', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.SudoModeHandler.authenticate.callCount.should.equal(1)
|
||||
return this.SudoModeHandler.authenticate
|
||||
.calledWith(this.user.email, this.password)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should not activate sudo mode', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when authentication produces an error', function() {
|
||||
beforeEach(function() {
|
||||
this.SudoModeHandler.authenticate = sinon
|
||||
.stub()
|
||||
.callsArgWith(2, new Error('woops'))
|
||||
return (this.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should return next with an error', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.next.callCount.should.equal(1)
|
||||
return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
|
||||
})
|
||||
|
||||
it('should get the user from storage', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.UserGetter.getUser.callCount.should.equal(1)
|
||||
return this.UserGetter.getUser
|
||||
.calledWith('some_object_id', { email: 1 })
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should try to authenticate the user with the password', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.SudoModeHandler.authenticate.callCount.should.equal(1)
|
||||
return this.SudoModeHandler.authenticate
|
||||
.calledWith(this.user.email, this.password)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should not activate sudo mode', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
return this.SudoModeHandler.activateSudoMode.callCount.should.equal(0)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when sudo mode activation produces an error', function() {
|
||||
beforeEach(function() {
|
||||
this.SudoModeHandler.activateSudoMode = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, new Error('woops'))
|
||||
return (this.next = sinon.stub())
|
||||
})
|
||||
|
||||
it('should return next with an error', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.next.callCount.should.equal(1)
|
||||
return expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
|
||||
})
|
||||
|
||||
it('should get the user from storage', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.UserGetter.getUser.callCount.should.equal(1)
|
||||
return this.UserGetter.getUser
|
||||
.calledWith('some_object_id', { email: 1 })
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should try to authenticate the user with the password', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.SudoModeHandler.authenticate.callCount.should.equal(1)
|
||||
return this.SudoModeHandler.authenticate
|
||||
.calledWith(this.user.email, this.password)
|
||||
.should.equal(true)
|
||||
})
|
||||
|
||||
it('should have tried to activate sudo mode', function() {
|
||||
this.SudoModeController.submitPassword(this.req, this.res, this.next)
|
||||
this.SudoModeHandler.activateSudoMode.callCount.should.equal(1)
|
||||
return this.SudoModeHandler.activateSudoMode
|
||||
.calledWith(this.user._id)
|
||||
.should.equal(true)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,329 +0,0 @@
|
||||
/* eslint-disable
|
||||
handle-callback-err,
|
||||
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 SandboxedModule = require('sandboxed-module')
|
||||
const assert = require('assert')
|
||||
require('chai').should()
|
||||
const { expect } = require('chai')
|
||||
const sinon = require('sinon')
|
||||
const modulePath = require('path').join(
|
||||
__dirname,
|
||||
'../../../../app/src/Features/SudoMode/SudoModeHandler'
|
||||
)
|
||||
|
||||
describe('SudoModeHandler', function() {
|
||||
beforeEach(function() {
|
||||
this.userId = 'some_user_id'
|
||||
this.email = 'someuser@example.com'
|
||||
this.user = {
|
||||
_id: this.userId,
|
||||
email: this.email
|
||||
}
|
||||
this.rclient = { get: sinon.stub(), set: sinon.stub(), del: sinon.stub() }
|
||||
this.RedisWrapper = { client: () => this.rclient }
|
||||
return (this.SudoModeHandler = SandboxedModule.require(modulePath, {
|
||||
globals: {
|
||||
console: console
|
||||
},
|
||||
requires: {
|
||||
'../../infrastructure/RedisWrapper': this.RedisWrapper,
|
||||
'logger-sharelatex': (this.logger = {
|
||||
log: sinon.stub(),
|
||||
err: sinon.stub()
|
||||
}),
|
||||
'../Authentication/AuthenticationManager': (this.AuthenticationManager = {}),
|
||||
'settings-sharelatex': (this.Settings = {}),
|
||||
'../V1/V1Handler': (this.V1Handler = { authWithV1: sinon.stub() }),
|
||||
'../User/UserGetter': (this.UserGetter = { getUser: sinon.stub() })
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
describe('_buildKey', function() {
|
||||
it('should build a properly formed key', function() {
|
||||
return expect(this.SudoModeHandler._buildKey('123')).to.equal(
|
||||
'SudoMode:{123}'
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('activateSudoMode', function() {
|
||||
beforeEach(function() {
|
||||
return (this.call = cb => {
|
||||
return this.SudoModeHandler.activateSudoMode(this.userId, cb)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when all goes well', function() {
|
||||
beforeEach(function() {
|
||||
return (this.rclient.set = sinon.stub().callsArgWith(4, null))
|
||||
})
|
||||
|
||||
it('should not produce an error', function(done) {
|
||||
return this.call(err => {
|
||||
expect(err).to.equal(null)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should set a value in redis', function(done) {
|
||||
return this.call(err => {
|
||||
expect(this.rclient.set.callCount).to.equal(1)
|
||||
expect(
|
||||
this.rclient.set.calledWith(
|
||||
'SudoMode:{some_user_id}',
|
||||
'1',
|
||||
'EX',
|
||||
60 * 60
|
||||
)
|
||||
).to.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when user id is not supplied', function() {
|
||||
beforeEach(function() {
|
||||
return (this.call = cb => {
|
||||
return this.SudoModeHandler.activateSudoMode(null, cb)
|
||||
})
|
||||
})
|
||||
|
||||
it('should produce an error', function(done) {
|
||||
return this.call(err => {
|
||||
expect(err).to.not.equal(null)
|
||||
expect(err).to.be.instanceof(Error)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not set value in redis', function(done) {
|
||||
return this.call(err => {
|
||||
expect(this.rclient.set.callCount).to.equal(0)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when rclient.set produces an error', function() {
|
||||
beforeEach(function() {
|
||||
return (this.rclient.set = sinon
|
||||
.stub()
|
||||
.callsArgWith(4, new Error('woops')))
|
||||
})
|
||||
|
||||
it('should produce an error', function(done) {
|
||||
return this.call(err => {
|
||||
expect(err).to.not.equal(null)
|
||||
expect(err).to.be.instanceof(Error)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('clearSudoMode', function() {
|
||||
beforeEach(function() {
|
||||
this.rclient.del = sinon.stub().callsArgWith(1, null)
|
||||
return (this.call = cb => {
|
||||
return this.SudoModeHandler.clearSudoMode(this.userId, cb)
|
||||
})
|
||||
})
|
||||
|
||||
it('should not produce an error', function(done) {
|
||||
return this.call(err => {
|
||||
expect(err).to.equal(null)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should delete key from redis', function(done) {
|
||||
return this.call(err => {
|
||||
expect(this.rclient.del.callCount).to.equal(1)
|
||||
expect(this.rclient.del.calledWith('SudoMode:{some_user_id}')).to.equal(
|
||||
true
|
||||
)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
describe('when rclient.del produces an error', function() {
|
||||
beforeEach(function() {
|
||||
return (this.rclient.del = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, new Error('woops')))
|
||||
})
|
||||
|
||||
it('should produce an error', function(done) {
|
||||
return this.call(err => {
|
||||
expect(err).to.not.equal(null)
|
||||
expect(err).to.be.instanceof(Error)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when user id is not supplied', function() {
|
||||
beforeEach(function() {
|
||||
return (this.call = cb => {
|
||||
return this.SudoModeHandler.clearSudoMode(null, cb)
|
||||
})
|
||||
})
|
||||
|
||||
it('should produce an error', function(done) {
|
||||
return this.call(err => {
|
||||
expect(err).to.not.equal(null)
|
||||
expect(err).to.be.instanceof(Error)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not delete value in redis', function(done) {
|
||||
return this.call(err => {
|
||||
expect(this.rclient.del.callCount).to.equal(0)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('authenticate', function() {
|
||||
beforeEach(function() {
|
||||
return (this.AuthenticationManager.authenticate = sinon
|
||||
.stub()
|
||||
.callsArgWith(2, null, this.user))
|
||||
})
|
||||
|
||||
it('should call AuthenticationManager.authenticate', function(done) {
|
||||
return this.SudoModeHandler.authenticate(
|
||||
this.email,
|
||||
'password',
|
||||
(err, user) => {
|
||||
expect(err).to.not.exist
|
||||
expect(user).to.exist
|
||||
expect(user).to.deep.equal(this.user)
|
||||
expect(this.AuthenticationManager.authenticate.callCount).to.equal(1)
|
||||
return done()
|
||||
}
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('isSudoModeActive', function() {
|
||||
beforeEach(function() {
|
||||
return (this.call = cb => {
|
||||
return this.SudoModeHandler.isSudoModeActive(this.userId, cb)
|
||||
})
|
||||
})
|
||||
|
||||
describe('when sudo-mode is active for that user', function() {
|
||||
beforeEach(function() {
|
||||
return (this.rclient.get = sinon.stub().callsArgWith(1, null, '1'))
|
||||
})
|
||||
|
||||
it('should not produce an error', function(done) {
|
||||
return this.call((err, isActive) => {
|
||||
expect(err).to.equal(null)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should get the value from redis', function(done) {
|
||||
return this.call((err, isActive) => {
|
||||
expect(this.rclient.get.callCount).to.equal(1)
|
||||
expect(
|
||||
this.rclient.get.calledWith('SudoMode:{some_user_id}')
|
||||
).to.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should produce a true result', function(done) {
|
||||
return this.call((err, isActive) => {
|
||||
expect(isActive).to.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when sudo-mode is not active for that user', function() {
|
||||
beforeEach(function() {
|
||||
return (this.rclient.get = sinon.stub().callsArgWith(1, null, null))
|
||||
})
|
||||
|
||||
it('should not produce an error', function(done) {
|
||||
return this.call((err, isActive) => {
|
||||
expect(err).to.equal(null)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should get the value from redis', function(done) {
|
||||
return this.call((err, isActive) => {
|
||||
expect(this.rclient.get.callCount).to.equal(1)
|
||||
expect(
|
||||
this.rclient.get.calledWith('SudoMode:{some_user_id}')
|
||||
).to.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should produce a false result', function(done) {
|
||||
return this.call((err, isActive) => {
|
||||
expect(isActive).to.equal(false)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when rclient.get produces an error', function() {
|
||||
beforeEach(function() {
|
||||
return (this.rclient.get = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, new Error('woops')))
|
||||
})
|
||||
|
||||
it('should produce an error', function(done) {
|
||||
return this.call((err, isActive) => {
|
||||
expect(err).to.not.equal(null)
|
||||
expect(err).to.be.instanceof(Error)
|
||||
expect(isActive).to.be.oneOf([null, undefined])
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when user id is not supplied', function() {
|
||||
beforeEach(function() {
|
||||
return (this.call = cb => {
|
||||
return this.SudoModeHandler.isSudoModeActive(null, cb)
|
||||
})
|
||||
})
|
||||
|
||||
it('should produce an error', function(done) {
|
||||
return this.call(err => {
|
||||
expect(err).to.not.equal(null)
|
||||
expect(err).to.be.instanceof(Error)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not get value in redis', function(done) {
|
||||
return this.call(err => {
|
||||
expect(this.rclient.get.callCount).to.equal(0)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -1,233 +0,0 @@
|
||||
/* eslint-disable
|
||||
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 SandboxedModule = require('sandboxed-module')
|
||||
const assert = require('assert')
|
||||
require('chai').should()
|
||||
const { expect } = require('chai')
|
||||
const sinon = require('sinon')
|
||||
const modulePath = require('path').join(
|
||||
__dirname,
|
||||
'../../../../app/src/Features/SudoMode/SudoModeMiddleware'
|
||||
)
|
||||
|
||||
describe('SudoModeMiddleware', function() {
|
||||
beforeEach(function() {
|
||||
this.userId = 'some_user_id'
|
||||
this.SudoModeHandler = { isSudoModeActive: sinon.stub() }
|
||||
this.AuthenticationController = {
|
||||
getLoggedInUserId: sinon.stub().returns(this.userId),
|
||||
setRedirectInSession: sinon.stub()
|
||||
}
|
||||
return (this.SudoModeMiddleware = SandboxedModule.require(modulePath, {
|
||||
globals: {
|
||||
console: console
|
||||
},
|
||||
requires: {
|
||||
'./SudoModeHandler': this.SudoModeHandler,
|
||||
'../Authentication/AuthenticationController': this
|
||||
.AuthenticationController,
|
||||
'logger-sharelatex': {
|
||||
log: sinon.stub(),
|
||||
warn: sinon.stub(),
|
||||
err: sinon.stub()
|
||||
},
|
||||
'settings-sharelatex': (this.Settings = {})
|
||||
}
|
||||
}))
|
||||
})
|
||||
|
||||
describe('protectPage', function() {
|
||||
beforeEach(function() {
|
||||
this.externalAuth = false
|
||||
return (this.call = cb => {
|
||||
this.req = {
|
||||
externalAuthenticationSystemUsed: sinon
|
||||
.stub()
|
||||
.returns(this.externalAuth)
|
||||
}
|
||||
this.res = { redirect: sinon.stub() }
|
||||
this.next = sinon.stub()
|
||||
this.SudoModeMiddleware.protectPage(this.req, this.res, this.next)
|
||||
return cb()
|
||||
})
|
||||
})
|
||||
|
||||
describe('when sudo mode is active', function() {
|
||||
beforeEach(function() {
|
||||
this.AuthenticationController.getLoggedInUserId = sinon
|
||||
.stub()
|
||||
.returns(this.userId)
|
||||
return (this.SudoModeHandler.isSudoModeActive = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, true))
|
||||
})
|
||||
|
||||
it('should get the current user id', function(done) {
|
||||
return this.call(() => {
|
||||
this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
|
||||
1
|
||||
)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should check if sudo-mode is active', function(done) {
|
||||
return this.call(() => {
|
||||
this.SudoModeHandler.isSudoModeActive.callCount.should.equal(1)
|
||||
this.SudoModeHandler.isSudoModeActive
|
||||
.calledWith(this.userId)
|
||||
.should.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should call next', function(done) {
|
||||
return this.call(() => {
|
||||
this.next.callCount.should.equal(1)
|
||||
expect(this.next.lastCall.args[0]).to.equal(undefined)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when sudo mode is not active', function() {
|
||||
beforeEach(function() {
|
||||
this.AuthenticationController.setRedirectInSession = sinon.stub()
|
||||
this.AuthenticationController.getLoggedInUserId = sinon
|
||||
.stub()
|
||||
.returns(this.userId)
|
||||
return (this.SudoModeHandler.isSudoModeActive = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, null, false))
|
||||
})
|
||||
|
||||
it('should get the current user id', function(done) {
|
||||
return this.call(() => {
|
||||
this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
|
||||
1
|
||||
)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should check if sudo-mode is active', function(done) {
|
||||
return this.call(() => {
|
||||
this.SudoModeHandler.isSudoModeActive.callCount.should.equal(1)
|
||||
this.SudoModeHandler.isSudoModeActive
|
||||
.calledWith(this.userId)
|
||||
.should.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should set redirect in session', function(done) {
|
||||
return this.call(() => {
|
||||
this.AuthenticationController.setRedirectInSession.callCount.should.equal(
|
||||
1
|
||||
)
|
||||
this.AuthenticationController.setRedirectInSession
|
||||
.calledWith(this.req)
|
||||
.should.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should redirect to the password-prompt page', function(done) {
|
||||
return this.call(() => {
|
||||
this.res.redirect.callCount.should.equal(1)
|
||||
this.res.redirect.calledWith('/confirm-password').should.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when isSudoModeActive produces an error', function() {
|
||||
beforeEach(function() {
|
||||
this.AuthenticationController.getLoggedInUserId = sinon
|
||||
.stub()
|
||||
.returns(this.userId)
|
||||
return (this.SudoModeHandler.isSudoModeActive = sinon
|
||||
.stub()
|
||||
.callsArgWith(1, new Error('woops')))
|
||||
})
|
||||
|
||||
it('should get the current user id', function(done) {
|
||||
return this.call(() => {
|
||||
this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
|
||||
1
|
||||
)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should check if sudo-mode is active', function(done) {
|
||||
return this.call(() => {
|
||||
this.SudoModeHandler.isSudoModeActive.callCount.should.equal(1)
|
||||
this.SudoModeHandler.isSudoModeActive
|
||||
.calledWith(this.userId)
|
||||
.should.equal(true)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should call next with an error', function(done) {
|
||||
return this.call(() => {
|
||||
this.next.callCount.should.equal(1)
|
||||
expect(this.next.lastCall.args[0]).to.be.instanceof(Error)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
describe('when external auth is being used', function() {
|
||||
beforeEach(function() {
|
||||
this.externalAuth = true
|
||||
return (this.call = cb => {
|
||||
this.req = {
|
||||
externalAuthenticationSystemUsed: sinon
|
||||
.stub()
|
||||
.returns(this.externalAuth)
|
||||
}
|
||||
this.res = { redirect: sinon.stub() }
|
||||
this.next = sinon.stub()
|
||||
this.SudoModeMiddleware.protectPage(this.req, this.res, this.next)
|
||||
return cb()
|
||||
})
|
||||
})
|
||||
|
||||
it('should immediately return next with no args', function(done) {
|
||||
return this.call(() => {
|
||||
this.next.callCount.should.equal(1)
|
||||
expect(this.next.lastCall.args[0]).to.not.exist
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not get the current user id', function(done) {
|
||||
return this.call(() => {
|
||||
this.AuthenticationController.getLoggedInUserId.callCount.should.equal(
|
||||
0
|
||||
)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
|
||||
it('should not check if sudo-mode is active', function(done) {
|
||||
return this.call(() => {
|
||||
this.SudoModeHandler.isSudoModeActive.callCount.should.equal(0)
|
||||
return done()
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -77,7 +77,6 @@ describe('UserController', function() {
|
||||
revokeAllUserSessions: sinon.stub().resolves()
|
||||
}
|
||||
}
|
||||
this.SudoModeHandler = { clearSudoMode: sinon.stub() }
|
||||
this.HttpErrorHandler = {
|
||||
badRequest: sinon.stub(),
|
||||
conflict: sinon.stub(),
|
||||
@@ -113,7 +112,6 @@ describe('UserController', function() {
|
||||
}),
|
||||
'./UserHandler': this.UserHandler,
|
||||
'./UserSessionsManager': this.UserSessionsManager,
|
||||
'../SudoMode/SudoModeHandler': this.SudoModeHandler,
|
||||
'../Errors/HttpErrorHandler': this.HttpErrorHandler,
|
||||
'settings-sharelatex': this.settings,
|
||||
'logger-sharelatex': (this.logger = {
|
||||
@@ -495,24 +493,8 @@ describe('UserController', function() {
|
||||
this.UserController.logout(this.req, this.res)
|
||||
})
|
||||
|
||||
it('should clear sudo-mode', function(done) {
|
||||
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
||||
this.SudoModeHandler.clearSudoMode = sinon.stub()
|
||||
this.res.redirect = url => {
|
||||
url.should.equal('/login')
|
||||
this.SudoModeHandler.clearSudoMode.callCount.should.equal(1)
|
||||
this.SudoModeHandler.clearSudoMode
|
||||
.calledWith(this.user._id)
|
||||
.should.equal(true)
|
||||
done()
|
||||
}
|
||||
|
||||
this.UserController.logout(this.req, this.res)
|
||||
})
|
||||
|
||||
it('should untrack session', function(done) {
|
||||
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
||||
this.SudoModeHandler.clearSudoMode = sinon.stub()
|
||||
this.res.redirect = url => {
|
||||
url.should.equal('/login')
|
||||
this.UserSessionsManager.untrackSession.callCount.should.equal(1)
|
||||
@@ -528,7 +510,6 @@ describe('UserController', function() {
|
||||
it('should redirect after logout', function(done) {
|
||||
this.req.body.redirect = '/institutional-login'
|
||||
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
||||
this.SudoModeHandler.clearSudoMode = sinon.stub()
|
||||
this.res.redirect = url => {
|
||||
url.should.equal(this.req.body.redirect)
|
||||
done()
|
||||
@@ -538,7 +519,6 @@ describe('UserController', function() {
|
||||
|
||||
it('should redirect to login after logout when no redirect set', function(done) {
|
||||
this.req.session.destroy = sinon.stub().callsArgWith(0)
|
||||
this.SudoModeHandler.clearSudoMode = sinon.stub()
|
||||
this.res.redirect = url => {
|
||||
url.should.equal('/login')
|
||||
done()
|
||||
|
||||
Reference in New Issue
Block a user