SamlLogHandler.test.mjs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. import { vi, expect } from 'vitest'
  2. import sinon from 'sinon'
  3. const modulePath = '../../../../app/src/Features/SamlLog/SamlLogHandler.mjs'
  4. describe('SamlLogHandler', function () {
  5. let SamlLog, SamlLogHandler
  6. let data, providerId, samlLog, sessionId
  7. beforeEach(async function (ctx) {
  8. samlLog = {
  9. save: sinon.stub(),
  10. }
  11. SamlLog = function () {
  12. return samlLog
  13. }
  14. ctx.logger = {
  15. error: sinon.stub(),
  16. }
  17. vi.doMock('@overleaf/logger', () => ({
  18. default: ctx.logger,
  19. }))
  20. vi.doMock('../../../../app/src/models/SamlLog', () => ({ SamlLog }))
  21. SamlLogHandler = (await import(modulePath)).default
  22. data = { foo: true }
  23. providerId = 'provider-id'
  24. sessionId = 'session-id'
  25. })
  26. describe('with valid data object', function () {
  27. beforeEach(async function () {
  28. await SamlLogHandler.promises.log(
  29. {
  30. session: { saml: { providerId } },
  31. sessionID: sessionId,
  32. path: '/saml/ukamf',
  33. },
  34. data
  35. )
  36. })
  37. it('should log data', function () {
  38. samlLog.providerId.should.equal(providerId)
  39. samlLog.sessionId.should.equal(sessionId.substr(0, 8))
  40. samlLog.jsonData.should.equal(
  41. JSON.stringify({
  42. foo: true,
  43. samlSession: { providerId: 'provider-id' },
  44. })
  45. )
  46. expect(samlLog.data).to.be.undefined
  47. samlLog.save.should.have.been.calledOnce
  48. })
  49. })
  50. describe('when a json stringify error occurs', function () {
  51. beforeEach(async function () {
  52. const circularRef = {}
  53. circularRef.circularRef = circularRef
  54. await SamlLogHandler.promises.log(
  55. {
  56. session: { saml: { providerId } },
  57. sessionID: sessionId,
  58. path: '/saml/ukamf',
  59. },
  60. circularRef
  61. )
  62. })
  63. it('should log without data and log error', function (ctx) {
  64. samlLog.providerId.should.equal(providerId)
  65. samlLog.sessionId.should.equal(sessionId.substr(0, 8))
  66. expect(samlLog.data).to.be.undefined
  67. expect(samlLog.jsonData).to.be.undefined
  68. samlLog.save.should.have.been.calledOnce
  69. ctx.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  70. { providerId, sessionId: sessionId.substr(0, 8) },
  71. 'SamlLog JSON.stringify Error'
  72. )
  73. })
  74. })
  75. describe('when logging error occurs', function () {
  76. let err
  77. beforeEach(async function () {
  78. err = new Error()
  79. samlLog.save = sinon.stub().rejects(err)
  80. await SamlLogHandler.promises.log(
  81. {
  82. session: { saml: { universityId: providerId } },
  83. sessionID: sessionId,
  84. path: '/saml/ukamf',
  85. },
  86. data
  87. )
  88. })
  89. it('should log error', function (ctx) {
  90. ctx.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  91. {
  92. err,
  93. sessionId: sessionId.substr(0, 8),
  94. },
  95. 'SamlLog Error'
  96. )
  97. })
  98. })
  99. describe('with /saml/group-sso path', function () {
  100. let err
  101. beforeEach(async function () {
  102. err = new Error()
  103. samlLog.save = sinon.stub().rejects(err)
  104. await SamlLogHandler.promises.log(
  105. {
  106. session: { saml: { universityId: providerId } },
  107. sessionID: sessionId,
  108. path: '/saml/group-sso',
  109. },
  110. data
  111. )
  112. })
  113. it('should log error', function (ctx) {
  114. ctx.logger.error.should.have.been.calledOnce.and.calledWithMatch(
  115. {
  116. err,
  117. sessionId: sessionId.substr(0, 8),
  118. },
  119. 'SamlLog Error'
  120. )
  121. })
  122. })
  123. describe('with a path not in the allow list', function () {
  124. let err
  125. beforeEach(async function () {
  126. err = new Error()
  127. samlLog.save = sinon.stub().rejects(err)
  128. await SamlLogHandler.promises.log(
  129. {
  130. session: { saml: { universityId: providerId } },
  131. sessionID: sessionId,
  132. path: '/unsupported',
  133. },
  134. data
  135. )
  136. })
  137. it('should not log any error', function (ctx) {
  138. ctx.logger.error.should.not.have.been.called
  139. })
  140. })
  141. })