def test_exchange_credential_success(
    service_account_exchanger, auth_scheme, monkeypatch
):
  """Test successful exchange of service account credentials."""
  mock_credentials = MagicMock()
  mock_credentials.token = "mock_access_token"

  # Mock the from_service_account_info method
  mock_from_service_account_info = MagicMock(return_value=mock_credentials)
  target_path = (
      "google.adk.tools.openapi_tool.auth.credential_exchangers."
      "service_account_exchanger.service_account.Credentials."
      "from_service_account_info"
  )
  monkeypatch.setattr(
      target_path,
      mock_from_service_account_info,
  )

  # Mock the refresh method
  mock_credentials.refresh = MagicMock()

  # Create a valid AuthCredential with service account info
  auth_credential = AuthCredential(
      auth_type=AuthCredentialTypes.SERVICE_ACCOUNT,
      service_account=ServiceAccount(
          service_account_credential=ServiceAccountCredential(
              type_="service_account",
              project_id="your_project_id",
              private_key_id="your_private_key_id",
              private_key="-----BEGIN PRIVATE KEY-----...",
              client_email="...@....iam.gserviceaccount.com",
              client_id="your_client_id",
              auth_uri="https://accounts.google.com/o/oauth2/auth",
              token_uri="https://oauth2.googleapis.com/token",
              auth_provider_x509_cert_url=(
                  "https://www.googleapis.com/oauth2/v1/certs"
              ),
              client_x509_cert_url=(
                  "https://www.googleapis.com/robot/v1/metadata/x509/..."
              ),
              universe_domain="googleapis.com",
          ),
          scopes=["https://www.googleapis.com/auth/cloud-platform"],
      ),
  )

  result = service_account_exchanger.exchange_credential(
      auth_scheme, auth_credential
  )

  assert result.auth_type == AuthCredentialTypes.HTTP
  assert result.http.scheme == "bearer"
  assert result.http.credentials.token == "mock_access_token"
  mock_from_service_account_info.assert_called_once()
  mock_credentials.refresh.assert_called_once()
Test successful exchange of service account credentials.
tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py