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 | """ |
| 2 | Pytest configuration and shared fixtures for Composio integration tests. |
| 3 | """ |
| 4 | |
| 5 | import os |
| 6 | import sys |
| 7 | from pathlib import Path |
| 8 | |
| 9 | import pytest |
| 10 | |
| 11 | # Add the python directory to the path for imports |
| 12 | python_dir = Path(__file__).parent.parent.parent |
| 13 | sys.path.insert(0, str(python_dir)) |
| 14 | |
| 15 | # Test configuration |
| 16 | API_KEY = os.getenv("COMPOSIO_API_KEY") |
| 17 | |
| 18 | if not API_KEY: |
| 19 | pytest.skip( |
| 20 | "COMPOSIO_API_KEY environment variable not set", allow_module_level=True |
| 21 | ) |
| 22 | |
| 23 | |
| 24 | @pytest.fixture(scope="session", autouse=True) |
| 25 | def setup_environment(): |
| 26 | """Set up the test environment for all tests.""" |
| 27 | os.environ["COMPOSIO_API_KEY"] = API_KEY |
| 28 | yield |
| 29 | |
| 30 | |
| 31 | @pytest.fixture(scope="session") |
| 32 | def composio_client(): |
| 33 | """Provide a Composio client instance for all tests.""" |
| 34 | from composio import Composio |
| 35 | |
| 36 | return Composio() |
| 37 | |
| 38 | |
| 39 | @pytest.fixture(scope="session") |
| 40 | def auth_configs(composio_client): |
| 41 | """Get available auth configurations for testing.""" |
| 42 | try: |
| 43 | configs_response = composio_client.auth_configs.list() |
| 44 | |
| 45 | # Debug what we get |
| 46 | print(f"Raw response type: {type(configs_response)}") |
| 47 | |
| 48 | # Handle different response formats |
| 49 | if hasattr(configs_response, "items"): |
| 50 | # Direct access to items |
| 51 | items = configs_response.items |
| 52 | print(f"Found items directly: {len(items) if items else 0}") |
| 53 | return list(items) if items else [] |
| 54 | elif hasattr(configs_response, "__iter__"): |
| 55 | # If it's being converted to an iterable of tuples, find the items tuple |
| 56 | items_tuple = None |
| 57 | for item in configs_response: |
| 58 | if isinstance(item, tuple) and len(item) == 2 and item[0] == "items": |
| 59 | items_tuple = item[1] |
| 60 | break |
| 61 | |
| 62 | if items_tuple: |
| 63 | print(f"Found items in tuple: {len(items_tuple)}") |
| 64 | return list(items_tuple) |
| 65 | |
| 66 | return [] |
| 67 | |
| 68 | except Exception as e: |
| 69 | print(f"Exception in auth_configs fixture: {e}") |
| 70 | import traceback |
| 71 | |
| 72 | traceback.print_exc() |
| 73 | return [] |
| 74 | |
| 75 | |
| 76 | @pytest.fixture |
| 77 | def sample_mcp_config_data(): |
| 78 | """Provide sample data for MCP configuration testing.""" |
| 79 | import time |
| 80 | |
| 81 | return { |
| 82 | "name": f"pytest_test_{int(time.time())}", |
| 83 | "server_config": [ |
| 84 | {"auth_config_id": "test_auth_id", "allowed_tools": ["GMAIL_FETCH_EMAILS"]} |
| 85 | ], |
| 86 | "options": {"is_chat_auth": True}, |
| 87 | } |
| 88 | |
| 89 | |
| 90 | @pytest.fixture |
| 91 | def test_user_id(): |
| 92 | """Provide a consistent test user ID.""" |
| 93 | return "pytest_integration_user_123" |
| 94 | |
| 95 | |
| 96 | # Track created resources for cleanup |
| 97 | _created_mcp_servers = [] |
| 98 | |
| 99 | |
| 100 | @pytest.fixture |
| 101 | def mcp_server_cleanup(composio_client): |
| 102 | """Fixture to track and cleanup MCP servers created during tests.""" |
| 103 | created_servers = [] |
| 104 | |
| 105 | yield created_servers |
| 106 | |
| 107 | # Cleanup after test |
| 108 | for server_id in created_servers: |
| 109 | try: |
| 110 | composio_client.mcp.delete(server_id) |
| 111 | print(f"✅ Cleaned up MCP server: {server_id}") |
| 112 | except Exception as e: |
| 113 | print(f"⚠️ Failed to cleanup MCP server {server_id}: {e}") |
| 114 | |
| 115 | |
| 116 | def pytest_configure(config): |
| 117 | """Configure pytest with custom settings.""" |
| 118 | # Add timeout marker |
| 119 | config.addinivalue_line( |
| 120 | "markers", "timeout: mark test to run with a specific timeout" |
| 121 | ) |