|
2 | 2 |
|
3 | 3 | from unittest.mock import patch, MagicMock |
4 | 4 |
|
| 5 | +from mindsdb.integrations.handlers.rest_api_handler import ( |
| 6 | + connection_args as exported_connection_args, |
| 7 | +) |
| 8 | +from mindsdb.integrations.handlers.rest_api_handler.connection_args import connection_args |
| 9 | +from mindsdb.integrations.handlers.rest_api_handler.oauth_connection_args import ( |
| 10 | + oauth_connection_args, |
| 11 | +) |
| 12 | +from mindsdb.integrations.handlers.rest_api_handler.rest_connection_args import ( |
| 13 | + rest_connection_args, |
| 14 | +) |
5 | 15 | from mindsdb.integrations.handlers.rest_api_handler.rest_api_handler import RestApiHandler |
| 16 | +from mindsdb.integrations.libs.const import HANDLER_CONNECTION_ARG_TYPE as ARG_TYPE |
6 | 17 | from mindsdb.integrations.libs.passthrough import PassthroughProtocol |
7 | 18 | from mindsdb.integrations.libs.passthrough_types import PassthroughRequest, PassthroughResponse |
8 | 19 | from mindsdb.integrations.libs.response import ( |
@@ -163,3 +174,90 @@ def test_test_passthrough_with_no_network(self): |
163 | 174 | assert isinstance(result, dict) |
164 | 175 | assert result["ok"] is False |
165 | 176 | assert result["error_code"] in ("network", "unknown") |
| 177 | + |
| 178 | + |
| 179 | +class TestConnectionArgsSchema: |
| 180 | + """The exported connection_args is the union of REST + auth modules.""" |
| 181 | + |
| 182 | + def test_rest_module_only_holds_passthrough_fields(self): |
| 183 | + assert set(rest_connection_args.keys()) == { |
| 184 | + "base_url", |
| 185 | + "default_headers", |
| 186 | + "allowed_hosts", |
| 187 | + "test_path", |
| 188 | + } |
| 189 | + |
| 190 | + def test_auth_module_only_holds_auth_fields(self): |
| 191 | + assert set(oauth_connection_args.keys()) == { |
| 192 | + "auth_type", |
| 193 | + "bearer_token", |
| 194 | + "token_url", |
| 195 | + "client_id", |
| 196 | + "client_secret", |
| 197 | + "scope", |
| 198 | + "audience", |
| 199 | + "token_auth_method", |
| 200 | + } |
| 201 | + |
| 202 | + def test_bearer_token_lives_in_auth_module(self): |
| 203 | + # bearer_token is an auth strategy, not a REST/passthrough setting. |
| 204 | + assert "bearer_token" not in rest_connection_args |
| 205 | + assert "bearer_token" in oauth_connection_args |
| 206 | + |
| 207 | + def test_aggregated_connection_args_includes_rest_fields(self): |
| 208 | + for key in ("base_url", "default_headers", "allowed_hosts", "test_path"): |
| 209 | + assert key in connection_args |
| 210 | + |
| 211 | + def test_aggregated_connection_args_includes_bearer_token(self): |
| 212 | + assert "bearer_token" in connection_args |
| 213 | + |
| 214 | + def test_aggregated_connection_args_includes_oauth_fields(self): |
| 215 | + for key in ( |
| 216 | + "auth_type", |
| 217 | + "token_url", |
| 218 | + "client_id", |
| 219 | + "client_secret", |
| 220 | + "scope", |
| 221 | + "audience", |
| 222 | + "token_auth_method", |
| 223 | + ): |
| 224 | + assert key in connection_args |
| 225 | + |
| 226 | + def test_client_secret_marked_secret_and_pwd(self): |
| 227 | + spec = connection_args["client_secret"] |
| 228 | + assert spec["type"] == ARG_TYPE.PWD |
| 229 | + assert spec.get("secret") is True |
| 230 | + |
| 231 | + def test_bearer_token_marked_secret_and_pwd(self): |
| 232 | + spec = connection_args["bearer_token"] |
| 233 | + assert spec["type"] == ARG_TYPE.PWD |
| 234 | + assert spec.get("secret") is True |
| 235 | + |
| 236 | + def test_package_exports_aggregated_args(self): |
| 237 | + # The handler package re-exports connection_args; make sure the |
| 238 | + # aggregator and the package-level export are the same object. |
| 239 | + assert exported_connection_args is connection_args |
| 240 | + |
| 241 | + |
| 242 | +class TestBackwardCompatibleBearerInit: |
| 243 | + """Existing bearer-only configs must still initialize and validate.""" |
| 244 | + |
| 245 | + def test_legacy_config_initializes(self): |
| 246 | + handler = _make_handler( |
| 247 | + { |
| 248 | + "base_url": "https://api.example.com", |
| 249 | + "bearer_token": "legacy-token", |
| 250 | + } |
| 251 | + ) |
| 252 | + assert handler.connection_data["base_url"] == "https://api.example.com" |
| 253 | + assert handler.connection_data["bearer_token"] == "legacy-token" |
| 254 | + |
| 255 | + def test_legacy_config_check_connection_succeeds(self): |
| 256 | + handler = _make_handler( |
| 257 | + { |
| 258 | + "base_url": "https://api.example.com", |
| 259 | + "bearer_token": "legacy-token", |
| 260 | + } |
| 261 | + ) |
| 262 | + response = handler.check_connection() |
| 263 | + assert response.success is True |
0 commit comments