-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_pkce.py
60 lines (49 loc) · 1.9 KB
/
test_pkce.py
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
from urllib.parse import urlparse, parse_qs
from carto_auth.pkce import CartoPKCE
def test_url_properly_created():
carto_pkce = CartoPKCE(open_browser=False, org="org_123")
url = carto_pkce.get_authorize_url()
parsed_url = urlparse(url)
parsed_qs = parse_qs(parsed_url.query)
assert parsed_qs["code_challenge_method"] == ["S256"]
assert parsed_qs["redirect_uri"] == ["https://app.carto.com/auth/token"]
assert parsed_qs["response_type"] == ["code"]
assert parsed_qs["client_id"][0] == "0dxb8HR3ATXCxJiPOJVHsLoHoAtbRX6u"
assert len(parsed_qs["code_challenge"]) > 0
assert parsed_qs["organization"] == ["org_123"]
def test_token_from_input_prompt(mocker, requests_mock):
mocker.patch(
"carto_auth.pkce.CartoPKCE._input",
return_value="carto.com/autorize?code=abcde",
)
requests_mock.post(
"https://auth.carto.com/oauth/token",
json={
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX",
"scope": "read:tokens write:tokens read:imports write:imports "
"read:connections write:connections",
"expires_in": 86400,
"token_type": "Bearer",
},
)
carto_pkce = CartoPKCE(open_browser=False)
carto_pkce.get_pkce_handshake_parameters()
assert len(carto_pkce._code_challenge) > 0
assert len(carto_pkce._code_verifier) > 0
def test_token_from_input_mocked(mocker, requests_mock):
mocker.patch(
"carto_auth.pkce.CartoPKCE._input",
return_value="carto.com/autorize?code=abcde",
)
requests_mock.post(
"https://auth.carto.com/oauth/token",
json={
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX",
"scope": "",
"expires_in": 86400,
"token_type": "Bearer",
},
)
carto_pkce = CartoPKCE(open_browser=False)
code = carto_pkce.get_auth_response()
assert code == "abcde"