|
25 | 25 | from google.adk.agents.callback_context import CallbackContext |
26 | 26 | from google.adk.auth.auth_credential import AuthCredentialTypes |
27 | 27 | from google.adk.auth.auth_tool import AuthConfig |
| 28 | +from google.adk.auth.auth_tool import AuthToolArguments |
28 | 29 | from google.adk.flows.llm_flows.functions import REQUEST_EUC_FUNCTION_CALL_NAME |
29 | 30 | from google.adk.integrations.agent_identity import gcp_auth_provider |
30 | 31 | from google.adk.integrations.agent_identity import GcpAuthProvider |
@@ -399,7 +400,9 @@ async def test_get_auth_credential_returns_token_if_consent_was_completed( |
399 | 400 | function_call = Mock() |
400 | 401 | function_call.id = "auth-req-1" |
401 | 402 | function_call.name = REQUEST_EUC_FUNCTION_CALL_NAME |
402 | | - function_call.args = {"function_call_id": "call-123"} |
| 403 | + function_call.args = AuthToolArguments( |
| 404 | + function_call_id="call-123", auth_config=auth_config |
| 405 | + ).model_dump(by_alias=True, exclude_none=True) |
403 | 406 |
|
404 | 407 | event1 = Mock() |
405 | 408 | event1.get_function_calls.return_value = [function_call] |
@@ -432,3 +435,43 @@ async def test_get_auth_credential_returns_token_if_consent_was_completed( |
432 | 435 | assert auth_credential.auth_type == AuthCredentialTypes.HTTP |
433 | 436 | assert auth_credential.http.scheme == "bearer" |
434 | 437 | assert auth_credential.http.credentials.token == "test-token" |
| 438 | + |
| 439 | + |
| 440 | +async def test_get_auth_credential_raises_error_if_consent_canceled( |
| 441 | + mock_operation, auth_config, context, provider |
| 442 | +): |
| 443 | + function_call = Mock() |
| 444 | + function_call.id = "auth-req-1" |
| 445 | + function_call.name = REQUEST_EUC_FUNCTION_CALL_NAME |
| 446 | + function_call.args = AuthToolArguments( |
| 447 | + function_call_id="call-123", auth_config=auth_config |
| 448 | + ).model_dump(by_alias=True, exclude_none=True) |
| 449 | + |
| 450 | + event1 = Mock() |
| 451 | + event1.get_function_calls.return_value = [function_call] |
| 452 | + event1.get_function_responses.return_value = [] |
| 453 | + |
| 454 | + function_response = Mock() |
| 455 | + function_response.id = "auth-req-1" |
| 456 | + function_response.name = REQUEST_EUC_FUNCTION_CALL_NAME |
| 457 | + |
| 458 | + event2 = Mock() |
| 459 | + event2.get_function_calls.return_value = [] |
| 460 | + event2.get_function_responses.return_value = [function_response] |
| 461 | + |
| 462 | + context.session.events = [event1, event2] |
| 463 | + context.function_call_id = "call-123" |
| 464 | + |
| 465 | + meta = RetrieveCredentialsMetadata({ |
| 466 | + "uri_consent_required": { |
| 467 | + "authorization_uri": "https://example.com/auth", |
| 468 | + "consent_nonce": "sample-nonce", |
| 469 | + } |
| 470 | + }) |
| 471 | + mock_operation.metadata.value = RetrieveCredentialsMetadata.serialize(meta) |
| 472 | + mock_operation.done = False |
| 473 | + |
| 474 | + with pytest.raises( |
| 475 | + RuntimeError, match="Failed to retrieve consent based credential." |
| 476 | + ): |
| 477 | + await provider.get_auth_credential(auth_config, context) |
0 commit comments