def test_exchange_credential_use_default_credential_success(
    service_account_exchanger, auth_scheme, monkeypatch
):
  """Test successful exchange of service account credentials using default credential."""
  mock_credentials = MagicMock()
  mock_credentials.token = "mock_access_token"
  mock_google_auth_default = MagicMock(
      return_value=(mock_credentials, "test_project")
  )
  monkeypatch.setattr(google.auth, "default", mock_google_auth_default)

  auth_credential = AuthCredential(
      auth_type=AuthCredentialTypes.SERVICE_ACCOUNT,
      service_account=ServiceAccount(
          use_default_credential=True,
          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_google_auth_default.assert_called_once()
  mock_credentials.refresh.assert_called_once()
Test successful exchange of service account credentials using default credential.
tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py