Files
overleaf-cep/services/web/test/unit/src/SamlLog/SamlLogHandlerTests.js
Miguel Serrano f4454cfe7e [web] Make SamlLogHandler.log() calls asynchronous (#17207)
* [web] Refactor exports in ErrorController

* [web] Make SamlLogHandler.log() async

* [web] await for SamlLogHandler.log() in ErrorController

* [web] await for SamlLogHandler.log() in SAMLMiddleware

* [web] await for SamlLogHandler.log() async controllers

* [web] await for SamlLogHandler.log() in SAMLManager

* [web] Remove explicit wait when testing SAML logs

After making the logs asynchronouse the wait
is no longer needed

* [web] Avoid using async with SamlLogHandler.log on callbacks

* Add expressifyErrorHandler to promise-utils

* Tighten assertion in SAMLMiddlewareTests

Co-authored-by: Jakob Ackermann <jakob.ackermann@overleaf.com>

* Updated SamlLogHandler.log to await for promise

---------

Co-authored-by: Jakob Ackermann <jakob.ackermann@overleaf.com>
GitOrigin-RevId: 3645923fae8096a9ba25dc9087f1a36231528569
2024-02-23 09:03:14 +00:00

110 lines
2.8 KiB
JavaScript

const SandboxedModule = require('sandboxed-module')
const sinon = require('sinon')
const { expect } = require('chai')
const modulePath = '../../../../app/src/Features/SamlLog/SamlLogHandler'
describe('SamlLogHandler', function () {
let SamlLog, SamlLogHandler, SamlLogModel
let data, providerId, samlLog, sessionId
beforeEach(function () {
samlLog = {
save: sinon.stub(),
}
SamlLog = function () {
return samlLog
}
SamlLogModel = { SamlLog }
SamlLogHandler = SandboxedModule.require(modulePath, {
requires: {
'../../models/SamlLog': SamlLogModel,
},
})
data = { foo: true }
providerId = 'provider-id'
sessionId = 'session-id'
})
describe('with valid data object', function () {
beforeEach(async function () {
await SamlLogHandler.promises.log(
{
session: { saml: { universityId: providerId } },
sessionID: sessionId,
},
data
)
})
it('should log data', function () {
samlLog.providerId.should.equal(providerId)
samlLog.sessionId.should.equal(sessionId.substr(0, 8))
samlLog.jsonData.should.equal(
JSON.stringify({
foo: true,
samlSession: { universityId: 'provider-id' },
})
)
expect(samlLog.data).to.be.undefined
samlLog.save.should.have.been.calledOnce
})
})
describe('when a json stringify error occurs', function () {
beforeEach(async function () {
const circularRef = {}
circularRef.circularRef = circularRef
await SamlLogHandler.promises.log(
{
session: { saml: { universityId: providerId } },
sessionID: sessionId,
},
circularRef
)
})
it('should log without data and log error', function () {
samlLog.providerId.should.equal(providerId)
samlLog.sessionId.should.equal(sessionId.substr(0, 8))
expect(samlLog.data).to.be.undefined
expect(samlLog.jsonData).to.be.undefined
samlLog.save.should.have.been.calledOnce
this.logger.error.should.have.been.calledOnce.and.calledWithMatch(
{ providerId, sessionId: sessionId.substr(0, 8) },
'SamlLog JSON.stringify Error'
)
})
})
describe('when logging error occurs', function () {
let err
beforeEach(async function () {
err = new Error()
samlLog.save = sinon.stub().rejects(err)
await SamlLogHandler.promises.log(
{
session: { saml: { universityId: providerId } },
sessionID: sessionId,
},
data
)
})
it('should log error', function () {
this.logger.error.should.have.been.calledOnce.and.calledWithMatch(
{
err,
sessionId: sessionId.substr(0, 8),
},
'SamlLog Error'
)
})
})
})