SafeJsonParseTest.js 1.7 KB

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