|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# Copyright (c) 2025-present WATonomous. All rights reserved. |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Integration tests for MCP server authentication.""" |
| 17 | + |
| 18 | +import time |
| 19 | +import unittest |
| 20 | + |
| 21 | +import launch |
| 22 | +import launch.actions |
| 23 | +import launch_ros.actions |
| 24 | +import launch_testing.actions |
| 25 | +import pytest |
| 26 | +import requests |
| 27 | + |
| 28 | + |
| 29 | +@pytest.mark.launch_test |
| 30 | +def generate_test_description(): |
| 31 | + """Launch the MCP server with authentication enabled.""" |
| 32 | + # Test API key |
| 33 | + test_api_key = "test_secret_key_12345" |
| 34 | + |
| 35 | + mcp_server_node = launch_ros.actions.LifecycleNode( |
| 36 | + package="robot_mcp_server", |
| 37 | + executable="robot_mcp_server_node", |
| 38 | + name="mcp_http_server", |
| 39 | + namespace="", |
| 40 | + parameters=[ |
| 41 | + { |
| 42 | + "server.host": "127.0.0.2", |
| 43 | + "server.port": 18081, |
| 44 | + "server.api_key": test_api_key, |
| 45 | + "server.enable_https": False, |
| 46 | + } |
| 47 | + ], |
| 48 | + output="screen", |
| 49 | + ) |
| 50 | + |
| 51 | + # Use nav2_lifecycle_manager to automatically activate the node |
| 52 | + lifecycle_manager = launch_ros.actions.Node( |
| 53 | + package="nav2_lifecycle_manager", |
| 54 | + executable="lifecycle_manager", |
| 55 | + name="test_lifecycle_manager", |
| 56 | + parameters=[{"autostart": True, "node_names": ["mcp_http_server"], "bond_timeout": 4.0}], |
| 57 | + output="screen", |
| 58 | + ) |
| 59 | + |
| 60 | + return launch.LaunchDescription( |
| 61 | + [ |
| 62 | + mcp_server_node, |
| 63 | + lifecycle_manager, |
| 64 | + launch_testing.actions.ReadyToTest(), |
| 65 | + ] |
| 66 | + ) |
| 67 | + |
| 68 | + |
| 69 | +class TestAuthentication(unittest.TestCase): |
| 70 | + """Active tests for authentication.""" |
| 71 | + |
| 72 | + BASE_URL = "http://127.0.0.2:18081" |
| 73 | + VALID_API_KEY = "test_secret_key_12345" |
| 74 | + |
| 75 | + @classmethod |
| 76 | + def setUpClass(cls): |
| 77 | + """Wait for server to be ready.""" |
| 78 | + time.sleep(5) |
| 79 | + |
| 80 | + def test_valid_api_key(self): |
| 81 | + """Test request with valid API key succeeds.""" |
| 82 | + payload = {"jsonrpc": "2.0", "method": "test", "id": 1} |
| 83 | + headers = {"Authorization": f"Bearer {self.VALID_API_KEY}"} |
| 84 | + |
| 85 | + response = requests.post(f"{self.BASE_URL}/mcp", json=payload, headers=headers) |
| 86 | + |
| 87 | + assert response.status_code == 200 |
| 88 | + data = response.json() |
| 89 | + assert "result" in data or "message" in data |
| 90 | + |
| 91 | + def test_invalid_api_key(self): |
| 92 | + """Test request with invalid API key is rejected.""" |
| 93 | + payload = {"jsonrpc": "2.0", "method": "test", "id": 1} |
| 94 | + headers = {"Authorization": "Bearer wrong_key"} |
| 95 | + |
| 96 | + response = requests.post(f"{self.BASE_URL}/mcp", json=payload, headers=headers) |
| 97 | + |
| 98 | + assert response.status_code == 401 |
| 99 | + data = response.json() |
| 100 | + assert "error" in data |
| 101 | + |
| 102 | + def test_missing_authorization_header(self): |
| 103 | + """Test request without Authorization header is rejected.""" |
| 104 | + payload = {"jsonrpc": "2.0", "method": "test", "id": 1} |
| 105 | + |
| 106 | + response = requests.post(f"{self.BASE_URL}/mcp", json=payload) |
| 107 | + |
| 108 | + assert response.status_code == 401 |
| 109 | + data = response.json() |
| 110 | + assert "error" in data |
| 111 | + |
| 112 | + def test_malformed_authorization_header(self): |
| 113 | + """Test request with malformed Authorization header is rejected.""" |
| 114 | + payload = {"jsonrpc": "2.0", "method": "test", "id": 1} |
| 115 | + headers = {"Authorization": "InvalidFormat"} |
| 116 | + |
| 117 | + response = requests.post(f"{self.BASE_URL}/mcp", json=payload, headers=headers) |
| 118 | + |
| 119 | + assert response.status_code == 401 |
| 120 | + data = response.json() |
| 121 | + assert "error" in data |
| 122 | + |
| 123 | + def test_authorization_header_without_bearer(self): |
| 124 | + """Test request with Authorization header but no Bearer prefix is rejected.""" |
| 125 | + payload = {"jsonrpc": "2.0", "method": "test", "id": 1} |
| 126 | + headers = {"Authorization": self.VALID_API_KEY} |
| 127 | + |
| 128 | + response = requests.post(f"{self.BASE_URL}/mcp", json=payload, headers=headers) |
| 129 | + |
| 130 | + assert response.status_code == 401 |
| 131 | + data = response.json() |
| 132 | + assert "error" in data |
| 133 | + |
| 134 | + |
| 135 | +@launch_testing.post_shutdown_test() |
| 136 | +class TestAuthenticationShutdown(unittest.TestCase): |
| 137 | + """Post-shutdown tests.""" |
| 138 | + |
| 139 | + def test_exit_codes(self, proc_info): |
| 140 | + """Check that all processes exited cleanly.""" |
| 141 | + launch_testing.asserts.assertExitCodes(proc_info) |
0 commit comments