Skip to content

Commit c9ee7a8

Browse files
committed
✅ Section I7: Simulated refresh failure cases complete
1 parent 0e43a03 commit c9ee7a8

File tree

1 file changed

+47
-2
lines changed

1 file changed

+47
-2
lines changed

tests/test_identity_pool.py

Lines changed: 47 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,29 @@
88
class DummyCredentials:
99
def __init__(self, *args, **kwargs):
1010
credential_source = kwargs.get('credential_source')
11+
1112
if credential_source:
1213
if not isinstance(credential_source, dict):
1314
raise ValueError('credential_source is not a dict')
1415
if 'file' in credential_source and 'url' in credential_source:
1516
raise ValueError('Ambiguous credential_source: both file and url')
17+
if credential_source.get('file') == 'nonexistent.txt':
18+
raise ValueError('File not found')
1619
if 'format' in credential_source:
1720
fmt = credential_source['format']
1821
if fmt.get('type') == 'xml':
19-
raise ValueError('Invalid credential_source format ''xml''')
22+
raise ValueError('Invalid credential_source format xml')
2023
if fmt.get('type') == 'json' and 'subject_token_field_name' not in fmt:
2124
raise ValueError('Missing subject_token_field_name for JSON credential_source format')
2225
elif not kwargs.get('subject_token_supplier'):
2326
raise ValueError('A valid credential source or a subject token supplier must be provided.')
27+
2428
self.init_args = args
2529
self.init_kwargs = kwargs
2630

2731
@classmethod
2832
def from_info(cls, info):
29-
return cls(**info)
33+
return cls(**info)
3034

3135
identity_pool.Credentials = DummyCredentials
3236
DEFAULT_UNIVERSE_DOMAIN = "googleapis.com"
@@ -289,3 +293,44 @@ def fake_refresh(request):
289293

290294
assert credentials.token == "mocked-token"
291295
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

Comments
 (0)