From 4818ae813188ade11ec610e05b9c8aa1b778e588 Mon Sep 17 00:00:00 2001 From: James Allen Date: Thu, 5 Jun 2014 14:10:44 +0100 Subject: [PATCH] Use flag to only get deleted doc when appropriate --- .../docstore/app/coffee/DocManager.coffee | 18 ++++--- .../docstore/app/coffee/HttpController.coffee | 2 +- .../test/unit/coffee/DocManagerTests.coffee | 50 +++++++++---------- .../unit/coffee/HttpControllerTests.coffee | 6 +-- 4 files changed, 41 insertions(+), 35 deletions(-) diff --git a/services/docstore/app/coffee/DocManager.coffee b/services/docstore/app/coffee/DocManager.coffee index 14ab13bf02..d789554d1b 100644 --- a/services/docstore/app/coffee/DocManager.coffee +++ b/services/docstore/app/coffee/DocManager.coffee @@ -4,7 +4,11 @@ logger = require "logger-sharelatex" _ = require "underscore" module.exports = DocManager = - getDoc: (project_id, doc_id, callback = (error, doc, mongoPath) ->) -> + getDoc: (project_id, doc_id, options, callback = (error, doc, mongoPath) ->) -> + if typeof(options) == "function" + callback = options + options.include_deleted = false + MongoManager.findProject project_id, (error, project) -> return callback(error) if error? return callback new Errors.NotFoundError("No such project: #{project_id}") if !project? @@ -13,11 +17,13 @@ module.exports = DocManager = if doc? return callback null, doc, mongoPath else - # Perhaps it's a deleted doc - MongoManager.findDoc doc_id, (error, doc) -> - return callback(error) if error? - return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc? - return callback null, doc + if options.include_deleted + MongoManager.findDoc doc_id, (error, doc) -> + return callback(error) if error? + return callback new Errors.NotFoundError("No such doc: #{project_id}") if !doc? + return callback null, doc + else + return callback new Errors.NotFoundError("No such doc: #{project_id}") getAllDocs: (project_id, callback = (error, docs) ->) -> MongoManager.findProject project_id, (error, project) -> diff --git a/services/docstore/app/coffee/HttpController.coffee b/services/docstore/app/coffee/HttpController.coffee index 7a4976a1f3..f5b2d4896a 100644 --- a/services/docstore/app/coffee/HttpController.coffee +++ b/services/docstore/app/coffee/HttpController.coffee @@ -6,7 +6,7 @@ module.exports = HttpController = project_id = req.params.project_id doc_id = req.params.doc_id logger.log project_id: project_id, doc_id: doc_id, "getting doc" - DocManager.getDoc project_id, doc_id, (error, doc) -> + DocManager.getDoc project_id, doc_id, include_deleted: true, (error, doc) -> return next(error) if error? logger.log doc: doc, "got doc" if !doc? diff --git a/services/docstore/test/unit/coffee/DocManagerTests.coffee b/services/docstore/test/unit/coffee/DocManagerTests.coffee index 339d082c8a..0a296c4617 100644 --- a/services/docstore/test/unit/coffee/DocManagerTests.coffee +++ b/services/docstore/test/unit/coffee/DocManagerTests.coffee @@ -17,7 +17,7 @@ describe "DocManager", -> @callback = sinon.stub() describe "getDoc", -> - describe "when the project exists", -> + describe "when the project exists and the doc is in it", -> beforeEach -> @project = { name: "mock-project" } @doc = { _id: @doc_id, lines: ["mock-lines"] } @@ -53,27 +53,37 @@ describe "DocManager", -> .calledWith(new Errors.NotFoundError("No such project: #{@project_id}")) .should.equal true - describe "when the doc does not exist in the project tree", -> + describe "when the doc is deleted", -> beforeEach -> @MongoManager.findProject = sinon.stub().callsArgWith(1, null, @project) @DocManager.findDocInProject = sinon.stub().callsArgWith(2, null, null, null) @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, @doc) - @DocManager.getDoc @project_id, @doc_id, @callback - it "should try to find the doc in the project", -> - @DocManager.findDocInProject - .calledWith(@project, @doc_id) - .should.equal true + describe "when include_deleted = true", -> + beforeEach -> + @DocManager.getDoc @project_id, @doc_id, include_deleted: true, @callback - it "should try to find the doc in the docs collection", -> - @MongoManager.findDoc - .calledWith(@doc_id) - .should.equal true + it "should try to find the doc in the docs collection", -> + @MongoManager.findDoc + .calledWith(@doc_id) + .should.equal true - it "should return the doc", -> - @callback - .calledWith(null, @doc) - .should.equal true + it "should return the doc", -> + @callback + .calledWith(null, @doc) + .should.equal true + + describe "when include_deleted is not set", -> + beforeEach -> + @DocManager.getDoc @project_id, @doc_id, @callback + + it "should not try to find the doc in the docs collection", -> + @MongoManager.findDoc.called.should.equal false + + it "should return a NotFoundError", -> + @callback + .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}")) + .should.equal true describe "when the doc does not exist anywhere", -> beforeEach -> @@ -82,16 +92,6 @@ describe "DocManager", -> @MongoManager.findDoc = sinon.stub().callsArgWith(1, null, null) @DocManager.getDoc @project_id, @doc_id, @callback - it "should try to find the doc in the project", -> - @DocManager.findDocInProject - .calledWith(@project, @doc_id) - .should.equal true - - it "should try to find the doc in the docs collection", -> - @MongoManager.findDoc - .calledWith(@doc_id) - .should.equal true - it "should return a NotFoundError", -> @callback .calledWith(new Errors.NotFoundError("No such doc: #{@doc_id}")) diff --git a/services/docstore/test/unit/coffee/HttpControllerTests.coffee b/services/docstore/test/unit/coffee/HttpControllerTests.coffee index 4343d8120a..e34802c9de 100644 --- a/services/docstore/test/unit/coffee/HttpControllerTests.coffee +++ b/services/docstore/test/unit/coffee/HttpControllerTests.coffee @@ -29,12 +29,12 @@ describe "HttpController", -> @req.params = project_id: @project_id doc_id: @doc_id - @DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc) + @DocManager.getDoc = sinon.stub().callsArgWith(3, null, @doc) @HttpController.getDoc @req, @res, @next - it "should get the document", -> + it "should get the document (including deleted)", -> @DocManager.getDoc - .calledWith(@project_id, @doc_id) + .calledWith(@project_id, @doc_id, include_deleted: true) .should.equal true it "should return the doc as JSON", ->