Просмотр исходного кода

return 404 if doc is deleted and include_deleted is not set to true

Henry Oswald 11 лет назад
Родитель
Сommit
a0832c79e6

+ 0 - 1
services/docstore/app/coffee/DocManager.coffee

@@ -6,7 +6,6 @@ async = require "async"
 
 module.exports = DocManager =
 	getDoc: (project_id, doc_id, callback = (error, doc, mongoPath) ->) ->
-
 		MongoManager.findDoc doc_id, (err, docFromDocCollection)->
 			return callback(err) if err?
 			MongoManager.findProject project_id, (error, project) ->

+ 3 - 0
services/docstore/app/coffee/HttpController.coffee

@@ -5,12 +5,15 @@ module.exports = HttpController =
 	getDoc: (req, res, next = (error) ->) ->
 		project_id = req.params.project_id
 		doc_id     = req.params.doc_id
+		include_deleted = req.query?.include_deleted == "true"
 		logger.log project_id: project_id, doc_id: doc_id, "getting doc"
 		DocManager.getDoc project_id, doc_id, (error, doc) ->
 			return next(error) if error?
 			logger.log doc: doc, "got doc"
 			if !doc?
 				res.send 404
+			else if doc.deleted && !include_deleted
+				res.send 404
 			else
 				res.json HttpController._buildDocView(doc)
 

+ 7 - 3
services/docstore/test/acceptance/coffee/GettingDocsTests.coffee

@@ -21,14 +21,14 @@ describe "Getting a doc", ->
 
 	describe "when the doc exists", ->
 		it "should get the doc lines and version", (done) ->
-			DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
+			DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
 				doc.lines.should.deep.equal @lines
 				done()
 
 	describe "when the doc does not exist", ->
 		it "should return a 404", (done) ->
 			missing_doc_id = ObjectId()
-			DocstoreClient.getDoc @project_id, missing_doc_id, (error, res, doc) ->
+			DocstoreClient.getDoc @project_id, missing_doc_id, {}, (error, res, doc) ->
 				res.statusCode.should.equal 404
 				done()
 
@@ -38,9 +38,13 @@ describe "Getting a doc", ->
 			DocstoreClient.createDeletedDoc @project_id, @deleted_doc_id, @lines, done
 
 		it "should return the doc", (done) ->
-			DocstoreClient.getDoc @project_id, @deleted_doc_id, (error, res, doc) =>
+			DocstoreClient.getDoc @project_id, @deleted_doc_id, {include_deleted:true},(error, res, doc) =>
 				doc.lines.should.deep.equal @lines
 				doc.deleted.should.equal true
 				done()
 
+		it "should return a 404 when the query string is not set", (done)->
+			DocstoreClient.getDoc @project_id, @deleted_doc_id, {},(error, res, doc) =>
+				res.statusCode.should.equal 404
+				done()
 	

+ 5 - 5
services/docstore/test/acceptance/coffee/UpdatingDocsTests.coffee

@@ -29,7 +29,7 @@ describe "Applying updates to a doc", ->
 			@body.modified.should.equal true
 
 		it "should update the doc in the API", (done) ->
-			DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
+			DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
 				doc.lines.should.deep.equal @newLines
 				done()
 
@@ -42,7 +42,7 @@ describe "Applying updates to a doc", ->
 			@body.modified.should.equal false
 
 		it "should not update the doc in the API", (done) ->
-			DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
+			DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
 				doc.lines.should.deep.equal @originalLines
 				done()
 
@@ -74,7 +74,7 @@ describe "Applying updates to a doc", ->
 				@res.statusCode.should.equal 400
 
 			it "should not update the doc in the API", (done) ->
-				DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
+				DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
 					doc.lines.should.deep.equal @originalLines
 					done()
 
@@ -87,7 +87,7 @@ describe "Applying updates to a doc", ->
 				@res.statusCode.should.equal 400
 
 			it "should not update the doc in the API", (done) ->
