File: test_oauth.py

package info (click to toggle)
python-stripe 13.2.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,476 kB
  • sloc: python: 187,843; makefile: 13; sh: 9
file content (117 lines) | stat: -rw-r--r-- 4,162 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import pytest

from urllib.parse import parse_qs, urlparse

import stripe
from stripe._stripe_client import StripeClient


class TestOAuth(object):
    @pytest.fixture
    def stripe_client(self, http_client_mock) -> StripeClient:
        return StripeClient(
            "sk_test_123",
            http_client=http_client_mock.get_mock_http_client(),
            client_id="ca_123",
        )

    def test_authorize_url(self, stripe_client: StripeClient):
        url = stripe_client.oauth.authorize_url(
            {
                "scope": "read_write",
                "state": "csrf_token",
                "stripe_user": {
                    "email": "test@example.com",
                    "url": "https://example.com/profile/test",
                    "country": "US",
                },
            }
        )
        o = urlparse(url)
        params = parse_qs(o.query)

        url_express = stripe_client.oauth.authorize_url(
            {"scope": "read_write", "state": "csrf_token"}, {"express": True}
        )
        o_express = urlparse(url_express)

        assert o.scheme == "https"
        assert o.netloc == "connect.stripe.com"
        assert o.path == "/oauth/authorize"
        assert o_express.path == "/express/oauth/authorize"

        assert params["client_id"] == ["ca_123"]
        assert params["scope"] == ["read_write"]
        assert params["stripe_user[email]"] == ["test@example.com"]
        assert params["stripe_user[url]"] == [
            "https://example.com/profile/test"
        ]
        assert params["stripe_user[country]"] == ["US"]

    def test_token(self, stripe_client: StripeClient, http_client_mock):
        http_client_mock.stub_request(
            "post",
            path="/oauth/token",
            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"}',
        )

        resp = stripe_client.oauth.token(
            {
                "grant_type": "authorization_code",
                "code": "this_is_an_authorization_code",
            }
        )
        http_client_mock.assert_requested(
            "post",
            api_base=stripe.connect_api_base,
            path="/oauth/token",
            post_data="grant_type=authorization_code&code=this_is_an_authorization_code",
        )
        assert resp["access_token"] == "sk_access_token"

    def test_deauthorize(self, stripe_client: StripeClient, http_client_mock):
        http_client_mock.stub_request(
            "post",
            path="/oauth/deauthorize",
            rbody='{"stripe_user_id":"acct_test_deauth"}',
        )

        resp = stripe_client.oauth.deauthorize(
            {
                "stripe_user_id": "acct_test_deauth",
            }
        )
        http_client_mock.assert_requested(
            "post",
            api_base=stripe.connect_api_base,
            path="/oauth/deauthorize",
            post_data="client_id=ca_123&stripe_user_id=acct_test_deauth",
        )
        assert resp["stripe_user_id"] == "acct_test_deauth"

    def test_uses_client_connect_api_base(self, http_client_mock):
        http_client_mock.stub_request(
            "post",
            path="/oauth/token",
            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"}',
        )

        expected_api_base = "https://connect.example.com"
        client = stripe.StripeClient(
            "sk_test_123",
            base_addresses={"connect": expected_api_base},
            http_client=http_client_mock.get_mock_http_client(),
        )

        resp = client.oauth.token(
            {
                "grant_type": "authorization_code",
                "code": "ac_123456789",
            }
        )
        http_client_mock.assert_requested(
            "post",
            api_base=expected_api_base,
            path="/oauth/token",
        )
        assert resp["access_token"] == "sk_access_token"