SamlLogHandlerTests.js 2.5 KB

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