/
test_auth.py
245 lines (195 loc) · 6.89 KB
/
test_auth.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
import json
from pathlib import Path
import pytest
import requests
from .context import Auth
# pylint: disable=unused-import
from .fixtures import (
auth_mock,
auth_live,
)
from .fixtures import TOKEN, PROJECT_ID, PROJECT_APIKEY, WORKSPACE_ID
def test_auth_kwargs():
auth = Auth(
project_id=PROJECT_ID,
project_api_key=PROJECT_APIKEY,
env="abc",
authenticate=False,
retry=False,
)
assert auth.env == "abc"
assert not auth.authenticate
def test_no_credentials_raises(auth_mock):
auth_mock.project_id = None
auth_mock.project_api_key = None
with pytest.raises(ValueError):
auth_mock._find_credentials()
def test_find_credentials_cfg_file(auth_mock):
auth_mock.project_id = None
auth_mock.project_api_key = None
fp = Path(__file__).resolve().parent / "mock_data" / "test_config.json"
auth_mock.cfg_file = fp
auth_mock._find_credentials()
assert auth_mock.project_id is not None
assert auth_mock.project_api_key is not None
def test_endpoint(auth_mock):
assert auth_mock._endpoint() == "https://api.up42.com"
auth_mock.env = "abc"
assert auth_mock._endpoint() == "https://api.up42.abc"
def test_get_token(auth_mock):
auth_mock._get_token()
assert auth_mock.token == TOKEN
@pytest.mark.live
def test_get_token_raises_wrong_credentials_live(auth_live):
auth_live.project_id = "123"
with pytest.raises(ValueError) as e:
auth_live._get_token()
assert (
"Authentication was not successful, check the provided project credentials."
in str(e.value)
)
@pytest.mark.live
def test_get_token_live(auth_live):
assert hasattr(auth_live, "token")
def test_get_workspace(auth_mock):
auth_mock._get_workspace()
assert auth_mock.workspace_id == WORKSPACE_ID
@pytest.mark.live
def test_get_workspace_live(auth_live):
assert hasattr(auth_live, "workspace_id")
def test_generate_headers(auth_mock):
version = (
Path(__file__)
.resolve()
.parents[1]
.joinpath("up42/_version.txt")
.read_text(encoding="utf-8")
)
assert (
isinstance(version, str) and "\n" not in version
), "check integrity of your version file"
expected_headers = {
"Content-Type": "application/json",
"Authorization": "Bearer token_1011",
"cache-control": "no-cache",
"X-UP42-info": f"python/{version}",
}
assert auth_mock._generate_headers(token="token_1011") == expected_headers
def test_request_helper(auth_mock, requests_mock):
requests_mock.get(url="http://test.com", json={"data": {"xyz": 789}, "error": {}})
response = auth_mock._request_helper(
request_type="GET", url="http://test.com", data={}, querystring={}
)
response_json = json.loads(response.text)
assert response_json == {"data": {"xyz": 789}, "error": {}}
def test_request(auth_mock, requests_mock):
requests_mock.get(url="http://test.com", json={"data": {"xyz": 789}, "error": {}})
response_json = auth_mock._request(request_type="GET", url="http://test.com")
assert response_json == {"data": {"xyz": 789}, "error": {}}
def test_request_non200_raises(auth_mock, requests_mock):
requests_mock.get(
url="http://test.com",
json={"data": {}, "error": {"code": 403, "message": "some 403 error message"}},
status_code=403,
)
with pytest.raises(requests.exceptions.RequestException) as e:
auth_mock._request(request_type="GET", url="http://test.com")
assert "some 403 error message!" in str(e.value)
def test_request_non200_raises_error_not_dict(auth_mock, requests_mock):
requests_mock.get(
url="http://test.com",
json={"data": {}, "error": "Not found!"},
status_code=403,
)
with pytest.raises(requests.exceptions.RequestException) as e:
auth_mock._request(request_type="GET", url="http://test.com")
assert "Not found!" in str(e.value)
def test_request_token_still_timed_out_after_retry_raises(auth_mock, requests_mock):
a = requests_mock.get(
"http://test.com",
[
{
"status_code": 401,
"json": {
"data": {},
"error": {"code": 401, "message": "token timeout"},
},
},
{
"status_code": 401,
"json": {
"data": {},
"error": {"code": 401, "message": "token timeout"},
},
},
{"json": {"data": {"xyz": 789}, "error": {}}},
],
)
with pytest.raises(requests.exceptions.RequestException):
auth_mock._request(request_type="GET", url="http://test.com")
assert a.call_count == 2
def test_request_token_timed_out_retry(auth_mock, requests_mock):
a = requests_mock.get(
"http://test.com",
[
{
"status_code": 401,
"json": {
"data": {},
"error": {"code": 401, "message": "token timeout"},
},
},
{"json": {"data": {"xyz": 789}, "error": {}}},
],
)
response_json = auth_mock._request(request_type="GET", url="http://test.com")
assert response_json == {"data": {"xyz": 789}, "error": {}}
assert a.call_count == 2
def test_request_rate_limited_retry(auth_mock, requests_mock):
a = requests_mock.get(
"http://test.com",
[
{
"status_code": 429,
"json": {
"data": {},
"error": {"code": 429, "message": "rate limited"},
},
},
{"json": {"data": {"xyz": 789}, "error": {}}},
],
)
response_json = auth_mock._request(request_type="GET", url="http://test.com")
assert response_json == {"data": {"xyz": 789}, "error": {}}
assert a.call_count == 2
def test_request_token_timeout_during_rate_limitation(auth_mock, requests_mock):
a = requests_mock.get(
"http://test.com",
[
{
"status_code": 429,
"json": {
"data": {},
"error": {"code": 429, "message": "rate limited"},
},
},
{
"status_code": 401,
"json": {
"data": {},
"error": {"code": 401, "message": "token timeout"},
},
},
{
"status_code": 429,
"json": {
"data": {},
"error": {"code": 429, "message": "rate limited"},
},
},
{"json": {"data": {"xyz": 789}, "error": {}}},
],
)
response_json = auth_mock._request(request_type="GET", url="http://test.com")
assert response_json == {"data": {"xyz": 789}, "error": {}}
assert a.call_count == 4