Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.
Mirrored from https://github.com/Camo67/composio.git
Synced Nov 21, 03:37 UTC
| 1 | """Error mapping for ``Tools.get_raw_composio_tool_by_slug``. |
| 2 | |
| 3 | Only an unknown slug becomes ``ToolNotFoundError``. Other client failures, |
| 4 | such as an invalid API key, must surface unchanged so callers can tell a |
| 5 | missing tool from a rejected request (parity with the TypeScript SDK). |
| 6 | """ |
| 7 | |
| 8 | from unittest.mock import Mock |
| 9 | |
| 10 | import httpx |
| 11 | import pytest |
| 12 | from composio_client import AuthenticationError, BadRequestError, NotFoundError |
| 13 | |
| 14 | from composio import exceptions |
| 15 | from composio.core.models.tools import Tools |
| 16 | from tests.conftest import mock_http_client |
| 17 | |
| 18 | |
| 19 | def _status_error(cls, status: int): |
| 20 | request = httpx.Request("GET", "https://backend.composio.dev/api/v3/tools/X") |
| 21 | response = httpx.Response(status, request=request) |
| 22 | return cls("error", response=response, body=None) |
| 23 | |
| 24 | |
| 25 | @pytest.fixture |
| 26 | def mock_client() -> Mock: |
| 27 | return mock_http_client() |
| 28 | |
| 29 | |
| 30 | @pytest.fixture |
| 31 | def tools(mock_client: Mock) -> Tools: |
| 32 | return Tools(client=mock_client, provider=Mock()) |
| 33 | |
| 34 | |
| 35 | def test_unknown_slug_raises_tool_not_found(tools: Tools, mock_client: Mock) -> None: |
| 36 | not_found = _status_error(NotFoundError, 404) |
| 37 | mock_client.tools.retrieve.side_effect = not_found |
| 38 | |
| 39 | with pytest.raises(exceptions.ToolNotFoundError) as exc_info: |
| 40 | tools.get_raw_composio_tool_by_slug("NONEXISTENT_TOOL") |
| 41 | |
| 42 | assert exc_info.value.__cause__ is not_found |
| 43 | assert isinstance(exc_info.value, exceptions.NotFoundError) |
| 44 | assert "NONEXISTENT_TOOL" in str(exc_info.value) |
| 45 | |
| 46 | |
| 47 | def test_malformed_slug_raises_tool_not_found(tools: Tools, mock_client: Mock) -> None: |
| 48 | mock_client.tools.retrieve.side_effect = _status_error(BadRequestError, 400) |
| 49 | |
| 50 | with pytest.raises(exceptions.ToolNotFoundError): |
| 51 | tools.get_raw_composio_tool_by_slug("malformed slug") |
| 52 | |
| 53 | |
| 54 | def test_not_found_strips_control_characters(tools: Tools, mock_client: Mock) -> None: |
| 55 | request = httpx.Request("GET", "https://backend.composio.dev/api/v3/tools/X") |
| 56 | response = httpx.Response(404, request=request) |
| 57 | mock_client.tools.retrieve.side_effect = NotFoundError( |
| 58 | "error", |
| 59 | response=response, |
| 60 | body={ |
| 61 | "error": { |
| 62 | "message": "missing\x1b[31m\n[!] run: evil", |
| 63 | "code": 1404, |
| 64 | "slug": "TOOL_NOT_FOUND", |
| 65 | "status": 404, |
| 66 | } |
| 67 | }, |
| 68 | ) |
| 69 | |
| 70 | with pytest.raises(exceptions.ToolNotFoundError) as exc_info: |
| 71 | tools.get_raw_composio_tool_by_slug("GMAIL_SEND_EMAIL") |
| 72 | |
| 73 | assert str(exc_info.value) == ( |
| 74 | "Tool with slug GMAIL_SEND_EMAIL not found: missing [!] run: evil" |
| 75 | ) |
| 76 | |
| 77 | |
| 78 | def test_not_found_includes_the_server_message(tools: Tools, mock_client: Mock) -> None: |
| 79 | request = httpx.Request("GET", "https://backend.composio.dev/api/v3/tools/X") |
| 80 | response = httpx.Response(404, request=request) |
| 81 | not_found = NotFoundError( |
| 82 | "error", |
| 83 | response=response, |
| 84 | body={ |
| 85 | "error": { |
| 86 | "message": "No tool with that slug in the pinned version", |
| 87 | "code": 1404, |
| 88 | "slug": "TOOL_NOT_FOUND", |
| 89 | "status": 404, |
| 90 | "suggested_fix": "Use the latest version", |
| 91 | } |
| 92 | }, |
| 93 | ) |
| 94 | mock_client.tools.retrieve.side_effect = not_found |
| 95 | |
| 96 | with pytest.raises(exceptions.ToolNotFoundError) as exc_info: |
| 97 | tools.get_raw_composio_tool_by_slug("GMAIL_SEND_EMAIL") |
| 98 | |
| 99 | assert str(exc_info.value) == ( |
| 100 | "Tool with slug GMAIL_SEND_EMAIL not found: " |
| 101 | "No tool with that slug in the pinned version" |
| 102 | ) |
| 103 | assert exc_info.value.__cause__ is not_found |
| 104 | |
| 105 | |
| 106 | def test_invalid_api_key_is_not_tool_not_found(tools: Tools, mock_client: Mock) -> None: |
| 107 | unauthorized = _status_error(AuthenticationError, 401) |
| 108 | mock_client.tools.retrieve.side_effect = unauthorized |
| 109 | |
| 110 | with pytest.raises(AuthenticationError) as exc_info: |
| 111 | tools.get_raw_composio_tool_by_slug("SLACK_FETCH_CONVERSATION_HISTORY") |
| 112 | |
| 113 | assert exc_info.value is unauthorized |
| 114 | assert exc_info.value.status_code == 401 |
| 115 | |
| 116 | |
| 117 | def test_execute_surfaces_auth_error_from_schema_fetch( |
| 118 | tools: Tools, mock_client: Mock |
| 119 | ) -> None: |
| 120 | mock_client.tools.retrieve.side_effect = _status_error(AuthenticationError, 401) |
| 121 | |
| 122 | with pytest.raises(AuthenticationError): |
| 123 | tools.execute("SLACK_FETCH_CONVERSATION_HISTORY", {}, user_id="user") |
| 124 | |
| 125 | mock_client.tools.execute.assert_not_called() |