فهرست منبع

Add in guards on bad data from web API

James Allen 10 سال پیش
والد
کامیت
efab68e6b2

+ 4 - 0
services/document-updater/app/coffee/PersistenceManager.coffee

@@ -35,6 +35,10 @@ module.exports = PersistenceManager =
 					body = JSON.parse body
 				catch e
 					return callback(e)
+				if !body.lines?
+					return callback(new Error("web API response had no doc lines"))
+				if !body.version? or not body.version instanceof Number
+					return callback(new Error("web API response had no valid doc version"))
 				return callback null, body.lines, body.version
 			else if res.statusCode == 404
 				return callback(new Errors.NotFoundError("doc not not found: #{url}"))

+ 17 - 1
services/document-updater/test/unit/coffee/PersistenceManager/getDocTests.coffee

@@ -84,5 +84,21 @@ describe "PersistenceManager.getDoc", ->
 
 		it "should time the execution", ->
 			@Metrics.Timer::done.called.should.equal true
-		
+
+	describe "when request returns an doc without lines", ->
+		beforeEach ->
+			@request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(version: @version))
+			@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
+
+		it "should return and error", ->
+			@callback.calledWith(new Error("web API response had no doc lines")).should.equal true
+
+	describe "when request returns an doc without a version", ->
+		beforeEach ->
+			@request.callsArgWith(1, null, {statusCode: 200}, JSON.stringify(lines: @lines))
+			@PersistenceManager.getDoc(@project_id, @doc_id, @callback)
+
+		it "should return and error", ->
+			@callback.calledWith(new Error("web API response had no valid doc version")).should.equal true
+