فهرست منبع

serialize saml log data

GitOrigin-RevId: b432b55c8dfa2990b2b96298e6c62b53a036a644
Ersun Warncke 6 سال پیش
والد
کامیت
4ed005b855

+ 5 - 1
services/web/app/src/Features/SamlLog/SamlLogHandler.js

@@ -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')

+ 2 - 1
services/web/app/src/models/SamlLog.js

@@ -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: '' }
   },

+ 90 - 0
services/web/test/unit/src/SamlLog/SamlLogHandlerTests.js

@@ -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'
+      )
+    })
+  })
+})