From 08699d7aa265ad0622a0ef82cb00ec0ac892aeba Mon Sep 17 00:00:00 2001 From: James Allen Date: Tue, 28 Mar 2017 15:46:58 +0100 Subject: [PATCH] Handle a 404 from Recurly if account doesn't exist --- .../Subscription/RecurlyWrapper.coffee | 6 +++- .../Subscription/RecurlyWrapperTests.coffee | 32 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) diff --git a/services/web/app/coffee/Features/Subscription/RecurlyWrapper.coffee b/services/web/app/coffee/Features/Subscription/RecurlyWrapper.coffee index ce40847a27..fddd4f3567 100644 --- a/services/web/app/coffee/Features/Subscription/RecurlyWrapper.coffee +++ b/services/web/app/coffee/Features/Subscription/RecurlyWrapper.coffee @@ -475,9 +475,13 @@ module.exports = RecurlyWrapper = url: "accounts/#{account_id}/subscriptions" qs: state: "active" + expect404: true }, (error, response, body) -> return callback(error) if error? - RecurlyWrapper._parseSubscriptionsXml body, callback + if response.statusCode == 404 + return callback null, [] + else + RecurlyWrapper._parseSubscriptionsXml body, callback _parseSubscriptionsXml: (xml, callback) -> RecurlyWrapper._parseXmlAndGetAttribute xml, "subscriptions", callback diff --git a/services/web/test/UnitTests/coffee/Subscription/RecurlyWrapperTests.coffee b/services/web/test/UnitTests/coffee/Subscription/RecurlyWrapperTests.coffee index c55efdb3c5..d951653310 100644 --- a/services/web/test/UnitTests/coffee/Subscription/RecurlyWrapperTests.coffee +++ b/services/web/test/UnitTests/coffee/Subscription/RecurlyWrapperTests.coffee @@ -1030,3 +1030,35 @@ describe "RecurlyWrapper", -> @call (err, result) => expect(err).to.be.instanceof Error done() + + describe "listAccountActiveSubscriptions", -> + beforeEach -> + @user_id = "mock-user-id" + @callback = sinon.stub() + @RecurlyWrapper.apiRequest = sinon.stub().yields(null, @response = {"mock": "response"}, @body = "") + @RecurlyWrapper._parseSubscriptionsXml = sinon.stub().yields(null, @subscriptions = ["mock", "subscriptions"]) + + describe "with an account", -> + beforeEach -> + @RecurlyWrapper.listAccountActiveSubscriptions @user_id, @callback + + it "should send a request to Recurly", -> + @RecurlyWrapper.apiRequest + .calledWith({ + url: "accounts/#{@user_id}/subscriptions" + qs: + state: "active" + expect404: true + }) + .should.equal true + + it "should return the subscriptions", -> + @callback.calledWith(null, @subscriptions).should.equal true + + describe "without an account", -> + beforeEach -> + @response.statusCode = 404 + @RecurlyWrapper.listAccountActiveSubscriptions @user_id, @callback + + it "should return an empty array of subscriptions", -> + @callback.calledWith(null, []).should.equal true \ No newline at end of file