|
| 1 | +# Copyright 2025 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +from fastapi.openapi.models import OAuth2 |
| 16 | +from fastapi.openapi.models import OAuthFlowAuthorizationCode |
| 17 | +from fastapi.openapi.models import OAuthFlows |
| 18 | +from google.adk.auth.auth_credential import AuthCredential |
| 19 | +from google.adk.auth.auth_credential import AuthCredentialTypes |
| 20 | +from google.adk.auth.auth_credential import OAuth2Auth |
| 21 | +from google.adk.auth.auth_tool import AuthConfig |
| 22 | +import pytest |
| 23 | + |
| 24 | + |
| 25 | +class TestAuthConfig: |
| 26 | + """Tests for the AuthConfig method.""" |
| 27 | + |
| 28 | + |
| 29 | +@pytest.fixture |
| 30 | +def oauth2_auth_scheme(): |
| 31 | + """Create an OAuth2 auth scheme for testing.""" |
| 32 | + # Create the OAuthFlows object first |
| 33 | + flows = OAuthFlows( |
| 34 | + authorizationCode=OAuthFlowAuthorizationCode( |
| 35 | + authorizationUrl="https://example.com/oauth2/authorize", |
| 36 | + tokenUrl="https://example.com/oauth2/token", |
| 37 | + scopes={"read": "Read access", "write": "Write access"}, |
| 38 | + ) |
| 39 | + ) |
| 40 | + |
| 41 | + # Then create the OAuth2 object with the flows |
| 42 | + return OAuth2(flows=flows) |
| 43 | + |
| 44 | + |
| 45 | +@pytest.fixture |
| 46 | +def oauth2_credentials(): |
| 47 | + """Create OAuth2 credentials for testing.""" |
| 48 | + return AuthCredential( |
| 49 | + auth_type=AuthCredentialTypes.OAUTH2, |
| 50 | + oauth2=OAuth2Auth( |
| 51 | + client_id="mock_client_id", |
| 52 | + client_secret="mock_client_secret", |
| 53 | + redirect_uri="https://example.com/callback", |
| 54 | + ), |
| 55 | + ) |
| 56 | + |
| 57 | + |
| 58 | +@pytest.fixture |
| 59 | +def auth_config(oauth2_auth_scheme, oauth2_credentials): |
| 60 | + """Create an AuthConfig for testing.""" |
| 61 | + # Create a copy of the credentials for the exchanged_auth_credential |
| 62 | + exchanged_credential = oauth2_credentials.model_copy(deep=True) |
| 63 | + |
| 64 | + return AuthConfig( |
| 65 | + auth_scheme=oauth2_auth_scheme, |
| 66 | + raw_auth_credential=oauth2_credentials, |
| 67 | + exchanged_auth_credential=exchanged_credential, |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +def test_get_credential_key(auth_config): |
| 72 | + """Test generating a unique credential key.""" |
| 73 | + |
| 74 | + key = auth_config.get_credential_key() |
| 75 | + assert key.startswith("adk_oauth2_") |
| 76 | + assert "_oauth2_" in key |
| 77 | + |
| 78 | + |
| 79 | +def test_get_credential_key_with_extras(auth_config): |
| 80 | + """Test generating a key when model_extra exists.""" |
| 81 | + # Add model_extra to test cleanup |
| 82 | + |
| 83 | + original_key = auth_config.get_credential_key() |
| 84 | + key = auth_config.get_credential_key() |
| 85 | + |
| 86 | + auth_config.auth_scheme.model_extra["extra_field"] = "value" |
| 87 | + auth_config.raw_auth_credential.model_extra["extra_field"] = "value" |
| 88 | + |
| 89 | + assert original_key == key |
| 90 | + assert "extra_field" in auth_config.auth_scheme.model_extra |
| 91 | + assert "extra_field" in auth_config.raw_auth_credential.model_extra |
0 commit comments