SamlLogHandlerTests.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. const APP_ROOT = '../../../../app/src'
  2. const SandboxedModule = require('sandboxed-module')
  3. const sinon = require('sinon')
  4. const { expect } = require('chai')
  5. const modulePath = `${APP_ROOT}/Features/SamlLog/SamlLogHandler`
  6. describe('SamlLogHandler', function () {
  7. let SamlLog, SamlLogHandler, SamlLogModel
  8. let data, providerId, samlLog, sessionId
  9. beforeEach(function () {
  10. samlLog = {
  11. save: sinon.stub(),
  12. }
  13. SamlLog = function () {
  14. return samlLog
  15. }
  16. SamlLogModel = { SamlLog }
  17. SamlLogHandler = SandboxedModule.require(modulePath, {
  18. requires: {
  19. '../../models/SamlLog': SamlLogModel,
  20. },
  21. })
  22. data = { foo: true }
  23. providerId = 'provider-id'
  24. sessionId = 'session-id'
  25. })
  26. describe('with valid data object', function () {
  27. beforeEach(function () {
  28. SamlLogHandler.log(providerId, sessionId, data)
  29. })
  30. it('should log data', function () {
  31. samlLog.providerId.should.equal(providerId)
  32. samlLog.sessionId.should.equal(sessionId.substr(0, 8))
  33. samlLog.jsonData.should.equal('{"foo":true}')
  34. expect(samlLog.data).to.be.undefined
  35. samlLog.save.should.have.been.calledOnce
  36. })
  37. })
  38. describe('when a json stringify error occurs', function () {
  39. beforeEach(function () {
  40. const circularRef = {}
  41. circularRef.circularRef = circularRef
  42. SamlLogHandler.log(providerId, sessionId, circularRef)
  43. })
  44. it('should log without data and log error', function () {
  45. samlLog.providerId.should.equal(providerId)
  46. samlLog.sessionId.should.equal(sessionId.substr(0, 8))
  47. expect(samlLog.data).to.be.undefined
  48. expect(samlLog.jsonData).to.be.undefined
  49. samlLog.save.should.have.been.calledOnce
  50. this.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  51. { providerId, sessionId: sessionId.substr(0, 8) },
  52. 'SamlLog JSON.stringify Error'
  53. )
  54. })
  55. })
  56. describe('when logging error occurs', function () {
  57. beforeEach(function () {
  58. samlLog.save = sinon.stub().yields('error')
  59. SamlLogHandler.log(providerId, sessionId, data)
  60. })
  61. it('should log error', function () {
  62. this.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  63. { err: 'error', providerId, sessionId: sessionId.substr(0, 8) },
  64. 'SamlLog Error'
  65. )
  66. })
  67. })
  68. })