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

Merge pull request #302 from sharelatex/msw-history-failure

Bail out if history API request fails
Alasdair Smith 8 лет назад
Родитель
Сommit
e3811d33e9

+ 8 - 3
services/web/app/coffee/Features/History/HistoryController.coffee

@@ -46,9 +46,14 @@ module.exports = HistoryController =
 				"X-User-Id": user_id
 				"X-User-Id": user_id
 		}, (error, response, body) ->
 		}, (error, response, body) ->
 			return next(error) if error?
 			return next(error) if error?
-			HistoryManager.injectUserDetails body, (error, data) ->
-				return next(error) if error?
-				res.json data
+			if 200 <= response.statusCode < 300
+				HistoryManager.injectUserDetails body, (error, data) ->
+					return next(error) if error?
+					res.json data
+			else
+				error = new Error("history api responded with non-success code: #{response.statusCode}")
+				logger.error err: error, user_id: user_id, "error proxying request to history api"
+				next(error)
 
 
 	buildHistoryServiceUrl: (useProjectHistory) ->
 	buildHistoryServiceUrl: (useProjectHistory) ->
 		# choose a history service, either document-level (trackchanges)
 		# choose a history service, either document-level (trackchanges)

+ 18 - 1
services/web/test/unit/coffee/History/HistoryControllerTests.coffee

@@ -122,7 +122,7 @@ describe "HistoryController", ->
 			@res =
 			@res =
 				json: sinon.stub()
 				json: sinon.stub()
 			@next = sinon.stub()
 			@next = sinon.stub()
-			@request.yields(null, {}, @data = "mock-data")
+			@request.yields(null, {statusCode: 200}, @data = "mock-data")
 			@HistoryManager.injectUserDetails = sinon.stub().yields(null, @data_with_users = "mock-injected-data")
 			@HistoryManager.injectUserDetails = sinon.stub().yields(null, @data_with_users = "mock-injected-data")
 
 
 		describe "for a project with the project history flag", ->
 		describe "for a project with the project history flag", ->
@@ -182,3 +182,20 @@ describe "HistoryController", ->
 
 
 			it "should return the data with users to the client", ->
 			it "should return the data with users to the client", ->
 				@res.json.calledWith(@data_with_users).should.equal true
 				@res.json.calledWith(@data_with_users).should.equal true
+
+	describe "proxyToHistoryApiAndInjectUserDetails (with the history API failing)", ->
+		beforeEach ->
+			@req = { url: "/mock/url", method: "POST", useProjectHistory: true }
+			@res = { json: sinon.stub() }
+			@next = sinon.stub()
+			@request.yields(null, {statusCode: 500}, @data = "mock-data")
+			@HistoryManager.injectUserDetails = sinon.stub().yields(null, @data_with_users = "mock-injected-data")
+			@HistoryController.proxyToHistoryApiAndInjectUserDetails @req, @res, @next
+
+		it "should not inject the user data", ->
+			@HistoryManager.injectUserDetails
+				.calledWith(@data)
+				.should.equal false
+
+		it "should not return the data with users to the client", ->
+			@res.json.calledWith(@data_with_users).should.equal false