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 | """Tests for ``composio.experimental.custom_toolkits``.""" |
| 2 | |
| 3 | import warnings |
| 4 | from unittest.mock import Mock |
| 5 | |
| 6 | import pytest |
| 7 | from composio_client import omit |
| 8 | |
| 9 | from composio import exceptions |
| 10 | from composio.client.types import custom_upsert_params |
| 11 | from composio.core.models import experimental as experimental_module |
| 12 | from composio.core.models.experimental import ( |
| 13 | ExperimentalAPI, |
| 14 | ExperimentalCustomToolkits, |
| 15 | ) |
| 16 | |
| 17 | TOOLKIT_CONFIG: custom_upsert_params.ToolkitConfig = { |
| 18 | "name": "My Toolkit", |
| 19 | "mcp_url": "https://mcp.example.com/mcp", |
| 20 | "auth_schemes": [ |
| 21 | { |
| 22 | "mode": "API_KEY", |
| 23 | "headers": {"Authorization": "Bearer {{generic_api_key}}"}, |
| 24 | } |
| 25 | ], |
| 26 | } |
| 27 | |
| 28 | |
| 29 | @pytest.fixture |
| 30 | def mock_client() -> Mock: |
| 31 | client = Mock() |
| 32 | client.custom = Mock() |
| 33 | return client |
| 34 | |
| 35 | |
| 36 | @pytest.fixture |
| 37 | def custom_toolkits(mock_client: Mock) -> ExperimentalCustomToolkits: |
| 38 | return ExperimentalCustomToolkits(client=mock_client) |
| 39 | |
| 40 | |
| 41 | class TestExperimentalCustomToolkits: |
| 42 | def test_mounted_on_experimental_api(self, mock_client): |
| 43 | experimental = ExperimentalAPI(client=mock_client) |
| 44 | |
| 45 | assert isinstance(experimental.custom_toolkits, ExperimentalCustomToolkits) |
| 46 | assert experimental.custom_toolkits._client is mock_client |
| 47 | |
| 48 | def test_create_passes_params_through(self, custom_toolkits, mock_client): |
| 49 | mock_client.custom.create_toolkit.return_value = "created" |
| 50 | |
| 51 | result = custom_toolkits.create(**TOOLKIT_CONFIG, user_id="user_123") |
| 52 | |
| 53 | assert result == "created" |
| 54 | mock_client.custom.create_toolkit.assert_called_once_with( |
| 55 | **TOOLKIT_CONFIG, user_id="user_123" |
| 56 | ) |
| 57 | |
| 58 | def test_update_passes_slug_and_params_through(self, custom_toolkits, mock_client): |
| 59 | mock_client.custom.update_toolkits.return_value = "updated" |
| 60 | |
| 61 | result = custom_toolkits.update( |
| 62 | "CUSTOM_MY_TOOLKIT_K7F2Q", name="Renamed", user_id="user_123" |
| 63 | ) |
| 64 | |
| 65 | assert result == "updated" |
| 66 | mock_client.custom.update_toolkits.assert_called_once_with( |
| 67 | "CUSTOM_MY_TOOLKIT_K7F2Q", name="Renamed", user_id="user_123" |
| 68 | ) |
| 69 | |
| 70 | def test_upsert_warns_once_per_process( |
| 71 | self, custom_toolkits, mock_client, monkeypatch |
| 72 | ): |
| 73 | monkeypatch.setattr(experimental_module, "_upsert_warning_emitted", False) |
| 74 | |
| 75 | with warnings.catch_warnings(record=True) as caught: |
| 76 | warnings.simplefilter("always") |
| 77 | custom_toolkits.upsert(slug="MY_TOOLKIT", toolkit_config=TOOLKIT_CONFIG) |
| 78 | custom_toolkits.upsert(slug="MY_TOOLKIT", toolkit_config=TOOLKIT_CONFIG) |
| 79 | |
| 80 | deprecations = [ |
| 81 | w |
| 82 | for w in caught |
| 83 | if issubclass(w.category, exceptions.ComposioDeprecationWarning) |
| 84 | ] |
| 85 | assert len(deprecations) == 1 |
| 86 | assert "create()" in str(deprecations[0].message) |
| 87 | |
| 88 | def test_upsert_passes_params_through( |
| 89 | self, custom_toolkits, mock_client, monkeypatch |
| 90 | ): |
| 91 | mock_client.custom.upsert.return_value = "upserted" |
| 92 | monkeypatch.setattr(experimental_module, "_upsert_warning_emitted", False) |
| 93 | |
| 94 | with pytest.warns( |
| 95 | exceptions.ComposioDeprecationWarning, |
| 96 | match=r"custom_toolkits\.upsert\(\) is deprecated", |
| 97 | ): |
| 98 | result = custom_toolkits.upsert( |
| 99 | slug="MY_TOOLKIT", toolkit_config=TOOLKIT_CONFIG |
| 100 | ) |
| 101 | |
| 102 | assert result == "upserted" |
| 103 | mock_client.custom.upsert.assert_called_once_with( |
| 104 | slug="MY_TOOLKIT", toolkit_config=TOOLKIT_CONFIG |
| 105 | ) |
| 106 | |
| 107 | def test_sync_with_connected_account(self, custom_toolkits, mock_client): |
| 108 | mock_client.custom.sync.return_value = "synced" |
| 109 | |
| 110 | result = custom_toolkits.sync("CUSTOM_MY_TOOLKIT", connected_account_id="ca_1") |
| 111 | |
| 112 | assert result == "synced" |
| 113 | mock_client.custom.sync.assert_called_once_with( |
| 114 | slug="CUSTOM_MY_TOOLKIT", connected_account_id="ca_1" |
| 115 | ) |
| 116 | |
| 117 | def test_sync_without_connected_account(self, custom_toolkits, mock_client): |
| 118 | custom_toolkits.sync("CUSTOM_MY_TOOLKIT") |
| 119 | |
| 120 | mock_client.custom.sync.assert_called_once_with( |
| 121 | slug="CUSTOM_MY_TOOLKIT", connected_account_id=omit |
| 122 | ) |
| 123 | |
| 124 | def test_delete(self, custom_toolkits, mock_client): |
| 125 | mock_client.custom.delete_toolkit.return_value = "deleted" |
| 126 | |
| 127 | result = custom_toolkits.delete("CUSTOM_MY_TOOLKIT") |
| 128 | |
| 129 | assert result == "deleted" |
| 130 | mock_client.custom.delete_toolkit.assert_called_once_with("CUSTOM_MY_TOOLKIT") |
| 131 | |
| 132 | def test_requires_client(self): |
| 133 | custom_toolkits = ExperimentalCustomToolkits(client=None) |
| 134 | |
| 135 | with pytest.raises(exceptions.ValidationError): |
| 136 | custom_toolkits.create(**TOOLKIT_CONFIG, user_id="all") |
| 137 | with pytest.raises(exceptions.ValidationError): |
| 138 | custom_toolkits.update("CUSTOM_MY_TOOLKIT", name="Renamed") |
| 139 | with pytest.raises(exceptions.ValidationError): |
| 140 | custom_toolkits.sync("CUSTOM_MY_TOOLKIT") |
| 141 | with pytest.raises(exceptions.ValidationError): |
| 142 | custom_toolkits.delete("CUSTOM_MY_TOOLKIT") |