|
| 1 | +"""Integration tests for example plugins.""" |
| 2 | + |
| 3 | +import sys |
| 4 | +from pathlib import Path |
| 5 | + |
| 6 | +import pytest |
| 7 | +from google.protobuf.empty_pb2 import Empty |
| 8 | + |
| 9 | +# Add examples to path. |
| 10 | +examples_dir = Path(__file__).parent.parent.parent / "examples" |
| 11 | +sys.path.insert(0, str(examples_dir)) |
| 12 | + |
| 13 | + |
| 14 | +@pytest.fixture |
| 15 | +def mock_context(): |
| 16 | + """Provide a mock gRPC context.""" |
| 17 | + |
| 18 | + class MockContext: |
| 19 | + """Mock gRPC ServicerContext.""" |
| 20 | + |
| 21 | + def __init__(self): |
| 22 | + """Initialize mock context.""" |
| 23 | + self.invocation_metadata = [] |
| 24 | + |
| 25 | + return MockContext() |
| 26 | + |
| 27 | + |
| 28 | +class TestSimplePlugin: |
| 29 | + """Integration tests for simple_plugin example.""" |
| 30 | + |
| 31 | + @pytest.mark.asyncio |
| 32 | + async def test_simple_plugin_metadata(self, mock_context): |
| 33 | + """Simple plugin should return correct metadata.""" |
| 34 | + from simple_plugin.main import SimplePlugin |
| 35 | + |
| 36 | + plugin = SimplePlugin() |
| 37 | + metadata = await plugin.GetMetadata(Empty(), mock_context) |
| 38 | + |
| 39 | + assert metadata.name == "simple-plugin" |
| 40 | + assert metadata.version == "1.0.0" |
| 41 | + assert "custom header" in metadata.description.lower() |
| 42 | + |
| 43 | + @pytest.mark.asyncio |
| 44 | + async def test_simple_plugin_capabilities(self, mock_context): |
| 45 | + """Simple plugin should declare REQUEST flow.""" |
| 46 | + from simple_plugin.main import SimplePlugin |
| 47 | + |
| 48 | + from mcpd_plugins.v1.plugins.plugin_pb2 import FLOW_REQUEST |
| 49 | + |
| 50 | + plugin = SimplePlugin() |
| 51 | + capabilities = await plugin.GetCapabilities(Empty(), mock_context) |
| 52 | + |
| 53 | + assert len(capabilities.flows) == 1 |
| 54 | + assert capabilities.flows[0] == FLOW_REQUEST |
| 55 | + |
| 56 | + @pytest.mark.asyncio |
| 57 | + async def test_simple_plugin_adds_header(self, mock_context): |
| 58 | + """Simple plugin should add X-Simple-Plugin header.""" |
| 59 | + from simple_plugin.main import SimplePlugin |
| 60 | + |
| 61 | + from mcpd_plugins.v1.plugins.plugin_pb2 import HTTPRequest |
| 62 | + |
| 63 | + plugin = SimplePlugin() |
| 64 | + request = HTTPRequest( |
| 65 | + method="GET", |
| 66 | + url="https://example.com/test", |
| 67 | + path="/test", |
| 68 | + ) |
| 69 | + request.headers["User-Agent"] = "test" |
| 70 | + |
| 71 | + response = await plugin.HandleRequest(request, mock_context) |
| 72 | + |
| 73 | + assert getattr(response, "continue") is True |
| 74 | + assert "X-Simple-Plugin" in response.modified_request.headers |
| 75 | + assert response.modified_request.headers["X-Simple-Plugin"] == "processed" |
| 76 | + |
| 77 | + |
| 78 | +class TestAuthPlugin: |
| 79 | + """Integration tests for auth_plugin example.""" |
| 80 | + |
| 81 | + @pytest.mark.asyncio |
| 82 | + async def test_auth_plugin_rejects_missing_token(self, mock_context): |
| 83 | + """Auth plugin should reject requests without Authorization header.""" |
| 84 | + from auth_plugin.main import AuthPlugin |
| 85 | + |
| 86 | + from mcpd_plugins.v1.plugins.plugin_pb2 import HTTPRequest |
| 87 | + |
| 88 | + plugin = AuthPlugin() |
| 89 | + request = HTTPRequest( |
| 90 | + method="GET", |
| 91 | + url="https://example.com/test", |
| 92 | + path="/test", |
| 93 | + ) |
| 94 | + |
| 95 | + response = await plugin.HandleRequest(request, mock_context) |
| 96 | + |
| 97 | + assert getattr(response, "continue") is False |
| 98 | + assert response.status_code == 401 |
| 99 | + |
| 100 | + @pytest.mark.asyncio |
| 101 | + async def test_auth_plugin_rejects_invalid_token(self, mock_context): |
| 102 | + """Auth plugin should reject requests with invalid token.""" |
| 103 | + from auth_plugin.main import AuthPlugin |
| 104 | + |
| 105 | + from mcpd_plugins.v1.plugins.plugin_pb2 import HTTPRequest |
| 106 | + |
| 107 | + plugin = AuthPlugin() |
| 108 | + request = HTTPRequest( |
| 109 | + method="GET", |
| 110 | + url="https://example.com/test", |
| 111 | + path="/test", |
| 112 | + ) |
| 113 | + request.headers["Authorization"] = "Bearer wrong-token" |
| 114 | + |
| 115 | + response = await plugin.HandleRequest(request, mock_context) |
| 116 | + |
| 117 | + assert getattr(response, "continue") is False |
| 118 | + assert response.status_code == 401 |
| 119 | + |
| 120 | + @pytest.mark.asyncio |
| 121 | + async def test_auth_plugin_accepts_valid_token(self, mock_context): |
| 122 | + """Auth plugin should accept requests with valid token.""" |
| 123 | + from auth_plugin.main import AuthPlugin |
| 124 | + |
| 125 | + from mcpd_plugins.v1.plugins.plugin_pb2 import HTTPRequest |
| 126 | + |
| 127 | + plugin = AuthPlugin() |
| 128 | + request = HTTPRequest( |
| 129 | + method="GET", |
| 130 | + url="https://example.com/test", |
| 131 | + path="/test", |
| 132 | + ) |
| 133 | + request.headers["Authorization"] = "Bearer secret-token-123" |
| 134 | + |
| 135 | + response = await plugin.HandleRequest(request, mock_context) |
| 136 | + |
| 137 | + assert getattr(response, "continue") is True |
| 138 | + |
| 139 | + |
| 140 | +class TestLoggingPlugin: |
| 141 | + """Integration tests for logging_plugin example.""" |
| 142 | + |
| 143 | + @pytest.mark.asyncio |
| 144 | + async def test_logging_plugin_supports_both_flows(self, mock_context): |
| 145 | + """Logging plugin should support both REQUEST and RESPONSE flows.""" |
| 146 | + from logging_plugin.main import LoggingPlugin |
| 147 | + |
| 148 | + from mcpd_plugins.v1.plugins.plugin_pb2 import FLOW_REQUEST, FLOW_RESPONSE |
| 149 | + |
| 150 | + plugin = LoggingPlugin() |
| 151 | + capabilities = await plugin.GetCapabilities(Empty(), mock_context) |
| 152 | + |
| 153 | + assert len(capabilities.flows) == 2 |
| 154 | + assert FLOW_REQUEST in capabilities.flows |
| 155 | + assert FLOW_RESPONSE in capabilities.flows |
| 156 | + |
| 157 | + @pytest.mark.asyncio |
| 158 | + async def test_logging_plugin_logs_request(self, mock_context, caplog): |
| 159 | + """Logging plugin should log request details.""" |
| 160 | + from logging_plugin.main import LoggingPlugin |
| 161 | + |
| 162 | + from mcpd_plugins.v1.plugins.plugin_pb2 import HTTPRequest |
| 163 | + |
| 164 | + plugin = LoggingPlugin() |
| 165 | + request = HTTPRequest( |
| 166 | + method="GET", |
| 167 | + url="https://example.com/test", |
| 168 | + path="/test", |
| 169 | + ) |
| 170 | + |
| 171 | + response = await plugin.HandleRequest(request, mock_context) |
| 172 | + |
| 173 | + assert getattr(response, "continue") is True |
| 174 | + # Check that logging occurred (test output capture). |
| 175 | + assert "INCOMING REQUEST" in caplog.text or getattr(response, "continue") is True |
0 commit comments