forked from google/adk-python
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_base_google_credentials_manager.py
More file actions
464 lines (391 loc) · 18.1 KB
/
test_base_google_credentials_manager.py
File metadata and controls
464 lines (391 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# Copyright 2025 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from unittest.mock import Mock
from unittest.mock import patch
from google.adk.auth.auth_tool import AuthConfig
from google.adk.tools._google_credentials import GoogleCredentialsManager
from google.adk.tools.bigquery.bigquery_credentials import BIGQUERY_TOKEN_CACHE_KEY
from google.adk.tools.bigquery.bigquery_credentials import BigQueryCredentialsConfig
from google.adk.tools.tool_context import ToolContext
from google.auth.credentials import Credentials as AuthCredentials
from google.auth.exceptions import RefreshError
# Mock the Google OAuth and API dependencies
from google.oauth2.credentials import Credentials as OAuthCredentials
import pytest
class TestGoogleCredentialsManager:
"""Test suite for GoogleCredentialsManager OAuth flow handling.
This class tests the complex credential management logic including
credential validation, refresh, OAuth flow orchestration, and the
new token caching functionality through tool_context.state.
"""
@pytest.fixture
def mock_tool_context(self):
"""Create a mock ToolContext for testing.
The ToolContext is the interface between tools and the broader
agent framework, handling OAuth flows and state management.
Now includes state dictionary for testing caching behavior.
"""
context = Mock(spec=ToolContext)
context.get_auth_response = Mock(return_value=None)
context.request_credential = Mock()
context.state = {}
return context
@pytest.fixture
def credentials_config(self):
"""Create a basic credentials configuration for testing."""
return BigQueryCredentialsConfig(
client_id="test_client_id",
client_secret="test_client_secret",
scopes=["https://www.googleapis.com/auth/calendar"],
)
@pytest.fixture
def manager(self, credentials_config):
"""Create a credentials manager instance for testing."""
return GoogleCredentialsManager(credentials_config)
@pytest.mark.parametrize(
("credentials_class",),
[
pytest.param(OAuthCredentials, id="oauth"),
pytest.param(AuthCredentials, id="auth"),
],
)
@pytest.mark.asyncio
async def test_get_valid_credentials_with_valid_existing_creds(
self, manager, mock_tool_context, credentials_class
):
"""Test that valid existing credentials are returned immediately.
When credentials are already valid, no refresh or OAuth flow
should be needed. This is the optimal happy path scenario.
"""
# Create mock credentials that are already valid
mock_creds = Mock(spec=credentials_class)
mock_creds.valid = True
manager.credentials_config.credentials = mock_creds
result = await manager.get_valid_credentials(mock_tool_context)
assert result == mock_creds
# Verify no OAuth flow was triggered
mock_tool_context.get_auth_response.assert_not_called()
mock_tool_context.request_credential.assert_not_called()
@pytest.mark.parametrize(
("valid",),
[
pytest.param(False, id="invalid"),
pytest.param(True, id="valid"),
],
)
@pytest.mark.asyncio
async def test_get_valid_credentials_with_existing_non_oauth_creds(
self, manager, mock_tool_context, valid
):
"""Test that existing non-oauth credentials are returned immediately.
When credentials are of non-oauth type, no refresh or OAuth flow
is triggered irrespective of whether or not it is valid.
"""
# Create mock credentials that are already valid
mock_creds = Mock(spec=AuthCredentials)
mock_creds.valid = valid
manager.credentials_config.credentials = mock_creds
result = await manager.get_valid_credentials(mock_tool_context)
assert result == mock_creds
# Verify no OAuth flow was triggered
mock_tool_context.get_auth_response.assert_not_called()
mock_tool_context.request_credential.assert_not_called()
@pytest.mark.asyncio
async def test_get_credentials_from_cache_when_none_in_manager(
self, manager, mock_tool_context
):
"""Test retrieving credentials from tool_context cache when manager has none.
This tests the new caching functionality where credentials can be
retrieved from the tool context state when the manager instance
doesn't have them loaded.
"""
# Manager starts with no credentials
manager.credentials_config.credentials = None
# Create mock cached credentials JSON that would be stored in cache
mock_cached_creds_json = json.dumps({
"token": "cached_token",
"refresh_token": "cached_refresh_token",
"client_id": "test_client_id",
"client_secret": "test_client_secret",
})
# Set up the tool context state to contain cached credentials
mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY] = mock_cached_creds_json
# Mock the Credentials.from_authorized_user_info method
with patch(
"google.oauth2.credentials.Credentials.from_authorized_user_info"
) as mock_from_json:
mock_creds = Mock(spec=OAuthCredentials)
mock_creds.valid = True
mock_from_json.return_value = mock_creds
result = await manager.get_valid_credentials(mock_tool_context)
# Verify credentials were created from cached JSON
mock_from_json.assert_called_once_with(
json.loads(mock_cached_creds_json), manager.credentials_config.scopes
)
# Verify loaded credentials were not cached into manager
assert manager.credentials_config.credentials is None
# Verify valid cached credentials were returned
assert result == mock_creds
@pytest.mark.asyncio
async def test_no_credentials_in_manager_or_cache(
self, manager, mock_tool_context
):
"""Test OAuth flow when no credentials exist in manager or cache.
This tests the scenario where both the manager and cache are empty,
requiring a new OAuth flow to be initiated.
"""
# Manager starts with no credentials
manager.credentials_config.credentials = None
# Cache is also empty (state dict doesn't contain the key)
result = await manager.get_valid_credentials(mock_tool_context)
# Should trigger OAuth flow and return None (flow in progress)
assert result is None
mock_tool_context.request_credential.assert_called_once()
@pytest.mark.asyncio
@patch("google.auth.transport.requests.Request")
async def test_refresh_cached_credentials_success(
self, mock_request_class, manager, mock_tool_context
):
"""Test successful refresh of expired credentials retrieved from cache.
This tests the interaction between caching and refresh functionality,
ensuring that expired cached credentials can be refreshed properly.
"""
# Manager starts with no default credentials
manager.credentials_config.credentials = None
# Create mock cached credentials JSON
mock_cached_creds_json = json.dumps({
"token": "expired_token",
"refresh_token": "valid_refresh_token",
"client_id": "test_client_id",
"client_secret": "test_client_secret",
})
mock_refreshed_creds_json = json.dumps({
"token": "new_token",
"refresh_token": "valid_refresh_token",
"client_id": "test_client_id",
"client_secret": "test_client_secret",
})
# Set up the tool context state to contain cached credentials
mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY] = mock_cached_creds_json
# Create expired cached credentials with refresh token
mock_cached_creds = Mock(spec=OAuthCredentials)
mock_cached_creds.valid = False
mock_cached_creds.expired = True
mock_cached_creds.refresh_token = "valid_refresh_token"
mock_cached_creds.to_json.return_value = mock_refreshed_creds_json
# Mock successful refresh
def mock_refresh(request):
mock_cached_creds.valid = True
mock_cached_creds.refresh = Mock(side_effect=mock_refresh)
# Mock the Credentials.from_authorized_user_info method
with patch(
"google.oauth2.credentials.Credentials.from_authorized_user_info"
) as mock_from_json:
mock_from_json.return_value = mock_cached_creds
result = await manager.get_valid_credentials(mock_tool_context)
# Verify credentials were created from cached JSON
mock_from_json.assert_called_once_with(
json.loads(mock_cached_creds_json), manager.credentials_config.scopes
)
# Verify refresh was attempted and succeeded
mock_cached_creds.refresh.assert_called_once()
# Verify refreshed credentials were not cached into manager
assert manager.credentials_config.credentials is None
# Verify refreshed credentials were cached
assert (
"new_token"
== json.loads(mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY])[
"token"
]
)
assert result == mock_cached_creds
@pytest.mark.asyncio
@patch("google.auth.transport.requests.Request")
async def test_get_valid_credentials_with_refresh_success(
self, mock_request_class, manager, mock_tool_context
):
"""Test successful credential refresh when tokens are expired.
This tests the automatic token refresh capability that prevents
users from having to re-authenticate for every expired token.
"""
# Create expired credentials with refresh token
mock_creds = Mock(spec=OAuthCredentials)
mock_creds.valid = False
mock_creds.expired = True
mock_creds.refresh_token = "refresh_token"
# Mock successful refresh
def mock_refresh(request):
mock_creds.valid = True
mock_creds.refresh = Mock(side_effect=mock_refresh)
manager.credentials_config.credentials = mock_creds
result = await manager.get_valid_credentials(mock_tool_context)
assert result == mock_creds
mock_creds.refresh.assert_called_once()
# Verify credentials were cached after successful refresh
assert manager.credentials_config.credentials == mock_creds
@pytest.mark.asyncio
@patch("google.auth.transport.requests.Request")
async def test_get_valid_credentials_with_refresh_failure(
self, mock_request_class, manager, mock_tool_context
):
"""Test OAuth flow trigger when credential refresh fails.
When refresh tokens expire or become invalid, the system should
gracefully fall back to requesting a new OAuth flow.
"""
# Create expired credentials that fail to refresh
mock_creds = Mock(spec=OAuthCredentials)
mock_creds.valid = False
mock_creds.expired = True
mock_creds.refresh_token = "expired_refresh_token"
mock_creds.refresh = Mock(side_effect=RefreshError("Refresh failed"))
manager.credentials_config.credentials = mock_creds
result = await manager.get_valid_credentials(mock_tool_context)
# Should trigger OAuth flow and return None (flow in progress)
assert result is None
mock_tool_context.request_credential.assert_called_once()
@pytest.mark.asyncio
async def test_oauth_flow_completion_with_caching(
self, manager, mock_tool_context
):
"""Test successful OAuth flow completion with proper credential caching.
This tests the happy path where a user completes the OAuth flow
and the system successfully creates and caches new credentials
in both the manager and the tool context state.
"""
# Mock OAuth response indicating completed flow
mock_auth_response = Mock()
mock_auth_response.oauth2.access_token = "new_access_token"
mock_auth_response.oauth2.refresh_token = "new_refresh_token"
mock_tool_context.get_auth_response.return_value = mock_auth_response
# Create a mock credentials instance that will represent our created credentials
mock_creds = Mock(spec=OAuthCredentials)
# Make the JSON match what a real Credentials object would produce
mock_creds_json = (
'{"token": "new_access_token", "refresh_token": "new_refresh_token",'
' "token_uri": "https://oauth2.googleapis.com/token", "client_id":'
' "test_client_id", "client_secret": "test_client_secret", "scopes":'
' ["https://www.googleapis.com/auth/calendar"], "universe_domain":'
' "googleapis.com", "account": ""}'
)
mock_creds.to_json.return_value = mock_creds_json
# Use the full module path as it appears in the project structure
with patch(
"google.adk.tools._google_credentials.google.oauth2.credentials.Credentials",
return_value=mock_creds,
) as mock_credentials_class:
result = await manager.get_valid_credentials(mock_tool_context)
# Verify new credentials were created
assert result == mock_creds
# Verify credentials are created with correct parameters
mock_credentials_class.assert_called_once()
call_kwargs = mock_credentials_class.call_args[1]
assert call_kwargs["token"] == "new_access_token"
assert call_kwargs["refresh_token"] == "new_refresh_token"
# Verify credentials are not cached in manager
assert manager.credentials_config.credentials is None
# Verify credentials are also cached in tool context state
assert (
mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY] == mock_creds_json
)
@pytest.mark.asyncio
async def test_oauth_flow_in_progress(self, manager, mock_tool_context):
"""Test OAuth flow initiation when no auth response is available.
This tests the case where the OAuth flow needs to be started,
and the user hasn't completed authorization yet.
"""
# No existing credentials, no auth response (flow not completed)
manager.credentials_config.credentials = None
mock_tool_context.get_auth_response.return_value = None
result = await manager.get_valid_credentials(mock_tool_context)
# Should return None and request credential flow
assert result is None
mock_tool_context.request_credential.assert_called_once()
# Verify the auth configuration includes correct scopes and endpoints
call_args = mock_tool_context.request_credential.call_args[0][0]
assert isinstance(call_args, AuthConfig)
@pytest.mark.asyncio
async def test_cache_persistence_across_manager_instances(
self, credentials_config, mock_tool_context
):
"""Test that cached credentials persist across different manager instances.
This tests the key benefit of the tool context caching - that
credentials can be shared between different instances of the
credential manager, avoiding redundant OAuth flows.
"""
# Create first manager instance and simulate OAuth completion
manager1 = GoogleCredentialsManager(credentials_config)
# Mock OAuth response for first manager
mock_auth_response = Mock()
mock_auth_response.oauth2.access_token = "cached_access_token"
mock_auth_response.oauth2.refresh_token = "cached_refresh_token"
mock_tool_context.get_auth_response.return_value = mock_auth_response
# Create the mock credentials instance that will be returned by the constructor
mock_creds = Mock(spec=OAuthCredentials)
# Make sure our mock JSON matches the structure that real Credentials objects produce
mock_creds_json = (
'{"token": "cached_access_token", "refresh_token":'
' "cached_refresh_token", "token_uri":'
' "https://oauth2.googleapis.com/token", "client_id": "test_client_id",'
' "client_secret": "test_client_secret", "scopes":'
' ["https://www.googleapis.com/auth/calendar"], "universe_domain":'
' "googleapis.com", "account": ""}'
)
mock_creds.to_json.return_value = mock_creds_json
mock_creds.valid = True
# Use the correct module path - without the 'src.' prefix
with patch(
"google.adk.tools._google_credentials.google.oauth2.credentials.Credentials",
return_value=mock_creds,
) as mock_credentials_class:
# Complete OAuth flow with first manager
result1 = await manager1.get_valid_credentials(mock_tool_context)
# Verify credentials were cached in tool context
assert BIGQUERY_TOKEN_CACHE_KEY in mock_tool_context.state
cached_creds_json = mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY]
assert cached_creds_json == mock_creds_json
# Create second manager instance (simulating new request/session)
manager2 = GoogleCredentialsManager(credentials_config)
credentials_config.credentials = None
# Reset auth response to None (no new OAuth flow available)
mock_tool_context.get_auth_response.return_value = None
# Mock the from_authorized_user_info method for the second manager
with patch(
"google.adk.tools._google_credentials.google.oauth2.credentials.Credentials.from_authorized_user_info"
) as mock_from_json:
mock_cached_creds = Mock(spec=OAuthCredentials)
mock_cached_creds.valid = True
mock_from_json.return_value = mock_cached_creds
# Get credentials with second manager
result2 = await manager2.get_valid_credentials(mock_tool_context)
# Verify second manager retrieved cached credentials successfully
assert result2 == mock_cached_creds
assert manager2.credentials_config.credentials is None
assert (
cached_creds_json == mock_tool_context.state[BIGQUERY_TOKEN_CACHE_KEY]
)
# The from_authorized_user_info should be called with the complete JSON structure
mock_from_json.assert_called_once()
# Extract the actual argument that was passed to verify it's the right JSON structure
actual_json_arg = mock_from_json.call_args[0][0]
# We need to parse and compare the structure rather than exact string match
# since the order of keys in JSON might differ
import json
expected_data = json.loads(mock_creds_json)
actual_data = (
actual_json_arg
if isinstance(actual_json_arg, dict)
else json.loads(actual_json_arg)
)
assert actual_data == expected_data