SafeJsonParseTest.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. /* eslint-disable
  2. camelcase,
  3. handle-callback-err,
  4. no-return-assign,
  5. no-useless-escape,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  13. */
  14. require('chai').should()
  15. const { expect } = require('chai')
  16. const SandboxedModule = require('sandboxed-module')
  17. const modulePath = '../../../app/js/SafeJsonParse'
  18. const sinon = require('sinon')
  19. describe('SafeJsonParse', function () {
  20. beforeEach(function () {
  21. return (this.SafeJsonParse = SandboxedModule.require(modulePath, {
  22. requires: {
  23. 'settings-sharelatex': (this.Settings = {
  24. maxUpdateSize: 16 * 1024
  25. }),
  26. 'logger-sharelatex': (this.logger = { error: sinon.stub() })
  27. }
  28. }))
  29. })
  30. return describe('parse', function () {
  31. it('should parse documents correctly', function (done) {
  32. return this.SafeJsonParse.parse('{"foo": "bar"}', (error, parsed) => {
  33. expect(parsed).to.deep.equal({ foo: 'bar' })
  34. return done()
  35. })
  36. })
  37. it('should return an error on bad data', function (done) {
  38. return this.SafeJsonParse.parse('blah', (error, parsed) => {
  39. expect(error).to.exist
  40. return done()
  41. })
  42. })
  43. return it('should return an error on oversized data', function (done) {
  44. // we have a 2k overhead on top of max size
  45. const big_blob = Array(16 * 1024).join('A')
  46. const data = `{\"foo\": \"${big_blob}\"}`
  47. this.Settings.maxUpdateSize = 2 * 1024
  48. return this.SafeJsonParse.parse(data, (error, parsed) => {
  49. this.logger.error.called.should.equal(true)
  50. expect(error).to.exist
  51. return done()
  52. })
  53. })
  54. })
  55. })