-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest_utils.py
190 lines (150 loc) · 5.25 KB
/
test_utils.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
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import pytest
import pathlib
from datetime import datetime, timedelta
from carto_auth.errors import CredentialsError
from carto_auth.utils import (
get_cache_filepath,
get_oauth_token_info,
get_m2m_token_info,
get_api_base_url,
load_cache_file,
save_cache_file,
is_token_expired,
)
HERE = pathlib.Path(__file__).parent
def test_get_oauth_token_info(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",
},
)
token_info = get_oauth_token_info(open_browser=False)
assert token_info["access_token"] == "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX"
assert token_info["expiration"] >= int(
(datetime.utcnow() + timedelta(seconds=86400)).timestamp()
)
# FIXME in CI
# mocker.patch(
# "webbrowser.open_new",
# return_value=True,
# )
# token_info = get_oauth_token_info(open_browser=True)
def test_get_oauth_token_info_error(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",
text="{",
)
with pytest.raises(CredentialsError):
get_oauth_token_info(open_browser=False)
requests_mock.post(
"https://auth.carto.com/oauth/token",
json={},
)
with pytest.raises(CredentialsError):
get_oauth_token_info(open_browser=False)
def test_get_m2m_token_info(requests_mock):
requests_mock.post(
"https://auth.carto.com/oauth/token",
json={
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX",
"scope": "",
"expires_in": 86400,
"token_type": "Bearer",
},
)
token_info = get_m2m_token_info("1234", "1234567890")
assert token_info["access_token"] == "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX"
assert token_info["expiration"] >= int(
(datetime.utcnow() + timedelta(seconds=86400)).timestamp()
)
def test_get_m2m_token_info_error(requests_mock):
requests_mock.post(
"https://auth.carto.com/oauth/token",
text="{",
)
with pytest.raises(CredentialsError):
get_m2m_token_info("1234", "1234567890")
requests_mock.post(
"https://auth.carto.com/oauth/token",
json={},
)
with pytest.raises(CredentialsError):
get_m2m_token_info("1234", "1234567890")
def test_get_api_base_url(requests_mock):
requests_mock.get(
"https://accounts.app.carto.com/accounts",
json={
"tenant_domain": "clausa.app.carto.com",
},
)
requests_mock.get(
"https://clausa.app.carto.com/config.yaml",
text="""
apis:
baseUrl: "https://gcp-us-east1.api.carto.com"
""",
)
api_base_url = get_api_base_url("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX")
assert api_base_url == "https://gcp-us-east1.api.carto.com"
def test_get_api_base_url_error(requests_mock):
requests_mock.get(
"https://accounts.app.carto.com/accounts",
text="wrong json",
)
with pytest.raises(CredentialsError):
get_api_base_url("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX")
requests_mock.get(
"https://accounts.app.carto.com/accounts",
json={
"tenant_domain": "clausa.app.carto.com",
},
)
requests_mock.get(
"https://clausa.app.carto.com/config.yaml",
text="wrong yaml",
)
with pytest.raises(CredentialsError):
get_api_base_url("eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX")
def test_get_cache_filepath():
filepath = get_cache_filepath("m2m")
assert str(filepath).endswith(".carto-auth/token_m2m.json")
def test_load_cache_file():
data = load_cache_file(HERE / "fixtures/token_ok.json")
assert data["api_base_url"] == "https://gcp-us-east1.api.carto.com"
assert data["access_token"] == "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX"
assert data["expiration"] == 1667471700
def test_load_cache_file_none():
data = load_cache_file(HERE / "fixtures/token_error.json")
assert data is None
data = load_cache_file(HERE / "__token__.json")
assert data is None
def test_save_cache_file(tmp_path):
data = {
"api_base_url": "https://gcp-us-east1.api.carto.com",
"access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX",
"expiration": 1667471700,
}
save_cache_file(tmp_path / "carto_token_saved.json", data)
data = load_cache_file(tmp_path / "carto_token_saved.json")
assert data["api_base_url"] == "https://gcp-us-east1.api.carto.com"
assert data["access_token"] == "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpX"
assert data["expiration"] == 1667471700
def test_is_token_expired():
now = datetime.utcnow()
assert is_token_expired(None) is True
assert is_token_expired(0) is True
assert is_token_expired(1) is True
assert is_token_expired((now - timedelta(seconds=10)).timestamp()) is True
assert is_token_expired((now + timedelta(seconds=10)).timestamp()) is False