serialize saml log data

GitOrigin-RevId: b432b55c8dfa2990b2b96298e6c62b53a036a644
This commit is contained in:
Ersun Warncke 2019-12-03 08:34:18 -04:00 committed by sharelatex
parent 176604a938
commit 4ed005b855
3 changed files with 97 additions and 2 deletions

View file

@ -5,7 +5,11 @@ function log(providerId, sessionId, data) {
const samlLog = new SamlLog()
samlLog.providerId = (providerId || '').toString()
samlLog.sessionId = sessionId
samlLog.data = data
try {
samlLog.jsonData = JSON.stringify(data)
} catch (err) {
logger.error({ err, sessionId, providerId }, 'SamlLog JSON.stringify Error')
}
samlLog.save(err => {
if (err) {
logger.error({ err, sessionId, providerId }, 'SamlLog Error')

View file

@ -4,7 +4,8 @@ const { Schema } = mongoose
const SamlLogSchema = new Schema(
{
createdAt: { type: Date, default: () => new Date() },
data: { type: Object, default: {} },
data: { type: Object },
jsonData: { type: String },
providerId: { type: String, default: '' },
sessionId: { type: String, default: '' }
},

View file

@ -0,0 +1,90 @@
const APP_ROOT = '../../../../app/src'
const SandboxedModule = require('sandboxed-module')
const chai = require('chai')
const sinon = require('sinon')
const sinonChai = require('sinon-chai')
chai.use(sinonChai)
const { expect } = chai
const modulePath = `${APP_ROOT}/Features/SamlLog/SamlLogHandler`
describe('SamlLogHandler', function() {
let LoggerSharelatex, SamlLog, SamlLogHandler, SamlLogModel
let data, providerId, samlLog, sessionId
beforeEach(function() {
LoggerSharelatex = {
error: sinon.stub()
}
samlLog = {
save: sinon.stub()
}
SamlLog = function() {
return samlLog
}
SamlLogModel = { SamlLog }
SamlLogHandler = SandboxedModule.require(modulePath, {
globals: { console },
requires: {
'../../models/SamlLog': SamlLogModel,
'logger-sharelatex': LoggerSharelatex
}
})
data = { foo: true }
providerId = 'provider-id'
sessionId = 'session-id'
})
describe('with valid data object', function() {
beforeEach(function() {
SamlLogHandler.log(providerId, sessionId, data)
})
it('should log data', function() {
samlLog.providerId.should.equal(providerId)
samlLog.sessionId.should.equal(sessionId)
samlLog.jsonData.should.equal('{"foo":true}')
expect(samlLog.data).to.be.undefined
samlLog.save.should.have.been.calledOnce
})
})
describe('when a json stringify error occurs', function() {
beforeEach(function() {
const circularRef = {}
circularRef.circularRef = circularRef
SamlLogHandler.log(providerId, sessionId, circularRef)
})
it('should log without data and log error', function() {
samlLog.providerId.should.equal(providerId)
samlLog.sessionId.should.equal(sessionId)
expect(samlLog.data).to.be.undefined
expect(samlLog.jsonData).to.be.undefined
samlLog.save.should.have.been.calledOnce
LoggerSharelatex.error.should.have.been.calledOnce.and.calledWithMatch(
{ providerId, sessionId },
'SamlLog JSON.stringify Error'
)
})
})
describe('when logging error occurs', function() {
beforeEach(function() {
samlLog.save = sinon.stub().yields('error')
SamlLogHandler.log(providerId, sessionId, data)
})
it('should log error', function() {
LoggerSharelatex.error.should.have.been.calledOnce.and.calledWithMatch(
{ err: 'error', providerId, sessionId },
'SamlLog Error'
)
})
})
})