google-adk 0.0.3__py3-none-any.whl → 0.0.4__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.
Files changed (184) hide show
  1. google/adk/agents/run_config.py +4 -0
  2. google/adk/auth/auth_preprocessor.py +19 -16
  3. google/adk/cli/browser/index.html +1 -1
  4. google/adk/cli/browser/{main-SY2WYYGV.js → main-CU22TRPI.js} +30 -30
  5. google/adk/cli/cli.py +8 -8
  6. google/adk/cli/cli_deploy.py +2 -4
  7. google/adk/cli/cli_tools_click.py +6 -6
  8. google/adk/flows/llm_flows/contents.py +21 -1
  9. google/adk/flows/llm_flows/functions.py +3 -1
  10. google/adk/models/google_llm.py +0 -1
  11. google/adk/runners.py +13 -2
  12. google/adk/version.py +1 -1
  13. {google_adk-0.0.3.dist-info → google_adk-0.0.4.dist-info}/METADATA +4 -2
  14. google_adk-0.0.4.dist-info/RECORD +175 -0
  15. {google_adk-0.0.3.dist-info → google_adk-0.0.4.dist-info}/WHEEL +1 -1
  16. google_adk-0.0.4.dist-info/licenses/LICENSE +202 -0
  17. google/adk/._version.py +0 -0
  18. google/adk/docs/Makefile +0 -20
  19. google/adk/docs/build/doctrees/google-adk.doctree +0 -0
  20. google/adk/docs/build/html/_sources/google-adk.rst.txt +0 -98
  21. google/adk/docs/build/html/_sources/index.rst.txt +0 -7
  22. google/adk/docs/build/html/_static/autodoc_pydantic.css +0 -27
  23. google/adk/docs/build/html/_static/basic.css +0 -925
  24. google/adk/docs/build/html/_static/debug.css +0 -85
  25. google/adk/docs/build/html/_static/doctools.js +0 -156
  26. google/adk/docs/build/html/_static/documentation_options.js +0 -29
  27. google/adk/docs/build/html/_static/file.png +0 -0
  28. google/adk/docs/build/html/_static/language_data.js +0 -199
  29. google/adk/docs/build/html/_static/minus.png +0 -0
  30. google/adk/docs/build/html/_static/plus.png +0 -0
  31. google/adk/docs/build/html/_static/pygments.css +0 -274
  32. google/adk/docs/build/html/_static/scripts/furo-extensions.js +0 -16
  33. google/adk/docs/build/html/_static/scripts/furo.js +0 -19
  34. google/adk/docs/build/html/_static/scripts/furo.js.LICENSE.txt +0 -7
  35. google/adk/docs/build/html/_static/scripts/furo.js.map +0 -1
  36. google/adk/docs/build/html/_static/searchtools.js +0 -620
  37. google/adk/docs/build/html/_static/skeleton.css +0 -312
  38. google/adk/docs/build/html/_static/sphinx_highlight.js +0 -170
  39. google/adk/docs/build/html/_static/styles/furo-extensions.css +0 -18
  40. google/adk/docs/build/html/_static/styles/furo-extensions.css.map +0 -1
  41. google/adk/docs/build/html/_static/styles/furo.css +0 -18
  42. google/adk/docs/build/html/_static/styles/furo.css.map +0 -1
  43. google/adk/docs/build/html/genindex.html +0 -861
  44. google/adk/docs/build/html/google-adk.html +0 -5461
  45. google/adk/docs/build/html/index.html +0 -567
  46. google/adk/docs/build/html/objects.inv +0 -0
  47. google/adk/docs/build/html/py-modindex.html +0 -373
  48. google/adk/docs/build/html/search.html +0 -333
  49. google/adk/docs/build/html/searchindex.js +0 -17
  50. google/adk/docs/source/conf.py +0 -133
  51. google/adk/docs/source/google-adk.rst +0 -98
  52. google/adk/docs/source/index.rst +0 -7
  53. google/adk/tests/__init__.py +0 -14
  54. google/adk/tests/integration/.env.example +0 -10
  55. google/adk/tests/integration/__init__.py +0 -18
  56. google/adk/tests/integration/conftest.py +0 -119
  57. google/adk/tests/integration/fixture/__init__.py +0 -14
  58. google/adk/tests/integration/fixture/agent_with_config/__init__.py +0 -15
  59. google/adk/tests/integration/fixture/agent_with_config/agent.py +0 -88
  60. google/adk/tests/integration/fixture/callback_agent/__init__.py +0 -15
  61. google/adk/tests/integration/fixture/callback_agent/agent.py +0 -105
  62. google/adk/tests/integration/fixture/context_update_test/OWNERS +0 -1
  63. google/adk/tests/integration/fixture/context_update_test/__init__.py +0 -15
  64. google/adk/tests/integration/fixture/context_update_test/agent.py +0 -43
  65. google/adk/tests/integration/fixture/context_update_test/successful_test.session.json +0 -582
  66. google/adk/tests/integration/fixture/context_variable_agent/__init__.py +0 -15
  67. google/adk/tests/integration/fixture/context_variable_agent/agent.py +0 -115
  68. google/adk/tests/integration/fixture/customer_support_ma/__init__.py +0 -15
  69. google/adk/tests/integration/fixture/customer_support_ma/agent.py +0 -172
  70. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/__init__.py +0 -15
  71. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/agent.py +0 -338
  72. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/order_query.test.json +0 -69
  73. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/test_config.json +0 -6
  74. google/adk/tests/integration/fixture/flow_complex_spark/__init__.py +0 -15
  75. google/adk/tests/integration/fixture/flow_complex_spark/agent.py +0 -182
  76. google/adk/tests/integration/fixture/flow_complex_spark/sample.session.json +0 -190
  77. google/adk/tests/integration/fixture/hello_world_agent/__init__.py +0 -15
  78. google/adk/tests/integration/fixture/hello_world_agent/agent.py +0 -95
  79. google/adk/tests/integration/fixture/hello_world_agent/roll_die.test.json +0 -24
  80. google/adk/tests/integration/fixture/hello_world_agent/test_config.json +0 -6
  81. google/adk/tests/integration/fixture/home_automation_agent/__init__.py +0 -15
  82. google/adk/tests/integration/fixture/home_automation_agent/agent.py +0 -304
  83. google/adk/tests/integration/fixture/home_automation_agent/simple_test.test.json +0 -5
  84. google/adk/tests/integration/fixture/home_automation_agent/simple_test2.test.json +0 -5
  85. google/adk/tests/integration/fixture/home_automation_agent/test_config.json +0 -5
  86. google/adk/tests/integration/fixture/home_automation_agent/test_files/dependent_tool_calls.test.json +0 -18
  87. google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/eval_data.test.json +0 -17
  88. google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/test_config.json +0 -6
  89. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_multi_turn_conversation.test.json +0 -18
  90. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test.test.json +0 -17
  91. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test2.test.json +0 -5
  92. google/adk/tests/integration/fixture/home_automation_agent/test_files/test_config.json +0 -5
  93. google/adk/tests/integration/fixture/tool_agent/__init__.py +0 -15
  94. google/adk/tests/integration/fixture/tool_agent/agent.py +0 -218
  95. google/adk/tests/integration/fixture/tool_agent/files/Agent_test_plan.pdf +0 -0
  96. google/adk/tests/integration/fixture/trip_planner_agent/__init__.py +0 -15
  97. google/adk/tests/integration/fixture/trip_planner_agent/agent.py +0 -110
  98. google/adk/tests/integration/fixture/trip_planner_agent/initial.session.json +0 -13
  99. google/adk/tests/integration/fixture/trip_planner_agent/test_config.json +0 -5
  100. google/adk/tests/integration/fixture/trip_planner_agent/test_files/initial.session.json +0 -13
  101. google/adk/tests/integration/fixture/trip_planner_agent/test_files/test_config.json +0 -5
  102. google/adk/tests/integration/fixture/trip_planner_agent/test_files/trip_inquiry_sub_agent.test.json +0 -7
  103. google/adk/tests/integration/fixture/trip_planner_agent/trip_inquiry.test.json +0 -19
  104. google/adk/tests/integration/models/__init__.py +0 -14
  105. google/adk/tests/integration/models/test_google_llm.py +0 -65
  106. google/adk/tests/integration/test_callback.py +0 -70
  107. google/adk/tests/integration/test_context_variable.py +0 -67
  108. google/adk/tests/integration/test_evalute_agent_in_fixture.py +0 -76
  109. google/adk/tests/integration/test_multi_agent.py +0 -28
  110. google/adk/tests/integration/test_multi_turn.py +0 -42
  111. google/adk/tests/integration/test_single_agent.py +0 -23
  112. google/adk/tests/integration/test_sub_agent.py +0 -26
  113. google/adk/tests/integration/test_system_instruction.py +0 -177
  114. google/adk/tests/integration/test_tools.py +0 -287
  115. google/adk/tests/integration/test_with_test_file.py +0 -34
  116. google/adk/tests/integration/tools/__init__.py +0 -14
  117. google/adk/tests/integration/utils/__init__.py +0 -16
  118. google/adk/tests/integration/utils/asserts.py +0 -75
  119. google/adk/tests/integration/utils/test_runner.py +0 -97
  120. google/adk/tests/unittests/__init__.py +0 -14
  121. google/adk/tests/unittests/agents/__init__.py +0 -14
  122. google/adk/tests/unittests/agents/test_base_agent.py +0 -407
  123. google/adk/tests/unittests/agents/test_langgraph_agent.py +0 -191
  124. google/adk/tests/unittests/agents/test_llm_agent_callbacks.py +0 -138
  125. google/adk/tests/unittests/agents/test_llm_agent_fields.py +0 -231
  126. google/adk/tests/unittests/agents/test_loop_agent.py +0 -136
  127. google/adk/tests/unittests/agents/test_parallel_agent.py +0 -92
  128. google/adk/tests/unittests/agents/test_sequential_agent.py +0 -114
  129. google/adk/tests/unittests/artifacts/__init__.py +0 -14
  130. google/adk/tests/unittests/artifacts/test_artifact_service.py +0 -276
  131. google/adk/tests/unittests/auth/test_auth_handler.py +0 -575
  132. google/adk/tests/unittests/conftest.py +0 -73
  133. google/adk/tests/unittests/fast_api/__init__.py +0 -14
  134. google/adk/tests/unittests/fast_api/test_fast_api.py +0 -269
  135. google/adk/tests/unittests/flows/__init__.py +0 -14
  136. google/adk/tests/unittests/flows/llm_flows/__init__.py +0 -14
  137. google/adk/tests/unittests/flows/llm_flows/_test_examples.py +0 -142
  138. google/adk/tests/unittests/flows/llm_flows/test_agent_transfer.py +0 -311
  139. google/adk/tests/unittests/flows/llm_flows/test_functions_long_running.py +0 -244
  140. google/adk/tests/unittests/flows/llm_flows/test_functions_request_euc.py +0 -346
  141. google/adk/tests/unittests/flows/llm_flows/test_functions_sequential.py +0 -93
  142. google/adk/tests/unittests/flows/llm_flows/test_functions_simple.py +0 -258
  143. google/adk/tests/unittests/flows/llm_flows/test_identity.py +0 -66
  144. google/adk/tests/unittests/flows/llm_flows/test_instructions.py +0 -164
  145. google/adk/tests/unittests/flows/llm_flows/test_model_callbacks.py +0 -142
  146. google/adk/tests/unittests/flows/llm_flows/test_other_configs.py +0 -46
  147. google/adk/tests/unittests/flows/llm_flows/test_tool_callbacks.py +0 -269
  148. google/adk/tests/unittests/models/__init__.py +0 -14
  149. google/adk/tests/unittests/models/test_google_llm.py +0 -224
  150. google/adk/tests/unittests/models/test_litellm.py +0 -804
  151. google/adk/tests/unittests/models/test_models.py +0 -60
  152. google/adk/tests/unittests/sessions/__init__.py +0 -14
  153. google/adk/tests/unittests/sessions/test_session_service.py +0 -227
  154. google/adk/tests/unittests/sessions/test_vertex_ai_session_service.py +0 -246
  155. google/adk/tests/unittests/streaming/__init__.py +0 -14
  156. google/adk/tests/unittests/streaming/test_streaming.py +0 -50
  157. google/adk/tests/unittests/tools/__init__.py +0 -14
  158. google/adk/tests/unittests/tools/apihub_tool/clients/test_apihub_client.py +0 -499
  159. google/adk/tests/unittests/tools/apihub_tool/test_apihub_toolset.py +0 -204
  160. google/adk/tests/unittests/tools/application_integration_tool/clients/test_connections_client.py +0 -600
  161. google/adk/tests/unittests/tools/application_integration_tool/clients/test_integration_client.py +0 -630
  162. google/adk/tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py +0 -345
  163. google/adk/tests/unittests/tools/google_api_tool/__init__.py +0 -13
  164. google/adk/tests/unittests/tools/google_api_tool/test_googleapi_to_openapi_converter.py +0 -657
  165. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_auto_auth_credential_exchanger.py +0 -145
  166. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_base_auth_credential_exchanger.py +0 -68
  167. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_oauth2_exchanger.py +0 -153
  168. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py +0 -196
  169. google/adk/tests/unittests/tools/openapi_tool/auth/test_auth_helper.py +0 -573
  170. google/adk/tests/unittests/tools/openapi_tool/common/test_common.py +0 -436
  171. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test.yaml +0 -1367
  172. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +0 -628
  173. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py +0 -139
  174. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py +0 -406
  175. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py +0 -966
  176. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_tool_auth_handler.py +0 -201
  177. google/adk/tests/unittests/tools/retrieval/__init__.py +0 -14
  178. google/adk/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py +0 -147
  179. google/adk/tests/unittests/tools/test_agent_tool.py +0 -167
  180. google/adk/tests/unittests/tools/test_base_tool.py +0 -141
  181. google/adk/tests/unittests/tools/test_build_function_declaration.py +0 -277
  182. google/adk/tests/unittests/utils.py +0 -304
  183. google_adk-0.0.3.dist-info/RECORD +0 -340
  184. {google_adk-0.0.3.dist-info → google_adk-0.0.4.dist-info}/entry_points.txt +0 -0
