From 7a64f040f196814bd39f3e3e5a7e6df3c52638c8 Mon Sep 17 00:00:00 2001 From: James Allen Date: Mon, 3 Apr 2017 16:04:54 +0100 Subject: [PATCH] Test and fix _mongoDocToS3Doc --- .../app/coffee/DocArchiveManager.coffee | 4 +- .../unit/coffee/DocArchiveManagerTests.coffee | 41 ++++++++++++++++++- 2 files changed, 43 insertions(+), 2 deletions(-) diff --git a/services/docstore/app/coffee/DocArchiveManager.coffee b/services/docstore/app/coffee/DocArchiveManager.coffee index 9c16d13d67..12f4b47c82 100644 --- a/services/docstore/app/coffee/DocArchiveManager.coffee +++ b/services/docstore/app/coffee/DocArchiveManager.coffee @@ -98,6 +98,8 @@ module.exports = DocArchive = return callback null, mongo_doc _mongoDocToS3Doc: (doc, callback = (error, s3_doc) ->) -> + if !doc.lines? + return callback(new Error("doc has no lines")) json = JSON.stringify({ lines: doc.lines ranges: doc.ranges @@ -105,7 +107,7 @@ module.exports = DocArchive = }) if json.indexOf("\u0000") != -1 error = new Error("null bytes detected") - logger.error {err: error, project_id, doc_id}, error.message + logger.err {err: error, doc, json}, error.message return callback(error) return callback null, json diff --git a/services/docstore/test/unit/coffee/DocArchiveManagerTests.coffee b/services/docstore/test/unit/coffee/DocArchiveManagerTests.coffee index 81ed38d177..2b9287d912 100644 --- a/services/docstore/test/unit/coffee/DocArchiveManagerTests.coffee +++ b/services/docstore/test/unit/coffee/DocArchiveManagerTests.coffee @@ -75,11 +75,13 @@ describe "DocArchiveManager", -> "logger-sharelatex": log:-> err:-> + @globals = + JSON: JSON @error = "my errror" @project_id = ObjectId().toString() @stubbedError = new Errors.NotFoundError("Error in S3 request") - @DocArchiveManager = SandboxedModule.require modulePath, requires: @requires + @DocArchiveManager = SandboxedModule.require modulePath, requires: @requires, globals: @globals describe "archiveDoc", -> @@ -253,4 +255,41 @@ describe "DocArchiveManager", -> }, (error, doc) -> expect(error).to.exist done() + + describe "_mongoDocToS3Doc", -> + describe "with a valid doc", -> + it "should return the json version", (done) -> + @DocArchiveManager._mongoDocToS3Doc doc = { + lines: ["doc", "lines"] + ranges: { "mock": "ranges" } + }, (err, s3_doc) -> + expect(s3_doc).to.equal JSON.stringify({ + lines: ["doc", "lines"] + ranges: { "mock": "ranges" } + schema_v: 1 + }) + done() + + describe "with null bytes in the result", -> + beforeEach -> + @_stringify = JSON.stringify + JSON.stringify = sinon.stub().returns '{"bad": "\u0000"}' + + afterEach -> + JSON.stringify = @_stringify + + it "should return an error", (done) -> + @DocArchiveManager._mongoDocToS3Doc { + lines: ["doc", "lines"] + ranges: { "mock": "ranges" } + }, (err, s3_doc) -> + expect(err).to.exist + done() + + describe "without doc lines", -> + it "should return an error", (done) -> + @DocArchiveManager._mongoDocToS3Doc {}, (err, s3_doc) -> + expect(err).to.exist + done() +