|
| 1 | +"""Tests for tool name validation utilities (SEP-986).""" |
| 2 | + |
| 3 | +import logging |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from mcp.shared.tool_name_validation import ( |
| 8 | + issue_tool_name_warning, |
| 9 | + validate_and_warn_tool_name, |
| 10 | + validate_tool_name, |
| 11 | +) |
| 12 | + |
| 13 | + |
| 14 | +class TestValidateToolName: |
| 15 | + """Tests for validate_tool_name function.""" |
| 16 | + |
| 17 | + class TestValidNames: |
| 18 | + """Test cases for valid tool names.""" |
| 19 | + |
| 20 | + @pytest.mark.parametrize( |
| 21 | + "tool_name", |
| 22 | + [ |
| 23 | + "getUser", |
| 24 | + "get_user_profile", |
| 25 | + "user-profile-update", |
| 26 | + "admin.tools.list", |
| 27 | + "DATA_EXPORT_v2.1", |
| 28 | + "a", |
| 29 | + "a" * 128, |
| 30 | + ], |
| 31 | + ids=[ |
| 32 | + "simple_alphanumeric", |
| 33 | + "with_underscores", |
| 34 | + "with_dashes", |
| 35 | + "with_dots", |
| 36 | + "mixed_characters", |
| 37 | + "single_character", |
| 38 | + "max_length_128", |
| 39 | + ], |
| 40 | + ) |
| 41 | + def test_accepts_valid_names(self, tool_name: str) -> None: |
| 42 | + """Valid tool names should pass validation with no warnings.""" |
| 43 | + result = validate_tool_name(tool_name) |
| 44 | + assert result.is_valid is True |
| 45 | + assert result.warnings == [] |
| 46 | + |
| 47 | + class TestInvalidNames: |
| 48 | + """Test cases for invalid tool names.""" |
| 49 | + |
| 50 | + def test_rejects_empty_name(self) -> None: |
| 51 | + """Empty names should be rejected.""" |
| 52 | + result = validate_tool_name("") |
| 53 | + assert result.is_valid is False |
| 54 | + assert "Tool name cannot be empty" in result.warnings |
| 55 | + |
| 56 | + def test_rejects_name_exceeding_max_length(self) -> None: |
| 57 | + """Names exceeding 128 characters should be rejected.""" |
| 58 | + result = validate_tool_name("a" * 129) |
| 59 | + assert result.is_valid is False |
| 60 | + assert any("exceeds maximum length of 128 characters (current: 129)" in w for w in result.warnings) |
| 61 | + |
| 62 | + @pytest.mark.parametrize( |
| 63 | + "tool_name,expected_char", |
| 64 | + [ |
| 65 | + ("get user profile", "' '"), |
| 66 | + ("get,user,profile", "','"), |
| 67 | + ("user/profile/update", "'/'"), |
| 68 | + |
| 69 | + ], |
| 70 | + ids=[ |
| 71 | + "with_spaces", |
| 72 | + "with_commas", |
| 73 | + "with_slashes", |
| 74 | + "with_at_symbol", |
| 75 | + ], |
| 76 | + ) |
| 77 | + def test_rejects_invalid_characters(self, tool_name: str, expected_char: str) -> None: |
| 78 | + """Names with invalid characters should be rejected.""" |
| 79 | + result = validate_tool_name(tool_name) |
| 80 | + assert result.is_valid is False |
| 81 | + assert any("invalid characters" in w and expected_char in w for w in result.warnings) |
| 82 | + |
| 83 | + def test_rejects_multiple_invalid_chars(self) -> None: |
| 84 | + """Names with multiple invalid chars should list all of them.""" |
| 85 | + result = validate_tool_name("user name@domain,com") |
| 86 | + assert result.is_valid is False |
| 87 | + warning = next(w for w in result.warnings if "invalid characters" in w) |
| 88 | + assert "' '" in warning |
| 89 | + assert "'@'" in warning |
| 90 | + assert "','" in warning |
| 91 | + |
| 92 | + def test_rejects_unicode_characters(self) -> None: |
| 93 | + """Names with unicode characters should be rejected.""" |
| 94 | + result = validate_tool_name("user-\u00f1ame") # n with tilde |
| 95 | + assert result.is_valid is False |
| 96 | + |
| 97 | + class TestWarningsForProblematicPatterns: |
| 98 | + """Test cases for valid names that generate warnings.""" |
| 99 | + |
| 100 | + def test_warns_on_leading_dash(self) -> None: |
| 101 | + """Names starting with dash should generate warning but be valid.""" |
| 102 | + result = validate_tool_name("-get-user") |
| 103 | + assert result.is_valid is True |
| 104 | + assert any("starts or ends with a dash" in w for w in result.warnings) |
| 105 | + |
| 106 | + def test_warns_on_trailing_dash(self) -> None: |
| 107 | + """Names ending with dash should generate warning but be valid.""" |
| 108 | + result = validate_tool_name("get-user-") |
| 109 | + assert result.is_valid is True |
| 110 | + assert any("starts or ends with a dash" in w for w in result.warnings) |
| 111 | + |
| 112 | + def test_warns_on_leading_dot(self) -> None: |
| 113 | + """Names starting with dot should generate warning but be valid.""" |
| 114 | + result = validate_tool_name(".get.user") |
| 115 | + assert result.is_valid is True |
| 116 | + assert any("starts or ends with a dot" in w for w in result.warnings) |
| 117 | + |
| 118 | + def test_warns_on_trailing_dot(self) -> None: |
| 119 | + """Names ending with dot should generate warning but be valid.""" |
| 120 | + result = validate_tool_name("get.user.") |
| 121 | + assert result.is_valid is True |
| 122 | + assert any("starts or ends with a dot" in w for w in result.warnings) |
| 123 | + |
| 124 | + |
| 125 | +class TestIssueToolNameWarning: |
| 126 | + """Tests for issue_tool_name_warning function.""" |
| 127 | + |
| 128 | + def test_logs_warnings(self, caplog: pytest.LogCaptureFixture) -> None: |
| 129 | + """Warnings should be logged at WARNING level.""" |
| 130 | + warnings = ["Warning 1", "Warning 2"] |
| 131 | + with caplog.at_level(logging.WARNING): |
| 132 | + issue_tool_name_warning("test-tool", warnings) |
| 133 | + |
| 134 | + assert 'Tool name validation warning for "test-tool"' in caplog.text |
| 135 | + assert "- Warning 1" in caplog.text |
| 136 | + assert "- Warning 2" in caplog.text |
| 137 | + assert "Tool registration will proceed" in caplog.text |
| 138 | + assert "SEP-986" in caplog.text |
| 139 | + |
| 140 | + def test_no_logging_for_empty_warnings(self, caplog: pytest.LogCaptureFixture) -> None: |
| 141 | + """Empty warnings list should not produce any log output.""" |
| 142 | + with caplog.at_level(logging.WARNING): |
| 143 | + issue_tool_name_warning("test-tool", []) |
| 144 | + |
| 145 | + assert caplog.text == "" |
| 146 | + |
| 147 | + |
| 148 | +class TestValidateAndWarnToolName: |
| 149 | + """Tests for validate_and_warn_tool_name function.""" |
| 150 | + |
| 151 | + def test_returns_true_for_valid_name(self) -> None: |
| 152 | + """Valid names should return True.""" |
| 153 | + assert validate_and_warn_tool_name("valid-tool-name") is True |
| 154 | + |
| 155 | + def test_returns_false_for_invalid_name(self) -> None: |
| 156 | + """Invalid names should return False.""" |
| 157 | + assert validate_and_warn_tool_name("") is False |
| 158 | + assert validate_and_warn_tool_name("a" * 129) is False |
| 159 | + assert validate_and_warn_tool_name("invalid name") is False |
| 160 | + |
| 161 | + def test_logs_warnings_for_invalid_name(self, caplog: pytest.LogCaptureFixture) -> None: |
| 162 | + """Invalid names should trigger warning logs.""" |
| 163 | + with caplog.at_level(logging.WARNING): |
| 164 | + validate_and_warn_tool_name("invalid name") |
| 165 | + |
| 166 | + assert "Tool name validation warning" in caplog.text |
| 167 | + |
| 168 | + def test_no_warnings_for_clean_valid_name(self, caplog: pytest.LogCaptureFixture) -> None: |
| 169 | + """Clean valid names should not produce any log output.""" |
| 170 | + with caplog.at_level(logging.WARNING): |
| 171 | + result = validate_and_warn_tool_name("clean-tool-name") |
| 172 | + |
| 173 | + assert result is True |
| 174 | + assert caplog.text == "" |
| 175 | + |
| 176 | + |
| 177 | +class TestEdgeCases: |
| 178 | + """Test edge cases and robustness.""" |
| 179 | + |
| 180 | + @pytest.mark.parametrize( |
| 181 | + "tool_name,is_valid,expected_warning_fragment", |
| 182 | + [ |
| 183 | + ("...", True, "starts or ends with a dot"), |
| 184 | + ("---", True, "starts or ends with a dash"), |
| 185 | + ("///", False, "invalid characters"), |
| 186 | + ("user@name123", False, "invalid characters"), |
| 187 | + ], |
| 188 | + ids=[ |
| 189 | + "only_dots", |
| 190 | + "only_dashes", |
| 191 | + "only_slashes", |
| 192 | + "mixed_valid_invalid", |
| 193 | + ], |
| 194 | + ) |
| 195 | + def test_edge_cases(self, tool_name: str, is_valid: bool, expected_warning_fragment: str) -> None: |
| 196 | + """Various edge cases should be handled correctly.""" |
| 197 | + result = validate_tool_name(tool_name) |
| 198 | + assert result.is_valid is is_valid |
| 199 | + assert any(expected_warning_fragment in w for w in result.warnings) |
0 commit comments