Skip to content

Commit 8d29471

Browse files
committed
fix tests
1 parent 6eaa6be commit 8d29471

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

packages/toolbox-core/tests/test_auth_methods.py

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ def reset_cache():
4444
auth_methods._token_cache = original_cache
4545

4646

47+
48+
49+
4750
@pytest.mark.asyncio
4851
class TestAsyncAuthMethods:
4952
"""Tests for asynchronous Google ID token fetching."""
@@ -61,7 +64,8 @@ async def test_aget_google_id_token_success_first_call(
6164
mock_fetch.return_value = MOCK_ID_TOKEN
6265
mock_verify.return_value = {"exp": MOCK_EXPIRY_TIMESTAMP}
6366

64-
token = await auth_methods.aget_google_id_token(MOCK_AUDIENCE)
67+
token_getter = auth_methods.aget_google_id_token(MOCK_AUDIENCE)
68+
token = await token_getter()
6569

6670
mock_default.assert_called_once()
6771
mock_fetch.assert_called_once_with(ANY, MOCK_AUDIENCE)
@@ -72,15 +76,14 @@ async def test_aget_google_id_token_success_first_call(
7276
@patch("toolbox_core.auth_methods.google.auth.default")
7377
async def test_aget_google_id_token_success_uses_cache(self, mock_default):
7478
"""Tests that subsequent calls use the cached token if valid."""
75-
# Prime the cache with a valid token
7679
auth_methods._token_cache["token"] = MOCK_ID_TOKEN
7780
auth_methods._token_cache["expires_at"] = auth_methods.datetime.now(
7881
auth_methods.timezone.utc
7982
) + auth_methods.timedelta(hours=1)
8083

81-
token = await auth_methods.aget_google_id_token(MOCK_AUDIENCE)
84+
token_getter = auth_methods.aget_google_id_token(MOCK_AUDIENCE)
85+
token = await token_getter()
8286

83-
# The underlying auth function should not be called if cache is valid
8487
mock_default.assert_not_called()
8588
assert token == f"{auth_methods.BEARER_TOKEN_PREFIX}{MOCK_ID_TOKEN}"
8689

@@ -97,14 +100,13 @@ async def test_aget_google_id_token_refreshes_expired_cache(
97100
auth_methods._token_cache["token"] = "expired_token"
98101
auth_methods._token_cache["expires_at"] = auth_methods.datetime.now(
99102
auth_methods.timezone.utc
100-
) - auth_methods.timedelta(
101-
seconds=100
102-
) # Expired
103+
) - auth_methods.timedelta(seconds=100)
103104

104105
mock_fetch.return_value = MOCK_ID_TOKEN
105106
mock_verify.return_value = {"exp": MOCK_EXPIRY_TIMESTAMP}
106107

107-
token = await auth_methods.aget_google_id_token(MOCK_AUDIENCE)
108+
token_getter = auth_methods.aget_google_id_token(MOCK_AUDIENCE)
109+
token = await token_getter()
108110

109111
mock_default.assert_called_once()
110112
mock_fetch.assert_called_once()
@@ -122,8 +124,8 @@ async def test_aget_raises_if_no_audience_and_no_local_token(
122124
"""Tests that the async function propagates the missing audience exception."""
123125
error_msg = "You are not authenticating using User Credentials."
124126
with pytest.raises(Exception, match=error_msg):
125-
# Call without audience to trigger the error path
126-
await auth_methods.aget_google_id_token()
127+
token_getter = auth_methods.aget_google_id_token()
128+
await token_getter()
127129

128130
mock_default.assert_called_once()
129131
mock_fetch.assert_not_called()
@@ -149,7 +151,8 @@ def test_get_google_id_token_success_local_creds(
149151
mock_request_instance = MagicMock()
150152
mock_request.return_value = mock_request_instance
151153

152-
token = auth_methods.get_google_id_token(MOCK_AUDIENCE)
154+
token_getter = auth_methods.get_google_id_token(MOCK_AUDIENCE)
155+
token = token_getter()
153156

154157
mock_default.assert_called_once_with()
155158
mock_session.assert_called_once_with(mock_creds)
@@ -167,7 +170,8 @@ def test_get_google_id_token_success_uses_cache(self, mock_default):
167170
auth_methods.timezone.utc
168171
) + auth_methods.timedelta(hours=1)
169172

170-
token = auth_methods.get_google_id_token(MOCK_AUDIENCE)
173+
token_getter = auth_methods.get_google_id_token(MOCK_AUDIENCE)
174+
token = token_getter()
171175

172176
mock_default.assert_not_called()
173177
assert token == f"{auth_methods.BEARER_TOKEN_PREFIX}{MOCK_ID_TOKEN}"
@@ -185,7 +189,7 @@ def test_get_google_id_token_fetch_failure(
185189
mock_fetch.side_effect = GoogleAuthError("Fetch failed")
186190

187191
with pytest.raises(GoogleAuthError, match="Fetch failed"):
188-
auth_methods.get_google_id_token(MOCK_AUDIENCE)
192+
auth_methods.get_google_id_token(MOCK_AUDIENCE)()
189193

190194
assert auth_methods._token_cache["token"] is None
191195
mock_default.assert_called_once()
@@ -208,15 +212,13 @@ def test_get_google_id_token_validation_failure(
208212
with pytest.raises(
209213
ValueError, match="Failed to validate and cache the new token"
210214
):
211-
auth_methods.get_google_id_token(MOCK_AUDIENCE)
215+
auth_methods.get_google_id_token(MOCK_AUDIENCE)()
212216

213-
# Verify cache is cleared on validation failure
214217
assert auth_methods._token_cache["token"] is None
215218

216219
@patch("toolbox_core.auth_methods.id_token.fetch_id_token")
217220
@patch(
218221
"toolbox_core.auth_methods.google.auth.default",
219-
# Simulate credentials that DON'T have a pre-existing id_token
220222
return_value=(MagicMock(id_token=None), MOCK_PROJECT_ID),
221223
)
222224
def test_get_raises_if_no_audience_and_no_local_token(
@@ -225,8 +227,7 @@ def test_get_raises_if_no_audience_and_no_local_token(
225227
"""Tests exception is raised if audience is required but not provided."""
226228
error_msg = "You are not authenticating using User Credentials."
227229
with pytest.raises(Exception, match=error_msg):
228-
# Call without an audience to trigger the error path
229-
auth_methods.get_google_id_token()
230+
auth_methods.get_google_id_token()()
230231

231232
mock_default.assert_called_once()
232-
mock_fetch.assert_not_called()
233+
mock_fetch.assert_not_called()

0 commit comments

Comments
 (0)