Test and fix _mongoDocToS3Doc

This commit is contained in:
James Allen 2017-04-03 16:04:54 +01:00
parent bf187f30ec
commit 7a64f040f1
2 changed files with 43 additions and 2 deletions

View file

@ -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

View file

@ -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()