@@ -1,630 +0,0 @@
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
- from google.adk.tools.application_integration_tool.clients.integration_client import IntegrationClient
20
- import google.auth
21
- import google.auth.transport.requests
22
- from google.auth.transport.requests import Request
23
- from google.oauth2 import service_account
24
- import pytest
25
- import requests
26
- from requests import exceptions
27
-
28
-
29
- @pytest.fixture
30
- def project():
31
- return "test-project"
32
-
33
-
34
- @pytest.fixture
35
- def location():
36
- return "us-central1"
37
-
38
-
39
- @pytest.fixture
40
- def integration_name():
41
- return "test-integration"
42
-
43
-
44
- @pytest.fixture
45
- def trigger_name():
46
- return "test-trigger"
47
-
48
-
49
- @pytest.fixture
50
- def connection_name():
51
- return "test-connection"
52
-
53
-
54
- @pytest.fixture
55
- def mock_credentials():
56
- creds = mock.create_autospec(google.auth.credentials.Credentials)
57
- creds.token = "test_token"
58
- return creds
59
-
60
-
61
- @pytest.fixture
62
- def mock_auth_request():
63
- return mock.create_autospec(Request)
64
-
65
-
66
- @pytest.fixture
67
- def mock_connections_client():
68
- with mock.patch(
69
- "google.adk.tools.application_integration_tool.clients.integration_client.ConnectionsClient"
70
- ) as mock_client:
71
- mock_instance = mock.create_autospec(ConnectionsClient)
72
- mock_client.return_value = mock_instance
73
- yield mock_client
74
-
75
-
76
- class TestIntegrationClient:
77
-
78
- def test_initialization(
79
- self, project, location, integration_name, trigger_name, connection_name
80
- ):
81
- client = IntegrationClient(
82
- project=project,
83
- location=location,
84
- integration=integration_name,
85
- trigger=trigger_name,
86
- connection=connection_name,
87
- entity_operations={"entity": ["LIST"]},
88
- actions=["action1"],
89
- service_account_json=json.dumps({"email": "test@example.com"}),
90
- )
91
- assert client.project == project
92
- assert client.location == location
93
- assert client.integration == integration_name
94
- assert client.trigger == trigger_name
95
- assert client.connection == connection_name
96
- assert client.entity_operations == {"entity": ["LIST"]}
97
- assert client.actions == ["action1"]
98
- assert client.service_account_json == json.dumps(
99
- {"email": "test@example.com"}
100
- )
101
- assert client.credential_cache is None
102
-
103
- def test_get_openapi_spec_for_integration_success(
104
- self,
105
- project,
106
- location,
107
- integration_name,
108
- trigger_name,
109
- mock_credentials,
110
- mock_connections_client,
111
- ):
112
- expected_spec = {"openapi": "3.0.0", "info": {"title": "Test Integration"}}
113
- mock_response = mock.MagicMock()
114
- mock_response.status_code = 200
115
- mock_response.json.return_value = {"openApiSpec": json.dumps(expected_spec)}
116
-
117
- with mock.patch.object(
118
- IntegrationClient,
119
- "_get_access_token",
120
- return_value=mock_credentials.token,
121
- ), mock.patch("requests.post", return_value=mock_response):
122
- client = IntegrationClient(
123
- project=project,
124
- location=location,
125
- integration=integration_name,
126
- trigger=trigger_name,
127
- connection=None,
128
- entity_operations=None,
129
- actions=None,
130
- service_account_json=None,
131
- )
132
- spec = client.get_openapi_spec_for_integration()
133
- assert spec == expected_spec
134
- requests.post.assert_called_once_with(
135
- f"https://{location}-integrations.googleapis.com/v1/projects/{project}/locations/{location}:generateOpenApiSpec",
136
- headers={
137
- "Content-Type": "application/json",
138
- "Authorization": f"Bearer {mock_credentials.token}",
139
- },
140
- json={
141
- "apiTriggerResources": [{
142
- "integrationResource": integration_name,
143
- "triggerId": [trigger_name],
144
- }],
145
- "fileFormat": "JSON",
146
- },
147
- )
148
-
149
- def test_get_openapi_spec_for_integration_credential_error(
150
- self,
151
- project,
152
- location,
153
- integration_name,
154
- trigger_name,
155
- mock_connections_client,
156
- ):
157
- with mock.patch.object(
158
- IntegrationClient,
159
- "_get_access_token",
160
- side_effect=ValueError(
161
- "Please provide a service account that has the required permissions"
162
- " to access the connection."
163
- ),
164
- ):
165
- client = IntegrationClient(
166
- project=project,
167
- location=location,
168
- integration=integration_name,
169
- trigger=trigger_name,
170
- connection=None,
171
- entity_operations=None,
172
- actions=None,
173
- service_account_json=None,
174
- )
175
- with pytest.raises(
176
- Exception,
177
- match=(
178
- "An unexpected error occurred: Please provide a service account"
179
- " that has the required permissions to access the connection."
180
- ),
181
- ):
182
- client.get_openapi_spec_for_integration()
183
-
184
- @pytest.mark.parametrize(
185
- "status_code, response_text",
186
- [(404, "Not Found"), (400, "Bad Request"), (404, ""), (400, "")],
187
- )
188
- def test_get_openapi_spec_for_integration_request_error_not_found_or_bad_request(
189
- self,
190
- project,
191
- location,
192
- integration_name,
193
- trigger_name,
194
- mock_credentials,
195
- status_code,
196
- response_text,
197
- mock_connections_client,
198
- ):
199
- mock_response = mock.MagicMock()
200
- mock_response.status_code = status_code
201
- mock_response.raise_for_status.side_effect = exceptions.HTTPError(
202
- f"HTTP error {status_code}: {response_text}"
203
- )
204
-
205
- with mock.patch.object(
206
- IntegrationClient,
207
- "_get_access_token",
208
- return_value=mock_credentials.token,
209
- ), mock.patch("requests.post", return_value=mock_response):
210
- client = IntegrationClient(
211
- project=project,
212
- location=location,
213
- integration=integration_name,
214
- trigger=trigger_name,
215
- connection=None,
216
- entity_operations=None,
217
- actions=None,
218
- service_account_json=None,
219
- )
220
- with pytest.raises(
221
- ValueError,
222
- match=(
223
- "Invalid request. Please check the provided values of"
224
- f" project\\({project}\\), location\\({location}\\),"
225
- f" integration\\({integration_name}\\) and"
226
- f" trigger\\({trigger_name}\\)."
227
- ),
228
- ):
229
- client.get_openapi_spec_for_integration()
230
-
231
- def test_get_openapi_spec_for_integration_other_request_error(
232
- self,
233
- project,
234
- location,
235
- integration_name,
236
- trigger_name,
237
- mock_credentials,
238
- mock_connections_client,
239
- ):
240
- mock_response = mock.MagicMock()
241
- mock_response.status_code = 500
242
- mock_response.raise_for_status.side_effect = exceptions.HTTPError(
243
- "Internal Server Error"
244
- )
245
-
246
- with mock.patch.object(
247
- IntegrationClient,
248
- "_get_access_token",
249
- return_value=mock_credentials.token,
250
- ), mock.patch("requests.post", return_value=mock_response):
251
- client = IntegrationClient(
252
- project=project,
253
- location=location,
254
- integration=integration_name,
255
- trigger=trigger_name,
256
- connection=None,
257
- entity_operations=None,
258
- actions=None,
259
- service_account_json=None,
260
- )
261
- with pytest.raises(ValueError, match="Request error: "):
262
- client.get_openapi_spec_for_integration()
263
-
264
- def test_get_openapi_spec_for_integration_unexpected_error(
265
- self,
266
- project,
267
- location,
268
- integration_name,
269
- trigger_name,
270
- mock_credentials,
271
- mock_connections_client,
272
- ):
273
- with mock.patch.object(
274
- IntegrationClient,
275
- "_get_access_token",
276
- return_value=mock_credentials.token,
277
- ), mock.patch(
278
- "requests.post", side_effect=Exception("Something went wrong")
279
- ):
280
- client = IntegrationClient(
281
- project=project,
282
- location=location,
283
- integration=integration_name,
284
- trigger=trigger_name,
285
- connection=None,
286
- entity_operations=None,
287
- actions=None,
288
- service_account_json=None,
289
- )
290
- with pytest.raises(
291
- Exception, match="An unexpected error occurred: Something went wrong"
292
- ):
293
- client.get_openapi_spec_for_integration()
294
-
295
- def test_get_openapi_spec_for_connection_no_entity_operations_or_actions(
296
- self, project, location, connection_name, mock_connections_client
297
- ):
298
- client = IntegrationClient(
299
- project=project,
300
- location=location,
301
- integration=None,
302
- trigger=None,
303
- connection=connection_name,
304
- entity_operations=None,
305
- actions=None,
306
- service_account_json=None,
307
- )
308
- with pytest.raises(
309
- ValueError,
310
- match=(
311
- "No entity operations or actions provided. Please provide at least"
312
- " one of them."
313
- ),
314
- ):
315
- client.get_openapi_spec_for_connection()
316
-
317
- def test_get_openapi_spec_for_connection_with_entity_operations(
318
- self, project, location, connection_name, mock_connections_client
319
- ):
320
- entity_operations = {"entity1": ["LIST", "GET"]}
321
-
322
- mock_connections_client_instance = mock_connections_client.return_value
323
- mock_connections_client_instance.get_connector_base_spec.return_value = {
324
- "components": {"schemas": {}},
325
- "paths": {},
326
- }
327
- mock_connections_client_instance.get_entity_schema_and_operations.return_value = (
328
- {"type": "object", "properties": {"id": {"type": "string"}}},
329
- ["LIST", "GET"],
330
- )
331
- mock_connections_client_instance.connector_payload.return_value = {
332
- "type": "object"
333
- }
334
- mock_connections_client_instance.list_operation.return_value = {"get": {}}
335
- mock_connections_client_instance.list_operation_request.return_value = {
336
- "type": "object"
337
- }
338
- mock_connections_client_instance.get_operation.return_value = {"get": {}}
339
- mock_connections_client_instance.get_operation_request.return_value = {
340
- "type": "object"
341
- }
342
-
343
- client = IntegrationClient(
344
- project=project,
345
- location=location,
346
- integration=None,
347
- trigger=None,
348
- connection=connection_name,
349
- entity_operations=entity_operations,
350
- actions=None,
351
- service_account_json=None,
352
- )
353
- spec = client.get_openapi_spec_for_connection()
354
- assert "paths" in spec
355
- assert (
356
- f"/v2/projects/{project}/locations/{location}/integrations/ExecuteConnection:execute?triggerId=api_trigger/ExecuteConnection#list_entity1"
357
- in spec["paths"]
358
- )
359
- assert (
360
- f"/v2/projects/{project}/locations/{location}/integrations/ExecuteConnection:execute?triggerId=api_trigger/ExecuteConnection#get_entity1"
361
- in spec["paths"]
362
- )
363
- mock_connections_client.assert_called_once_with(
364
- project, location, connection_name, None
365
- )
366
- mock_connections_client_instance.get_connector_base_spec.assert_called_once()
367
- mock_connections_client_instance.get_entity_schema_and_operations.assert_any_call(
368
- "entity1"
369
- )
370
- mock_connections_client_instance.connector_payload.assert_any_call(
371
- {"type": "object", "properties": {"id": {"type": "string"}}}
372
- )
373
- mock_connections_client_instance.list_operation.assert_called_once()
374
- mock_connections_client_instance.get_operation.assert_called_once()
375
-
376
- def test_get_openapi_spec_for_connection_with_actions(
377
- self, project, location, connection_name, mock_connections_client
378
- ):
379
- actions = ["TestAction"]
380
- mock_connections_client_instance = (
381
- mock_connections_client.return_value
382
- ) # Corrected line
383
- mock_connections_client_instance.get_connector_base_spec.return_value = {
384
- "components": {"schemas": {}},
385
- "paths": {},
386
- }
387
- mock_connections_client_instance.get_action_schema.return_value = {
388
- "inputSchema": {
389
- "type": "object",
390
- "properties": {"input": {"type": "string"}},
391
- },
392
- "outputSchema": {
393
- "type": "object",
394
- "properties": {"output": {"type": "string"}},
395
- },
396
- "displayName": "TestAction",
397
- }
398
- mock_connections_client_instance.connector_payload.side_effect = [
399
- {"type": "object"},
400
- {"type": "object"},
401
- ]
402
- mock_connections_client_instance.action_request.return_value = {
403
- "type": "object"
404
- }
405
- mock_connections_client_instance.action_response.return_value = {
406
- "type": "object"
407
- }
408
- mock_connections_client_instance.get_action_operation.return_value = {
409
- "post": {}
410
- }
411
-
412
- client = IntegrationClient(
413
- project=project,
414
- location=location,
415
- integration=None,
416
- trigger=None,
417
- connection=connection_name,
418
- entity_operations=None,
419
- actions=actions,
420
- service_account_json=None,
421
- )
422
- spec = client.get_openapi_spec_for_connection()
423
- assert "paths" in spec
424
- assert (
425
- f"/v2/projects/{project}/locations/{location}/integrations/ExecuteConnection:execute?triggerId=api_trigger/ExecuteConnection#TestAction"
426
- in spec["paths"]
427
- )
428
- mock_connections_client.assert_called_once_with(
429
- project, location, connection_name, None
430
- )
431
- mock_connections_client_instance.get_connector_base_spec.assert_called_once()
432
- mock_connections_client_instance.get_action_schema.assert_called_once_with(
433
- "TestAction"
434
- )
435
- mock_connections_client_instance.connector_payload.assert_any_call(
436
- {"type": "object", "properties": {"input": {"type": "string"}}}
437
- )
438
- mock_connections_client_instance.connector_payload.assert_any_call(
439
- {"type": "object", "properties": {"output": {"type": "string"}}}
440
- )
441
- mock_connections_client_instance.action_request.assert_called_once_with(
442
- "TestAction"
443
- )
444
- mock_connections_client_instance.action_response.assert_called_once_with(
445
- "TestAction"
446
- )
447
- mock_connections_client_instance.get_action_operation.assert_called_once()
448
-
449
- def test_get_openapi_spec_for_connection_invalid_operation(
450
- self, project, location, connection_name, mock_connections_client
451
- ):
452
- entity_operations = {"entity1": ["INVALID"]}
453
- mock_connections_client_instance = mock_connections_client.return_value
454
- mock_connections_client_instance.get_connector_base_spec.return_value = {
455
- "components": {"schemas": {}},
456
- "paths": {},
457
- }
458
- mock_connections_client_instance.get_entity_schema_and_operations.return_value = (
459
- {"type": "object", "properties": {"id": {"type": "string"}}},
460
- ["LIST", "GET"],
461
- )
462
-
463
- client = IntegrationClient(
464
- project=project,
465
- location=location,
466
- integration=None,
467
- trigger=None,
468
- connection=connection_name,
469
- entity_operations=entity_operations,
470
- actions=None,
471
- service_account_json=None,
472
- )
473
- with pytest.raises(
474
- ValueError, match="Invalid operation: INVALID for entity: entity1"
475
- ):
476
- client.get_openapi_spec_for_connection()
477
-
478
- def test_get_access_token_with_service_account_json(
479
- self, project, location, integration_name, trigger_name, connection_name
480
- ):
481
- service_account_json = json.dumps({
482
- "client_email": "test@example.com",
483
- "private_key": "test_key",
484
- })
485
- mock_creds = mock.create_autospec(service_account.Credentials)
486
- mock_creds.token = "sa_token"
487
- mock_creds.expired = False
488
-
489
- with mock.patch(
490
- "google.oauth2.service_account.Credentials.from_service_account_info",
491
- return_value=mock_creds,
492
- ), mock.patch.object(mock_creds, "refresh", return_value=None):
493
- client = IntegrationClient(
494
- project=project,
495
- location=location,
496
- integration=integration_name,
497
- trigger=trigger_name,
498
- connection=connection_name,
499
- entity_operations=None,
500
- actions=None,
501
- service_account_json=service_account_json,
502
- )
503
- token = client._get_access_token()
504
- assert token == "sa_token"
505
- service_account.Credentials.from_service_account_info.assert_called_once_with(
506
- json.loads(service_account_json),
507
- scopes=["https://www.googleapis.com/auth/cloud-platform"],
508
- )
509
- mock_creds.refresh.assert_called_once()
510
-
511
- def test_get_access_token_with_default_credentials(
512
- self,
513
- project,
514
- location,
515
- integration_name,
516
- trigger_name,
517
- connection_name,
518
- mock_credentials,
519
- ):
520
- mock_credentials.expired = False
521
- with mock.patch(
522
- "google.adk.tools.application_integration_tool.clients.integration_client.default_service_credential",
523
- return_value=(mock_credentials, "test_project_id"),
524
- ), mock.patch.object(mock_credentials, "refresh", return_value=None):
525
- client = IntegrationClient(
526
- project=project,
527
- location=location,
528
- integration=integration_name,
529
- trigger=trigger_name,
530
- connection=connection_name,
531
- entity_operations=None,
532
- actions=None,
533
- service_account_json=None,
534
- )
535
- token = client._get_access_token()
536
- assert token == "test_token"
537
-
538
- def test_get_access_token_no_valid_credentials(
539
- self, project, location, integration_name, trigger_name, connection_name
540
- ):
541
- with mock.patch(
542
- "google.adk.tools.application_integration_tool.clients.integration_client.default_service_credential",
543
- return_value=(None, None),
544
- ), mock.patch(
545
- "google.oauth2.service_account.Credentials.from_service_account_info",
546
- return_value=None,
547
- ):
548
- client = IntegrationClient(
549
- project=project,
550
- location=location,
551
- integration=integration_name,
552
- trigger=trigger_name,
553
- connection=connection_name,
554
- entity_operations=None,
555
- actions=None,
556
- service_account_json=None,
557
- )
558
- try:
559
- client._get_access_token()
560
- assert False, "ValueError was not raised" # Explicitly fail if no error
561
- except ValueError as e:
562
- assert (
563
- "Please provide a service account that has the required permissions"
564
- " to access the connection."
565
- in str(e)
566
- )
567
-
568
- def test_get_access_token_uses_cached_token(
569
- self,
570
- project,
571
- location,
572
- integration_name,
573
- trigger_name,
574
- connection_name,
575
- mock_credentials,
576
- ):
577
- mock_credentials.token = "cached_token"
578
- mock_credentials.expired = False
579
- client = IntegrationClient(
580
- project=project,
581
- location=location,
582
- integration=integration_name,
583
- trigger=trigger_name,
584
- connection=connection_name,
585
- entity_operations=None,
586
- actions=None,
587
- service_account_json=None,
588
- )
589
- client.credential_cache = mock_credentials # Simulate a cached credential
590
- with mock.patch("google.auth.default") as mock_default, mock.patch(
591
- "google.oauth2.service_account.Credentials.from_service_account_info"
592
- ) as mock_sa:
593
- token = client._get_access_token()
594
- assert token == "cached_token"
595
- mock_default.assert_not_called()
596
- mock_sa.assert_not_called()
597
-
598
- def test_get_access_token_refreshes_expired_token(
599
- self,
600
- project,
601
- location,
602
- integration_name,
603
- trigger_name,
604
- connection_name,
605
- mock_credentials,
606
- ):
607
- mock_credentials = mock.create_autospec(google.auth.credentials.Credentials)
608
- mock_credentials.token = "old_token"
609
- mock_credentials.expired = True
610
- mock_credentials.refresh.return_value = None
611
- mock_credentials.token = "new_token" # Simulate token refresh
612
-
613
- with mock.patch(
614
- "google.adk.tools.application_integration_tool.clients.integration_client.default_service_credential",
615
- return_value=(mock_credentials, "test_project_id"),
616
- ):
617
- client = IntegrationClient(
618
- project=project,
619
- location=location,
620
- integration=integration_name,
621
- trigger=trigger_name,
622
- connection=connection_name,
623
- entity_operations=None,
624
- actions=None,
625
- service_account_json=None,
626
- )
627
- client.credential_cache = mock_credentials
628
- token = client._get_access_token()
629
- assert token == "new_token"
630
- mock_credentials.refresh.assert_called_once()