SamlLogHandlerTests.js 2.7 KB

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