mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
Use flag to only get deleted doc when appropriate
This commit is contained in:
parent
7870b4fb55
commit
4818ae8131
4 changed files with 41 additions and 35 deletions
|
@ -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) ->
|
||||
|
|
|
@ -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?
|
||||
|
|
|
@ -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}"))
|
||||
|
|
|
@ -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", ->
|
||||
|
|
Loading…
Reference in a new issue