|
5 | 5 | from pytest_httpserver import HTTPServer |
6 | 6 |
|
7 | 7 | from .conftest import TEST_HOST |
| 8 | +from .conftest import TEST_TOKEN |
| 9 | +from _incydr_sdk.core.auth import RefreshTokenAuth |
8 | 10 | from _incydr_sdk.core.models import CSVModel |
9 | 11 | from _incydr_sdk.core.models import Model |
| 12 | +from _incydr_sdk.core.settings import IncydrSettings |
| 13 | +from _incydr_sdk.exceptions import AuthMissingError |
10 | 14 | from incydr import Client |
11 | 15 |
|
12 | 16 |
|
@@ -122,3 +126,120 @@ class Test(Model): |
122 | 126 | def test_user_agent(httpserver_auth: HTTPServer): |
123 | 127 | c = Client() |
124 | 128 | assert c._session.headers["User-Agent"].startswith("incydrSDK") |
| 129 | + |
| 130 | + |
| 131 | +@pytest.fixture |
| 132 | +def httpserver_refresh_token_auth(httpserver: HTTPServer, monkeypatch): |
| 133 | + monkeypatch.setenv("incydr_url", TEST_HOST) |
| 134 | + monkeypatch.setenv("incydr_refresh_token", "test_refresh_token") |
| 135 | + monkeypatch.setenv("incydr_refresh_url", f"{TEST_HOST}/v1/refresh") |
| 136 | + |
| 137 | + refresh_response = { |
| 138 | + "accessToken": { |
| 139 | + "tokenValue": TEST_TOKEN, |
| 140 | + "expiresAt": "2099-01-01T00:00:00Z", |
| 141 | + }, |
| 142 | + "refreshToken": { |
| 143 | + "tokenValue": "new_refresh_token", |
| 144 | + "expiresAt": "2099-01-01T00:00:00Z", |
| 145 | + }, |
| 146 | + } |
| 147 | + httpserver.expect_request("/v1/refresh", method="POST").respond_with_json( |
| 148 | + refresh_response |
| 149 | + ) |
| 150 | + return httpserver |
| 151 | + |
| 152 | + |
| 153 | +def test_client_init_with_refresh_token_and_refresh_url_uses_refresh_token_auth( |
| 154 | + httpserver_refresh_token_auth: HTTPServer, |
| 155 | +): |
| 156 | + c = Client() |
| 157 | + assert isinstance(c._session.auth, RefreshTokenAuth) |
| 158 | + assert c.settings.refresh_token.get_secret_value() == "test_refresh_token" |
| 159 | + assert c.settings.refresh_url == f"{TEST_HOST}/v1/refresh" |
| 160 | + |
| 161 | + |
| 162 | +def test_client_init_with_refresh_token_does_not_require_api_client_credentials( |
| 163 | + httpserver_refresh_token_auth: HTTPServer, |
| 164 | +): |
| 165 | + c = Client() |
| 166 | + assert c.settings.api_client_id is None |
| 167 | + assert c.settings.api_client_secret is None |
| 168 | + |
| 169 | + |
| 170 | +def test_client_init_with_refresh_token_passed_as_args( |
| 171 | + httpserver: HTTPServer, monkeypatch |
| 172 | +): |
| 173 | + monkeypatch.setenv("incydr_url", TEST_HOST) |
| 174 | + |
| 175 | + refresh_response = { |
| 176 | + "accessToken": { |
| 177 | + "tokenValue": TEST_TOKEN, |
| 178 | + "expiresAt": "2099-01-01T00:00:00Z", |
| 179 | + }, |
| 180 | + "refreshToken": { |
| 181 | + "tokenValue": "new_refresh_token", |
| 182 | + "expiresAt": "2099-01-01T00:00:00Z", |
| 183 | + }, |
| 184 | + } |
| 185 | + httpserver.expect_request("/v1/refresh", method="POST").respond_with_json( |
| 186 | + refresh_response |
| 187 | + ) |
| 188 | + |
| 189 | + c = Client( |
| 190 | + refresh_token="arg_refresh_token", |
| 191 | + refresh_url=f"{TEST_HOST}/v1/refresh", |
| 192 | + ) |
| 193 | + assert isinstance(c._session.auth, RefreshTokenAuth) |
| 194 | + assert c.settings.refresh_token.get_secret_value() == "arg_refresh_token" |
| 195 | + |
| 196 | + |
| 197 | +def test_settings_with_only_refresh_token_raises_auth_missing_error(monkeypatch): |
| 198 | + with pytest.raises(AuthMissingError) as exc_info: |
| 199 | + IncydrSettings( |
| 200 | + url=TEST_HOST, |
| 201 | + refresh_token="test_refresh_token", |
| 202 | + _env_file="", |
| 203 | + ) |
| 204 | + |
| 205 | + assert "api_client_id" in exc_info.value.error_keys |
| 206 | + assert "api_client_secret" in exc_info.value.error_keys |
| 207 | + |
| 208 | + |
| 209 | +def test_settings_with_only_refresh_url_raises_auth_missing_error(monkeypatch): |
| 210 | + with pytest.raises(AuthMissingError) as exc_info: |
| 211 | + IncydrSettings( |
| 212 | + url=TEST_HOST, |
| 213 | + refresh_url=f"{TEST_HOST}/v1/refresh", |
| 214 | + _env_file="", |
| 215 | + ) |
| 216 | + |
| 217 | + assert "api_client_id" in exc_info.value.error_keys |
| 218 | + assert "api_client_secret" in exc_info.value.error_keys |
| 219 | + |
| 220 | + |
| 221 | +def test_client_prefers_refresh_token_auth_when_both_auth_methods_provided( |
| 222 | + httpserver: HTTPServer, monkeypatch |
| 223 | +): |
| 224 | + monkeypatch.setenv("incydr_url", TEST_HOST) |
| 225 | + monkeypatch.setenv("incydr_api_client_id", "env_id") |
| 226 | + monkeypatch.setenv("incydr_api_client_secret", "env_secret") |
| 227 | + monkeypatch.setenv("incydr_refresh_token", "test_refresh_token") |
| 228 | + monkeypatch.setenv("incydr_refresh_url", f"{TEST_HOST}/v1/refresh") |
| 229 | + |
| 230 | + refresh_response = { |
| 231 | + "accessToken": { |
| 232 | + "tokenValue": TEST_TOKEN, |
| 233 | + "expiresAt": "2099-01-01T00:00:00Z", |
| 234 | + }, |
| 235 | + "refreshToken": { |
| 236 | + "tokenValue": "new_refresh_token", |
| 237 | + "expiresAt": "2099-01-01T00:00:00Z", |
| 238 | + }, |
| 239 | + } |
| 240 | + httpserver.expect_request("/v1/refresh", method="POST").respond_with_json( |
| 241 | + refresh_response |
| 242 | + ) |
| 243 | + |
| 244 | + c = Client() |
| 245 | + assert isinstance(c._session.auth, RefreshTokenAuth) |
0 commit comments