|
17 | 17 | from unittest.mock import patch |
18 | 18 |
|
19 | 19 | from authlib.oauth2.rfc6749 import OAuth2Token |
| 20 | +from fastapi.openapi.models import OAuth2 |
| 21 | +from fastapi.openapi.models import OAuthFlowClientCredentials |
| 22 | +from fastapi.openapi.models import OAuthFlows |
20 | 23 | from google.adk.auth.auth_credential import AuthCredential |
21 | 24 | from google.adk.auth.auth_credential import AuthCredentialTypes |
22 | 25 | from google.adk.auth.auth_credential import OAuth2Auth |
@@ -218,3 +221,116 @@ async def test_exchange_authlib_not_available(self): |
218 | 221 | # Should return original credential when authlib is not available |
219 | 222 | assert result == credential |
220 | 223 | assert result.oauth2.access_token is None |
| 224 | + |
| 225 | + @patch("google.adk.auth.oauth2_credential_util.OAuth2Session") |
| 226 | + @pytest.mark.asyncio |
| 227 | + async def test_exchange_client_credentials_success(self, mock_oauth2_session): |
| 228 | + """Test successful client credentials exchange.""" |
| 229 | + # Setup mock |
| 230 | + mock_client = Mock() |
| 231 | + mock_oauth2_session.return_value = mock_client |
| 232 | + mock_tokens = OAuth2Token({ |
| 233 | + "access_token": "client_access_token", |
| 234 | + "expires_at": int(time.time()) + 3600, |
| 235 | + "expires_in": 3600, |
| 236 | + }) |
| 237 | + mock_client.fetch_token.return_value = mock_tokens |
| 238 | + |
| 239 | + # Create OAuth2 scheme with client credentials flow |
| 240 | + flows = OAuthFlows( |
| 241 | + clientCredentials=OAuthFlowClientCredentials( |
| 242 | + tokenUrl="https://example.com/token", |
| 243 | + scopes={"read": "Read access", "write": "Write access"}, |
| 244 | + ) |
| 245 | + ) |
| 246 | + scheme = OAuth2(flows=flows) |
| 247 | + |
| 248 | + credential = AuthCredential( |
| 249 | + auth_type=AuthCredentialTypes.OAUTH2, |
| 250 | + oauth2=OAuth2Auth( |
| 251 | + client_id="test_client_id", |
| 252 | + client_secret="test_client_secret", |
| 253 | + ), |
| 254 | + ) |
| 255 | + |
| 256 | + exchanger = OAuth2CredentialExchanger() |
| 257 | + result = await exchanger.exchange(credential, scheme) |
| 258 | + |
| 259 | + # Verify client credentials exchange was successful |
| 260 | + assert result.oauth2.access_token == "client_access_token" |
| 261 | + mock_client.fetch_token.assert_called_once_with( |
| 262 | + "https://example.com/token", |
| 263 | + grant_type="client_credentials", |
| 264 | + ) |
| 265 | + |
| 266 | + @patch("google.adk.auth.oauth2_credential_util.OAuth2Session") |
| 267 | + @pytest.mark.asyncio |
| 268 | + async def test_exchange_client_credentials_failure(self, mock_oauth2_session): |
| 269 | + """Test client credentials exchange failure.""" |
| 270 | + # Setup mock to raise exception during fetch_token |
| 271 | + mock_client = Mock() |
| 272 | + mock_oauth2_session.return_value = mock_client |
| 273 | + mock_client.fetch_token.side_effect = Exception( |
| 274 | + "Client credentials fetch failed" |
| 275 | + ) |
| 276 | + |
| 277 | + # Create OAuth2 scheme with client credentials flow |
| 278 | + flows = OAuthFlows( |
| 279 | + clientCredentials=OAuthFlowClientCredentials( |
| 280 | + tokenUrl="https://example.com/token", scopes={"read": "Read access"} |
| 281 | + ) |
| 282 | + ) |
| 283 | + scheme = OAuth2(flows=flows) |
| 284 | + |
| 285 | + credential = AuthCredential( |
| 286 | + auth_type=AuthCredentialTypes.OAUTH2, |
| 287 | + oauth2=OAuth2Auth( |
| 288 | + client_id="test_client_id", |
| 289 | + client_secret="test_client_secret", |
| 290 | + ), |
| 291 | + ) |
| 292 | + |
| 293 | + exchanger = OAuth2CredentialExchanger() |
| 294 | + result = await exchanger.exchange(credential, scheme) |
| 295 | + |
| 296 | + # Should return original credential when client credentials exchange fails |
| 297 | + assert result == credential |
| 298 | + assert result.oauth2.access_token is None |
| 299 | + mock_client.fetch_token.assert_called_once() |
| 300 | + |
| 301 | + @pytest.mark.asyncio |
| 302 | + async def test_determine_grant_type_client_credentials(self): |
| 303 | + """Test grant type determination for client credentials.""" |
| 304 | + flows = OAuthFlows( |
| 305 | + clientCredentials=OAuthFlowClientCredentials( |
| 306 | + tokenUrl="https://example.com/token", scopes={"read": "Read access"} |
| 307 | + ) |
| 308 | + ) |
| 309 | + scheme = OAuth2(flows=flows) |
| 310 | + |
| 311 | + exchanger = OAuth2CredentialExchanger() |
| 312 | + grant_type = exchanger._determine_grant_type(scheme) |
| 313 | + |
| 314 | + from google.adk.auth.auth_schemes import OAuthGrantType |
| 315 | + |
| 316 | + assert grant_type == OAuthGrantType.CLIENT_CREDENTIALS |
| 317 | + |
| 318 | + @pytest.mark.asyncio |
| 319 | + async def test_determine_grant_type_openid_connect(self): |
| 320 | + """Test grant type determination for OpenID Connect (defaults to auth code).""" |
| 321 | + scheme = OpenIdConnectWithConfig( |
| 322 | + type_="openIdConnect", |
| 323 | + openId_connect_url=( |
| 324 | + "https://example.com/.well-known/openid_configuration" |
| 325 | + ), |
| 326 | + authorization_endpoint="https://example.com/auth", |
| 327 | + token_endpoint="https://example.com/token", |
| 328 | + scopes=["openid"], |
| 329 | + ) |
| 330 | + |
| 331 | + exchanger = OAuth2CredentialExchanger() |
| 332 | + grant_type = exchanger._determine_grant_type(scheme) |
| 333 | + |
| 334 | + from google.adk.auth.auth_schemes import OAuthGrantType |
| 335 | + |
| 336 | + assert grant_type == OAuthGrantType.AUTHORIZATION_CODE |
0 commit comments