12
12
# See the License for the specific language governing permissions and
13
13
# limitations under the License.
14
14
15
- from unittest .mock import AsyncMock , MagicMock , patch , PropertyMock
15
+ from unittest .mock import AsyncMock , MagicMock , PropertyMock , patch
16
16
17
17
import pytest
18
18
22
22
MOCK_GOOGLE_ID_TOKEN = "test_id_token_123"
23
23
MOCK_PROJECT_ID = "test-project"
24
24
# A realistic expiry timestamp (e.g., 1 hour from now)
25
- MOCK_EXPIRY_DATETIME = auth_methods .datetime .now (auth_methods .timezone .utc ) + auth_methods .timedelta (hours = 1 )
25
+ MOCK_EXPIRY_DATETIME = auth_methods .datetime .now (
26
+ auth_methods .timezone .utc
27
+ ) + auth_methods .timedelta (hours = 1 )
26
28
27
29
28
30
# Expected exception messages from auth_methods.py
@@ -56,7 +58,9 @@ async def test_aget_google_id_token_success_first_call(
56
58
"""Tests successful fetching of an async token on the first call."""
57
59
mock_creds_instance = AsyncMock ()
58
60
mock_creds_instance .id_token = MOCK_GOOGLE_ID_TOKEN
59
- type(mock_creds_instance ).expiry = PropertyMock (return_value = MOCK_EXPIRY_DATETIME )
61
+ type(mock_creds_instance ).expiry = PropertyMock (
62
+ return_value = MOCK_EXPIRY_DATETIME
63
+ )
60
64
mock_default_async .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
61
65
62
66
mock_async_req_instance = MagicMock ()
@@ -85,9 +89,10 @@ async def test_aget_google_id_token_success_uses_cache(
85
89
):
86
90
"""Tests that subsequent calls use the cached token if valid."""
87
91
auth_methods ._cached_google_id_token ["token" ] = MOCK_GOOGLE_ID_TOKEN
88
- auth_methods ._cached_google_id_token ["expires_at" ] = (
89
- auth_methods .datetime .now (auth_methods .timezone .utc ) +
90
- auth_methods .timedelta (seconds = auth_methods .CACHE_REFRESH_MARGIN_SECONDS + 100 )
92
+ auth_methods ._cached_google_id_token ["expires_at" ] = auth_methods .datetime .now (
93
+ auth_methods .timezone .utc
94
+ ) + auth_methods .timedelta (
95
+ seconds = auth_methods .CACHE_REFRESH_MARGIN_SECONDS + 100
91
96
) # Ensure it's valid
92
97
93
98
token = await auth_methods .aget_google_id_token ()
@@ -106,13 +111,17 @@ async def test_aget_google_id_token_refreshes_expired_cache(
106
111
):
107
112
"""Tests that an expired cached token triggers a refresh."""
108
113
auth_methods ._cached_google_id_token ["token" ] = "expired_token"
109
- auth_methods ._cached_google_id_token ["expires_at" ] = (
110
- auth_methods .datetime .now (auth_methods .timezone .utc ) - auth_methods .timedelta (seconds = 100 )
114
+ auth_methods ._cached_google_id_token ["expires_at" ] = auth_methods .datetime .now (
115
+ auth_methods .timezone .utc
116
+ ) - auth_methods .timedelta (
117
+ seconds = 100
111
118
) # Expired
112
119
113
120
mock_creds_instance = AsyncMock ()
114
121
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 )
122
+ type(mock_creds_instance ).expiry = PropertyMock (
123
+ return_value = MOCK_EXPIRY_DATETIME
124
+ )
116
125
mock_default_async .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
117
126
118
127
mock_async_req_instance = MagicMock ()
@@ -125,8 +134,9 @@ async def test_aget_google_id_token_refreshes_expired_cache(
125
134
mock_creds_instance .refresh .assert_called_once_with (mock_async_req_instance )
126
135
assert token == f"{ auth_methods .BEARER_TOKEN_PREFIX } { MOCK_GOOGLE_ID_TOKEN } "
127
136
assert auth_methods ._cached_google_id_token ["token" ] == MOCK_GOOGLE_ID_TOKEN
128
- assert auth_methods ._cached_google_id_token ["expires_at" ] == MOCK_EXPIRY_DATETIME
129
-
137
+ assert (
138
+ auth_methods ._cached_google_id_token ["expires_at" ] == MOCK_EXPIRY_DATETIME
139
+ )
130
140
131
141
@pytest .mark .asyncio
132
142
@patch ("toolbox_core.auth_methods._aiohttp_requests.Request" )
@@ -137,7 +147,9 @@ async def test_aget_google_id_token_fetch_failure(
137
147
"""Tests error handling when fetching the token fails (no id_token returned)."""
138
148
mock_creds_instance = AsyncMock ()
139
149
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
150
+ type(mock_creds_instance ).expiry = PropertyMock (
151
+ return_value = MOCK_EXPIRY_DATETIME
152
+ ) # Still need expiry for update_cache
141
153
mock_default_async .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
142
154
mock_async_req_class .return_value = MagicMock ()
143
155
@@ -178,11 +190,13 @@ async def test_aget_google_id_token_no_expiry_info(
178
190
"""Tests that a token without expiry info is still cached but effectively expired."""
179
191
mock_creds_instance = AsyncMock ()
180
192
mock_creds_instance .id_token = MOCK_GOOGLE_ID_TOKEN
181
- type(mock_creds_instance ).expiry = PropertyMock (return_value = None ) # Simulate no expiry info
193
+ type(mock_creds_instance ).expiry = PropertyMock (
194
+ return_value = None
195
+ ) # Simulate no expiry info
182
196
mock_default_async .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
183
197
184
198
mock_async_req_class .return_value = MagicMock ()
185
-
199
+
186
200
token = await auth_methods .aget_google_id_token ()
187
201
188
202
assert token == f"{ auth_methods .BEARER_TOKEN_PREFIX } { MOCK_GOOGLE_ID_TOKEN } "
@@ -208,7 +222,9 @@ def test_get_google_id_token_success_first_call(
208
222
"""Tests successful fetching of a sync token on the first call."""
209
223
mock_creds_instance = MagicMock ()
210
224
mock_creds_instance .id_token = MOCK_GOOGLE_ID_TOKEN
211
- type(mock_creds_instance ).expiry = PropertyMock (return_value = MOCK_EXPIRY_DATETIME )
225
+ type(mock_creds_instance ).expiry = PropertyMock (
226
+ return_value = MOCK_EXPIRY_DATETIME
227
+ )
212
228
mock_sync_default .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
213
229
214
230
mock_session_instance = MagicMock ()
@@ -241,9 +257,10 @@ def test_get_google_id_token_success_uses_cache(
241
257
):
242
258
"""Tests that subsequent calls use the cached token if valid."""
243
259
auth_methods ._cached_google_id_token ["token" ] = MOCK_GOOGLE_ID_TOKEN
244
- auth_methods ._cached_google_id_token ["expires_at" ] = (
245
- auth_methods .datetime .now (auth_methods .timezone .utc ) +
246
- auth_methods .timedelta (seconds = auth_methods .CACHE_REFRESH_MARGIN_SECONDS + 100 )
260
+ auth_methods ._cached_google_id_token ["expires_at" ] = auth_methods .datetime .now (
261
+ auth_methods .timezone .utc
262
+ ) + auth_methods .timedelta (
263
+ seconds = auth_methods .CACHE_REFRESH_MARGIN_SECONDS + 100
247
264
) # Ensure it's valid
248
265
249
266
token = auth_methods .get_google_id_token ()
@@ -267,13 +284,17 @@ def test_get_google_id_token_refreshes_expired_cache(
267
284
"""Tests that an expired cached token triggers a refresh."""
268
285
# Prime the cache with an expired token
269
286
auth_methods ._cached_google_id_token ["token" ] = "expired_token_sync"
270
- auth_methods ._cached_google_id_token ["expires_at" ] = (
271
- auth_methods .datetime .now (auth_methods .timezone .utc ) - auth_methods .timedelta (seconds = 100 )
287
+ auth_methods ._cached_google_id_token ["expires_at" ] = auth_methods .datetime .now (
288
+ auth_methods .timezone .utc
289
+ ) - auth_methods .timedelta (
290
+ seconds = 100
272
291
) # Expired
273
292
274
293
mock_creds_instance = MagicMock ()
275
294
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 )
295
+ type(mock_creds_instance ).expiry = PropertyMock (
296
+ return_value = MOCK_EXPIRY_DATETIME
297
+ )
277
298
mock_sync_default .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
278
299
279
300
mock_session_instance = MagicMock ()
@@ -290,7 +311,9 @@ def test_get_google_id_token_refreshes_expired_cache(
290
311
mock_creds_instance .refresh .assert_called_once_with (mock_sync_request_instance )
291
312
assert token == f"{ auth_methods .BEARER_TOKEN_PREFIX } { MOCK_GOOGLE_ID_TOKEN } "
292
313
assert auth_methods ._cached_google_id_token ["token" ] == MOCK_GOOGLE_ID_TOKEN
293
- assert auth_methods ._cached_google_id_token ["expires_at" ] == MOCK_EXPIRY_DATETIME
314
+ assert (
315
+ auth_methods ._cached_google_id_token ["expires_at" ] == MOCK_EXPIRY_DATETIME
316
+ )
294
317
295
318
@patch ("toolbox_core.auth_methods.Request" )
296
319
@patch ("toolbox_core.auth_methods.AuthorizedSession" )
@@ -301,7 +324,9 @@ def test_get_google_id_token_fetch_failure(
301
324
"""Tests error handling when fetching the token fails (no id_token returned)."""
302
325
mock_creds_instance = MagicMock ()
303
326
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
327
+ type(mock_creds_instance ).expiry = PropertyMock (
328
+ return_value = MOCK_EXPIRY_DATETIME
329
+ ) # Still need expiry for update_cache
305
330
mock_sync_default .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
306
331
307
332
mock_session_instance = MagicMock ()
@@ -357,7 +382,9 @@ def test_get_google_id_token_no_expiry_info(
357
382
"""Tests that a token without expiry info is still cached but effectively expired."""
358
383
mock_creds_instance = MagicMock ()
359
384
mock_creds_instance .id_token = MOCK_GOOGLE_ID_TOKEN
360
- type(mock_creds_instance ).expiry = PropertyMock (return_value = None ) # Simulate no expiry info
385
+ type(mock_creds_instance ).expiry = PropertyMock (
386
+ return_value = None
387
+ ) # Simulate no expiry info
361
388
mock_sync_default .return_value = (mock_creds_instance , MOCK_PROJECT_ID )
362
389
363
390
mock_session_instance = MagicMock ()
0 commit comments