2020-06-23 13:30:03 -04:00
|
|
|
/* eslint-disable
|
|
|
|
camelcase,
|
|
|
|
handle-callback-err,
|
|
|
|
no-return-assign,
|
|
|
|
no-useless-escape,
|
|
|
|
*/
|
|
|
|
// TODO: This file was created by bulk-decaffeinate.
|
|
|
|
// Fix any style issues and re-enable lint.
|
2020-06-23 13:29:59 -04:00
|
|
|
/*
|
|
|
|
* decaffeinate suggestions:
|
|
|
|
* DS102: Remove unnecessary code created because of implicit returns
|
|
|
|
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
|
|
|
|
*/
|
|
|
|
require('chai').should();
|
|
|
|
const {
|
|
|
|
expect
|
|
|
|
} = require("chai");
|
|
|
|
const SandboxedModule = require('sandboxed-module');
|
|
|
|
const modulePath = '../../../app/js/SafeJsonParse';
|
|
|
|
const sinon = require("sinon");
|
2015-12-01 06:05:49 -05:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
describe('SafeJsonParse', function() {
|
|
|
|
beforeEach(function() {
|
|
|
|
return this.SafeJsonParse = SandboxedModule.require(modulePath, { requires: {
|
|
|
|
"settings-sharelatex": (this.Settings = {
|
2020-03-24 04:14:15 -04:00
|
|
|
maxUpdateSize: 16 * 1024
|
2020-06-23 13:29:59 -04:00
|
|
|
}),
|
|
|
|
"logger-sharelatex": (this.logger = {error: sinon.stub()})
|
|
|
|
}
|
|
|
|
});});
|
2015-12-01 06:05:49 -05:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
return describe("parse", function() {
|
|
|
|
it("should parse documents correctly", function(done) {
|
2020-06-23 13:30:03 -04:00
|
|
|
return this.SafeJsonParse.parse('{"foo": "bar"}', (error, parsed) => {
|
2020-06-23 13:29:59 -04:00
|
|
|
expect(parsed).to.deep.equal({foo: "bar"});
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
});
|
2015-12-01 06:05:49 -05:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
it("should return an error on bad data", function(done) {
|
2020-06-23 13:30:03 -04:00
|
|
|
return this.SafeJsonParse.parse('blah', (error, parsed) => {
|
2020-06-23 13:29:59 -04:00
|
|
|
expect(error).to.exist;
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
});
|
2015-12-01 06:05:49 -05:00
|
|
|
|
2020-06-23 13:29:59 -04:00
|
|
|
return it("should return an error on oversized data", function(done) {
|
|
|
|
// we have a 2k overhead on top of max size
|
|
|
|
const big_blob = Array(16*1024).join("A");
|
|
|
|
const data = `{\"foo\": \"${big_blob}\"}`;
|
|
|
|
this.Settings.maxUpdateSize = 2 * 1024;
|
|
|
|
return this.SafeJsonParse.parse(data, (error, parsed) => {
|
|
|
|
this.logger.error.called.should.equal(true);
|
|
|
|
expect(error).to.exist;
|
|
|
|
return done();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|