فهرست منبع

add unit tests for getProjectDocs

Brian Gough 9 سال پیش
والد
کامیت
f696ccb0d9

+ 8 - 4
services/document-updater/app/coffee/ProjectManager.coffee

@@ -58,7 +58,7 @@ module.exports = ProjectManager =
 				else
 				else
 					callback(null)
 					callback(null)
 
 
-	getProjectDocs: (project_id, projectStateHash, excludeVersions = {}, _callback = (error) ->) ->
+	getProjectDocs: (project_id, projectStateHash, excludeVersions = {}, _callback = (error, docs) ->) ->
 		timer = new Metrics.Timer("projectManager.getProjectDocs")
 		timer = new Metrics.Timer("projectManager.getProjectDocs")
 		callback = (args...) ->
 		callback = (args...) ->
 			timer.done()
 			timer.done()
@@ -78,12 +78,16 @@ module.exports = ProjectManager =
 						jobs.push (cb) ->
 						jobs.push (cb) ->
 							# check the doc version first
 							# check the doc version first
 							RedisManager.getDocVersion doc_id, (error, version) ->
 							RedisManager.getDocVersion doc_id, (error, version) ->
-								return cb(error) if error?
+								if error?
+									logger.error err: error, project_id: project_id, doc_id: doc_id, "error getting project doc version"
+									return cb(error)
 								# skip getting the doc if we already have that version
 								# skip getting the doc if we already have that version
-								return cb() if version is excludeVersions[doc_id]
+								return cb() if version? and version is excludeVersions[doc_id]
 								# otherwise get the doc lines from redis
 								# otherwise get the doc lines from redis
 								RedisManager.getDocLines doc_id, (error, lines) ->
 								RedisManager.getDocLines doc_id, (error, lines) ->
-									return cb(error) if error?
+									if error?
+										logger.error err: error, project_id: project_id, doc_id: doc_id, "error getting project doc lines"
+										return cb(error)
 									docs.push {_id: doc_id, lines: lines, v: version}
 									docs.push {_id: doc_id, lines: lines, v: version}
 									cb()
 									cb()
 				async.series jobs, (error) ->
 				async.series jobs, (error) ->

+ 59 - 1
services/document-updater/test/unit/coffee/HttpController/HttpControllerTests.coffee

@@ -12,7 +12,7 @@ describe "HttpController", ->
 			"./ProjectManager": @ProjectManager = {}
 			"./ProjectManager": @ProjectManager = {}
 			"logger-sharelatex" : @logger = { log: sinon.stub() }
 			"logger-sharelatex" : @logger = { log: sinon.stub() }
 			"./Metrics": @Metrics = {}
 			"./Metrics": @Metrics = {}
-
+			"./Errors" : Errors
 		@Metrics.Timer = class Timer
 		@Metrics.Timer = class Timer
 			done: sinon.stub()
 			done: sinon.stub()
 		@project_id = "project-id-123"
 		@project_id = "project-id-123"
@@ -434,3 +434,61 @@ describe "HttpController", ->
 				@next
 				@next
 					.calledWith(new Error("oops"))
 					.calledWith(new Error("oops"))
 					.should.equal true
 					.should.equal true
+
+	describe "getProjectDocs", ->
+		beforeEach ->
+			@state = "01234567890abcdef"
+			@docs = [{_id: "1234", lines: "hello", v: 23}, {_id: "4567", lines: "world", v: 45}]
+			@req =
+				params:
+					project_id: @project_id
+				query:
+					state: @state
+
+		describe "successfully", ->
+			beforeEach ->
+				@ProjectManager.getProjectDocs = sinon.stub().callsArgWith(3,null, @docs)
+				@HttpController.getProjectDocs(@req, @res, @next)
+
+			it "should get docs from the project manager", ->
+				@ProjectManager.getProjectDocs
+					.calledWith(@project_id, @state, {})
+					.should.equal true
+
+			it "should return a successful response", ->
+				@res.send
+					.calledWith(@docs)
+					.should.equal true
+
+			it "should log the request", ->
+				@logger.log
+					.calledWith({project_id: @project_id, exclude: []}, "getting docs via http")
+					.should.equal true
+
+			it "should log the response", ->
+				@logger.log
+					.calledWith({project_id: @project_id, result: ["1234:23", "4567:45"]}, "got docs via http")
+					.should.equal true
+
+			it "should time the request", ->
+				@Metrics.Timer::done.called.should.equal true
+
+		describe "when there is a conflict", ->
+			beforeEach ->
+				@ProjectManager.getProjectDocs = sinon.stub().callsArgWith(3, new Errors.ProjectStateChangedError("project state changed"))
+				@HttpController.getProjectDocs(@req, @res, @next)
+
+			it "should return an HTTP 409 Conflict response", ->
+				@res.send
+					.calledWith(409)
+					.should.equal true
+
+		describe "when an error occurs", ->
+			beforeEach ->
+				@ProjectManager.getProjectDocs = sinon.stub().callsArgWith(3, new Error("oops"))
+				@HttpController.getProjectDocs(@req, @res, @next)
+
+			it "should call next with the error", ->
+				@next
+					.calledWith(new Error("oops"))
+					.should.equal true