/
test_auth.py
281 lines (226 loc) · 8.08 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
import json
from pathlib import Path
import pytest
import requests
from up42.utils import get_up42_py_version
from .context import Auth
# pylint: disable=unused-import
from .fixtures import (
PROJECT_APIKEY,
PROJECT_ID,
TOKEN,
WORKSPACE_ID,
auth_account_live,
auth_account_mock,
auth_live,
auth_mock,
auth_project_live,
auth_project_mock,
password_test_live,
project_api_key_live,
project_id_live,
username_test_live,
)
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():
with pytest.raises(ValueError):
Auth()
def test_cfg_file_not_found():
fp = Path(__file__).resolve().parent / "mock_data" / "test_config_fake.json"
with pytest.raises(ValueError) as e:
Auth(cfg_file=fp)
assert "Selected config file does not exist!" in str(e.value)
def test_should_not_authenticate_with_config_file_if_not_requested():
fp = Path(__file__).resolve().parent / "mock_data" / "test_config.json"
Auth(cfg_file=fp, authenticate=False)
def test_should_set_endpoint_with_environment(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._credentials_id = "123"
with pytest.raises(ValueError) as e:
auth_live._get_token()
assert "Authentication" 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 = get_up42_py_version()
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",
"User-Agent": f"up42-py/{version} (https://github.com/up42/up42-py)",
}
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 "{'code': 403, 'message': '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_non200_raises_error_apiv2(auth_mock, requests_mock):
"""
Errors that are raised in the http response with the api v2 format
Live tests are included in the specific tests classes.
e.g. test_storage (test_get_assets_raise_error_live)
"""
requests_mock.get(
url="http://test.com",
json={"title": "Bad request", "status": 400},
status_code=400,
)
with pytest.raises(requests.exceptions.RequestException) as e:
auth_mock._request(request_type="GET", url="http://test.com")
assert "title" in str(e.value)
def test_request_200_raises_error_apiv2(auth_mock, requests_mock):
"""
Errors that are raised in a positive the http response
and included in the error key
"""
requests_mock.get(
url="http://test.com",
json={
"data": None,
"error": "Some default error not related to Http",
},
status_code=200,
)
with pytest.raises(ValueError) as e:
auth_mock._request(request_type="GET", url="http://test.com")
assert "error" 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