Skip to content

Commit 71fac64

Browse files
committed
fix tests
1 parent a578964 commit 71fac64

File tree

1 file changed

+27
-47
lines changed

1 file changed

+27
-47
lines changed

packages/toolbox-core/tests/test_auth_methods.py

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
import time
16-
from unittest.mock import AsyncMock, MagicMock, patch
15+
from unittest.mock import AsyncMock, MagicMock, patch, PropertyMock
1716

1817
import pytest
1918

@@ -23,7 +22,8 @@
2322
MOCK_GOOGLE_ID_TOKEN = "test_id_token_123"
2423
MOCK_PROJECT_ID = "test-project"
2524
# A realistic expiry timestamp (e.g., 1 hour from now)
26-
MOCK_EXPIRY_TIMESTAMP = time.time() + 3600
25+
MOCK_EXPIRY_DATETIME = auth_methods.datetime.now(auth_methods.timezone.utc) + auth_methods.timedelta(hours=1)
26+
2727

2828
# Expected exception messages from auth_methods.py
2929
FETCH_TOKEN_FAILURE_MSG = "Failed to fetch Google ID token."
@@ -48,21 +48,19 @@ class TestAsyncAuthMethods:
4848
"""Tests for asynchronous Google ID token fetching."""
4949

5050
@pytest.mark.asyncio
51-
@patch("toolbox_core.auth_methods._decode_jwt_and_get_expiry")
5251
@patch("toolbox_core.auth_methods._aiohttp_requests.Request")
5352
@patch("toolbox_core.auth_methods.default_async", new_callable=MagicMock)
5453
async def test_aget_google_id_token_success_first_call(
55-
self, mock_default_async, mock_async_req_class, mock_decode_expiry
54+
self, mock_default_async, mock_async_req_class
5655
):
5756
"""Tests successful fetching of an async token on the first call."""
5857
mock_creds_instance = AsyncMock()
5958
mock_creds_instance.id_token = MOCK_GOOGLE_ID_TOKEN
59+
type(mock_creds_instance).expiry = PropertyMock(return_value=MOCK_EXPIRY_DATETIME)
6060
mock_default_async.return_value = (mock_creds_instance, MOCK_PROJECT_ID)
61-
mock_decode_expiry.return_value = MOCK_EXPIRY_TIMESTAMP
6261

6362
mock_async_req_instance = MagicMock()
6463
mock_async_req_class.return_value = mock_async_req_instance
65-
6664
token = await auth_methods.aget_google_id_token()
6765

