-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_core.py
More file actions
245 lines (197 loc) · 7.73 KB
/
test_core.py
File metadata and controls
245 lines (197 loc) · 7.73 KB
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
from io import StringIO
import pytest
from pydantic import Field
from pytest_httpserver import HTTPServer
from .conftest import TEST_HOST
from .conftest import TEST_TOKEN
from _incydr_sdk.core.auth import RefreshTokenAuth
from _incydr_sdk.core.models import CSVModel
from _incydr_sdk.core.models import Model
from _incydr_sdk.core.settings import IncydrSettings
from _incydr_sdk.exceptions import AuthMissingError
from incydr import Client
def test_client_init_reads_environment_vars_when_no_arguments_passed(
httpserver_auth: HTTPServer,
):
c = Client()
assert c.settings.url == TEST_HOST
assert c.settings.api_client_id == "env_id"
assert c.settings.api_client_secret.get_secret_value() == "env_secret"
def test_client_init_prefers_passed_arg_when_environment_vars_set(
httpserver_auth: HTTPServer,
):
c = Client(api_client_id="key-456")
assert c.settings.api_client_id != "env_id"
assert c.settings.url == TEST_HOST
assert c.settings.api_client_secret.get_secret_value() == "env_secret"
def test_csv_model_parsing():
class Test(CSVModel):
required_field: str = Field(
csv_aliases=["required_field", "requiredField", "RF"]
)
csv_with_all_aliases = StringIO(
"""required_field,RF,requiredField,extra\n1,2,3,4\na,b,c,d\n"""
)
row_1, row_2 = tuple(Test.parse_csv(csv_with_all_aliases))
assert row_1.required_field == "1"
assert row_1.RF == "2"
assert row_1.requiredField == "3"
assert row_1.extra == "4"
assert row_2.required_field == "a"
assert row_2.RF == "b"
assert row_2.requiredField == "c"
assert row_2.extra == "d"
csv_with_two_aliases = StringIO("""RF,requiredField,extra\n2,3,4\nb,c,d\n""")
row_1, row_2 = tuple(Test.parse_csv(csv_with_two_aliases))
assert row_1.required_field == "3"
assert row_1.RF == "2"
assert row_1.requiredField == "3"
assert row_1.extra == "4"
assert row_2.required_field == "c"
assert row_2.RF == "b"
assert row_2.requiredField == "c"
assert row_2.extra == "d"
csv_with_one_alias = StringIO("""RF,extra\n2,4\nb,d\n""")
row_1, row_2 = tuple(Test.parse_csv(csv_with_one_alias))
assert row_1.required_field == "2"
assert row_1.RF == "2"
assert row_1.extra == "4"
assert row_2.required_field == "b"
assert row_2.RF == "b"
assert row_2.extra == "d"
csv_with_no_required_aliases = StringIO("""data,extra\n1,2\na,b\n""")
with pytest.raises(ValueError) as err:
list(Test.parse_csv(csv_with_no_required_aliases))
assert (
str(err.value)
== "CSV header missing column: Value error, 'required_field' required. Valid column aliases: ['required_field', 'requiredField', 'RF']"
)
def test_json_lines_model_parsing():
class Test(Model):
field_1: str
field_2: int
json_lines = StringIO(
"""{ "field_1": "value1", "field_2": 10 }\n{ "field_1": "value2", "field_2": 20 }"""
)
line_1, line_2 = list(Test.parse_json_lines(json_lines))
assert line_1.field_1 == "value1"
assert line_1.field_2 == 10
assert line_2.field_1 == "value2"
assert line_2.field_2 == 20
not_json_lines = StringIO(
"""{"field_1": "value1","field_2": 10}\n{\n\t"field_1": "value1",\n\t"field_2": 10\n},\n{\n\t"field_1": "value_2",\n\t"field_2": 20\n }"""
)
with pytest.raises(ValueError) as err:
list(Test.parse_json_lines(not_json_lines))
assert (
str(err.value)
== "Unable to parse line 2. Expecting JSONLines format: https://jsonlines.org"
)
invalid_model_data = StringIO(
"""{ "field_1": "value1", "field_2": 10 }\n{ "field_1": "value2", "field_2": "not_int" }"""
)
with pytest.raises(ValueError) as err:
list(Test.parse_json_lines(invalid_model_data))
assert "Error parsing object on line 2: 1 validation error for Test" in str(
err.value
)
assert "Input should be a valid integer" in str(err.value)
def test_user_agent(httpserver_auth: HTTPServer):
c = Client()
assert c._session.headers["User-Agent"].startswith("incydrSDK")
@pytest.fixture
def httpserver_refresh_token_auth(httpserver: HTTPServer, monkeypatch):
monkeypatch.setenv("incydr_url", TEST_HOST)
monkeypatch.setenv("incydr_refresh_token", "test_refresh_token")
monkeypatch.setenv("incydr_refresh_url", f"{TEST_HOST}/v1/refresh")
refresh_response = {
"accessToken": {
"tokenValue": TEST_TOKEN,
"expiresAt": "2099-01-01T00:00:00Z",
},
"refreshToken": {
"tokenValue": "new_refresh_token",
"expiresAt": "2099-01-01T00:00:00Z",
},
}
httpserver.expect_request("/v1/refresh", method="POST").respond_with_json(
refresh_response
)
return httpserver
def test_client_init_with_refresh_token_and_refresh_url_uses_refresh_token_auth(
httpserver_refresh_token_auth: HTTPServer,
):
c = Client()
assert isinstance(c._session.auth, RefreshTokenAuth)
assert c.settings.refresh_token.get_secret_value() == "test_refresh_token"
assert c.settings.refresh_url == f"{TEST_HOST}/v1/refresh"
def test_client_init_with_refresh_token_does_not_require_api_client_credentials(
httpserver_refresh_token_auth: HTTPServer,
):
c = Client()
assert c.settings.api_client_id is None
assert c.settings.api_client_secret is None
def test_client_init_with_refresh_token_passed_as_args(
httpserver: HTTPServer, monkeypatch
):
monkeypatch.setenv("incydr_url", TEST_HOST)
refresh_response = {
"accessToken": {
"tokenValue": TEST_TOKEN,
"expiresAt": "2099-01-01T00:00:00Z",
},
"refreshToken": {
"tokenValue": "new_refresh_token",
"expiresAt": "2099-01-01T00:00:00Z",
},
}
httpserver.expect_request("/v1/refresh", method="POST").respond_with_json(
refresh_response
)
c = Client(
refresh_token="arg_refresh_token",
refresh_url=f"{TEST_HOST}/v1/refresh",
)
assert isinstance(c._session.auth, RefreshTokenAuth)
assert c.settings.refresh_token.get_secret_value() == "arg_refresh_token"
def test_settings_with_only_refresh_token_raises_auth_missing_error(monkeypatch):
with pytest.raises(AuthMissingError) as exc_info:
IncydrSettings(
url=TEST_HOST,
refresh_token="test_refresh_token",
_env_file="",
)
assert "api_client_id" in exc_info.value.error_keys
assert "api_client_secret" in exc_info.value.error_keys
def test_settings_with_only_refresh_url_raises_auth_missing_error(monkeypatch):
with pytest.raises(AuthMissingError) as exc_info:
IncydrSettings(
url=TEST_HOST,
refresh_url=f"{TEST_HOST}/v1/refresh",
_env_file="",
)
assert "api_client_id" in exc_info.value.error_keys
assert "api_client_secret" in exc_info.value.error_keys
def test_client_prefers_refresh_token_auth_when_both_auth_methods_provided(
httpserver: HTTPServer, monkeypatch
):
monkeypatch.setenv("incydr_url", TEST_HOST)
monkeypatch.setenv("incydr_api_client_id", "env_id")
monkeypatch.setenv("incydr_api_client_secret", "env_secret")
monkeypatch.setenv("incydr_refresh_token", "test_refresh_token")
monkeypatch.setenv("incydr_refresh_url", f"{TEST_HOST}/v1/refresh")
refresh_response = {
"accessToken": {
"tokenValue": TEST_TOKEN,
"expiresAt": "2099-01-01T00:00:00Z",
},
"refreshToken": {
"tokenValue": "new_refresh_token",
"expiresAt": "2099-01-01T00:00:00Z",
},
}
httpserver.expect_request("/v1/refresh", method="POST").respond_with_json(
refresh_response
)
c = Client()
assert isinstance(c._session.auth, RefreshTokenAuth)