google-adk 0.0.2__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 (153) hide show
  1. google/adk/agents/run_config.py +4 -0
  2. google/adk/auth/auth_preprocessor.py +19 -16
  3. google/adk/cli/agent_graph.py +31 -5
  4. google/adk/cli/browser/index.html +1 -1
  5. google/adk/cli/browser/{main-XUU6OGCC.js → main-CU22TRPI.js} +30 -30
  6. google/adk/cli/cli.py +8 -8
  7. google/adk/cli/cli_deploy.py +2 -4
  8. google/adk/cli/cli_tools_click.py +57 -12
  9. google/adk/cli/fast_api.py +19 -9
  10. google/adk/flows/llm_flows/contents.py +21 -1
  11. google/adk/flows/llm_flows/functions.py +3 -1
  12. google/adk/models/google_llm.py +0 -1
  13. google/adk/runners.py +13 -2
  14. google/adk/version.py +1 -1
  15. {google_adk-0.0.2.dist-info → google_adk-0.0.4.dist-info}/METADATA +4 -2
  16. google_adk-0.0.4.dist-info/RECORD +175 -0
  17. {google_adk-0.0.2.dist-info → google_adk-0.0.4.dist-info}/WHEEL +1 -1
  18. google/adk/cli/media_streamer/__init__.py +0 -19
  19. google/adk/cli/media_streamer/index.html +0 -228
  20. google/adk/tests/__init__.py +0 -14
  21. google/adk/tests/integration/.env.example +0 -10
  22. google/adk/tests/integration/__init__.py +0 -18
  23. google/adk/tests/integration/conftest.py +0 -119
  24. google/adk/tests/integration/fixture/__init__.py +0 -14
  25. google/adk/tests/integration/fixture/agent_with_config/__init__.py +0 -15
  26. google/adk/tests/integration/fixture/agent_with_config/agent.py +0 -88
  27. google/adk/tests/integration/fixture/callback_agent/__init__.py +0 -15
  28. google/adk/tests/integration/fixture/callback_agent/agent.py +0 -105
  29. google/adk/tests/integration/fixture/context_update_test/OWNERS +0 -1
  30. google/adk/tests/integration/fixture/context_update_test/__init__.py +0 -15
  31. google/adk/tests/integration/fixture/context_update_test/agent.py +0 -43
  32. google/adk/tests/integration/fixture/context_update_test/successful_test.session.json +0 -582
  33. google/adk/tests/integration/fixture/context_variable_agent/__init__.py +0 -15
  34. google/adk/tests/integration/fixture/context_variable_agent/agent.py +0 -115
  35. google/adk/tests/integration/fixture/customer_support_ma/__init__.py +0 -15
  36. google/adk/tests/integration/fixture/customer_support_ma/agent.py +0 -172
  37. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/__init__.py +0 -15
  38. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/agent.py +0 -338
  39. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/order_query.test.json +0 -69
  40. google/adk/tests/integration/fixture/ecommerce_customer_service_agent/test_config.json +0 -6
  41. google/adk/tests/integration/fixture/flow_complex_spark/__init__.py +0 -15
  42. google/adk/tests/integration/fixture/flow_complex_spark/agent.py +0 -182
  43. google/adk/tests/integration/fixture/flow_complex_spark/sample.debug.log +0 -243
  44. google/adk/tests/integration/fixture/flow_complex_spark/sample.session.json +0 -190
  45. google/adk/tests/integration/fixture/hello_world_agent/__init__.py +0 -15
  46. google/adk/tests/integration/fixture/hello_world_agent/agent.py +0 -95
  47. google/adk/tests/integration/fixture/hello_world_agent/roll_die.test.json +0 -24
  48. google/adk/tests/integration/fixture/hello_world_agent/test_config.json +0 -6
  49. google/adk/tests/integration/fixture/home_automation_agent/__init__.py +0 -15
  50. google/adk/tests/integration/fixture/home_automation_agent/agent.py +0 -304
  51. google/adk/tests/integration/fixture/home_automation_agent/simple_test.test.json +0 -5
  52. google/adk/tests/integration/fixture/home_automation_agent/simple_test2.test.json +0 -5
  53. google/adk/tests/integration/fixture/home_automation_agent/test_config.json +0 -5
  54. google/adk/tests/integration/fixture/home_automation_agent/test_files/dependent_tool_calls.test.json +0 -18
  55. google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/eval_data.test.json +0 -17
  56. google/adk/tests/integration/fixture/home_automation_agent/test_files/memorizing_past_events/test_config.json +0 -6
  57. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_multi_turn_conversation.test.json +0 -18
  58. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test.test.json +0 -17
  59. google/adk/tests/integration/fixture/home_automation_agent/test_files/simple_test2.test.json +0 -5
  60. google/adk/tests/integration/fixture/home_automation_agent/test_files/test_config.json +0 -5
  61. google/adk/tests/integration/fixture/tool_agent/__init__.py +0 -15
  62. google/adk/tests/integration/fixture/tool_agent/agent.py +0 -218
  63. google/adk/tests/integration/fixture/tool_agent/files/Agent_test_plan.pdf +0 -0
  64. google/adk/tests/integration/fixture/trip_planner_agent/__init__.py +0 -15
  65. google/adk/tests/integration/fixture/trip_planner_agent/agent.py +0 -110
  66. google/adk/tests/integration/fixture/trip_planner_agent/initial.session.json +0 -13
  67. google/adk/tests/integration/fixture/trip_planner_agent/test_config.json +0 -5
  68. google/adk/tests/integration/fixture/trip_planner_agent/test_files/initial.session.json +0 -13
  69. google/adk/tests/integration/fixture/trip_planner_agent/test_files/test_config.json +0 -5
  70. google/adk/tests/integration/fixture/trip_planner_agent/test_files/trip_inquiry_sub_agent.test.json +0 -7
  71. google/adk/tests/integration/fixture/trip_planner_agent/trip_inquiry.test.json +0 -19
  72. google/adk/tests/integration/models/__init__.py +0 -14
  73. google/adk/tests/integration/models/test_google_llm.py +0 -65
  74. google/adk/tests/integration/test_callback.py +0 -70
  75. google/adk/tests/integration/test_context_variable.py +0 -67
  76. google/adk/tests/integration/test_evalute_agent_in_fixture.py +0 -76
  77. google/adk/tests/integration/test_multi_agent.py +0 -28
  78. google/adk/tests/integration/test_multi_turn.py +0 -42
  79. google/adk/tests/integration/test_single_agent.py +0 -23
  80. google/adk/tests/integration/test_sub_agent.py +0 -26
  81. google/adk/tests/integration/test_system_instruction.py +0 -177
  82. google/adk/tests/integration/test_tools.py +0 -287
  83. google/adk/tests/integration/test_with_test_file.py +0 -34
  84. google/adk/tests/integration/tools/__init__.py +0 -14
  85. google/adk/tests/integration/utils/__init__.py +0 -16
  86. google/adk/tests/integration/utils/asserts.py +0 -75
  87. google/adk/tests/integration/utils/test_runner.py +0 -97
  88. google/adk/tests/unittests/__init__.py +0 -14
  89. google/adk/tests/unittests/agents/__init__.py +0 -14
  90. google/adk/tests/unittests/agents/test_base_agent.py +0 -407
  91. google/adk/tests/unittests/agents/test_langgraph_agent.py +0 -191
  92. google/adk/tests/unittests/agents/test_llm_agent_callbacks.py +0 -138
  93. google/adk/tests/unittests/agents/test_llm_agent_fields.py +0 -231
  94. google/adk/tests/unittests/agents/test_loop_agent.py +0 -136
  95. google/adk/tests/unittests/agents/test_parallel_agent.py +0 -92
  96. google/adk/tests/unittests/agents/test_sequential_agent.py +0 -114
  97. google/adk/tests/unittests/artifacts/__init__.py +0 -14
  98. google/adk/tests/unittests/artifacts/test_artifact_service.py +0 -276
  99. google/adk/tests/unittests/auth/test_auth_handler.py +0 -575
  100. google/adk/tests/unittests/conftest.py +0 -73
  101. google/adk/tests/unittests/fast_api/__init__.py +0 -14
  102. google/adk/tests/unittests/fast_api/test_fast_api.py +0 -269
  103. google/adk/tests/unittests/flows/__init__.py +0 -14
  104. google/adk/tests/unittests/flows/llm_flows/__init__.py +0 -14
  105. google/adk/tests/unittests/flows/llm_flows/_test_examples.py +0 -142
  106. google/adk/tests/unittests/flows/llm_flows/test_agent_transfer.py +0 -311
  107. google/adk/tests/unittests/flows/llm_flows/test_functions_long_running.py +0 -244
  108. google/adk/tests/unittests/flows/llm_flows/test_functions_request_euc.py +0 -346
  109. google/adk/tests/unittests/flows/llm_flows/test_functions_sequential.py +0 -93
  110. google/adk/tests/unittests/flows/llm_flows/test_functions_simple.py +0 -258
  111. google/adk/tests/unittests/flows/llm_flows/test_identity.py +0 -66
  112. google/adk/tests/unittests/flows/llm_flows/test_instructions.py +0 -164
  113. google/adk/tests/unittests/flows/llm_flows/test_model_callbacks.py +0 -142
  114. google/adk/tests/unittests/flows/llm_flows/test_other_configs.py +0 -46
  115. google/adk/tests/unittests/flows/llm_flows/test_tool_callbacks.py +0 -269
  116. google/adk/tests/unittests/models/__init__.py +0 -14
  117. google/adk/tests/unittests/models/test_google_llm.py +0 -224
  118. google/adk/tests/unittests/models/test_litellm.py +0 -804
  119. google/adk/tests/unittests/models/test_models.py +0 -60
  120. google/adk/tests/unittests/sessions/__init__.py +0 -14
  121. google/adk/tests/unittests/sessions/test_session_service.py +0 -227
  122. google/adk/tests/unittests/sessions/test_vertex_ai_session_service.py +0 -246
  123. google/adk/tests/unittests/streaming/__init__.py +0 -14
  124. google/adk/tests/unittests/streaming/test_streaming.py +0 -50
  125. google/adk/tests/unittests/tools/__init__.py +0 -14
  126. google/adk/tests/unittests/tools/apihub_tool/clients/test_apihub_client.py +0 -499
  127. google/adk/tests/unittests/tools/apihub_tool/test_apihub_toolset.py +0 -204
  128. google/adk/tests/unittests/tools/application_integration_tool/clients/test_connections_client.py +0 -600
  129. google/adk/tests/unittests/tools/application_integration_tool/clients/test_integration_client.py +0 -630
  130. google/adk/tests/unittests/tools/application_integration_tool/test_application_integration_toolset.py +0 -345
  131. google/adk/tests/unittests/tools/google_api_tool/__init__.py +0 -13
  132. google/adk/tests/unittests/tools/google_api_tool/test_googleapi_to_openapi_converter.py +0 -657
  133. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_auto_auth_credential_exchanger.py +0 -145
  134. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_base_auth_credential_exchanger.py +0 -68
  135. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_oauth2_exchanger.py +0 -153
  136. google/adk/tests/unittests/tools/openapi_tool/auth/credential_exchangers/test_service_account_exchanger.py +0 -196
  137. google/adk/tests/unittests/tools/openapi_tool/auth/test_auth_helper.py +0 -573
  138. google/adk/tests/unittests/tools/openapi_tool/common/test_common.py +0 -436
  139. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test.yaml +0 -1367
  140. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_spec_parser.py +0 -628
  141. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_openapi_toolset.py +0 -139
  142. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_operation_parser.py +0 -406
  143. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_rest_api_tool.py +0 -966
  144. google/adk/tests/unittests/tools/openapi_tool/openapi_spec_parser/test_tool_auth_handler.py +0 -201
  145. google/adk/tests/unittests/tools/retrieval/__init__.py +0 -14
  146. google/adk/tests/unittests/tools/retrieval/test_vertex_ai_rag_retrieval.py +0 -147
  147. google/adk/tests/unittests/tools/test_agent_tool.py +0 -167
  148. google/adk/tests/unittests/tools/test_base_tool.py +0 -141
  149. google/adk/tests/unittests/tools/test_build_function_declaration.py +0 -277
  150. google/adk/tests/unittests/utils.py +0 -304
  151. google_adk-0.0.2.dist-info/RECORD +0 -308
  152. {google_adk-0.0.2.dist-info → google_adk-0.0.4.dist-info}/entry_points.txt +0 -0
  153. {google_adk-0.0.2.dist-info → google_adk-0.0.4.dist-info/licenses}/LICENSE +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()