6866
mock_default_async.assert_called_once_with()
@@ -76,49 +74,46 @@ async def test_aget_google_id_token_success_first_call(
7674
assert token == f"{auth_methods.BEARER_TOKEN_PREFIX}{MOCK_GOOGLE_ID_TOKEN}"
7775
assert auth_methods._cached_google_id_token["token"] == MOCK_GOOGLE_ID_TOKEN
7876
assert (
79-
auth_methods._cached_google_id_token["expires_at"] == MOCK_EXPIRY_TIMESTAMP
77+
auth_methods._cached_google_id_token["expires_at"] == MOCK_EXPIRY_DATETIME
8078
)
81-
mock_decode_expiry.assert_called_once_with(MOCK_GOOGLE_ID_TOKEN)
8279

8380
@pytest.mark.asyncio
84-
@patch("toolbox_core.auth_methods._decode_jwt_and_get_expiry")
8581
@patch("toolbox_core.auth_methods._aiohttp_requests.Request")
8682
@patch("toolbox_core.auth_methods.default_async", new_callable=MagicMock)
8783
async def test_aget_google_id_token_success_uses_cache(
88-
self, mock_default_async, mock_async_req_class, mock_decode_expiry
84+
self, mock_default_async, mock_async_req_class
8985
):
9086
"""Tests that subsequent calls use the cached token if valid."""
9187
auth_methods._cached_google_id_token["token"] = MOCK_GOOGLE_ID_TOKEN
9288
auth_methods._cached_google_id_token["expires_at"] = (
93-
time.time() + auth_methods.CACHE_REFRESH_MARGIN_SECONDS + 100
89+
auth_methods.datetime.now(auth_methods.timezone.utc) +
90+
auth_methods.timedelta(seconds=auth_methods.CACHE_REFRESH_MARGIN_SECONDS + 100)
9491
) # Ensure it's valid
9592

9693
token = await auth_methods.aget_google_id_token()
9794

9895
mock_default_async.assert_not_called()
9996
mock_async_req_class.assert_not_called()
100-
mock_decode_expiry.assert_not_called()
10197

10298
assert token == f"{auth_methods.BEARER_TOKEN_PREFIX}{MOCK_GOOGLE_ID_TOKEN}"
10399
assert auth_methods._cached_google_id_token["token"] == MOCK_GOOGLE_ID_TOKEN
104100

105101
@pytest.mark.asyncio
106-
@patch("toolbox_core.auth_methods._decode_jwt_and_get_expiry")
107102
@patch("toolbox_core.auth_methods._aiohttp_requests.Request")
108103
@patch("toolbox_core.auth_methods.default_async", new_callable=MagicMock)
109104
async def test_aget_google_id_token_refreshes_expired_cache(
110-
self, mock_default_async, mock_async_req_class, mock_decode_expiry
105+
self, mock_default_async, mock_async_req_class
111106
):
112107
"""Tests that an expired cached token triggers a refresh."""
113108
auth_methods._cached_google_id_token["token"] = "expired_token"
114109
auth_methods._cached_google_id_token["expires_at"] = (
115-
time.time() - 100
110+
auth_methods.datetime.now(auth_methods.timezone.utc) - auth_methods.timedelta(seconds=100)
116111
) # Expired
117112

118113
mock_creds_instance = AsyncMock()
119114
mock_creds_instance.id_token = MOCK_GOOGLE_ID_TOKEN # New token after refresh
115+
type(mock_creds_instance).expiry = PropertyMock(return_value=MOCK_EXPIRY_DATETIME)
120116
mock_default_async.return_value = (mock_creds_instance, MOCK_PROJECT_ID)
121-
mock_decode_expiry.return_value = MOCK_EXPIRY_TIMESTAMP
122117

123118
mock_async_req_instance = MagicMock()
124119
mock_async_req_class.return_value = mock_async_req_instance
@@ -130,10 +125,8 @@ async def test_aget_google_id_token_refreshes_expired_cache(
130125
mock_creds_instance.refresh.assert_called_once_with(mock_async_req_instance)
131126
assert token == f"{auth_methods.BEARER_TOKEN_PREFIX}{MOCK_GOOGLE_ID_TOKEN}"
132127
assert auth_methods._cached_google_id_token["token"] == MOCK_GOOGLE_ID_TOKEN
133-
assert (
134-
auth_methods._cached_google_id_token["expires_at"] == MOCK_EXPIRY_TIMESTAMP
135-
)
136-
mock_decode_expiry.assert_called_once_with(MOCK_GOOGLE_ID_TOKEN)
128+
assert auth_methods._cached_google_id_token["expires_at"] == MOCK_EXPIRY_DATETIME
129+
137130

138131
@pytest.mark.asyncio
139132
@patch("toolbox_core.auth_methods._aiohttp_requests.Request")
@@ -144,6 +137,7 @@ async def test_aget_google_id_token_fetch_failure(
144137
"""Tests error handling when fetching the token fails (no id_token returned)."""
145138
mock_creds_instance = AsyncMock()
146139
mock_creds_instance.id_token = None # Simulate no ID token after refresh
140+
type(mock_creds_instance).expiry = PropertyMock(return_value=MOCK_EXPIRY_DATETIME) # Still need expiry for update_cache
147141
mock_default_async.return_value = (mock_creds_instance, MOCK_PROJECT_ID)
148142
mock_async_req_class.return_value = MagicMock()
149143

@@ -176,20 +170,19 @@ async def test_aget_google_id_token_refresh_raises_exception(
176170
mock_creds_instance.refresh.assert_called_once()
177171

178172
@pytest.mark.asyncio
179-
@patch("toolbox_core.auth_methods._decode_jwt_and_get_expiry")
180173
@patch("toolbox_core.auth_methods._aiohttp_requests.Request")
181174
@patch("toolbox_core.auth_methods.default_async", new_callable=MagicMock)
182175
async def test_aget_google_id_token_no_expiry_info(
183-
self, mock_default_async, mock_async_req_class, mock_decode_expiry
176+
self, mock_default_async, mock_async_req_class
184177
):
185178
"""Tests that a token without expiry info is still cached but effectively expired."""
186179
mock_creds_instance = AsyncMock()
187180
mock_creds_instance.id_token = MOCK_GOOGLE_ID_TOKEN
181+
type(mock_creds_instance).expiry = PropertyMock(return_value=None) # Simulate no expiry info
188182
mock_default_async.return_value = (mock_creds_instance, MOCK_PROJECT_ID)
189-
mock_decode_expiry.return_value = None # Simulate no expiry info
190183

191184
mock_async_req_class.return_value = MagicMock()
192-
185+
193186
token = await auth_methods.aget_google_id_token()
194187

195188
assert token == f"{auth_methods.BEARER_TOKEN_PREFIX}{MOCK_GOOGLE_ID_TOKEN}"
@@ -198,13 +191,11 @@ async def test_aget_google_id_token_no_expiry_info(
198191
auth_methods._cached_google_id_token["expires_at"] == 0
199192
) # Should be 0 if no expiry
200193
mock_async_req_class.assert_called_once_with()
201-
mock_decode_expiry.assert_called_once_with(MOCK_GOOGLE_ID_TOKEN)
202194

203195

204196
class TestSyncAuthMethods:
205197
"""Tests for synchronous Google ID token fetching."""
206198

207-
@patch("toolbox_core.auth_methods._decode_jwt_and_get_expiry")
208199
@patch("toolbox_core.auth_methods.Request")
209200
@patch("toolbox_core.auth_methods.AuthorizedSession")
210201
@patch("toolbox_core.auth_methods.google.auth.default")
@@ -213,13 +204,12 @@ def test_get_google_id_token_success_first_call(
213204
mock_sync_default,
214205
mock_auth_session_class,
215206
mock_sync_req_class,
216-
mock_decode_expiry,
217207
):
218208
"""Tests successful fetching of a sync token on the first call."""
219209
mock_creds_instance = MagicMock()
220210
mock_creds_instance.id_token = MOCK_GOOGLE_ID_TOKEN
211+
type(mock_creds_instance).expiry = PropertyMock(return_value=MOCK_EXPIRY_DATETIME)
221212
mock_sync_default.return_value = (mock_creds_instance, MOCK_PROJECT_ID)
222-
mock_decode_expiry.return_value = MOCK_EXPIRY_TIMESTAMP
223213

224214
mock_session_instance = MagicMock()
225215
mock_auth_session_class.return_value = mock_session_instance
@@ -237,11 +227,9 @@ def test_get_google_id_token_success_first_call(
237227
assert token == f"{auth_methods.BEARER_TOKEN_PREFIX}{MOCK_GOOGLE_ID_TOKEN}"
238228
assert auth_methods._cached_google_id_token["token"] == MOCK_GOOGLE_ID_TOKEN
239229
assert (
240-
auth_methods._cached_google_id_token["expires_at"] == MOCK_EXPIRY_TIMESTAMP
230+
auth_methods._cached_google_id_token["expires_at"] == MOCK_EXPIRY_DATETIME
241231
)
242-
mock_decode_expiry.assert_called_once_with(MOCK_GOOGLE_ID_TOKEN)
243232

244-
@patch("toolbox_core.auth_methods._decode_jwt_and_get_expiry")
245233
@patch("toolbox_core.auth_methods.Request")
246234
@patch("toolbox_core.auth_methods.AuthorizedSession")
247235
@patch("toolbox_core.auth_methods.google.auth.default")
@@ -250,25 +238,23 @@ def test_get_google_id_token_success_uses_cache(
250238
mock_sync_default,
251239
mock_auth_session_class,
252240
mock_sync_req_class,
253-
mock_decode_expiry,
254241
):
255242
"""Tests that subsequent calls use the cached token if valid."""
256243
auth_methods._cached_google_id_token["token"] = MOCK_GOOGLE_ID_TOKEN
257244
auth_methods._cached_google_id_token["expires_at"] = (
258-
time.time() + auth_methods.CACHE_REFRESH_MARGIN_SECONDS + 100
245+
auth_methods.datetime.now(auth_methods.timezone.utc) +
246+
auth_methods.timedelta(seconds=auth_methods.CACHE_REFRESH_MARGIN_SECONDS + 100)
259247
) # Ensure it's valid
260248

261249
token = auth_methods.get_google_id_token()
262250

263251
mock_sync_default.assert_not_called()
264252
mock_auth_session_class.assert_not_called()
265253
mock_sync_req_class.assert_not_called()
266-
mock_decode_expiry.assert_not_called()
267254

268255
assert token == f"{auth_methods.BEARER_TOKEN_PREFIX}{MOCK_GOOGLE_ID_TOKEN}"
269256
assert auth_methods._cached_google_id_token["token"] == MOCK_GOOGLE_ID_TOKEN
270257

271-
@patch("toolbox_core.auth_methods._decode_jwt_and_get_expiry")
272258
@patch("toolbox_core.auth_methods.Request")
273259
@patch("toolbox_core.auth_methods.AuthorizedSession")
274260
@patch("toolbox_core.auth_methods.google.auth.default")
@@ -277,19 +263,18 @@ def test_get_google_id_token_refreshes_expired_cache(
277263
mock_sync_default,
278264
mock_auth_session_class,
279265
mock_sync_req_class,
280-
mock_decode_expiry,
281266
):
282267
"""Tests that an expired cached token triggers a refresh."""
283268
# Prime the cache with an expired token
284269
auth_methods._cached_google_id_token["token"] = "expired_token_sync"
285270
auth_methods._cached_google_id_token["expires_at"] = (
286-
time.time() - 100
271+
auth_methods.datetime.now(auth_methods.timezone.utc) - auth_methods.timedelta(seconds=100)
287272
) # Expired
288273

289274
mock_creds_instance = MagicMock()
290275
mock_creds_instance.id_token = MOCK_GOOGLE_ID_TOKEN # New token after refresh
276+
type(mock_creds_instance).expiry = PropertyMock(return_value=MOCK_EXPIRY_DATETIME)
291277
mock_sync_default.return_value = (mock_creds_instance, MOCK_PROJECT_ID)
292-
mock_decode_expiry.return_value = MOCK_EXPIRY_TIMESTAMP
293278

294279
mock_session_instance = MagicMock()
295280
mock_auth_session_class.return_value = mock_session_instance
@@ -305,10 +290,7 @@ def test_get_google_id_token_refreshes_expired_cache(
305290
mock_creds_instance.refresh.assert_called_once_with(mock_sync_request_instance)
306291
assert token == f"{auth_methods.BEARER_TOKEN_PREFIX}{MOCK_GOOGLE_ID_TOKEN}"
307292
assert auth_methods._cached_google_id_token["token"] == MOCK_GOOGLE_ID_TOKEN
308-
assert (
309-
auth_methods._cached_google_id_token["expires_at"] == MOCK_EXPIRY_TIMESTAMP
310-
)
311-
mock_decode_expiry.assert_called_once_with(MOCK_GOOGLE_ID_TOKEN)
293+
assert auth_methods._cached_google_id_token["expires_at"] == MOCK_EXPIRY_DATETIME
312294

313295
@patch("toolbox_core.auth_methods.Request")
314296
@patch("toolbox_core.auth_methods.AuthorizedSession")
@@ -319,6 +301,7 @@ def test_get_google_id_token_fetch_failure(
319301
"""Tests error handling when fetching the token fails (no id_token returned)."""
320302
mock_creds_instance = MagicMock()
321303
mock_creds_instance.id_token = None # Simulate no ID token after refresh
304+
type(mock_creds_instance).expiry = PropertyMock(return_value=MOCK_EXPIRY_DATETIME) # Still need expiry for update_cache
322305
mock_sync_default.return_value = (mock_creds_instance, MOCK_PROJECT_ID)
323306

324307
mock_session_instance = MagicMock()
@@ -362,7 +345,6 @@ def test_get_google_id_token_refresh_raises_exception(
362345
mock_sync_req_class.assert_called_once_with(mock_session_instance)
363346
mock_creds_instance.refresh.assert_called_once()
364347

365-
@patch("toolbox_core.auth_methods._decode_jwt_and_get_expiry")
366348
@patch("toolbox_core.auth_methods.Request")
367349
@patch("toolbox_core.auth_methods.AuthorizedSession")
368350
@patch("toolbox_core.auth_methods.google.auth.default")
@@ -371,13 +353,12 @@ def test_get_google_id_token_no_expiry_info(
371353
mock_sync_default,
372354
mock_auth_session_class,
373355
mock_sync_req_class,
374-
mock_decode_expiry,
375356
):
376357
"""Tests that a token without expiry info is still cached but effectively expired."""
377358
mock_creds_instance = MagicMock()
378359
mock_creds_instance.id_token = MOCK_GOOGLE_ID_TOKEN
360+
type(mock_creds_instance).expiry = PropertyMock(return_value=None) # Simulate no expiry info
379361
mock_sync_default.return_value = (mock_creds_instance, MOCK_PROJECT_ID)
380-
mock_decode_expiry.return_value = None # Simulate no expiry info
381362

382363
mock_session_instance = MagicMock()
383364
mock_auth_session_class.return_value = mock_session_instance
@@ -395,4 +376,3 @@ def test_get_google_id_token_no_expiry_info(
395376
mock_sync_default.assert_called_once_with()
396377
mock_auth_session_class.assert_called_once_with(mock_creds_instance)
397378
mock_sync_req_class.assert_called_once_with(mock_session_instance)
398-
mock_decode_expiry.assert_called_once_with(MOCK_GOOGLE_ID_TOKEN)

0 commit comments

Comments
 (0)