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,573 +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
- from unittest.mock import patch
16
-
17
- from fastapi.openapi.models import APIKey
18
- from fastapi.openapi.models import APIKeyIn
19
- from fastapi.openapi.models import HTTPBase
20
- from fastapi.openapi.models import HTTPBearer
21
- from fastapi.openapi.models import OAuth2
22
- from fastapi.openapi.models import OpenIdConnect
23
- from google.adk.auth.auth_credential import AuthCredential
24
- from google.adk.auth.auth_credential import AuthCredentialTypes
25
- from google.adk.auth.auth_credential import HttpAuth
26
- from google.adk.auth.auth_credential import HttpCredentials
27
- from google.adk.auth.auth_credential import ServiceAccount
28
- from google.adk.auth.auth_credential import ServiceAccountCredential
29
- from google.adk.auth.auth_schemes import AuthSchemeType
30
- from google.adk.auth.auth_schemes import OpenIdConnectWithConfig
31
- from google.adk.tools.openapi_tool.auth.auth_helpers import credential_to_param
32
- from google.adk.tools.openapi_tool.auth.auth_helpers import dict_to_auth_scheme
33
- from google.adk.tools.openapi_tool.auth.auth_helpers import INTERNAL_AUTH_PREFIX
34
- from google.adk.tools.openapi_tool.auth.auth_helpers import openid_dict_to_scheme_credential
35
- from google.adk.tools.openapi_tool.auth.auth_helpers import openid_url_to_scheme_credential
36
- from google.adk.tools.openapi_tool.auth.auth_helpers import service_account_dict_to_scheme_credential
37
- from google.adk.tools.openapi_tool.auth.auth_helpers import service_account_scheme_credential
38
- from google.adk.tools.openapi_tool.auth.auth_helpers import token_to_scheme_credential
39
- import pytest
40
- import requests
41
-
42
-
43
- def test_token_to_scheme_credential_api_key_header():
44
- scheme, credential = token_to_scheme_credential(
45
- "apikey", "header", "X-API-Key", "test_key"
46
- )
47
-
48
- assert isinstance(scheme, APIKey)
49
- assert scheme.type_ == AuthSchemeType.apiKey
50
- assert scheme.in_ == APIKeyIn.header
51
- assert scheme.name == "X-API-Key"
52
- assert credential == AuthCredential(
53
- auth_type=AuthCredentialTypes.API_KEY, api_key="test_key"
54
- )
55
-
56
-
57
- def test_token_to_scheme_credential_api_key_query():
58
- scheme, credential = token_to_scheme_credential(
59
- "apikey", "query", "api_key", "test_key"
60
- )
61
-
62
- assert isinstance(scheme, APIKey)
63
- assert scheme.type_ == AuthSchemeType.apiKey
64
- assert scheme.in_ == APIKeyIn.query
65
- assert scheme.name == "api_key"
66
- assert credential == AuthCredential(
67
- auth_type=AuthCredentialTypes.API_KEY, api_key="test_key"
68
- )
69
-
70
-
71
- def test_token_to_scheme_credential_api_key_cookie():
72
- scheme, credential = token_to_scheme_credential(
73
- "apikey", "cookie", "session_id", "test_key"
74
- )
75
-
76
- assert isinstance(scheme, APIKey)
77
- assert scheme.type_ == AuthSchemeType.apiKey
78
- assert scheme.in_ == APIKeyIn.cookie
79
- assert scheme.name == "session_id"
80
- assert credential == AuthCredential(
81
- auth_type=AuthCredentialTypes.API_KEY, api_key="test_key"
82
- )
83
-
84
-
85
- def test_token_to_scheme_credential_api_key_no_credential():
86
- scheme, credential = token_to_scheme_credential(
87
- "apikey", "cookie", "session_id"
88
- )
89
-
90
- assert isinstance(scheme, APIKey)
91
- assert credential is None
92
-
93
-
94
- def test_token_to_scheme_credential_oauth2_token():
95
- scheme, credential = token_to_scheme_credential(
96
- "oauth2Token", "header", "Authorization", "test_token"
97
- )
98
-
99
- assert isinstance(scheme, HTTPBearer)
100
- assert scheme.bearerFormat == "JWT"
101
- assert credential == AuthCredential(
102
- auth_type=AuthCredentialTypes.HTTP,
103
- http=HttpAuth(
104
- scheme="bearer", credentials=HttpCredentials(token="test_token")
105
- ),
106
- )
107
-
108
-
109
- def test_token_to_scheme_credential_oauth2_no_credential():
110
- scheme, credential = token_to_scheme_credential(
111
- "oauth2Token", "header", "Authorization"
112
- )
113
-
114
- assert isinstance(scheme, HTTPBearer)
115
- assert credential is None
116
-
117
-
118
- def test_service_account_dict_to_scheme_credential():
119
- config = {
120
- "type": "service_account",
121
- "project_id": "project_id",
122
- "private_key_id": "private_key_id",
123
- "private_key": "private_key",
124
- "client_email": "client_email",
125
- "client_id": "client_id",
126
- "auth_uri": "auth_uri",
127
- "token_uri": "token_uri",
128
- "auth_provider_x509_cert_url": "auth_provider_x509_cert_url",
129
- "client_x509_cert_url": "client_x509_cert_url",
130
- "universe_domain": "universe_domain",
131
- }
132
- scopes = ["scope1", "scope2"]
133
-
134
- scheme, credential = service_account_dict_to_scheme_credential(config, scopes)
135
-
136
- assert isinstance(scheme, HTTPBearer)
137
- assert scheme.bearerFormat == "JWT"
138
- assert credential.auth_type == AuthCredentialTypes.SERVICE_ACCOUNT
139
- assert credential.service_account.scopes == scopes
140
- assert (
141
- credential.service_account.service_account_credential.project_id
142
- == "project_id"
143
- )
144
-
145
-
146
- def test_service_account_scheme_credential():
147
- config = ServiceAccount(
148
- service_account_credential=ServiceAccountCredential(
149
- type="service_account",
150
- project_id="project_id",
151
- private_key_id="private_key_id",
152
- private_key="private_key",
153
- client_email="client_email",
154
- client_id="client_id",
155
- auth_uri="auth_uri",
156
- token_uri="token_uri",
157
- auth_provider_x509_cert_url="auth_provider_x509_cert_url",
158
- client_x509_cert_url="client_x509_cert_url",
159
- universe_domain="universe_domain",
160
- ),
161
- scopes=["scope1", "scope2"],
162
- )
163
-
164
- scheme, credential = service_account_scheme_credential(config)
165
-
166
- assert isinstance(scheme, HTTPBearer)
167
- assert scheme.bearerFormat == "JWT"
168
- assert credential.auth_type == AuthCredentialTypes.SERVICE_ACCOUNT
169
- assert credential.service_account == config
170
-
171
-
172
- def test_openid_dict_to_scheme_credential():
173
- config_dict = {
174
- "authorization_endpoint": "auth_url",
175
- "token_endpoint": "token_url",
176
- "openIdConnectUrl": "openid_url",
177
- }
178
- credential_dict = {
179
- "client_id": "client_id",
180
- "client_secret": "client_secret",
181
- "redirect_uri": "redirect_uri",
182
- }
183
- scopes = ["scope1", "scope2"]
184
-
185
- scheme, credential = openid_dict_to_scheme_credential(
186
- config_dict, scopes, credential_dict
187
- )
188
-
189
- assert isinstance(scheme, OpenIdConnectWithConfig)
190
- assert scheme.authorization_endpoint == "auth_url"
191
- assert scheme.token_endpoint == "token_url"
192
- assert scheme.scopes == scopes
193
- assert credential.auth_type == AuthCredentialTypes.OPEN_ID_CONNECT
194
- assert credential.oauth2.client_id == "client_id"
195
- assert credential.oauth2.client_secret == "client_secret"
196
- assert credential.oauth2.redirect_uri == "redirect_uri"
197
-
198
-
199
- def test_openid_dict_to_scheme_credential_no_openid_url():
200
- config_dict = {
201
- "authorization_endpoint": "auth_url",
202
- "token_endpoint": "token_url",
203
- }
204
- credential_dict = {
205
- "client_id": "client_id",
206
- "client_secret": "client_secret",
207
- "redirect_uri": "redirect_uri",
208
- }
209
- scopes = ["scope1", "scope2"]
210
-
211
- scheme, credential = openid_dict_to_scheme_credential(
212
- config_dict, scopes, credential_dict
213
- )
214
-
215
- assert scheme.openIdConnectUrl == ""
216
-
217
-
218
- def test_openid_dict_to_scheme_credential_google_oauth_credential():
219
- config_dict = {
220
- "authorization_endpoint": "auth_url",
221
- "token_endpoint": "token_url",
222
- "openIdConnectUrl": "openid_url",
223
- }
224
- credential_dict = {
225
- "web": {
226
- "client_id": "client_id",
227
- "client_secret": "client_secret",
228
- "redirect_uri": "redirect_uri",
229
- }
230
- }
231
- scopes = ["scope1", "scope2"]
232
-
233
- scheme, credential = openid_dict_to_scheme_credential(
234
- config_dict, scopes, credential_dict
235
- )
236
-
237
- assert isinstance(scheme, OpenIdConnectWithConfig)
238
- assert credential.auth_type == AuthCredentialTypes.OPEN_ID_CONNECT
239
- assert credential.oauth2.client_id == "client_id"
240
- assert credential.oauth2.client_secret == "client_secret"
241
- assert credential.oauth2.redirect_uri == "redirect_uri"
242
-
243
-
244
- def test_openid_dict_to_scheme_credential_invalid_config():
245
- config_dict = {
246
- "invalid_field": "value",
247
- }
248
- credential_dict = {
249
- "client_id": "client_id",
250
- "client_secret": "client_secret",
251
- }
252
- scopes = ["scope1", "scope2"]
253
-
254
- with pytest.raises(ValueError, match="Invalid OpenID Connect configuration"):
255
- openid_dict_to_scheme_credential(config_dict, scopes, credential_dict)
256
-
257
-
258
- def test_openid_dict_to_scheme_credential_missing_credential_fields():
259
- config_dict = {
260
- "authorization_endpoint": "auth_url",
261
- "token_endpoint": "token_url",
262
- }
263
- credential_dict = {
264
- "client_id": "client_id",
265
- }
266
- scopes = ["scope1", "scope2"]
267
-
268
- with pytest.raises(
269
- ValueError,
270
- match="Missing required fields in credential_dict: client_secret",
271
- ):
272
- openid_dict_to_scheme_credential(config_dict, scopes, credential_dict)
273
-
274
-
275
- @patch("requests.get")
276
- def test_openid_url_to_scheme_credential(mock_get):
277
- mock_response = {
278
- "authorization_endpoint": "auth_url",
279
- "token_endpoint": "token_url",
280
- "userinfo_endpoint": "userinfo_url",
281
- }
282
- mock_get.return_value.json.return_value = mock_response
283
- mock_get.return_value.raise_for_status.return_value = None
284
- credential_dict = {
285
- "client_id": "client_id",
286
- "client_secret": "client_secret",
287
- "redirect_uri": "redirect_uri",
288
- }
289
- scopes = ["scope1", "scope2"]
290
-
291
- scheme, credential = openid_url_to_scheme_credential(
292
- "openid_url", scopes, credential_dict
293
- )
294
-
295
- assert isinstance(scheme, OpenIdConnectWithConfig)
296
- assert scheme.authorization_endpoint == "auth_url"
297
- assert scheme.token_endpoint == "token_url"
298
- assert scheme.scopes == scopes
299
- assert credential.auth_type == AuthCredentialTypes.OPEN_ID_CONNECT
300
- assert credential.oauth2.client_id == "client_id"
301
- assert credential.oauth2.client_secret == "client_secret"
302
- assert credential.oauth2.redirect_uri == "redirect_uri"
303
- mock_get.assert_called_once_with("openid_url", timeout=10)
304
-
305
-
306
- @patch("requests.get")
307
- def test_openid_url_to_scheme_credential_no_openid_url(mock_get):
308
- mock_response = {
309
- "authorization_endpoint": "auth_url",
310
- "token_endpoint": "token_url",
311
- "userinfo_endpoint": "userinfo_url",
312
- }
313
- mock_get.return_value.json.return_value = mock_response
314
- mock_get.return_value.raise_for_status.return_value = None
315
- credential_dict = {
316
- "client_id": "client_id",
317
- "client_secret": "client_secret",
318
- "redirect_uri": "redirect_uri",
319
- }
320
- scopes = ["scope1", "scope2"]
321
-
322
- scheme, credential = openid_url_to_scheme_credential(
323
- "openid_url", scopes, credential_dict
324
- )
325
-
326
- assert scheme.openIdConnectUrl == "openid_url"
327
-
328
-
329
- @patch("requests.get")
330
- def test_openid_url_to_scheme_credential_request_exception(mock_get):
331
- mock_get.side_effect = requests.exceptions.RequestException("Test Error")
332
- credential_dict = {"client_id": "client_id", "client_secret": "client_secret"}
333
-
334
- with pytest.raises(
335
- ValueError, match="Failed to fetch OpenID configuration from openid_url"
336
- ):
337
- openid_url_to_scheme_credential("openid_url", [], credential_dict)
338
-
339
-
340
- @patch("requests.get")
341
- def test_openid_url_to_scheme_credential_invalid_json(mock_get):
342
- mock_get.return_value.json.side_effect = ValueError("Invalid JSON")
343
- mock_get.return_value.raise_for_status.return_value = None
344
- credential_dict = {"client_id": "client_id", "client_secret": "client_secret"}
345
-
346
- with pytest.raises(
347
- ValueError,
348
- match=(
349
- "Invalid JSON response from OpenID configuration endpoint openid_url"
350
- ),
351
- ):
352
- openid_url_to_scheme_credential("openid_url", [], credential_dict)
353
-
354
-
355
- def test_credential_to_param_api_key_header():
356
- auth_scheme = APIKey(
357
- **{"type": "apiKey", "in": "header", "name": "X-API-Key"}
358
- )
359
- auth_credential = AuthCredential(
360
- auth_type=AuthCredentialTypes.API_KEY, api_key="test_key"
361
- )
362
-
363
- param, kwargs = credential_to_param(auth_scheme, auth_credential)
364
-
365
- assert param.original_name == "X-API-Key"
366
- assert param.param_location == "header"
367
- assert kwargs == {INTERNAL_AUTH_PREFIX + "X-API-Key": "test_key"}
368
-
369
-
370
- def test_credential_to_param_api_key_query():
371
- auth_scheme = APIKey(**{"type": "apiKey", "in": "query", "name": "api_key"})
372
- auth_credential = AuthCredential(
373
- auth_type=AuthCredentialTypes.API_KEY, api_key="test_key"
374
- )
375
-
376
- param, kwargs = credential_to_param(auth_scheme, auth_credential)
377
-
378
- assert param.original_name == "api_key"
379
- assert param.param_location == "query"
380
- assert kwargs == {INTERNAL_AUTH_PREFIX + "api_key": "test_key"}
381
-
382
-
383
- def test_credential_to_param_api_key_cookie():
384
- auth_scheme = APIKey(
385
- **{"type": "apiKey", "in": "cookie", "name": "session_id"}
386
- )
387
- auth_credential = AuthCredential(
388
- auth_type=AuthCredentialTypes.API_KEY, api_key="test_key"
389
- )
390
-
391
- param, kwargs = credential_to_param(auth_scheme, auth_credential)
392
-
393
- assert param.original_name == "session_id"
394
- assert param.param_location == "cookie"
395
- assert kwargs == {INTERNAL_AUTH_PREFIX + "session_id": "test_key"}
396
-
397
-
398
- def test_credential_to_param_http_bearer():
399
- auth_scheme = HTTPBearer(bearerFormat="JWT")
400
- auth_credential = AuthCredential(
401
- auth_type=AuthCredentialTypes.HTTP,
402
- http=HttpAuth(
403
- scheme="bearer", credentials=HttpCredentials(token="test_token")
404
- ),
405
- )
406
-
407
- param, kwargs = credential_to_param(auth_scheme, auth_credential)
408
-
409
- assert param.original_name == "Authorization"
410
- assert param.param_location == "header"
411
- assert kwargs == {INTERNAL_AUTH_PREFIX + "Authorization": "Bearer test_token"}
412
-
413
-
414
- def test_credential_to_param_http_basic_not_supported():
415
- auth_scheme = HTTPBase(scheme="basic")
416
- auth_credential = AuthCredential(
417
- auth_type=AuthCredentialTypes.HTTP,
418
- http=HttpAuth(
419
- scheme="basic",
420
- credentials=HttpCredentials(username="user", password="password"),
421
- ),
422
- )
423
-
424
- with pytest.raises(
425
- NotImplementedError, match="Basic Authentication is not supported."
426
- ):
427
- credential_to_param(auth_scheme, auth_credential)
428
-
429
-
430
- def test_credential_to_param_http_invalid_credentials_no_http():
431
- auth_scheme = HTTPBase(scheme="basic")
432
- auth_credential = AuthCredential(auth_type=AuthCredentialTypes.HTTP)
433
-
434
- with pytest.raises(ValueError, match="Invalid HTTP auth credentials"):
435
- credential_to_param(auth_scheme, auth_credential)
436
-
437
-
438
- def test_credential_to_param_oauth2():
439
- auth_scheme = OAuth2(flows={})
440
- auth_credential = AuthCredential(
441
- auth_type=AuthCredentialTypes.HTTP,
442
- http=HttpAuth(
443
- scheme="bearer", credentials=HttpCredentials(token="test_token")
444
- ),
445
- )
446
-
447
- param, kwargs = credential_to_param(auth_scheme, auth_credential)
448
-
449
- assert param.original_name == "Authorization"
450
- assert param.param_location == "header"
451
- assert kwargs == {INTERNAL_AUTH_PREFIX + "Authorization": "Bearer test_token"}
452
-
453
-
454
- def test_credential_to_param_openid_connect():
455
- auth_scheme = OpenIdConnect(openIdConnectUrl="openid_url")
456
- auth_credential = AuthCredential(
457
- auth_type=AuthCredentialTypes.HTTP,
458
- http=HttpAuth(
459
- scheme="bearer", credentials=HttpCredentials(token="test_token")
460
- ),
461
- )
462
-
463
- param, kwargs = credential_to_param(auth_scheme, auth_credential)
464
-
465
- assert param.original_name == "Authorization"
466
- assert param.param_location == "header"
467
- assert kwargs == {INTERNAL_AUTH_PREFIX + "Authorization": "Bearer test_token"}
468
-
469
-
470
- def test_credential_to_param_openid_no_credential():
471
- auth_scheme = OpenIdConnect(openIdConnectUrl="openid_url")
472
-
473
- param, kwargs = credential_to_param(auth_scheme, None)
474
-
475
- assert param == None
476
- assert kwargs == None
477
-
478
-
479
- def test_credential_to_param_oauth2_no_credential():
480
- auth_scheme = OAuth2(flows={})
481
-
482
- param, kwargs = credential_to_param(auth_scheme, None)
483
-
484
- assert param == None
485
- assert kwargs == None
486
-
487
-
488
- def test_dict_to_auth_scheme_api_key():
489
- data = {"type": "apiKey", "in": "header", "name": "X-API-Key"}
490
-
491
- scheme = dict_to_auth_scheme(data)
492
-
493
- assert isinstance(scheme, APIKey)
494
- assert scheme.type_ == AuthSchemeType.apiKey
495
- assert scheme.in_ == APIKeyIn.header
496
- assert scheme.name == "X-API-Key"
497
-
498
-
499
- def test_dict_to_auth_scheme_http_bearer():
500
- data = {"type": "http", "scheme": "bearer", "bearerFormat": "JWT"}
501
-
502
- scheme = dict_to_auth_scheme(data)
503
-
504
- assert isinstance(scheme, HTTPBearer)
505
- assert scheme.scheme == "bearer"
506
- assert scheme.bearerFormat == "JWT"
507
-
508
-
509
- def test_dict_to_auth_scheme_http_base():
510
- data = {"type": "http", "scheme": "basic"}
511
-
512
- scheme = dict_to_auth_scheme(data)
513
-
514
- assert isinstance(scheme, HTTPBase)
515
- assert scheme.scheme == "basic"
516
-
517
-
518
- def test_dict_to_auth_scheme_oauth2():
519
- data = {
520
- "type": "oauth2",
521
- "flows": {
522
- "authorizationCode": {
523
- "authorizationUrl": "https://example.com/auth",
524
- "tokenUrl": "https://example.com/token",
525
- }
526
- },
527
- }
528
-
529
- scheme = dict_to_auth_scheme(data)
530
-
531
- assert isinstance(scheme, OAuth2)
532
- assert hasattr(scheme.flows, "authorizationCode")
533
-
534
-
535
- def test_dict_to_auth_scheme_openid_connect():
536
- data = {
537
- "type": "openIdConnect",
538
- "openIdConnectUrl": (
539
- "https://example.com/.well-known/openid-configuration"
540
- ),
541
- }
542
-
543
- scheme = dict_to_auth_scheme(data)
544
-
545
- assert isinstance(scheme, OpenIdConnect)
546
- assert (
547
- scheme.openIdConnectUrl
548
- == "https://example.com/.well-known/openid-configuration"
549
- )
550
-
551
-
552
- def test_dict_to_auth_scheme_missing_type():
553
- data = {"in": "header", "name": "X-API-Key"}
554
- with pytest.raises(
555
- ValueError, match="Missing 'type' field in security scheme dictionary."
556
- ):
557
- dict_to_auth_scheme(data)
558
-
559
-
560
- def test_dict_to_auth_scheme_invalid_type():
561
- data = {"type": "invalid", "in": "header", "name": "X-API-Key"}
562
- with pytest.raises(ValueError, match="Invalid security scheme type: invalid"):
563
- dict_to_auth_scheme(data)
564
-
565
-
566
- def test_dict_to_auth_scheme_invalid_data():
567
- data = {"type": "apiKey", "in": "header"} # Missing 'name'
568
- with pytest.raises(ValueError, match="Invalid security scheme data"):
569
- dict_to_auth_scheme(data)
570
-
571
-
572
- if __name__ == "__main__":
573
- pytest.main([__file__])