google-adk 0.0.1__py3-none-any.whl → 0.0.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- google/adk/__init__.py +20 -0
- google/adk/agents/__init__.py +32 -0
- google/adk/agents/active_streaming_tool.py +38 -0
- google/adk/agents/base_agent.py +345 -0
- google/adk/agents/callback_context.py +112 -0
- google/adk/agents/invocation_context.py +181 -0
- google/adk/agents/langgraph_agent.py +140 -0
- google/adk/agents/live_request_queue.py +64 -0
- google/adk/agents/llm_agent.py +376 -0
- google/adk/agents/loop_agent.py +62 -0
- google/adk/agents/parallel_agent.py +96 -0
- google/adk/agents/readonly_context.py +46 -0
- google/adk/agents/remote_agent.py +50 -0
- google/adk/agents/run_config.py +87 -0
- google/adk/agents/sequential_agent.py +45 -0
- google/adk/agents/transcription_entry.py +34 -0
- google/adk/artifacts/__init__.py +23 -0
- google/adk/artifacts/base_artifact_service.py +128 -0
- google/adk/artifacts/gcs_artifact_service.py +195 -0
- google/adk/artifacts/in_memory_artifact_service.py +133 -0
- google/adk/auth/__init__.py +22 -0
- google/adk/auth/auth_credential.py +220 -0
- google/adk/auth/auth_handler.py +268 -0
- google/adk/auth/auth_preprocessor.py +116 -0
- google/adk/auth/auth_schemes.py +67 -0
- google/adk/auth/auth_tool.py +55 -0
- google/adk/cli/__init__.py +15 -0
- google/adk/cli/__main__.py +18 -0
- google/adk/cli/agent_graph.py +122 -0
- google/adk/cli/browser/adk_favicon.svg +17 -0
- google/adk/cli/browser/assets/audio-processor.js +51 -0
- google/adk/cli/browser/assets/config/runtime-config.json +3 -0
- google/adk/cli/browser/index.html +33 -0
- google/adk/cli/browser/main-XUU6OGCC.js +75 -0
- google/adk/cli/browser/polyfills-FFHMD2TL.js +18 -0
- google/adk/cli/browser/styles-4VDSPQ37.css +17 -0
- google/adk/cli/cli.py +181 -0
- google/adk/cli/cli_deploy.py +181 -0
- google/adk/cli/cli_eval.py +282 -0
- google/adk/cli/cli_tools_click.py +479 -0
- google/adk/cli/fast_api.py +774 -0
- google/adk/cli/media_streamer/__init__.py +19 -0
- google/adk/cli/media_streamer/index.html +228 -0
- google/adk/cli/utils/__init__.py +49 -0
- google/adk/cli/utils/envs.py +57 -0
- google/adk/cli/utils/evals.py +93 -0
- google/adk/cli/utils/logs.py +72 -0
- google/adk/code_executors/__init__.py +49 -0
- google/adk/code_executors/base_code_executor.py +97 -0
- google/adk/code_executors/code_execution_utils.py +256 -0
- google/adk/code_executors/code_executor_context.py +202 -0
- google/adk/code_executors/container_code_executor.py +196 -0
- google/adk/code_executors/unsafe_local_code_executor.py +71 -0
- google/adk/code_executors/vertex_ai_code_executor.py +234 -0
- google/adk/evaluation/__init__.py +31 -0
- google/adk/evaluation/agent_evaluator.py +329 -0
- google/adk/evaluation/evaluation_constants.py +24 -0
- google/adk/evaluation/evaluation_generator.py +270 -0
- google/adk/evaluation/response_evaluator.py +135 -0
- google/adk/evaluation/trajectory_evaluator.py +184 -0
- google/adk/events/__init__.py +21 -0
- google/adk/events/event.py +130 -0
- google/adk/events/event_actions.py +55 -0
- google/adk/examples/__init__.py +28 -0
- google/adk/examples/base_example_provider.py +35 -0
- google/adk/examples/example.py +27 -0
- google/adk/examples/example_util.py +123 -0
- google/adk/examples/vertex_ai_example_store.py +104 -0
- google/adk/flows/__init__.py +14 -0
- google/adk/flows/llm_flows/__init__.py +20 -0
- google/adk/flows/llm_flows/_base_llm_processor.py +52 -0
- google/adk/flows/llm_flows/_code_execution.py +458 -0
- google/adk/flows/llm_flows/_nl_planning.py +129 -0
- google/adk/flows/llm_flows/agent_transfer.py +132 -0
- google/adk/flows/llm_flows/audio_transcriber.py +109 -0
- google/adk/flows/llm_flows/auto_flow.py +49 -0
- google/adk/flows/llm_flows/base_llm_flow.py +559 -0
- google/adk/flows/llm_flows/basic.py +72 -0
- google/adk/flows/llm_flows/contents.py +370 -0
- google/adk/flows/llm_flows/functions.py +486 -0
- google/adk/flows/llm_flows/identity.py +47 -0
- google/adk/flows/llm_flows/instructions.py +137 -0
- google/adk/flows/llm_flows/single_flow.py +57 -0
- google/adk/memory/__init__.py +35 -0
- google/adk/memory/base_memory_service.py +74 -0
- google/adk/memory/in_memory_memory_service.py +62 -0
- google/adk/memory/vertex_ai_rag_memory_service.py +177 -0
- google/adk/models/__init__.py +31 -0
- google/adk/models/anthropic_llm.py +243 -0
- google/adk/models/base_llm.py +87 -0
- google/adk/models/base_llm_connection.py +76 -0
- google/adk/models/gemini_llm_connection.py +200 -0
- google/adk/models/google_llm.py +331 -0
- google/adk/models/lite_llm.py +673 -0
- google/adk/models/llm_request.py +98 -0
- google/adk/models/llm_response.py +111 -0
- google/adk/models/registry.py +102 -0
- google/adk/planners/__init__.py +23 -0
- google/adk/planners/base_planner.py +66 -0
- google/adk/planners/built_in_planner.py +75 -0
- google/adk/planners/plan_re_act_planner.py +208 -0
- google/adk/runners.py +456 -0
- google/adk/sessions/__init__.py +41 -0
- google/adk/sessions/base_session_service.py +133 -0
- google/adk/sessions/database_session_service.py +522 -0
- google/adk/sessions/in_memory_session_service.py +206 -0
- google/adk/sessions/session.py +54 -0
- google/adk/sessions/state.py +71 -0
- google/adk/sessions/vertex_ai_session_service.py +356 -0
- google/adk/telemetry.py +189 -0
- google/adk/tests/__init__.py +14 -0
- google/adk/tests/integration/.env.example +10 -0
- google/adk/tests/integration/__init__.py +18 -0
- google/adk/tests/integration/conftest.py +119 -0
- google/adk/tests/integration/fixture/__init__.py +14 -0
- google/adk/tests/integration/fixture/agent_with_config/__init__.py +15 -0
- google/adk/tests/integration/fixture/agent_with_config/agent.py +88 -0
- google/adk/tests/integration/fixture/callback_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/callback_agent/agent.py +105 -0
- google/adk/tests/integration/fixture/context_update_test/OWNERS +1 -0
- google/adk/tests/integration/fixture/context_update_test/__init__.py +15 -0
- google/adk/tests/integration/fixture/context_update_test/agent.py +43 -0
- google/adk/tests/integration/fixture/context_update_test/successful_test.session.json +582 -0
- google/adk/tests/integration/fixture/context_variable_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/context_variable_agent/agent.py +115 -0
- google/adk/tests/integration/fixture/customer_support_ma/__init__.py +15 -0
- google/adk/tests/integration/fixture/customer_support_ma/agent.py +172 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/agent.py +338 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/order_query.test.json +69 -0
- google/adk/tests/integration/fixture/ecommerce_customer_service_agent/test_config.json +6 -0
- google/adk/tests/integration/fixture/flow_complex_spark/__init__.py +15 -0
- google/adk/tests/integration/fixture/flow_complex_spark/agent.py +182 -0
- google/adk/tests/integration/fixture/flow_complex_spark/sample.debug.log +243 -0
- google/adk/tests/integration/fixture/flow_complex_spark/sample.session.json +190 -0
- google/adk/tests/integration/fixture/hello_world_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/hello_world_agent/agent.py +95 -0
- google/adk/tests/integration/fixture/hello_world_agent/roll_die.test.json +24 -0
- google/adk/tests/integration/fixture/hello_world_agent/test_config.json +6 -0
- google/adk/tests/integration/fixture/home_automation_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/home_automation_agent/agent.py +304 -0
- google/adk/tests/integration/fixture/home_automation_agent/simple_test.test.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/simple_test2.test.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_config.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/dependent_tool_calls.test.json +18 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/eval_data.test.json +17 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/test_config.json +6 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_multi_turn_conversation.test.json +18 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test.test.json +17 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test2.test.json +5 -0
- google/adk/tests/integration/fixture/home_automation_agent/test_files/test_config.json +5 -0
- google/adk/tests/integration/fixture/tool_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/tool_agent/agent.py +218 -0
- google/adk/tests/integration/fixture/tool_agent/files/Agent_test_plan.pdf +0 -0
- google/adk/tests/integration/fixture/trip_planner_agent/__init__.py +15 -0
- google/adk/tests/integration/fixture/trip_planner_agent/agent.py +110 -0
- google/adk/tests/integration/fixture/trip_planner_agent/initial.session.json +13 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_config.json +5 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_files/initial.session.json +13 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_files/test_config.json +5 -0
- google/adk/tests/integration/fixture/trip_planner_agent/test_files/trip_inquiry_sub_agent.test.json +7 -0
- google/adk/tests/integration/fixture/trip_planner_agent/trip_inquiry.test.json +19 -0
- google/adk/tests/integration/models/__init__.py +14 -0
- google/adk/tests/integration/models/test_google_llm.py +65 -0
- google/adk/tests/integration/test_callback.py +70 -0
- google/adk/tests/integration/test_context_variable.py +67 -0
- google/adk/tests/integration/test_evalute_agent_in_fixture.py +76 -0
- google/adk/tests/integration/test_multi_agent.py +28 -0
- google/adk/tests/integration/test_multi_turn.py +42 -0
- google/adk/tests/integration/test_single_agent.py +23 -0
- google/adk/tests/integration/test_sub_agent.py +26 -0
- google/adk/tests/integration/test_system_instruction.py +177 -0
- google/adk/tests/integration/test_tools.py +287 -0
- google/adk/tests/integration/test_with_test_file.py +34 -0
- google/adk/tests/integration/tools/__init__.py +14 -0
- google/adk/tests/integration/utils/__init__.py +16 -0
- google/adk/tests/integration/utils/asserts.py +75 -0
- google/adk/tests/integration/utils/test_runner.py +97 -0
- google/adk/tests/unittests/__init__.py +14 -0
- google/adk/tests/unittests/agents/__init__.py +14 -0
- google/adk/tests/unittests/agents/test_base_agent.py +407 -0
- google/adk/tests/unittests/agents/test_langgraph_agent.py +191 -0
- google/adk/tests/unittests/agents/test_llm_agent_callbacks.py +138 -0
- google/adk/tests/unittests/agents/test_llm_agent_fields.py +231 -0
- google/adk/tests/unittests/agents/test_loop_agent.py +136 -0
- google/adk/tests/unittests/agents/test_parallel_agent.py +92 -0
- google/adk/tests/unittests/agents/test_sequential_agent.py +114 -0
- google/adk/tests/unittests/artifacts/__init__.py +14 -0
- google/adk/tests/unittests/artifacts/test_artifact_service.py +276 -0
- google/adk/tests/unittests/auth/test_auth_handler.py +575 -0
- google/adk/tests/unittests/conftest.py +73 -0
- google/adk/tests/unittests/fast_api/__init__.py +14 -0
- google/adk/tests/unittests/fast_api/test_fast_api.py +269 -0
- google/adk/tests/unittests/flows/__init__.py +14 -0
- google/adk/tests/unittests/flows/llm_flows/__init__.py +14 -0
- google/adk/tests/unittests/flows/llm_flows/_test_examples.py +142 -0
- google/adk/tests/unittests/flows/llm_flows/test_agent_transfer.py +311 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_long_running.py +244 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_request_euc.py +346 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_sequential.py +93 -0
- google/adk/tests/unittests/flows/llm_flows/test_functions_simple.py +258 -0
- google/adk/tests/unittests/flows/llm_flows/test_identity.py +66 -0
- google/adk/tests/unittests/flows/llm_flows/test_instructions.py +164 -0
- google/adk/tests/unittests/flows/llm_flows/test_model_callbacks.py +142 -0
- google/adk/tests/unittests/flows/llm_flows/test_other_configs.py +46 -0
- google/adk/tests/unittests/flows/llm_flows/test_tool_callbacks.py +269 -0
- google/adk/tests/unittests/models/__init__.py +14 -0
- google/adk/tests/unittests/models/test_google_llm.py +224 -0
- google/adk/tests/unittests/models/test_litellm.py +804 -0
- google/adk/tests/unittests/models/test_models.py +60 -0
- google/adk/tests/unittests/sessions/__init__.py +14 -0
- google/adk/tests/unittests/sessions/test_session_service.py +227 -0
- google/adk/tests/unittests/sessions/test_vertex_ai_session_service.py +246 -0
- google/adk/tests/unittests/streaming/__init__.py +14 -0
- google/adk/tests/unittests/streaming/test_streaming.py +50 -0
- google/adk/tests/unittests/tools/__init__.py +14 -0
- google/adk/tests/unittests/tools/apihub_tool/clients/test_apihub_client.py +499 -0
- google/adk/tests/unittests/tools/apihub_tool/test_apihub_toolset.py +204 -0
- google/adk/tests/unittests/tools/application_integration_tool/clients/test_connections_client.py +600 -0
- google/adk/tests/unittests/tools/application_integration_tool/clients/test_integration_client.py +630 -0
- google/adk/tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py +345 -0
- google/adk/tests/unittests/tools/google_api_tool/__init__.py +13 -0
- google/adk/tests/unittests/tools/google_api_tool/test_googleapi_to_openapi_converter.py +657 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_auto_auth_credential_exchanger.py +145 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_base_auth_credential_exchanger.py +68 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_oauth2_exchanger.py +153 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py +196 -0
- google/adk/tests/unittests/tools/openapi_tool/auth/test_auth_helper.py +573 -0
- google/adk/tests/unittests/tools/openapi_tool/common/test_common.py +436 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test.yaml +1367 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +628 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py +139 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py +406 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py +966 -0
- google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_tool_auth_handler.py +201 -0
- google/adk/tests/unittests/tools/retrieval/__init__.py +14 -0
- google/adk/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py +147 -0
- google/adk/tests/unittests/tools/test_agent_tool.py +167 -0
- google/adk/tests/unittests/tools/test_base_tool.py +141 -0
- google/adk/tests/unittests/tools/test_build_function_declaration.py +277 -0
- google/adk/tests/unittests/utils.py +304 -0
- google/adk/tools/__init__.py +51 -0
- google/adk/tools/_automatic_function_calling_util.py +346 -0
- google/adk/tools/agent_tool.py +176 -0
- google/adk/tools/apihub_tool/__init__.py +19 -0
- google/adk/tools/apihub_tool/apihub_toolset.py +209 -0
- google/adk/tools/apihub_tool/clients/__init__.py +13 -0
- google/adk/tools/apihub_tool/clients/apihub_client.py +332 -0
- google/adk/tools/apihub_tool/clients/secret_client.py +115 -0
- google/adk/tools/application_integration_tool/__init__.py +19 -0
- google/adk/tools/application_integration_tool/application_integration_toolset.py +230 -0
- google/adk/tools/application_integration_tool/clients/connections_client.py +903 -0
- google/adk/tools/application_integration_tool/clients/integration_client.py +253 -0
- google/adk/tools/base_tool.py +144 -0
- google/adk/tools/built_in_code_execution_tool.py +59 -0
- google/adk/tools/crewai_tool.py +72 -0
- google/adk/tools/example_tool.py +62 -0
- google/adk/tools/exit_loop_tool.py +23 -0
- google/adk/tools/function_parameter_parse_util.py +307 -0
- google/adk/tools/function_tool.py +87 -0
- google/adk/tools/get_user_choice_tool.py +28 -0
- google/adk/tools/google_api_tool/__init__.py +14 -0
- google/adk/tools/google_api_tool/google_api_tool.py +59 -0
- google/adk/tools/google_api_tool/google_api_tool_set.py +107 -0
- google/adk/tools/google_api_tool/google_api_tool_sets.py +55 -0
- google/adk/tools/google_api_tool/googleapi_to_openapi_converter.py +521 -0
- google/adk/tools/google_search_tool.py +68 -0
- google/adk/tools/langchain_tool.py +86 -0
- google/adk/tools/load_artifacts_tool.py +113 -0
- google/adk/tools/load_memory_tool.py +58 -0
- google/adk/tools/load_web_page.py +41 -0
- google/adk/tools/long_running_tool.py +39 -0
- google/adk/tools/mcp_tool/__init__.py +42 -0
- google/adk/tools/mcp_tool/conversion_utils.py +161 -0
- google/adk/tools/mcp_tool/mcp_tool.py +113 -0
- google/adk/tools/mcp_tool/mcp_toolset.py +272 -0
- google/adk/tools/openapi_tool/__init__.py +21 -0
- google/adk/tools/openapi_tool/auth/__init__.py +19 -0
- google/adk/tools/openapi_tool/auth/auth_helpers.py +498 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/__init__.py +25 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/auto_auth_credential_exchanger.py +105 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/base_credential_exchanger.py +55 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/oauth2_exchanger.py +117 -0
- google/adk/tools/openapi_tool/auth/credential_exchangers/service_account_exchanger.py +97 -0
- google/adk/tools/openapi_tool/common/__init__.py +19 -0
- google/adk/tools/openapi_tool/common/common.py +300 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/__init__.py +32 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/openapi_spec_parser.py +231 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/openapi_toolset.py +144 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/operation_parser.py +260 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/rest_api_tool.py +496 -0
- google/adk/tools/openapi_tool/openapi_spec_parser/tool_auth_handler.py +268 -0
- google/adk/tools/preload_memory_tool.py +72 -0
- google/adk/tools/retrieval/__init__.py +36 -0
- google/adk/tools/retrieval/base_retrieval_tool.py +37 -0
- google/adk/tools/retrieval/files_retrieval.py +33 -0
- google/adk/tools/retrieval/llama_index_retrieval.py +41 -0
- google/adk/tools/retrieval/vertex_ai_rag_retrieval.py +107 -0
- google/adk/tools/tool_context.py +90 -0
- google/adk/tools/toolbox_tool.py +46 -0
- google/adk/tools/transfer_to_agent_tool.py +21 -0
- google/adk/tools/vertex_ai_search_tool.py +96 -0
- google/adk/version.py +16 -0
- google_adk-0.0.1.dist-info/LICENSE.txt → google_adk-0.0.2.dist-info/LICENSE +32 -0
- google_adk-0.0.2.dist-info/METADATA +73 -0
- google_adk-0.0.2.dist-info/RECORD +308 -0
- {google_adk-0.0.1.dist-info → google_adk-0.0.2.dist-info}/WHEEL +1 -2
- google_adk-0.0.2.dist-info/entry_points.txt +3 -0
- agent_kit/__init__.py +0 -0
- google_adk-0.0.1.dist-info/METADATA +0 -15
- google_adk-0.0.1.dist-info/RECORD +0 -6
- google_adk-0.0.1.dist-info/top_level.txt +0 -1
google/adk/tests/unittests/tools/application_integration_tool/clients/test_connections_client.py
ADDED
@@ -0,0 +1,600 @@
|
|
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
|
+
import json
|
16
|
+
from unittest import mock
|
17
|
+
|
18
|
+
from google.adk.tools.application_integration_tool.clients.connections_client import ConnectionsClient
|
19
|
+
import google.auth
|
20
|
+
import pytest
|
21
|
+
import requests
|
22
|
+
from requests import exceptions
|
23
|
+
|
24
|
+
|
25
|
+
@pytest.fixture
|
26
|
+
def project():
|
27
|
+
return "test-project"
|
28
|
+
|
29
|
+
|
30
|
+
@pytest.fixture
|
31
|
+
def location():
|
32
|
+
return "us-central1"
|
33
|
+
|
34
|
+
|
35
|
+
@pytest.fixture
|
36
|
+
def connection_name():
|
37
|
+
return "test-connection"
|
38
|
+
|
39
|
+
|
40
|
+
@pytest.fixture
|
41
|
+
def mock_credentials():
|
42
|
+
creds = mock.create_autospec(google.auth.credentials.Credentials)
|
43
|
+
creds.token = "test_token"
|
44
|
+
creds.expired = False
|
45
|
+
return creds
|
46
|
+
|
47
|
+
|
48
|
+
@pytest.fixture
|
49
|
+
def mock_auth_request():
|
50
|
+
return mock.create_autospec(google.auth.transport.requests.Request)
|
51
|
+
|
52
|
+
|
53
|
+
class TestConnectionsClient:
|
54
|
+
|
55
|
+
def test_initialization(self, project, location, connection_name):
|
56
|
+
credentials = {"email": "test@example.com"}
|
57
|
+
client = ConnectionsClient(
|
58
|
+
project, location, connection_name, json.dumps(credentials)
|
59
|
+
)
|
60
|
+
assert client.project == project
|
61
|
+
assert client.location == location
|
62
|
+
assert client.connection == connection_name
|
63
|
+
assert client.connector_url == "https://connectors.googleapis.com"
|
64
|
+
assert client.service_account_json == json.dumps(credentials)
|
65
|
+
assert client.credential_cache is None
|
66
|
+
|
67
|
+
def test_execute_api_call_success(
|
68
|
+
self, project, location, connection_name, mock_credentials
|
69
|
+
):
|
70
|
+
credentials = {"email": "test@example.com"}
|
71
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
72
|
+
mock_response = mock.MagicMock()
|
73
|
+
mock_response.status_code = 200
|
74
|
+
mock_response.raise_for_status.return_value = None
|
75
|
+
mock_response.json.return_value = {"data": "test"}
|
76
|
+
|
77
|
+
with mock.patch.object(
|
78
|
+
client, "_get_access_token", return_value=mock_credentials.token
|
79
|
+
), mock.patch("requests.get", return_value=mock_response):
|
80
|
+
response = client._execute_api_call("https://test.url")
|
81
|
+
assert response.json() == {"data": "test"}
|
82
|
+
requests.get.assert_called_once_with(
|
83
|
+
"https://test.url",
|
84
|
+
headers={
|
85
|
+
"Content-Type": "application/json",
|
86
|
+
"Authorization": f"Bearer {mock_credentials.token}",
|
87
|
+
},
|
88
|
+
)
|
89
|
+
|
90
|
+
def test_execute_api_call_credential_error(
|
91
|
+
self, project, location, connection_name
|
92
|
+
):
|
93
|
+
credentials = {"email": "test@example.com"}
|
94
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
95
|
+
with mock.patch.object(
|
96
|
+
client,
|
97
|
+
"_get_access_token",
|
98
|
+
side_effect=google.auth.exceptions.DefaultCredentialsError("Test"),
|
99
|
+
):
|
100
|
+
with pytest.raises(PermissionError, match="Credentials error: Test"):
|
101
|
+
client._execute_api_call("https://test.url")
|
102
|
+
|
103
|
+
@pytest.mark.parametrize(
|
104
|
+
"status_code, response_text",
|
105
|
+
[(404, "Not Found"), (400, "Bad Request")],
|
106
|
+
)
|
107
|
+
def test_execute_api_call_request_error_not_found_or_bad_request(
|
108
|
+
self,
|
109
|
+
project,
|
110
|
+
location,
|
111
|
+
connection_name,
|
112
|
+
mock_credentials,
|
113
|
+
status_code,
|
114
|
+
response_text,
|
115
|
+
):
|
116
|
+
credentials = {"email": "test@example.com"}
|
117
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
118
|
+
mock_response = mock.MagicMock()
|
119
|
+
mock_response.status_code = status_code
|
120
|
+
mock_response.raise_for_status.side_effect = exceptions.HTTPError(
|
121
|
+
f"HTTP error {status_code}: {response_text}"
|
122
|
+
)
|
123
|
+
|
124
|
+
with mock.patch.object(
|
125
|
+
client, "_get_access_token", return_value=mock_credentials.token
|
126
|
+
), mock.patch("requests.get", return_value=mock_response):
|
127
|
+
with pytest.raises(
|
128
|
+
ValueError, match="Invalid request. Please check the provided"
|
129
|
+
):
|
130
|
+
client._execute_api_call("https://test.url")
|
131
|
+
|
132
|
+
def test_execute_api_call_other_request_error(
|
133
|
+
self, project, location, connection_name, mock_credentials
|
134
|
+
):
|
135
|
+
credentials = {"email": "test@example.com"}
|
136
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
137
|
+
mock_response = mock.MagicMock()
|
138
|
+
mock_response.status_code = 500
|
139
|
+
mock_response.raise_for_status.side_effect = exceptions.HTTPError(
|
140
|
+
"Internal Server Error"
|
141
|
+
)
|
142
|
+
|
143
|
+
with mock.patch.object(
|
144
|
+
client, "_get_access_token", return_value=mock_credentials.token
|
145
|
+
), mock.patch("requests.get", return_value=mock_response):
|
146
|
+
with pytest.raises(ValueError, match="Request error: "):
|
147
|
+
client._execute_api_call("https://test.url")
|
148
|
+
|
149
|
+
def test_execute_api_call_unexpected_error(
|
150
|
+
self, project, location, connection_name, mock_credentials
|
151
|
+
):
|
152
|
+
credentials = {"email": "test@example.com"}
|
153
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
154
|
+
with mock.patch.object(
|
155
|
+
client, "_get_access_token", return_value=mock_credentials.token
|
156
|
+
), mock.patch(
|
157
|
+
"requests.get", side_effect=Exception("Something went wrong")
|
158
|
+
):
|
159
|
+
with pytest.raises(
|
160
|
+
Exception, match="An unexpected error occurred: Something went wrong"
|
161
|
+
):
|
162
|
+
client._execute_api_call("https://test.url")
|
163
|
+
|
164
|
+
def test_get_connection_details_success_with_host(
|
165
|
+
self, project, location, connection_name, mock_credentials
|
166
|
+
):
|
167
|
+
credentials = {"email": "test@example.com"}
|
168
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
169
|
+
mock_response = mock.MagicMock()
|
170
|
+
mock_response.status_code = 200
|
171
|
+
mock_response.json.return_value = {
|
172
|
+
"serviceDirectory": "test_service",
|
173
|
+
"host": "test.host",
|
174
|
+
"tlsServiceDirectory": "tls_test_service",
|
175
|
+
"authOverrideEnabled": True,
|
176
|
+
}
|
177
|
+
|
178
|
+
with mock.patch.object(
|
179
|
+
client, "_execute_api_call", return_value=mock_response
|
180
|
+
):
|
181
|
+
details = client.get_connection_details()
|
182
|
+
assert details == {
|
183
|
+
"serviceName": "tls_test_service",
|
184
|
+
"host": "test.host",
|
185
|
+
"authOverrideEnabled": True,
|
186
|
+
}
|
187
|
+
|
188
|
+
def test_get_connection_details_success_without_host(
|
189
|
+
self, project, location, connection_name, mock_credentials
|
190
|
+
):
|
191
|
+
credentials = {"email": "test@example.com"}
|
192
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
193
|
+
mock_response = mock.MagicMock()
|
194
|
+
mock_response.status_code = 200
|
195
|
+
mock_response.json.return_value = {
|
196
|
+
"serviceDirectory": "test_service",
|
197
|
+
"authOverrideEnabled": False,
|
198
|
+
}
|
199
|
+
|
200
|
+
with mock.patch.object(
|
201
|
+
client, "_execute_api_call", return_value=mock_response
|
202
|
+
):
|
203
|
+
details = client.get_connection_details()
|
204
|
+
assert details == {
|
205
|
+
"serviceName": "test_service",
|
206
|
+
"host": "",
|
207
|
+
"authOverrideEnabled": False,
|
208
|
+
}
|
209
|
+
|
210
|
+
def test_get_connection_details_error(
|
211
|
+
self, project, location, connection_name
|
212
|
+
):
|
213
|
+
credentials = {"email": "test@example.com"}
|
214
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
215
|
+
with mock.patch.object(
|
216
|
+
client, "_execute_api_call", side_effect=ValueError("Request error")
|
217
|
+
):
|
218
|
+
with pytest.raises(ValueError, match="Request error"):
|
219
|
+
client.get_connection_details()
|
220
|
+
|
221
|
+
def test_get_entity_schema_and_operations_success(
|
222
|
+
self, project, location, connection_name, mock_credentials
|
223
|
+
):
|
224
|
+
credentials = {"email": "test@example.com"}
|
225
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
226
|
+
mock_execute_response_initial = mock.MagicMock()
|
227
|
+
mock_execute_response_initial.status_code = 200
|
228
|
+
mock_execute_response_initial.json.return_value = {
|
229
|
+
"name": "operations/test_op"
|
230
|
+
}
|
231
|
+
|
232
|
+
mock_execute_response_poll_done = mock.MagicMock()
|
233
|
+
mock_execute_response_poll_done.status_code = 200
|
234
|
+
mock_execute_response_poll_done.json.return_value = {
|
235
|
+
"done": True,
|
236
|
+
"response": {
|
237
|
+
"jsonSchema": {"type": "object"},
|
238
|
+
"operations": ["LIST", "GET"],
|
239
|
+
},
|
240
|
+
}
|
241
|
+
|
242
|
+
with mock.patch.object(
|
243
|
+
client,
|
244
|
+
"_execute_api_call",
|
245
|
+
side_effect=[
|
246
|
+
mock_execute_response_initial,
|
247
|
+
mock_execute_response_poll_done,
|
248
|
+
],
|
249
|
+
):
|
250
|
+
schema, operations = client.get_entity_schema_and_operations("entity1")
|
251
|
+
assert schema == {"type": "object"}
|
252
|
+
assert operations == ["LIST", "GET"]
|
253
|
+
assert (
|
254
|
+
mock.call(
|
255
|
+
f"https://connectors.googleapis.com/v1/projects/{project}/locations/{location}/connections/{connection_name}/connectionSchemaMetadata:getEntityType?entityId=entity1"
|
256
|
+
)
|
257
|
+
in client._execute_api_call.mock_calls
|
258
|
+
)
|
259
|
+
assert (
|
260
|
+
mock.call(f"https://connectors.googleapis.com/v1/operations/test_op")
|
261
|
+
in client._execute_api_call.mock_calls
|
262
|
+
)
|
263
|
+
|
264
|
+
def test_get_entity_schema_and_operations_no_operation_id(
|
265
|
+
self, project, location, connection_name, mock_credentials
|
266
|
+
):
|
267
|
+
credentials = {"email": "test@example.com"}
|
268
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
269
|
+
mock_execute_response = mock.MagicMock()
|
270
|
+
mock_execute_response.status_code = 200
|
271
|
+
mock_execute_response.json.return_value = {}
|
272
|
+
|
273
|
+
with mock.patch.object(
|
274
|
+
client, "_execute_api_call", return_value=mock_execute_response
|
275
|
+
):
|
276
|
+
with pytest.raises(
|
277
|
+
ValueError,
|
278
|
+
match=(
|
279
|
+
"Failed to get entity schema and operations for entity: entity1"
|
280
|
+
),
|
281
|
+
):
|
282
|
+
client.get_entity_schema_and_operations("entity1")
|
283
|
+
|
284
|
+
def test_get_entity_schema_and_operations_execute_api_call_error(
|
285
|
+
self, project, location, connection_name
|
286
|
+
):
|
287
|
+
credentials = {"email": "test@example.com"}
|
288
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
289
|
+
with mock.patch.object(
|
290
|
+
client, "_execute_api_call", side_effect=ValueError("Request error")
|
291
|
+
):
|
292
|
+
with pytest.raises(ValueError, match="Request error"):
|
293
|
+
client.get_entity_schema_and_operations("entity1")
|
294
|
+
|
295
|
+
def test_get_action_schema_success(
|
296
|
+
self, project, location, connection_name, mock_credentials
|
297
|
+
):
|
298
|
+
credentials = {"email": "test@example.com"}
|
299
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
300
|
+
mock_execute_response_initial = mock.MagicMock()
|
301
|
+
mock_execute_response_initial.status_code = 200
|
302
|
+
mock_execute_response_initial.json.return_value = {
|
303
|
+
"name": "operations/test_op"
|
304
|
+
}
|
305
|
+
|
306
|
+
mock_execute_response_poll_done = mock.MagicMock()
|
307
|
+
mock_execute_response_poll_done.status_code = 200
|
308
|
+
mock_execute_response_poll_done.json.return_value = {
|
309
|
+
"done": True,
|
310
|
+
"response": {
|
311
|
+
"inputJsonSchema": {
|
312
|
+
"type": "object",
|
313
|
+
"properties": {"input": {"type": "string"}},
|
314
|
+
},
|
315
|
+
"outputJsonSchema": {
|
316
|
+
"type": "object",
|
317
|
+
"properties": {"output": {"type": "string"}},
|
318
|
+
},
|
319
|
+
"description": "Test Action Description",
|
320
|
+
"displayName": "TestAction",
|
321
|
+
},
|
322
|
+
}
|
323
|
+
|
324
|
+
with mock.patch.object(
|
325
|
+
client,
|
326
|
+
"_execute_api_call",
|
327
|
+
side_effect=[
|
328
|
+
mock_execute_response_initial,
|
329
|
+
mock_execute_response_poll_done,
|
330
|
+
],
|
331
|
+
):
|
332
|
+
schema = client.get_action_schema("action1")
|
333
|
+
assert schema == {
|
334
|
+
"inputSchema": {
|
335
|
+
"type": "object",
|
336
|
+
"properties": {"input": {"type": "string"}},
|
337
|
+
},
|
338
|
+
"outputSchema": {
|
339
|
+
"type": "object",
|
340
|
+
"properties": {"output": {"type": "string"}},
|
341
|
+
},
|
342
|
+
"description": "Test Action Description",
|
343
|
+
"displayName": "TestAction",
|
344
|
+
}
|
345
|
+
assert (
|
346
|
+
mock.call(
|
347
|
+
f"https://connectors.googleapis.com/v1/projects/{project}/locations/{location}/connections/{connection_name}/connectionSchemaMetadata:getAction?actionId=action1"
|
348
|
+
)
|
349
|
+
in client._execute_api_call.mock_calls
|
350
|
+
)
|
351
|
+
assert (
|
352
|
+
mock.call(f"https://connectors.googleapis.com/v1/operations/test_op")
|
353
|
+
in client._execute_api_call.mock_calls
|
354
|
+
)
|
355
|
+
|
356
|
+
def test_get_action_schema_no_operation_id(
|
357
|
+
self, project, location, connection_name, mock_credentials
|
358
|
+
):
|
359
|
+
credentials = {"email": "test@example.com"}
|
360
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
361
|
+
mock_execute_response = mock.MagicMock()
|
362
|
+
mock_execute_response.status_code = 200
|
363
|
+
mock_execute_response.json.return_value = {}
|
364
|
+
|
365
|
+
with mock.patch.object(
|
366
|
+
client, "_execute_api_call", return_value=mock_execute_response
|
367
|
+
):
|
368
|
+
with pytest.raises(
|
369
|
+
ValueError, match="Failed to get action schema for action: action1"
|
370
|
+
):
|
371
|
+
client.get_action_schema("action1")
|
372
|
+
|
373
|
+
def test_get_action_schema_execute_api_call_error(
|
374
|
+
self, project, location, connection_name
|
375
|
+
):
|
376
|
+
credentials = {"email": "test@example.com"}
|
377
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
378
|
+
with mock.patch.object(
|
379
|
+
client, "_execute_api_call", side_effect=ValueError("Request error")
|
380
|
+
):
|
381
|
+
with pytest.raises(ValueError, match="Request error"):
|
382
|
+
client.get_action_schema("action1")
|
383
|
+
|
384
|
+
def test_get_connector_base_spec(self):
|
385
|
+
spec = ConnectionsClient.get_connector_base_spec()
|
386
|
+
assert "openapi" in spec
|
387
|
+
assert spec["info"]["title"] == "ExecuteConnection"
|
388
|
+
assert "components" in spec
|
389
|
+
assert "schemas" in spec["components"]
|
390
|
+
assert "operation" in spec["components"]["schemas"]
|
391
|
+
|
392
|
+
def test_get_action_operation(self):
|
393
|
+
operation = ConnectionsClient.get_action_operation(
|
394
|
+
"TestAction", "EXECUTE_ACTION", "TestActionDisplayName", "test_tool"
|
395
|
+
)
|
396
|
+
assert "post" in operation
|
397
|
+
assert operation["post"]["summary"] == "TestActionDisplayName"
|
398
|
+
assert "operationId" in operation["post"]
|
399
|
+
assert operation["post"]["operationId"] == "test_tool_TestActionDisplayName"
|
400
|
+
|
401
|
+
def test_list_operation(self):
|
402
|
+
operation = ConnectionsClient.list_operation(
|
403
|
+
"Entity1", '{"type": "object"}', "test_tool"
|
404
|
+
)
|
405
|
+
assert "post" in operation
|
406
|
+
assert operation["post"]["summary"] == "List Entity1"
|
407
|
+
assert "operationId" in operation["post"]
|
408
|
+
assert operation["post"]["operationId"] == "test_tool_list_Entity1"
|
409
|
+
|
410
|
+
def test_get_operation_static(self):
|
411
|
+
operation = ConnectionsClient.get_operation(
|
412
|
+
"Entity1", '{"type": "object"}', "test_tool"
|
413
|
+
)
|
414
|
+
assert "post" in operation
|
415
|
+
assert operation["post"]["summary"] == "Get Entity1"
|
416
|
+
assert "operationId" in operation["post"]
|
417
|
+
assert operation["post"]["operationId"] == "test_tool_get_Entity1"
|
418
|
+
|
419
|
+
def test_create_operation(self):
|
420
|
+
operation = ConnectionsClient.create_operation("Entity1", "test_tool")
|
421
|
+
assert "post" in operation
|
422
|
+
assert operation["post"]["summary"] == "Create Entity1"
|
423
|
+
assert "operationId" in operation["post"]
|
424
|
+
assert operation["post"]["operationId"] == "test_tool_create_Entity1"
|
425
|
+
|
426
|
+
def test_update_operation(self):
|
427
|
+
operation = ConnectionsClient.update_operation("Entity1", "test_tool")
|
428
|
+
assert "post" in operation
|
429
|
+
assert operation["post"]["summary"] == "Update Entity1"
|
430
|
+
assert "operationId" in operation["post"]
|
431
|
+
assert operation["post"]["operationId"] == "test_tool_update_Entity1"
|
432
|
+
|
433
|
+
def test_delete_operation(self):
|
434
|
+
operation = ConnectionsClient.delete_operation("Entity1", "test_tool")
|
435
|
+
assert "post" in operation
|
436
|
+
assert operation["post"]["summary"] == "Delete Entity1"
|
437
|
+
assert operation["post"]["operationId"] == "test_tool_delete_Entity1"
|
438
|
+
|
439
|
+
def test_create_operation_request(self):
|
440
|
+
schema = ConnectionsClient.create_operation_request("Entity1")
|
441
|
+
assert "type" in schema
|
442
|
+
assert schema["type"] == "object"
|
443
|
+
assert "properties" in schema
|
444
|
+
assert "connectorInputPayload" in schema["properties"]
|
445
|
+
|
446
|
+
def test_update_operation_request(self):
|
447
|
+
schema = ConnectionsClient.update_operation_request("Entity1")
|
448
|
+
assert "type" in schema
|
449
|
+
assert schema["type"] == "object"
|
450
|
+
assert "properties" in schema
|
451
|
+
assert "entityId" in schema["properties"]
|
452
|
+
|
453
|
+
def test_get_operation_request_static(self):
|
454
|
+
schema = ConnectionsClient.get_operation_request()
|
455
|
+
assert "type" in schema
|
456
|
+
assert schema["type"] == "object"
|
457
|
+
assert "properties" in schema
|
458
|
+
assert "entityId" in schema["properties"]
|
459
|
+
|
460
|
+
def test_delete_operation_request(self):
|
461
|
+
schema = ConnectionsClient.delete_operation_request()
|
462
|
+
assert "type" in schema
|
463
|
+
assert schema["type"] == "object"
|
464
|
+
assert "properties" in schema
|
465
|
+
assert "entityId" in schema["properties"]
|
466
|
+
|
467
|
+
def test_list_operation_request(self):
|
468
|
+
schema = ConnectionsClient.list_operation_request()
|
469
|
+
assert "type" in schema
|
470
|
+
assert schema["type"] == "object"
|
471
|
+
assert "properties" in schema
|
472
|
+
assert "filterClause" in schema["properties"]
|
473
|
+
|
474
|
+
def test_action_request(self):
|
475
|
+
schema = ConnectionsClient.action_request("TestAction")
|
476
|
+
assert "type" in schema
|
477
|
+
assert schema["type"] == "object"
|
478
|
+
assert "properties" in schema
|
479
|
+
assert "connectorInputPayload" in schema["properties"]
|
480
|
+
|
481
|
+
def test_action_response(self):
|
482
|
+
schema = ConnectionsClient.action_response("TestAction")
|
483
|
+
assert "type" in schema
|
484
|
+
assert schema["type"] == "object"
|
485
|
+
assert "properties" in schema
|
486
|
+
assert "connectorOutputPayload" in schema["properties"]
|
487
|
+
|
488
|
+
def test_execute_custom_query_request(self):
|
489
|
+
schema = ConnectionsClient.execute_custom_query_request()
|
490
|
+
assert "type" in schema
|
491
|
+
assert schema["type"] == "object"
|
492
|
+
assert "properties" in schema
|
493
|
+
assert "query" in schema["properties"]
|
494
|
+
|
495
|
+
def test_connector_payload(self):
|
496
|
+
client = ConnectionsClient("test-project", "us-central1", "test-connection")
|
497
|
+
schema = client.connector_payload(
|
498
|
+
json_schema={
|
499
|
+
"type": "object",
|
500
|
+
"properties": {
|
501
|
+
"input": {
|
502
|
+
"type": ["null", "string"],
|
503
|
+
"description": "description",
|
504
|
+
}
|
505
|
+
},
|
506
|
+
}
|
507
|
+
)
|
508
|
+
assert schema == {
|
509
|
+
"type": "object",
|
510
|
+
"properties": {
|
511
|
+
"input": {
|
512
|
+
"type": "string",
|
513
|
+
"nullable": True,
|
514
|
+
"description": "description",
|
515
|
+
}
|
516
|
+
},
|
517
|
+
}
|
518
|
+
|
519
|
+
def test_get_access_token_uses_cached_token(
|
520
|
+
self, project, location, connection_name, mock_credentials
|
521
|
+
):
|
522
|
+
credentials = {"email": "test@example.com"}
|
523
|
+
client = ConnectionsClient(project, location, connection_name, credentials)
|
524
|
+
client.credential_cache = mock_credentials
|
525
|
+
token = client._get_access_token()
|
526
|
+
assert token == "test_token"
|
527
|
+
|
528
|
+
def test_get_access_token_with_service_account_credentials(
|
529
|
+
self, project, location, connection_name
|
530
|
+
):
|
531
|
+
service_account_json = json.dumps({
|
532
|
+
"client_email": "test@example.com",
|
533
|
+
"private_key": "test_key",
|
534
|
+
})
|
535
|
+
client = ConnectionsClient(
|
536
|
+
project, location, connection_name, service_account_json
|
537
|
+
)
|
538
|
+
mock_creds = mock.create_autospec(google.oauth2.service_account.Credentials)
|
539
|
+
mock_creds.token = "sa_token"
|
540
|
+
mock_creds.expired = False
|
541
|
+
|
542
|
+
with mock.patch(
|
543
|
+
"google.oauth2.service_account.Credentials.from_service_account_info",
|
544
|
+
return_value=mock_creds,
|
545
|
+
), mock.patch.object(mock_creds, "refresh", return_value=None):
|
546
|
+
token = client._get_access_token()
|
547
|
+
assert token == "sa_token"
|
548
|
+
google.oauth2.service_account.Credentials.from_service_account_info.assert_called_once_with(
|
549
|
+
json.loads(service_account_json),
|
550
|
+
scopes=["https://www.googleapis.com/auth/cloud-platform"],
|
551
|
+
)
|
552
|
+
mock_creds.refresh.assert_called_once()
|
553
|
+
|
554
|
+
def test_get_access_token_with_default_credentials(
|
555
|
+
self, project, location, connection_name, mock_credentials
|
556
|
+
):
|
557
|
+
client = ConnectionsClient(project, location, connection_name, None)
|
558
|
+
with mock.patch(
|
559
|
+
"google.adk.tools.application_integration_tool.clients.connections_client.default_service_credential",
|
560
|
+
return_value=(mock_credentials, "test_project_id"),
|
561
|
+
), mock.patch.object(mock_credentials, "refresh", return_value=None):
|
562
|
+
token = client._get_access_token()
|
563
|
+
assert token == "test_token"
|
564
|
+
|
565
|
+
def test_get_access_token_no_valid_credentials(
|
566
|
+
self, project, location, connection_name
|
567
|
+
):
|
568
|
+
client = ConnectionsClient(project, location, connection_name, None)
|
569
|
+
with mock.patch(
|
570
|
+
"google.adk.tools.application_integration_tool.clients.connections_client.default_service_credential",
|
571
|
+
return_value=(None, None),
|
572
|
+
):
|
573
|
+
with pytest.raises(
|
574
|
+
ValueError,
|
575
|
+
match=(
|
576
|
+
"Please provide a service account that has the required"
|
577
|
+
" permissions"
|
578
|
+
),
|
579
|
+
):
|
580
|
+
client._get_access_token()
|
581
|
+
|
582
|
+
def test_get_access_token_refreshes_expired_token(
|
583
|
+
self, project, location, connection_name, mock_credentials
|
584
|
+
):
|
585
|
+
client = ConnectionsClient(project, location, connection_name, None)
|
586
|
+
mock_credentials.expired = True
|
587
|
+
mock_credentials.token = "old_token"
|
588
|
+
mock_credentials.refresh.return_value = None
|
589
|
+
|
590
|
+
client.credential_cache = mock_credentials
|
591
|
+
with mock.patch(
|
592
|
+
"google.adk.tools.application_integration_tool.clients.connections_client.default_service_credential",
|
593
|
+
return_value=(mock_credentials, "test_project_id"),
|
594
|
+
):
|
595
|
+
# Mock the refresh method directly on the instance within the context
|
596
|
+
with mock.patch.object(mock_credentials, "refresh") as mock_refresh:
|
597
|
+
mock_credentials.token = "new_token" # Set the expected new token
|
598
|
+
token = client._get_access_token()
|
599
|
+
assert token == "new_token"
|
600
|
+
mock_refresh.assert_called_once()
|