From 83444b60cd3ceb091e42212663f41c79a5a26bfb Mon Sep 17 00:00:00 2001 From: Yossef Mendelssohn Date: Wed, 26 Jul 2017 13:14:12 -0400 Subject: [PATCH 1/3] test handling of `invalid_client` error code An error in OAuth deauthorization could return the error code of `invalid_client`, but that isn't handled by the code. That leads to a `TypeError` instead of a clean, understandable error. --- test/stripe/stripe_client_test.rb | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/stripe/stripe_client_test.rb b/test/stripe/stripe_client_test.rb index 4d9da93b..1caea2d4 100644 --- a/test/stripe/stripe_client_test.rb +++ b/test/stripe/stripe_client_test.rb @@ -368,6 +368,24 @@ module Stripe assert_equal('invalid_grant', e.code) assert_equal('This authorization code has already been used. All tokens issued with this code have been revoked.', e.message) end + + should "raise OAuth::InvalidClientError when error is a string with value 'invalid_client'" do + stub_request(:post, "#{Stripe.connect_base}/oauth/deauthorize"). + to_return(body: JSON.generate({ + error: "invalid_client", + error_description: "This application is not connected to stripe account acct_19tLK7DSlTMT26Mk, or that account does not exist.", + }), status: 401) + + client = StripeClient.new + opts = {api_base: Stripe.connect_base} + e = assert_raises Stripe::OAuth::InvalidClientError do + client.execute_request(:post, '/oauth/deauthorize', opts) + end + + assert_equal(401, e.http_status) + assert_equal('invalid_client', e.code) + assert_equal('This application is not connected to stripe account acct_19tLK7DSlTMT26Mk, or that account does not exist.', e.message) + end end context "idempotency keys" do From 9e1e755ccdb1b8f30c910a094f8b3a5a59d74084 Mon Sep 17 00:00:00 2001 From: Yossef Mendelssohn Date: Wed, 26 Jul 2017 13:16:12 -0400 Subject: [PATCH 2/3] add OAuth::InvalidClientError Taking the explanation from https://stripe.com/docs/connect/oauth-reference#post-deauthorize-error-codes and trying to fit it in with the comment style. --- lib/stripe/errors.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/stripe/errors.rb b/lib/stripe/errors.rb index c5a5e756..f689ac32 100644 --- a/lib/stripe/errors.rb +++ b/lib/stripe/errors.rb @@ -114,6 +114,12 @@ module Stripe end end + # InvalidClientError is raised when the client doesn't belong to you, or + # the API key mode (live or test) doesn't match the client mode. Or the + # stripe_user_id doesn't exist or isn't connected to your application. + class InvalidClientError < OAuthError + end + # InvalidGrantError is raised when a specified code doesn't exist, is # expired, has been used, or doesn't belong to you; a refresh token doesn't # exist, or doesn't belong to you; or if an API key's mode (live or test) From 21036261fcc7481719ad1c55b7731241402524b1 Mon Sep 17 00:00:00 2001 From: Yossef Mendelssohn Date: Wed, 26 Jul 2017 13:19:50 -0400 Subject: [PATCH 3/3] handle `invalid_client` error code from deauth Actually handle the error code and make the right error, instead of sending it through the `specific_api_error`. --- lib/stripe/stripe_client.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/stripe/stripe_client.rb b/lib/stripe/stripe_client.rb index 6df836f3..b8949bf0 100644 --- a/lib/stripe/stripe_client.rb +++ b/lib/stripe/stripe_client.rb @@ -312,6 +312,7 @@ module Stripe }] case error_code + when 'invalid_client' then OAuth::InvalidClientError.new(*args) when 'invalid_grant' then OAuth::InvalidGrantError.new(*args) when 'invalid_request' then OAuth::InvalidRequestError.new(*args) when 'invalid_scope' then OAuth::InvalidScopeError.new(*args)