SamlLogHandlerTests.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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(async function () {
  27. await SamlLogHandler.promises.log(
  28. {
  29. session: { saml: { universityId: providerId } },
  30. sessionID: sessionId,
  31. path: '/saml/ukamf',
  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(async function () {
  51. const circularRef = {}
  52. circularRef.circularRef = circularRef
  53. await SamlLogHandler.promises.log(
  54. {
  55. session: { saml: { universityId: providerId } },
  56. sessionID: sessionId,
  57. path: '/saml/ukamf',
  58. },
  59. circularRef
  60. )
  61. })
  62. it('should log without data and log error', function () {
  63. samlLog.providerId.should.equal(providerId)
  64. samlLog.sessionId.should.equal(sessionId.substr(0, 8))
  65. expect(samlLog.data).to.be.undefined
  66. expect(samlLog.jsonData).to.be.undefined
  67. samlLog.save.should.have.been.calledOnce
  68. this.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  69. { providerId, sessionId: sessionId.substr(0, 8) },
  70. 'SamlLog JSON.stringify Error'
  71. )
  72. })
  73. })
  74. describe('when logging error occurs', function () {
  75. let err
  76. beforeEach(async function () {
  77. err = new Error()
  78. samlLog.save = sinon.stub().rejects(err)
  79. await SamlLogHandler.promises.log(
  80. {
  81. session: { saml: { universityId: providerId } },
  82. sessionID: sessionId,
  83. path: '/saml/ukamf',
  84. },
  85. data
  86. )
  87. })
  88. it('should log error', function () {
  89. this.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  90. {
  91. err,
  92. sessionId: sessionId.substr(0, 8),
  93. },
  94. 'SamlLog Error'
  95. )
  96. })
  97. })
  98. describe('with /saml/group-sso path', function () {
  99. let err
  100. beforeEach(async function () {
  101. err = new Error()
  102. samlLog.save = sinon.stub().rejects(err)
  103. await SamlLogHandler.promises.log(
  104. {
  105. session: { saml: { universityId: providerId } },
  106. sessionID: sessionId,
  107. path: '/saml/group-sso',
  108. },
  109. data
  110. )
  111. })
  112. it('should log error', function () {
  113. this.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  114. {
  115. err,
  116. sessionId: sessionId.substr(0, 8),
  117. },
  118. 'SamlLog Error'
  119. )
  120. })
  121. })
  122. describe('with a path not in the allow list', function () {
  123. let err
  124. beforeEach(async function () {
  125. err = new Error()
  126. samlLog.save = sinon.stub().rejects(err)
  127. await SamlLogHandler.promises.log(
  128. {
  129. session: { saml: { universityId: providerId } },
  130. sessionID: sessionId,
  131. path: '/unsupported',
  132. },
  133. data
  134. )
  135. })
  136. it('should not log any error', function () {
  137. this.logger.error.should.not.have.been.called
  138. })
  139. })
  140. })