diff --git a/services/web/app/coffee/Features/Documents/DocumentController.coffee b/services/web/app/coffee/Features/Documents/DocumentController.coffee index 3c47edabc4..9d147e8287 100644 --- a/services/web/app/coffee/Features/Documents/DocumentController.coffee +++ b/services/web/app/coffee/Features/Documents/DocumentController.coffee @@ -1,3 +1,4 @@ +ProjectGetter = require "../Project/ProjectGetter" ProjectLocator = require "../Project/ProjectLocator" ProjectEntityHandler = require "../Project/ProjectEntityHandler" ProjectEntityUpdateHandler = require "../Project/ProjectEntityUpdateHandler" @@ -9,25 +10,31 @@ module.exports = doc_id = req.params.doc_id plain = req?.query?.plain == 'true' logger.log doc_id:doc_id, project_id:project_id, "receiving get document request from api (docupdater)" - ProjectLocator.findElement {project_id: project_id, element_id: doc_id, type: 'doc'}, (error, doc, path) => - if error? - logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding element for getDocument" - return next(error) - ProjectEntityHandler.getDoc project_id, doc_id, (error, lines, rev, version, ranges) -> + ProjectGetter.getProject project_id, rootFolder: true, overleaf: true, (error, project) -> + return next(error) if error? + return res.sendStatus(404) if !project? + ProjectLocator.findElement {project: project, element_id: doc_id, type: 'doc'}, (error, doc, path) -> if error? - logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding doc contents for getDocument" + logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding element for getDocument" return next(error) - if plain - res.type "text/plain" - res.send lines.join('\n') - else - res.type "json" - res.send JSON.stringify { - lines: lines - version: version - ranges: ranges - pathname: path.fileSystem - } + ProjectEntityHandler.getDoc project_id, doc_id, (error, lines, rev, version, ranges) -> + if error? + logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding doc contents for getDocument" + return next(error) + if plain + res.type "text/plain" + res.send lines.join('\n') + + else + projectHistoryId = project?.overleaf?.history?.id + res.type "json" + res.send JSON.stringify { + lines: lines + version: version + ranges: ranges + pathname: path.fileSystem + projectHistoryId: projectHistoryId + } setDocument: (req, res, next = (error) ->) -> project_id = req.params.Project_id diff --git a/services/web/test/unit/coffee/Documents/DocumentControllerTests.coffee b/services/web/test/unit/coffee/Documents/DocumentControllerTests.coffee index c373cb93f8..6f4db7f8d5 100644 --- a/services/web/test/unit/coffee/Documents/DocumentControllerTests.coffee +++ b/services/web/test/unit/coffee/Documents/DocumentControllerTests.coffee @@ -15,6 +15,7 @@ describe "DocumentController", -> "logger-sharelatex": log:-> err:-> + "../Project/ProjectGetter": @ProjectGetter = {} "../Project/ProjectLocator": @ProjectLocator = {} "../Project/ProjectEntityHandler": @ProjectEntityHandler = {} "../Project/ProjectEntityUpdateHandler": @ProjectEntityUpdateHandler = {} @@ -35,39 +36,76 @@ describe "DocumentController", -> Project_id: @project_id doc_id: @doc_id - describe "when the document exists", -> + describe "when the project exists without project history enabled", -> + beforeEach -> + @project = _id: @project_id + @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project) + + describe "when the document exists", -> + beforeEach -> + @doc = _id: @doc_id + @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname) + @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges) + @DocumentController.getDocument(@req, @res, @next) + + it "should get the project", -> + @ProjectGetter.getProject + .calledWith(@project_id, rootFolder: true, overleaf: true) + .should.equal true + + it "should get the pathname of the document", -> + @ProjectLocator.findElement + .calledWith({project: @project, element_id: @doc_id, type: 'doc'}) + .should.equal true + + it "should get the document content", -> + @ProjectEntityHandler.getDoc + .calledWith(@project_id, @doc_id) + .should.equal true + + it "should return the document data to the client as JSON", -> + @res.type.should.equal "json" + @res.body.should.equal JSON.stringify + lines: @doc_lines + version: @version + ranges: @ranges + pathname: @pathname + + describe "when the document doesn't exist", -> + beforeEach -> + @ProjectLocator.findElement = sinon.stub().callsArgWith(1, new Errors.NotFoundError("not found")) + @DocumentController.getDocument(@req, @res, @next) + + it "should call next with the NotFoundError", -> + @next.calledWith(new Errors.NotFoundError("not found")) + .should.equal true + + describe "when project exists with project history enabled", -> beforeEach -> @doc = _id: @doc_id + @projectHistoryId = 1234 + @project = _id: @project_id, overleaf: history: id: @projectHistoryId + @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, @project) @ProjectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, fileSystem: @pathname) @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges) @DocumentController.getDocument(@req, @res, @next) - it "should get the pathname of the document", -> - @ProjectLocator.findElement - .calledWith({project_id: @project_id, element_id: @doc_id, type: 'doc'}) - .should.equal true - - it "should get the document content", -> - @ProjectEntityHandler.getDoc - .calledWith(@project_id, @doc_id) - .should.equal true - - it "should return the document data to the client as JSON", -> + it "should return the history id to the client as JSON", -> @res.type.should.equal "json" @res.body.should.equal JSON.stringify lines: @doc_lines version: @version ranges: @ranges pathname: @pathname + projectHistoryId: @projectHistoryId - describe "when the document doesn't exist", -> + describe "when the project does not exist", -> beforeEach -> - @ProjectLocator.findElement = sinon.stub().callsArgWith(1, new Errors.NotFoundError("not found")) + @ProjectGetter.getProject = sinon.stub().callsArgWith(2, null, null) @DocumentController.getDocument(@req, @res, @next) - it "should call next with the NotFoundError", -> - @next.calledWith(new Errors.NotFoundError("not found")) - .should.equal true + it "returns a 404", -> + @res.statusCode.should.equal 404 describe "setDocument", -> beforeEach ->