SamlLogHandlerTests.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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(
  29. {
  30. session: { saml: { universityId: providerId } },
  31. sessionID: sessionId,
  32. },
  33. data
  34. )
  35. })
  36. it('should log data', function () {
  37. samlLog.providerId.should.equal(providerId)
  38. samlLog.sessionId.should.equal(sessionId.substr(0, 8))
  39. samlLog.jsonData.should.equal(
  40. JSON.stringify({
  41. foo: true,
  42. samlSession: { universityId: 'provider-id' },
  43. })
  44. )
  45. expect(samlLog.data).to.be.undefined
  46. samlLog.save.should.have.been.calledOnce
  47. })
  48. })
  49. describe('when a json stringify error occurs', function () {
  50. beforeEach(function () {
  51. const circularRef = {}
  52. circularRef.circularRef = circularRef
  53. SamlLogHandler.log(
  54. {
  55. session: { saml: { universityId: providerId } },
  56. sessionID: sessionId,
  57. },
  58. circularRef
  59. )
  60. })
  61. it('should log without data and log error', function () {
  62. samlLog.providerId.should.equal(providerId)
  63. samlLog.sessionId.should.equal(sessionId.substr(0, 8))
  64. expect(samlLog.data).to.be.undefined
  65. expect(samlLog.jsonData).to.be.undefined
  66. samlLog.save.should.have.been.calledOnce
  67. this.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  68. { providerId, sessionId: sessionId.substr(0, 8) },
  69. 'SamlLog JSON.stringify Error'
  70. )
  71. })
  72. })
  73. describe('when logging error occurs', function () {
  74. beforeEach(function () {
  75. samlLog.save = sinon.stub().yields('error')
  76. SamlLogHandler.log(
  77. {
  78. session: { saml: { universityId: providerId } },
  79. sessionID: sessionId,
  80. },
  81. data
  82. )
  83. })
  84. it('should log error', function () {
  85. this.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  86. { err: 'error', providerId, sessionId: sessionId.substr(0, 8) },
  87. 'SamlLog Error'
  88. )
  89. })
  90. })
  91. })