| 1 |
import pytest |
| 2 |
|
| 3 |
from urllib.parse import parse_qs, urlparse |
| 4 |
|
| 5 |
import stripe |
| 6 |
from stripe._stripe_client import StripeClient |
| 7 |
|
| 8 |
|
| 9 |
class TestOAuth(object): |
| 10 |
@pytest.fixture |
| 11 |
def stripe_client(self, http_client_mock) -> StripeClient: |
| 12 |
return StripeClient( |
| 13 |
"sk_test_123", |
| 14 |
http_client=http_client_mock.get_mock_http_client(), |
| 15 |
client_id="ca_123", |
| 16 |
) |
| 17 |
|
| 18 |
def test_authorize_url(self, stripe_client: StripeClient): |
| 19 |
url = stripe_client.oauth.authorize_url( |
| 20 |
{ |
| 21 |
"scope": "read_write", |
| 22 |
"state": "csrf_token", |
| 23 |
"stripe_user": { |
| 24 |
"email": "[email protected]", |
| 25 |
"url": "https://example.com/profile/test", |
| 26 |
"country": "US", |
| 27 |
}, |
| 28 |
} |
| 29 |
) |
| 30 |
o = urlparse(url) |
| 31 |
params = parse_qs(o.query) |
| 32 |
|
| 33 |
url_express = stripe_client.oauth.authorize_url( |
| 34 |
{"scope": "read_write", "state": "csrf_token"}, {"express": True} |
| 35 |
) |
| 36 |
o_express = urlparse(url_express) |
| 37 |
|
| 38 |
assert o.scheme == "https" |
| 39 |
assert o.netloc == "connect.stripe.com" |
| 40 |
assert o.path == "/oauth/authorize" |
| 41 |
assert o_express.path == "/express/oauth/authorize" |
| 42 |
|
| 43 |
assert params["client_id"] == ["ca_123"] |
| 44 |
assert params["scope"] == ["read_write"] |
| 45 |
assert params["stripe_user[email]"] == ["[email protected]"] |
| 46 |
assert params["stripe_user[url]"] == [ |
| 47 |
"https://example.com/profile/test" |
| 48 |
] |
| 49 |
assert params["stripe_user[country]"] == ["US"] |
| 50 |
|
| 51 |
def test_token(self, stripe_client: StripeClient, http_client_mock): |
| 52 |
http_client_mock.stub_request( |
| 53 |
"post", |
| 54 |
path="/oauth/token", |
| 55 |
rbody='{"access_token":"sk_access_token","scope":"read_only","livemode":"false","token_type":"bearer","refresh_token":"sk_refresh_token","stripe_user_id":"acct_test","stripe_publishable_key":"pk_test"}', |
| 56 |
) |
| 57 |
|
| 58 |
resp = stripe_client.oauth.token( |
| 59 |
{ |
| 60 |
"grant_type": "authorization_code", |
| 61 |
"code": "this_is_an_authorization_code", |
| 62 |
} |
| 63 |
) |
| 64 |
http_client_mock.assert_requested( |
| 65 |
"post", |
| 66 |
api_base=stripe.connect_api_base, |
| 67 |
path="/oauth/token", |
| 68 |
post_data="grant_type=authorization_code&code=this_is_an_authorization_code", |
| 69 |
) |
| 70 |
assert resp["access_token"] == "sk_access_token" |
| 71 |
|
| 72 |
def test_deauthorize(self, stripe_client: StripeClient, http_client_mock): |
| 73 |
http_client_mock.stub_request( |
| 74 |
"post", |
| 75 |
path="/oauth/deauthorize", |
| 76 |
rbody='{"stripe_user_id":"acct_test_deauth"}', |
| 77 |
) |
| 78 |
|
| 79 |
resp = stripe_client.oauth.deauthorize( |
| 80 |
{ |
| 81 |
"stripe_user_id": "acct_test_deauth", |
| 82 |
} |
| 83 |
) |
| 84 |
http_client_mock.assert_requested( |
| 85 |
"post", |
| 86 |
api_base=stripe.connect_api_base, |
| 87 |
path="/oauth/deauthorize", |
| 88 |
post_data="client_id=ca_123&stripe_user_id=acct_test_deauth", |
| 89 |
) |
| 90 |
assert resp["stripe_user_id"] == "acct_test_deauth" |
| 91 |
|
| 92 |
def test_uses_client_connect_api_base(self, http_client_mock): |
| 93 |
http_client_mock.stub_request( |
| 94 |
"post", |
| 95 |
path="/oauth/token", |
| 96 |
rbody='{"access_token":"sk_access_token","scope":"read_only","livemode":"false","token_type":"bearer","refresh_token":"sk_refresh_token","stripe_user_id":"acct_test","stripe_publishable_key":"pk_test"}', |
| 97 |
) |
| 98 |
|
| 99 |
expected_api_base = "https://connect.example.com" |
| 100 |
client = stripe.StripeClient( |
| 101 |
"sk_test_123", |
| 102 |
base_addresses={"connect": expected_api_base}, |
| 103 |
http_client=http_client_mock.get_mock_http_client(), |
| 104 |
) |
| 105 |
|
| 106 |
resp = client.oauth.token( |
| 107 |
{ |
| 108 |
"grant_type": "authorization_code", |
| 109 |
"code": "ac_123456789", |
| 110 |
} |
| 111 |
) |
| 112 |
http_client_mock.assert_requested( |
| 113 |
"post", |
| 114 |
api_base=expected_api_base, |
| 115 |
path="/oauth/token", |
| 116 |
) |
| 117 |
assert resp["access_token"] == "sk_access_token" |