1111# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212# See the License for the specific language governing permissions and
1313# limitations under the License.
14+ """Unit tests for the GcpAuthProvider class."""
1415
1516from unittest .mock import AsyncMock
1617from unittest .mock import Mock
1718from unittest .mock import patch
1819
20+
1921from google .adk .agents .callback_context import CallbackContext
2022from google .adk .auth .auth_credential import AuthCredential
2123from google .adk .auth .auth_tool import AuthConfig
2224from google .adk .integrations .agent_identity import GcpAuthProvider
2325from google .adk .integrations .agent_identity import GcpAuthProviderScheme
24- from google .adk .integrations .agent_identity ._iam_connector_credentials_provider import _IamConnectorCredentialsProvider
2526import pytest
2627
2728
2829@pytest .fixture
2930def auth_config ():
30- scheme = GcpAuthProviderScheme (
31- name = "projects/test-project/locations/global/connectors/test-connector" ,
32- scopes = ["test-scope" ],
33- continue_uri = "https://example.com/continue" ,
34- )
35- return Mock (spec = AuthConfig , auth_scheme = scheme )
31+ config = Mock (spec = AuthConfig )
32+ config .auth_scheme = Mock (spec = GcpAuthProviderScheme )
33+ return config
3634
3735
3836@pytest .fixture
@@ -43,45 +41,72 @@ def context():
4341
4442
4543@pytest .fixture
46- def provider ():
44+ def gcp_auth_provider ():
4745 return GcpAuthProvider ()
4846
4947
50- def test_supported_auth_schemes (provider ):
48+ def test_supported_auth_schemes (gcp_auth_provider ):
5149 """Verify the provider supports the correct auth scheme."""
52- assert GcpAuthProviderScheme in provider .supported_auth_schemes
50+ assert GcpAuthProviderScheme in gcp_auth_provider .supported_auth_schemes
51+
52+
53+ async def test_get_auth_credential_raises_error_for_invalid_auth_scheme (
54+ context ,
55+ ):
56+ """Test get_auth_credential raises ValueError for invalid auth scheme."""
57+ provider = GcpAuthProvider ()
58+ invalid_auth_config = Mock (spec = AuthConfig )
59+ invalid_auth_config .auth_scheme = Mock () # Not GcpAuthProviderScheme
60+
61+ with pytest .raises (ValueError , match = "Expected GcpAuthProviderScheme, got" ):
62+ await provider .get_auth_credential (invalid_auth_config , context )
5363
5464
5565@patch (
5666 "google.adk.integrations.agent_identity.gcp_auth_provider._IamConnectorCredentialsProvider"
5767)
58- async def test_gcp_auth_provider_delegates_get_auth_credential (
59- mock_provider_class , auth_config , context
68+ async def test_get_auth_credential_routes_to_iam_connector_service_provider (
69+ mock_iam_cls , auth_config , context
6070):
61- """Test that get_auth_credential delegates to the internal provider."""
71+ """Test routing to IAM Connector Credentials service for legacy auth provider resource names."""
72+ auth_config .auth_scheme .name = (
73+ "projects/test-project/locations/test-location/connectors/test-connector"
74+ )
6275 provider = GcpAuthProvider ()
6376
6477 mock_credential = Mock (spec = AuthCredential )
65- mock_provider_instance = mock_provider_class .return_value
66- mock_provider_instance .get_auth_credential = AsyncMock (
78+ mock_iam_provider = mock_iam_cls .return_value
79+ mock_iam_provider .get_auth_credential = AsyncMock (
6780 return_value = mock_credential
6881 )
6982
7083 result = await provider .get_auth_credential (auth_config , context )
7184
7285 assert result == mock_credential
73- mock_provider_instance .get_auth_credential .assert_awaited_once_with (
86+ mock_iam_provider .get_auth_credential .assert_awaited_once_with (
7487 auth_scheme = auth_config .auth_scheme , context = context
7588 )
7689
7790
78- async def test_get_auth_credential_raises_error_for_invalid_auth_scheme (
79- context ,
91+ @patch (
92+ "google.adk.integrations.agent_identity.gcp_auth_provider._AgentIdentityCredentialsProvider"
93+ )
94+ async def test_get_auth_credential_routes_to_agent_identity_service_provider (
95+ mock_agent_cls , auth_config , context
8096):
81- """Test get_auth_credential raises ValueError for invalid auth scheme."""
97+ """Test routing to Agent Identity Credentials service for new auth provider resource names."""
98+ auth_config .auth_scheme .name = "projects/test-project/locations/test-location/authProviders/test-provider"
8299 provider = GcpAuthProvider ()
83- invalid_auth_config = Mock (spec = AuthConfig )
84- invalid_auth_config .auth_scheme = Mock () # Not GcpAuthProviderScheme
85100
86- with pytest .raises (ValueError , match = "Expected GcpAuthProviderScheme, got" ):
87- await provider .get_auth_credential (invalid_auth_config , context )
101+ mock_credential = Mock (spec = AuthCredential )
102+ mock_agent_provider = mock_agent_cls .return_value
103+ mock_agent_provider .get_auth_credential = AsyncMock (
104+ return_value = mock_credential
105+ )
106+
107+ result = await provider .get_auth_credential (auth_config , context )
108+
109+ assert result == mock_credential
110+ mock_agent_provider .get_auth_credential .assert_awaited_once_with (
111+ auth_scheme = auth_config .auth_scheme , context = context
112+ )
0 commit comments