Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
| 1 | """Test SDK functionality.""" |
| 2 | |
| 3 | import os |
| 4 | import typing as t |
| 5 | from unittest.mock import patch |
| 6 | |
| 7 | import composio_client |
| 8 | import httpx |
| 9 | import pytest |
| 10 | from composio_client import AutoAuth, ProjectAuth, Scope, UserAuth |
| 11 | from composio_client import ComposioError as ClientComposioError |
| 12 | |
| 13 | from composio import Composio, exceptions |
| 14 | from composio.client import HttpClient |
| 15 | from composio.core.provider._openai import OpenAIProvider |
| 16 | |
| 17 | |
| 18 | class TestComposioSDK: |
| 19 | """Test cases for Composio SDK.""" |
| 20 | |
| 21 | @pytest.mark.parametrize("config", [{}, {"api_key": None}, {"api_key": ""}]) |
| 22 | def test_sdk_requires_api_key(self, config): |
| 23 | """Missing credentials must explain SDK setup and the separate CLI install.""" |
| 24 | with patch.dict(os.environ, {}, clear=True): |
| 25 | with pytest.raises(exceptions.ApiKeyNotProvidedError) as exc_info: |
| 26 | Composio(**config) |
| 27 | |
| 28 | message = str(exc_info.value) |
| 29 | assert "COMPOSIO_API_KEY" in message |
| 30 | assert "Composio(api_key=" in message |
| 31 | assert "Optional: `composio login`" in message |
| 32 | assert "requires installing the CLI separately" in message |
| 33 | assert "https://docs.composio.dev/docs/cli#install" in message |
| 34 | assert "does not set `COMPOSIO_API_KEY`" in message |
| 35 | |
| 36 | @pytest.mark.parametrize("api_key", [None, ""]) |
| 37 | def test_sdk_explicit_missing_api_key_does_not_fall_back_to_env(self, api_key): |
| 38 | """Only an omitted ``api_key`` falls back to COMPOSIO_API_KEY.""" |
| 39 | with patch.dict(os.environ, {"COMPOSIO_API_KEY": "test-key"}): |
| 40 | with pytest.raises(exceptions.ApiKeyNotProvidedError): |
| 41 | Composio(api_key=api_key) |
| 42 | |
| 43 | def test_sdk_forwards_user_and_org_api_keys_to_client(self): |
| 44 | """User and org keys reach the API client, which selects them per operation.""" |
| 45 | sdk = Composio( |
| 46 | api_key="ak_test", user_api_key="uak_test", org_api_key="oak_test" |
| 47 | ) |
| 48 | assert sdk.client.auth_mode == "auto" |
| 49 | assert sdk.client.sdk_auth == AutoAuth( |
| 50 | project="ak_test", user="uak_test", organization="oak_test" |
| 51 | ) |
| 52 | |
| 53 | def test_sdk_reads_user_and_org_api_keys_from_env(self): |
| 54 | """The SDK resolves COMPOSIO_USER_API_KEY / COMPOSIO_ORG_API_KEY; the |
| 55 | client reads no environment.""" |
| 56 | with patch.dict( |
| 57 | os.environ, |
| 58 | { |
| 59 | "COMPOSIO_API_KEY": "ak_env", |
| 60 | "COMPOSIO_USER_API_KEY": "uak_env", |
| 61 | "COMPOSIO_ORG_API_KEY": "oak_env", |
| 62 | }, |
| 63 | ): |
| 64 | sdk = Composio() |
| 65 | assert sdk.client.sdk_auth == AutoAuth( |
| 66 | project="ak_env", user="uak_env", organization="oak_env" |
| 67 | ) |
| 68 | |
| 69 | def test_sdk_config_types(self): |
| 70 | """Test SDK configuration types.""" |
| 71 | from composio.sdk import SDKConfig |
| 72 | |
| 73 | # Test that SDKConfig is a TypedDict |
| 74 | assert hasattr(SDKConfig, "__annotations__") |
| 75 | |
| 76 | # Test that all expected fields are present |
| 77 | expected_fields = { |
| 78 | "environment", |
| 79 | "api_key", |
| 80 | "disable_api_key", |
| 81 | "user_api_key", |
| 82 | "org_api_key", |
| 83 | "org_id", |
| 84 | "project_id", |
| 85 | "base_url", |
| 86 | "timeout", |
| 87 | "max_retries", |
| 88 | "allow_tracking", |
| 89 | "file_download_dir", |
| 90 | "toolkit_versions", |
| 91 | "dangerously_allow_auto_upload_download_files", |
| 92 | "sensitive_file_upload_protection", |
| 93 | "file_upload_path_deny_segments", |
| 94 | "file_upload_dirs", |
| 95 | "http_client", |
| 96 | "logger", |
| 97 | "logging_level", |
| 98 | } |
| 99 | assert set(SDKConfig.__annotations__.keys()) == expected_fields |
| 100 | |
| 101 | def test_sdk_mounts_webhooks_and_logs(self): |
| 102 | from composio.core.models import Logs, Webhooks |
| 103 | |
| 104 | composio = Composio(api_key="test-key") |
| 105 | |
| 106 | assert isinstance(composio.webhooks, Webhooks) |
| 107 | assert isinstance(composio.logs, Logs) |
| 108 | assert composio.webhooks._client is composio._client |
| 109 | assert composio.logs._client is composio._client |
| 110 | |
| 111 | def test_sdk_mounts_keyring_and_custom_toolkits(self): |
| 112 | from composio.core.models import ( |
| 113 | CustomToolkits, |
| 114 | ExperimentalCustomToolkits, |
| 115 | Keyring, |
| 116 | ) |
| 117 | |
| 118 | composio = Composio(api_key="test-key") |
| 119 | |
| 120 | assert isinstance(composio.keyring, Keyring) |
| 121 | assert composio.keyring._client is composio._client |
| 122 | assert isinstance(composio.custom_toolkits, CustomToolkits) |
| 123 | assert composio.custom_toolkits._client is composio._client |
| 124 | # Deprecated paths keep working and resolve to the same object. |
| 125 | assert composio.experimental.custom_toolkits is composio.custom_toolkits |
| 126 | assert ExperimentalCustomToolkits is CustomToolkits |
| 127 | |
| 128 | def test_sdk_has_required_attributes(self): |
| 129 | """Test that SDK has required attributes after initialization.""" |
| 130 | with patch.dict(os.environ, {"COMPOSIO_API_KEY": "test-key"}): |
| 131 | with patch("composio.core.models.Tools"): |
| 132 | with patch("composio.core.models.Toolkits"): |
| 133 | with patch("composio.core.models.Triggers"): |
| 134 | with patch("composio.core.models.AuthConfigs"): |
| 135 | with patch("composio.core.models.ConnectedAccounts"): |
| 136 | sdk = Composio() |
| 137 | |
| 138 | # Check that all required attributes are present |
| 139 | assert hasattr(sdk, "tools") |
| 140 | assert hasattr(sdk, "toolkits") |
| 141 | assert hasattr(sdk, "triggers") |
| 142 | assert hasattr(sdk, "auth_configs") |
| 143 | assert hasattr(sdk, "connected_accounts") |
| 144 | assert hasattr(sdk, "provider") |
| 145 | assert hasattr(sdk, "client") |
| 146 | |
| 147 | def test_default_provider_is_isolated_per_instance(self): |
| 148 | """Regression test for #4369. |
| 149 | |
| 150 | The default provider used to be a module-level singleton. Because every |
| 151 | ``Tools`` instance rebinds ``provider.execute_tool`` to itself, the last |
| 152 | constructed ``Composio()`` silently took over tool execution for every |
| 153 | other instance, routing calls through the wrong API key. |
| 154 | """ |
| 155 | sdk_a = Composio(api_key="key-a") |
| 156 | sdk_b = Composio(api_key="key-b") |
| 157 | |
| 158 | assert isinstance(sdk_a.provider, OpenAIProvider) |
| 159 | assert isinstance(sdk_b.provider, OpenAIProvider) |
| 160 | assert sdk_a.provider is not sdk_b.provider |
| 161 | |
| 162 | # execute_tool is a functools.partial over Tools.execute; each provider |
| 163 | # must stay bound to the Tools of the instance that created it. |
| 164 | assert sdk_a.provider.execute_tool.func.__self__ is sdk_a.tools |
| 165 | assert sdk_b.provider.execute_tool.func.__self__ is sdk_b.tools |
| 166 | |
| 167 | def test_explicit_provider_is_used_unchanged(self): |
| 168 | """An explicitly passed provider instance is used as-is.""" |
| 169 | provider = OpenAIProvider() |
| 170 | sdk = Composio(provider=provider, api_key="key-a") |
| 171 | assert sdk.provider is provider |
| 172 | |
| 173 | def test_sdk_env_var_integration(self): |
| 174 | """Test that SDK properly integrates with environment variables for toolkit versions.""" |
| 175 | with patch.dict( |
| 176 | os.environ, |
| 177 | { |
| 178 | "COMPOSIO_API_KEY": "test-key", |
| 179 | "COMPOSIO_TOOLKIT_VERSION_GITHUB": "v1.0.0", |
| 180 | "COMPOSIO_TOOLKIT_VERSION_SLACK": "v2.0.0", |
| 181 | }, |
| 182 | ): |
| 183 | with patch("composio.sdk.Tools") as mock_tools_class: |
| 184 | with patch("composio.sdk.Toolkits"): |
| 185 | with patch("composio.sdk.Triggers"): |
| 186 | with patch("composio.sdk.AuthConfigs"): |
| 187 | with patch("composio.sdk.ConnectedAccounts"): |
| 188 | # Create SDK instance without explicit toolkit versions |
| 189 | Composio() |
| 190 | |
| 191 | # Verify that Tools was initialized with processed versions |
| 192 | mock_tools_class.assert_called_once() |
| 193 | call_args = mock_tools_class.call_args |
| 194 | |
| 195 | # The toolkit_versions should be a dict from env vars |
| 196 | toolkit_versions = call_args.kwargs.get( |
| 197 | "toolkit_versions" |
| 198 | ) |
| 199 | expected = {"github": "v1.0.0", "slack": "v2.0.0"} |
| 200 | assert toolkit_versions == expected |
| 201 | |
| 202 | def test_sdk_user_override_env_vars(self): |
| 203 | """Test that user-provided toolkit versions override environment variables.""" |
| 204 | with patch.dict( |
| 205 | os.environ, |
| 206 | { |
| 207 | "COMPOSIO_API_KEY": "test-key", |
| 208 | "COMPOSIO_TOOLKIT_VERSION_GITHUB": "env_version", |
| 209 | "COMPOSIO_TOOLKIT_VERSION_SLACK": "env_slack", |
| 210 | }, |
| 211 | ): |
| 212 | with patch("composio.sdk.Tools") as mock_tools_class: |
| 213 | with patch("composio.sdk.Toolkits"): |
| 214 | with patch("composio.sdk.Triggers"): |
| 215 | with patch("composio.sdk.AuthConfigs"): |
| 216 | with patch("composio.sdk.ConnectedAccounts"): |
| 217 | # User provides override |
| 218 | user_versions = { |
| 219 | "github": "user_override", |
| 220 | "jira": "user_jira", |
| 221 | } |
| 222 | Composio(toolkit_versions=user_versions) |
| 223 | |
| 224 | # Verify Tools was initialized with merged versions |
| 225 | mock_tools_class.assert_called_once() |
| 226 | call_args = mock_tools_class.call_args |
| 227 | |
| 228 | toolkit_versions = call_args.kwargs.get( |
| 229 | "toolkit_versions" |
| 230 | ) |
| 231 | expected = { |
| 232 | "github": "user_override", # User override |
| 233 | "slack": "env_slack", # From env |
| 234 | "jira": "user_jira", # User provided |
| 235 | } |
| 236 | assert toolkit_versions == expected |
| 237 | |
| 238 | |
| 239 | MCP_URL = "https://backend.composio.dev/api/v3/tool_router/session/session_123/mcp" |
| 240 | |
| 241 | |
| 242 | def _session_transport() -> t.Tuple[httpx.Client, t.List[httpx.Request]]: |
| 243 | requests: t.List[httpx.Request] = [] |
| 244 | |
| 245 | def handler(request: httpx.Request) -> httpx.Response: |
| 246 | requests.append(request) |
| 247 | return httpx.Response( |
| 248 | 200, |
| 249 | json={ |
| 250 | "session_id": "session_123", |
| 251 | "mcp": {"type": "http", "url": MCP_URL}, |
| 252 | "config": { |
| 253 | "user_id": "user_123", |
| 254 | "execute": {}, |
| 255 | "search": {}, |
| 256 | "preload": {"tools": []}, |
| 257 | }, |
| 258 | "config_version": 3, |
| 259 | "warnings": [], |
| 260 | }, |
| 261 | ) |
| 262 | |
| 263 | return httpx.Client(transport=httpx.MockTransport(handler)), requests |
| 264 | |
| 265 | |
| 266 | class TestUserOnlyInitialization: |
| 267 | """``disable_api_key`` turns the project key off and keeps one credential.""" |
| 268 | |
| 269 | def test_user_key_session_carries_only_the_user_key(self): |
| 270 | http_client, requests = _session_transport() |
| 271 | with patch.dict(os.environ, {"COMPOSIO_API_KEY": "ak_foreign_env_key"}): |
| 272 | sdk = Composio( |
| 273 | disable_api_key=True, |
| 274 | user_api_key="uak_user_key", |
| 275 | base_url="https://backend.composio.dev", |
| 276 | http_client=http_client, |
| 277 | ) |
| 278 | assert sdk.client.auth_mode == "user" |
| 279 | session = sdk.sessions.create(user_id="user_123", mcp=True) |
| 280 | |
| 281 | assert "x-api-key" not in requests[0].headers |
| 282 | assert requests[0].headers["x-user-api-key"] == "uak_user_key" |
| 283 | assert "x-org-id" not in requests[0].headers |
| 284 | assert "x-project-id" not in requests[0].headers |
| 285 | assert session.mcp.headers == {"x-user-api-key": "uak_user_key"} |
| 286 | |
| 287 | def test_user_key_session_carries_the_org_and_project_scope(self): |
| 288 | http_client, requests = _session_transport() |
| 289 | with patch.dict(os.environ, {"COMPOSIO_API_KEY": "ak_foreign_env_key"}): |
| 290 | sdk = Composio( |
| 291 | disable_api_key=True, |
| 292 | user_api_key="uak_user_key", |
| 293 | org_id="org_nano_abc", |
| 294 | project_id="proj_nano_xyz", |
| 295 | base_url="https://backend.composio.dev", |
| 296 | http_client=http_client, |
| 297 | ) |
| 298 | session = sdk.sessions.create(user_id="user_123", mcp=True) |
| 299 | |
| 300 | assert "x-api-key" not in requests[0].headers |
| 301 | assert requests[0].headers["x-user-api-key"] == "uak_user_key" |
| 302 | assert requests[0].headers["x-org-id"] == "org_nano_abc" |
| 303 | assert requests[0].headers["x-project-id"] == "proj_nano_xyz" |
| 304 | assert session.mcp.headers == { |
| 305 | "x-user-api-key": "uak_user_key", |
| 306 | "x-org-id": "org_nano_abc", |
| 307 | "x-project-id": "proj_nano_xyz", |
| 308 | } |
| 309 | |
| 310 | def test_user_key_is_read_from_the_environment_when_disabled(self): |
| 311 | with patch.dict( |
| 312 | os.environ, |
| 313 | { |
| 314 | "COMPOSIO_API_KEY": "ak_foreign_env_key", |
| 315 | "COMPOSIO_USER_API_KEY": "uak_env", |
| 316 | }, |
| 317 | ): |
| 318 | sdk = Composio(disable_api_key=True) |
| 319 | |
| 320 | assert sdk.client.sdk_auth == UserAuth("uak_env") |
| 321 | |
| 322 | def test_disable_without_a_user_key_raises(self): |
| 323 | with patch.dict( |
| 324 | os.environ, {"COMPOSIO_API_KEY": "ak_foreign_env_key"}, clear=True |
| 325 | ): |
| 326 | with pytest.raises(exceptions.UserApiKeyNotProvidedError): |
| 327 | Composio(disable_api_key=True) |
| 328 | |
| 329 | def test_user_auth_beside_a_raw_project_key_header_raises(self): |
| 330 | """The client owns the credential headers while ``auth`` holds a key.""" |
| 331 | http_client, requests = _session_transport() |
| 332 | with pytest.raises(ClientComposioError) as excinfo: |
| 333 | HttpClient( |
| 334 | provider="test", |
| 335 | auth=UserAuth("uak_user_key"), |
| 336 | default_headers={"X-Api-Key": "ak_raw_header"}, |
| 337 | http_client=http_client, |
| 338 | ) |
| 339 | |
| 340 | assert "`x-api-key`" in str(excinfo.value) |
| 341 | assert "ak_raw_header" not in str(excinfo.value) |
| 342 | assert requests == [] |
| 343 | |
| 344 | def test_client_clones_cannot_reintroduce_a_raw_project_key_header(self): |
| 345 | http_client, requests = _session_transport() |
| 346 | sdk = Composio( |
| 347 | disable_api_key=True, |
| 348 | user_api_key="uak_user_key", |
| 349 | base_url="https://backend.composio.dev", |
| 350 | http_client=http_client, |
| 351 | ) |
| 352 | |
| 353 | with pytest.raises(ClientComposioError): |
| 354 | sdk.client.with_options(default_headers={"x-api-key": "ak_raw_header"}) |
| 355 | sdk.sessions.create(user_id="user_123") |
| 356 | |
| 357 | assert len(requests) == 1 |
| 358 | assert "x-api-key" not in requests[0].headers |
| 359 | assert requests[0].headers["x-user-api-key"] == "uak_user_key" |
| 360 | |
| 361 | def test_disable_beside_an_explicit_project_key_raises(self): |
| 362 | with pytest.raises(exceptions.InvalidParams): |
| 363 | Composio(disable_api_key=True, api_key="ak_explicit", user_api_key="uak") |
| 364 | |
| 365 | def test_half_scope_raises(self): |
| 366 | with pytest.raises(exceptions.InvalidParams): |
| 367 | Composio(api_key="ak_test", org_id="org_nano_abc") |
| 368 | with pytest.raises(exceptions.InvalidParams): |
| 369 | Composio(api_key="ak_test", project_id="proj_nano_xyz") |
| 370 | |
| 371 | def test_empty_scope_id_counts_as_unset(self): |
| 372 | with pytest.raises(exceptions.InvalidParams): |
| 373 | Composio(api_key="ak_test", org_id="org_nano_abc", project_id="") |
| 374 | with pytest.raises(exceptions.InvalidParams): |
| 375 | Composio(api_key="ak_test", org_id="", project_id="proj_nano_xyz") |
| 376 | |
| 377 | sdk = Composio(api_key="ak_test", org_id="", project_id="") |
| 378 | assert sdk.client.scope is None |
| 379 | assert "x-org-id" not in sdk.client.default_headers |
| 380 | assert "x-project-id" not in sdk.client.default_headers |
| 381 | |
| 382 | def test_omitted_api_key_keeps_the_environment_fallback(self): |
| 383 | with patch.dict(os.environ, {"COMPOSIO_API_KEY": "ak_env"}): |
| 384 | sdk = Composio(user_api_key="uak") |
| 385 | assert sdk.client.sdk_auth == AutoAuth(project="ak_env", user="uak") |
| 386 | |
| 387 | def test_client_clones_stay_user_only_when_the_environment_changes(self): |
| 388 | with patch.dict(os.environ, {"COMPOSIO_API_KEY": "ak_foreign_env_key"}): |
| 389 | sdk = Composio(disable_api_key=True, user_api_key="uak_user_key") |
| 390 | clone = sdk.client.with_options(max_retries=0) |
| 391 | |
| 392 | assert sdk.client.sdk_auth == UserAuth("uak_user_key") |
| 393 | assert clone.sdk_auth == UserAuth("uak_user_key") |
| 394 | assert clone.auth_mode == "user" |
| 395 | assert clone.max_retries == 0 |
| 396 | |
| 397 | def test_generated_client_reads_no_environment(self): |
| 398 | """The SDK, not the generated client, resolves keys from the environment.""" |
| 399 | with patch.dict(os.environ, {"COMPOSIO_API_KEY": "ak_env"}): |
| 400 | client = composio_client.Composio(auth=UserAuth("uak")) |
| 401 | |
| 402 | assert client.auth_mode == "user" |
| 403 | |
| 404 | |
| 405 | def _recording_transport() -> t.Tuple[httpx.Client, t.List[httpx.Request]]: |
| 406 | requests: t.List[httpx.Request] = [] |
| 407 | |
| 408 | def handler(request: httpx.Request) -> httpx.Response: |
| 409 | requests.append(request) |
| 410 | return httpx.Response(200, json={}) |
| 411 | |
| 412 | return httpx.Client(transport=httpx.MockTransport(handler)), requests |
| 413 | |
| 414 | |
| 415 | #: An operation that accepts the project key, one that accepts only a user key |
| 416 | #: and one that accepts only an organization key. |
| 417 | PROJECT_OP = "/api/v3.1/tools" |
| 418 | USER_OP = "/api/v3.1/org/list" |
| 419 | ORG_OP = "/api/v3.1/org/owner/project/list" |
| 420 | |
| 421 | CREDENTIAL_HEADERS = ("x-api-key", "x-user-api-key", "x-org-api-key") |
| 422 | |
| 423 | |
| 424 | def _credentials_sent(sdk: Composio, path: str, requests: t.List[httpx.Request]): |
| 425 | sdk.client.get(path, cast_to=dict) |
| 426 | return { |
| 427 | name: requests[-1].headers[name] |
| 428 | for name in CREDENTIAL_HEADERS |
| 429 | if name in requests[-1].headers |
| 430 | } |
| 431 | |
| 432 | |
| 433 | class TestAuthAndScopeMapping: |
| 434 | """What the SDK resolves becomes the client's ``auth`` and ``scope``.""" |
| 435 | |
| 436 | @pytest.fixture(autouse=True) |
| 437 | def _isolated_environment(self): |
| 438 | with patch.dict(os.environ, {}, clear=True): |
| 439 | yield |
| 440 | |
| 441 | @pytest.mark.parametrize( |
| 442 | ("config", "expected"), |
| 443 | [ |
| 444 | ({"api_key": "ak"}, ProjectAuth("ak")), |
| 445 | ( |
| 446 | {"api_key": "ak", "user_api_key": "uak"}, |
| 447 | AutoAuth(project="ak", user="uak"), |
| 448 | ), |
| 449 | ( |
| 450 | {"api_key": "ak", "org_api_key": "oak"}, |
| 451 | AutoAuth(project="ak", organization="oak"), |
| 452 | ), |
| 453 | ( |
| 454 | {"api_key": "ak", "user_api_key": "uak", "org_api_key": "oak"}, |
| 455 | AutoAuth(project="ak", user="uak", organization="oak"), |
| 456 | ), |
| 457 | ({"disable_api_key": True, "user_api_key": "uak"}, UserAuth("uak")), |
| 458 | ( |
| 459 | {"disable_api_key": True, "user_api_key": "uak", "org_api_key": "oak"}, |
| 460 | UserAuth("uak"), |
| 461 | ), |
| 462 | # Empty keys count as unset, as the environment fallback always has. |
| 463 | ( |
| 464 | {"api_key": "ak", "user_api_key": "", "org_api_key": ""}, |
| 465 | ProjectAuth("ak"), |
| 466 | ), |
| 467 | ], |
| 468 | ) |
| 469 | def test_keys_map_to_auth(self, config, expected): |
| 470 | sdk = Composio(**config) |
| 471 | |
| 472 | assert sdk.client.sdk_auth == expected |
| 473 | assert sdk.client.auth_mode == expected.mode |
| 474 | |
| 475 | def test_environment_keys_map_to_auth(self): |
| 476 | with patch.dict( |
| 477 | os.environ, |
| 478 | {"COMPOSIO_API_KEY": "ak_env", "COMPOSIO_ORG_API_KEY": "oak_env"}, |
| 479 | ): |
| 480 | sdk = Composio() |
| 481 | |
| 482 | assert sdk.client.sdk_auth == AutoAuth(project="ak_env", organization="oak_env") |
| 483 | |
| 484 | def test_blank_environment_keys_count_as_unset(self): |
| 485 | with patch.dict( |
| 486 | os.environ, |
| 487 | { |
| 488 | "COMPOSIO_API_KEY": "ak_env", |
| 489 | "COMPOSIO_USER_API_KEY": " ", |
| 490 | "COMPOSIO_ORG_API_KEY": "", |
| 491 | }, |
| 492 | ): |
| 493 | sdk = Composio() |
| 494 | |
| 495 | assert sdk.client.sdk_auth == ProjectAuth("ak_env") |
| 496 | |
| 497 | def test_explicit_keys_win_over_the_environment(self): |
| 498 | with patch.dict( |
| 499 | os.environ, |
| 500 | {"COMPOSIO_USER_API_KEY": "uak_env", "COMPOSIO_ORG_API_KEY": "oak_env"}, |
| 501 | ): |
| 502 | sdk = Composio(api_key="ak", user_api_key="uak", org_api_key="oak") |
| 503 | |
| 504 | assert sdk.client.sdk_auth == AutoAuth( |
| 505 | project="ak", user="uak", organization="oak" |
| 506 | ) |
| 507 | |
| 508 | def test_project_key_alone_is_sent_on_every_operation(self): |
| 509 | http_client, requests = _recording_transport() |
| 510 | sdk = Composio(api_key="ak", http_client=http_client) |
| 511 | |
| 512 | for path in (PROJECT_OP, USER_OP, ORG_OP): |
| 513 | assert _credentials_sent(sdk, path, requests) == {"x-api-key": "ak"} |
| 514 | |
| 515 | def test_auto_auth_sends_exactly_one_credential_per_operation(self): |
| 516 | http_client, requests = _recording_transport() |
| 517 | sdk = Composio( |
| 518 | api_key="ak", user_api_key="uak", org_api_key="oak", http_client=http_client |
| 519 | ) |
| 520 | |
| 521 | assert _credentials_sent(sdk, PROJECT_OP, requests) == {"x-api-key": "ak"} |
| 522 | assert _credentials_sent(sdk, USER_OP, requests) == {"x-user-api-key": "uak"} |
| 523 | assert _credentials_sent(sdk, ORG_OP, requests) == {"x-org-api-key": "oak"} |
| 524 | |
| 525 | def test_user_only_sends_the_user_key_on_every_operation(self): |
| 526 | http_client, requests = _recording_transport() |
| 527 | sdk = Composio( |
| 528 | disable_api_key=True, |
| 529 | user_api_key="uak", |
| 530 | org_api_key="oak", |
| 531 | http_client=http_client, |
| 532 | ) |
| 533 | |
| 534 | for path in (PROJECT_OP, USER_OP, ORG_OP): |
| 535 | assert _credentials_sent(sdk, path, requests) == {"x-user-api-key": "uak"} |
| 536 | |
| 537 | def test_scope_ids_become_the_client_scope(self): |
| 538 | http_client, requests = _recording_transport() |
| 539 | sdk = Composio( |
| 540 | api_key="ak", |
| 541 | org_id="org_nano_abc", |
| 542 | project_id="proj_nano_xyz", |
| 543 | http_client=http_client, |
| 544 | ) |
| 545 | |
| 546 | assert sdk.client.scope == Scope( |
| 547 | organization_id="org_nano_abc", project_id="proj_nano_xyz" |
| 548 | ) |
| 549 | assert sdk.client.default_headers == {} |
| 550 | sdk.client.get(PROJECT_OP, cast_to=dict) |
| 551 | assert requests[0].headers["x-org-id"] == "org_nano_abc" |
| 552 | assert requests[0].headers["x-project-id"] == "proj_nano_xyz" |
| 553 | |
| 554 | def test_scope_survives_a_clone(self): |
| 555 | sdk = Composio(api_key="ak", org_id="org_nano_abc", project_id="proj_nano_xyz") |
| 556 | |
| 557 | clone = sdk.client.with_options(max_retries=0) |
| 558 | |
| 559 | assert clone.scope == sdk.client.scope |
| 560 | assert clone.sdk_auth == ProjectAuth("ak") |
| 561 | |
| 562 | def test_no_scope_leaves_the_client_unscoped(self): |
| 563 | http_client, requests = _recording_transport() |
| 564 | sdk = Composio(api_key="ak", http_client=http_client) |
| 565 | |
| 566 | assert sdk.client.scope is None |
| 567 | sdk.client.get(PROJECT_OP, cast_to=dict) |
| 568 | assert "x-org-id" not in requests[0].headers |
| 569 | assert "x-project-id" not in requests[0].headers |
| 570 | |
| 571 | def test_custom_headers_from_the_environment_are_still_sent(self): |
| 572 | http_client, requests = _recording_transport() |
| 573 | with patch.dict( |
| 574 | os.environ, {"COMPOSIO_CUSTOM_HEADERS": "x-trace: abc\nx-team: core "} |
| 575 | ): |
| 576 | sdk = Composio(api_key="ak", http_client=http_client) |
| 577 | |
| 578 | sdk.client.get(PROJECT_OP, cast_to=dict) |
| 579 | assert requests[0].headers["x-trace"] == "abc" |
| 580 | assert requests[0].headers["x-team"] == "core" |
| 581 | |
| 582 | def test_every_api_request_carries_a_request_id(self): |
| 583 | http_client, requests = _recording_transport() |
| 584 | sdk = Composio(api_key="ak", http_client=http_client) |
| 585 | |
| 586 | sdk.client.get(PROJECT_OP, cast_to=dict) |
| 587 | sdk.client.get(PROJECT_OP, cast_to=dict) |
| 588 | sdk.client.get("https://storage.invalid/file", cast_to=dict) |
| 589 | |
| 590 | first, second, foreign = (r.headers.get("x-request-id") for r in requests) |
| 591 | assert first and second and first != second |
| 592 | assert foreign is None |