/
test_auth.py
227 lines (187 loc) · 7.23 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
import pathlib
from typing import cast
import pytest
import requests
import requests_mock as req_mock
from up42 import auth as up42_auth
from up42 import utils
from .fixtures import fixtures_globals as constants
def test_auth_kwargs():
auth = up42_auth.Auth(
project_id=constants.PROJECT_ID,
project_api_key=constants.PROJECT_APIKEY,
authenticate=False,
retry=False,
)
assert not auth.authenticate
def test_no_credentials_raises():
with pytest.raises(ValueError):
up42_auth.Auth()
def test_should_fail_config_file_not_found(tmp_path):
config_path = tmp_path / "config_fake.json"
with pytest.raises(ValueError) as e:
up42_auth.Auth(cfg_file=config_path)
assert str(config_path) in str(e.value)
def test_should_not_authenticate_with_config_file_if_not_requested():
fp = pathlib.Path(__file__).resolve().parent / "mock_data" / "test_config.json"
up42_auth.Auth(cfg_file=fp, authenticate=False)
def test_get_workspace(auth_mock: up42_auth.Auth):
assert auth_mock.workspace_id == constants.WORKSPACE_ID
def test_should_set_headers(auth_mock: up42_auth.Auth, requests_mock: req_mock.Mocker):
expected_code = 207
test_url = "http://test.com"
version = utils.get_up42_py_version()
expected_headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer {constants.TOKEN}",
"cache-control": "no-cache",
"User-Agent": f"up42-py/{version} (https://github.com/up42/up42-py)",
}
requests_mock.get(test_url, request_headers=expected_headers, status_code=expected_code)
response = cast(requests.Response, auth_mock.request("GET", test_url, return_text=False))
assert response.status_code == expected_code
assert requests_mock.called
def test_request(auth_mock: up42_auth.Auth, requests_mock: req_mock.Mocker):
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: up42_auth.Auth, requests_mock: req_mock.Mocker):
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: up42_auth.Auth, requests_mock: req_mock.Mocker):
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: up42_auth.Auth, requests_mock: req_mock.Mocker):
"""
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: up42_auth.Auth, requests_mock: req_mock.Mocker):
"""
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: up42_auth.Auth, requests_mock: req_mock.Mocker):
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: up42_auth.Auth, requests_mock: req_mock.Mocker):
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: up42_auth.Auth, requests_mock: req_mock.Mocker):
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: up42_auth.Auth, requests_mock: req_mock.Mocker):
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