| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233 |
- /* 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()
- })
- })
- })
- })
- })
|