HttpErrorControllerTests.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const sinon = require('sinon')
  2. const { expect } = require('chai')
  3. const modulePath = '../../../../app/src/Features/Errors/HttpErrorController.js'
  4. const SandboxedModule = require('sandboxed-module')
  5. const MockResponse = require('../helpers/MockResponse')
  6. const MockRequest = require('../helpers/MockRequest')
  7. const Errors = require('../../../../app/src/Features/Errors/Errors')
  8. const HttpErrors = require('@overleaf/o-error/http')
  9. describe('HttpErrorController', function() {
  10. beforeEach(function() {
  11. this.req = new MockRequest()
  12. this.res = new MockResponse()
  13. this.AuthenticationController = {
  14. getLoggedInUserId: sinon.stub().returns(null)
  15. }
  16. this.logger = {
  17. warn: sinon.stub(),
  18. error: sinon.stub()
  19. }
  20. this.ErrorController = SandboxedModule.require(modulePath, {
  21. globals: { console },
  22. requires: {
  23. '../Authentication/AuthenticationController': this
  24. .AuthenticationController,
  25. 'logger-sharelatex': this.logger,
  26. './Errors': Errors,
  27. '@overleaf/o-error/http': HttpErrors
  28. }
  29. })
  30. })
  31. describe('handleError', function() {
  32. beforeEach(function() {})
  33. it('logs and return status code', function() {
  34. let error = new HttpErrors.UnprocessableEntityError()
  35. this.ErrorController.handleError(error, this.req, this.res)
  36. expect(this.res.statusCode).to.equal(422)
  37. sinon.assert.calledOnce(this.logger.warn)
  38. const { url, method, userId } = this.logger.warn.lastCall.args[0]
  39. expect(userId).to.not.be.defined
  40. expect(method).to.not.be.defined
  41. expect(url).to.not.be.defined
  42. })
  43. it('logs url method and userId', function() {
  44. let error = new HttpErrors.UnprocessableEntityError()
  45. this.AuthenticationController.getLoggedInUserId.returns('123abc')
  46. this.req.url = 'overleaf.url'
  47. this.req.method = 'GET'
  48. this.ErrorController.handleError(error, this.req, this.res)
  49. const { url, method, userId } = this.logger.warn.lastCall.args[0]
  50. expect(userId).to.equal('123abc')
  51. expect(method).to.equal('GET')
  52. expect(url).to.equal('overleaf.url')
  53. })
  54. it('logs and return status code when wrapped', function() {
  55. let cause = new Errors.SubscriptionAdminDeletionError()
  56. let error = new HttpErrors.UnprocessableEntityError({}).withCause(cause)
  57. this.ErrorController.handleError(error, this.req, this.res)
  58. expect(this.res.statusCode).to.equal(422)
  59. sinon.assert.calledOnce(this.logger.warn)
  60. })
  61. it('renders JSON with info', function() {
  62. let cause = new Errors.SubscriptionAdminDeletionError({
  63. info: {
  64. public: { some: 'data' }
  65. }
  66. })
  67. let error = new HttpErrors.UnprocessableEntityError({
  68. info: { public: { overwrite: 'data' } }
  69. }).withCause(cause)
  70. this.req.accepts = () => 'json'
  71. this.ErrorController.handleError(error, this.req, this.res)
  72. expect(this.res.statusCode).to.equal(422)
  73. expect(this.res.body).to.equal(
  74. JSON.stringify({
  75. overwrite: 'data'
  76. })
  77. )
  78. })
  79. it('renders HTML with info', function() {
  80. let cause = new Errors.SubscriptionAdminDeletionError()
  81. let error = new HttpErrors.UnprocessableEntityError({}).withCause(cause)
  82. this.req.accepts = () => 'html'
  83. this.ErrorController.handleError(error, this.req, this.res)
  84. expect(this.res.statusCode).to.equal(422)
  85. expect(this.res.renderedTemplate).to.equal('general/500')
  86. expect(this.res.renderedVariables).to.deep.equal({
  87. title: 'Client Error'
  88. })
  89. })
  90. })
  91. })