SafeJsonParseTest.js 1.7 KB

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