-				DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
+				DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
 					doc.lines.should.deep.equal @originalLines
 					done()
 
@@ -102,7 +102,7 @@ describe "Applying updates to a doc", ->
 			@body.modified.should.equal true
 
 		it "should update the doc in the API", (done) ->
-			DocstoreClient.getDoc @project_id, @doc_id, (error, res, doc) =>
+			DocstoreClient.getDoc @project_id, @doc_id, {}, (error, res, doc) =>
 				doc.lines.should.deep.equal @largeLines
 				done()
 

+ 2 - 1
services/docstore/test/acceptance/coffee/helpers/DocstoreClient.coffee

@@ -31,10 +31,11 @@ module.exports = DocstoreClient =
 	deleteProject: (project_id, callback = (error, res, body) ->) ->
 		db.projects.remove _id: project_id, callback
 
-	getDoc: (project_id, doc_id, callback = (error, res, body) ->) ->
+	getDoc: (project_id, doc_id, qs, callback = (error, res, body) ->) ->
 		request.get {
 			url: "http://localhost:3016/project/#{project_id}/doc/#{doc_id}"
 			json: true
+			qs:qs
 		}, callback
 
 	getAllDocs: (project_id, callback = (error, res, body) ->) ->

+ 34 - 10
services/docstore/test/unit/coffee/HttpControllerTests.coffee

@@ -13,7 +13,7 @@ describe "HttpController", ->
 			"./DocManager": @DocManager = {}
 			"logger-sharelatex": @logger = { log: sinon.stub(), error: sinon.stub() }
 		@res = { send: sinon.stub(), json: sinon.stub(), setHeader:sinon.stub() }
-		@req = {}
+		@req = { query:{}}
 		@next = sinon.stub()
 		@project_id = "mock-project-id"
 		@doc_id = "mock-doc-id"
@@ -23,16 +23,22 @@ describe "HttpController", ->
 			version: 42
 			rev: 5
 		}
+		@deletedDoc = {
+			deleted:true
+			_id: @doc_id
+			lines: ["mock", "lines", " here", "", "", " spaces "]
+			version: 42
+			rev: 5
+		}
 
 	describe "getDoc", ->
-		beforeEach ->
-			@req.params =
-				project_id: @project_id
-				doc_id: @doc_id
-			@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc)
 
 		describe "without deleted docs", ->
 			beforeEach ->
+				@req.params =
+					project_id: @project_id
+					doc_id: @doc_id
+				@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @doc)
 				@HttpController.getDoc @req, @res, @next
 
 			it "should get the document (including deleted)", ->
@@ -50,13 +56,31 @@ describe "HttpController", ->
 					})
 					.should.equal true
 
-		describe "with deleted docs", ->
+		describe "which is deleted", ->
 			beforeEach ->
+				@req.params =
+					project_id: @project_id
+					doc_id: @doc_id
+				@DocManager.getDoc = sinon.stub().callsArgWith(2, null, @deletedDoc)
+
+			it "should get the doc from the doc manager", ->
 				@HttpController.getDoc @req, @res, @next
+				@DocManager.getDoc.calledWith(@project_id, @doc_id).should.equal true
 
-			it "should get the document (without deleted)", ->
-				@DocManager.getDoc
-					.calledWith(@project_id, @doc_id)
+			it "should return 404 if the query string delete is not set ", ->
+				@HttpController.getDoc @req, @res, @next
+				@res.send.calledWith(404).should.equal true
+
+			it "should return the doc as JSON if include_deleted is set to true", ->
+				@req.query.include_deleted = "true"
+				@HttpController.getDoc @req, @res, @next
+				@res.json
+					.calledWith({
+						_id: @doc_id
+						lines: @doc.lines
+						rev: @doc.rev
+						deleted: true
+					})
 					.should.equal true
 
 	describe "getRawDoc", ->