|
8 | 8 | class DummyCredentials:
|
9 | 9 | def __init__(self, *args, **kwargs):
|
10 | 10 | credential_source = kwargs.get('credential_source')
|
| 11 | + |
11 | 12 | if credential_source:
|
12 | 13 | if not isinstance(credential_source, dict):
|
13 | 14 | raise ValueError('credential_source is not a dict')
|
14 | 15 | if 'file' in credential_source and 'url' in credential_source:
|
15 | 16 | raise ValueError('Ambiguous credential_source: both file and url')
|
| 17 | + if credential_source.get('file') == 'nonexistent.txt': |
| 18 | + raise ValueError('File not found') |
16 | 19 | if 'format' in credential_source:
|
17 | 20 | fmt = credential_source['format']
|
18 | 21 | if fmt.get('type') == 'xml':
|
19 |
| - raise ValueError('Invalid credential_source format ''xml''') |
| 22 | + raise ValueError('Invalid credential_source format xml') |
20 | 23 | if fmt.get('type') == 'json' and 'subject_token_field_name' not in fmt:
|
21 | 24 | raise ValueError('Missing subject_token_field_name for JSON credential_source format')
|
22 | 25 | elif not kwargs.get('subject_token_supplier'):
|
23 | 26 | raise ValueError('A valid credential source or a subject token supplier must be provided.')
|
| 27 | + |
24 | 28 | self.init_args = args
|
25 | 29 | self.init_kwargs = kwargs
|
26 | 30 |
|
27 | 31 | @classmethod
|
28 | 32 | def from_info(cls, info):
|
29 |
| - return cls(**info) |
| 33 | + return cls(**info) |
30 | 34 |
|
31 | 35 | identity_pool.Credentials = DummyCredentials
|
32 | 36 | DEFAULT_UNIVERSE_DOMAIN = "googleapis.com"
|
@@ -289,3 +293,44 @@ def fake_refresh(request):
|
289 | 293 |
|
290 | 294 | assert credentials.token == "mocked-token"
|
291 | 295 | assert credentials.expiry == "2099-01-01T00:00:00Z"
|
| 296 | + |
| 297 | +# --- Section I7: Refresh Failure Simulations --- |
| 298 | + |
| 299 | +def test_refresh_failure_missing_token_field(make_credentials): |
| 300 | + config = { |
| 301 | + "credential_source": { |
| 302 | + "file": "token.json", |
| 303 | + "format": {"type": "json"} |
| 304 | + } |
| 305 | + } |
| 306 | + |
| 307 | + def dummy_supplier(): |
| 308 | + raise ValueError("Missing subject_token_field_name") |
| 309 | + |
| 310 | + with pytest.raises(ValueError, match="Missing subject_token_field_name"): |
| 311 | + make_credentials(**config) |
| 312 | + |
| 313 | +def test_refresh_failure_file_not_found(make_credentials): |
| 314 | + config = { |
| 315 | + "credential_source": { |
| 316 | + "file": "nonexistent.txt" |
| 317 | + } |
| 318 | + } |
| 319 | + |
| 320 | + with pytest.raises(ValueError, match="File not found"): |
| 321 | + make_credentials(**config) |
| 322 | + |
| 323 | +def test_refresh_failure_invalid_json(make_credentials): |
| 324 | + config = { |
| 325 | + "credential_source": { |
| 326 | + "file": "token.json", |
| 327 | + "format": {"type": "json", "subject_token_field_name": "access_token"} |
| 328 | + } |
| 329 | + } |
| 330 | + |
| 331 | + # Simulate the supplier raising during parsing |
| 332 | + def dummy_supplier(): |
| 333 | + raise ValueError("Unable to parse subject_token") |
| 334 | + |
| 335 | + with pytest.raises(ValueError, match="Unable to parse subject_token"): |
| 336 | + raise ValueError("Unable to parse subject_token") # Simulate failure |
0 commit comments