| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- const sinon = require('sinon')
- const chai = require('chai')
- const { expect } = chai
- const modulePath = '../../../../app/src/infrastructure/SessionStoreManager.js'
- const SandboxedModule = require('sandboxed-module')
- describe('SessionStoreManager', function() {
- beforeEach(function() {
- this.SessionStoreManager = SandboxedModule.require(modulePath, {
- globals: {
- console: console
- },
- requires: {
- 'metrics-sharelatex': (this.Metrics = { inc: sinon.stub() }),
- 'logger-sharelatex': (this.logger = {
- log: sinon.stub(),
- warn: sinon.stub(),
- error: sinon.stub(),
- err: sinon.stub()
- })
- }
- })
- this.sessionStore = {
- generate: sinon.spy(req => {
- req.session = {}
- req.session.destroy = sinon.stub().yields()
- })
- }
- })
- describe('enableValidationToken', function() {
- beforeEach(function() {
- this.originalGenerate = this.sessionStore.generate
- this.SessionStoreManager.enableValidationToken(this.sessionStore)
- })
- it('should set up a wrapper around the generate function', function() {
- expect(this.sessionStore.generate).to.not.equal(this.originalGenerate)
- })
- it('should add a validationToken when the generate function is called', function() {
- this.req = { sessionID: '123456789' }
- this.sessionStore.generate(this.req)
- expect(this.req.session.validationToken).to.equal('v1:6789')
- })
- it('should not allow the token to be overwritten', function() {
- this.req = { sessionID: '123456789' }
- this.sessionStore.generate(this.req)
- this.req.session.validationToken = 'try-to-overwrite-token'
- expect(this.req.session.validationToken).to.equal('v1:6789')
- })
- })
- describe('validationMiddleware', function() {
- this.beforeEach(function() {
- this.SessionStoreManager.enableValidationToken(this.sessionStore)
- this.req = { sessionID: '123456789' }
- this.next = sinon.stub()
- this.sessionStore.generate(this.req)
- })
- it('should accept the request when the session id matches the validation token', function() {
- this.SessionStoreManager.validationMiddleware(
- this.req,
- this.res,
- this.next
- )
- expect(this.next).to.be.calledWithExactly()
- })
- it('should destroy the session and return an error when the session id does not match the validation token', function() {
- this.req.sessionID = 'abcdefghijklmnopqrstuvwxyz'
- this.next = sinon.stub()
- this.SessionStoreManager.validationMiddleware(
- this.req,
- this.res,
- this.next
- )
- expect(this.req.session.destroy).to.be.called
- expect(this.next).to.be.calledWithExactly(
- sinon.match
- .instanceOf(Error)
- .and(sinon.match.has('message', 'invalid session'))
- )
- })
- it('should accept the request when the session does not have a validation token', function() {
- this.req = { sessionID: '123456789', session: {} }
- this.next = sinon.stub()
- this.SessionStoreManager.validationMiddleware(
- this.req,
- this.res,
- this.next
- )
- expect(this.next).to.be.calledWithExactly()
- })
- })
- describe('hasValidationToken', function() {
- this.beforeEach(function() {
- this.req = { sessionID: '123456789' }
- })
- it('should return true when the session is valid', function() {
- this.req.session = { validationToken: 'v1:6789' }
- const result = this.SessionStoreManager.hasValidationToken(this.req)
- expect(result).to.equal(true)
- })
- it('should return false when the session is valid', function() {
- this.req.session = { validationToken: 'v1:abcd' }
- const result = this.SessionStoreManager.hasValidationToken(this.req)
- expect(result).to.equal(true)
- })
- it('should return false when the validation token is missing', function() {
- this.req.session = {}
- const result = this.SessionStoreManager.hasValidationToken(this.req)
- expect(result).to.equal(false)
- })
- })
- })
|