Skip to content

Commit 1d541cd

Browse files
committed
add tests and fix old ones
1 parent b24141f commit 1d541cd

File tree

3 files changed

+173
-3
lines changed

3 files changed

+173
-3
lines changed

mcp_gateway/plugins/tracing/xetrack.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,9 @@ def to_events(context: PluginContext, event: Dict[str, Any]) -> List[Dict[str, A
8484
else:
8585
response["content"] = f"<binary data ({len(content)} bytes)>"
8686
response["mime_type"] = mime_type
87+
elif isinstance(context_response, dict):
88+
# For plain dictionary responses
89+
response = context_response.copy()
8790
except Exception as e:
8891
response["error_getting_response"] = str(e)
8992

tests/test_basic_guardrail.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,14 @@ def test_secret_cleaner(
4646
assert "import requests" in cleaned_text
4747
assert "api_url = " in cleaned_text
4848

49-
def test_data_anonymizer(
49+
def test_sanitize_text(
5050
self, plugin: BasicGuardrailPlugin, pii_sample_text: str
5151
) -> None:
5252
"""
53-
Test that _data_anonymizer correctly applies both PII and secret anonymization.
53+
Test that _sanitize_text correctly applies secret anonymization.
54+
Note: BasicGuardrailPlugin only handles secrets, not PII.
5455
"""
55-
anonymized_text = plugin._data_anonymizer(pii_sample_text)
56+
anonymized_text = plugin._sanitize_text(pii_sample_text)
5657

5758
# GitHub OAuth token should be anonymized
5859
assert "gho_16C7e42F292c6912E7710c838347Ae178B4a" not in anonymized_text

tests/test_npm_version_simple.py

Lines changed: 166 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
#!/usr/bin/env python3
2+
3+
"""Simple test script to verify NPM version handling without pytest."""
4+
5+
import sys
6+
import os
7+
8+
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
9+
10+
from mcp_gateway.security_scanner.npm_collector import NPMCollector
11+
from mcp_gateway.security_scanner.config import Keys
12+
13+
14+
def test_parse_scoped_package_with_version():
15+
"""Test parsing scoped packages with version tags."""
16+
collector = NPMCollector("@upstash/context7-mcp@latest")
17+
18+
assert collector.package_name == "@upstash/context7-mcp"
19+
assert collector.version_tag == "latest"
20+
assert collector.original_package_name == "@upstash/context7-mcp@latest"
21+
print("✓ test_parse_scoped_package_with_version passed")
22+
23+
24+
def test_parse_scoped_package_without_version():
25+
"""Test parsing scoped packages without version tags."""
26+
collector = NPMCollector("@angular/core")
27+
28+
assert collector.package_name == "@angular/core"
29+
assert collector.version_tag is None
30+
assert collector.original_package_name == "@angular/core"
31+
print("✓ test_parse_scoped_package_without_version passed")
32+
33+
34+
def test_parse_regular_package_with_version():
35+
"""Test parsing regular packages with version tags."""
36+
collector = NPMCollector("express@4.18.2")
37+
38+
assert collector.package_name == "express"
39+
assert collector.version_tag == "4.18.2"
40+
assert collector.original_package_name == "express@4.18.2"
41+
print("✓ test_parse_regular_package_with_version passed")
42+
43+
44+
def test_parse_regular_package_without_version():
45+
"""Test parsing regular packages without version tags."""
46+
collector = NPMCollector("lodash")
47+
48+
assert collector.package_name == "lodash"
49+
assert collector.version_tag is None
50+
assert collector.original_package_name == "lodash"
51+
print("✓ test_parse_regular_package_without_version passed")
52+
53+
54+
def test_parse_scoped_package_with_numeric_version():
55+
"""Test parsing scoped packages with numeric versions."""
56+
collector = NPMCollector("@angular/core@16.0.0")
57+
58+
assert collector.package_name == "@angular/core"
59+
assert collector.version_tag == "16.0.0"
60+
assert collector.original_package_name == "@angular/core@16.0.0"
61+
print("✓ test_parse_scoped_package_with_numeric_version passed")
62+
63+
64+
def test_parse_scoped_package_with_beta_version():
65+
"""Test parsing scoped packages with beta versions."""
66+
collector = NPMCollector("@vue/cli@5.0.0-beta.1")
67+
68+
assert collector.package_name == "@vue/cli"
69+
assert collector.version_tag == "5.0.0-beta.1"
70+
assert collector.original_package_name == "@vue/cli@5.0.0-beta.1"
71+
print("✓ test_parse_scoped_package_with_beta_version passed")
72+
73+
74+
def test_get_all_data_includes_version_info():
75+
"""Test that get_all_data includes version information."""
76+
collector = NPMCollector("@upstash/context7-mcp@latest")
77+
78+
# Mock the raw data to simulate successful fetch
79+
collector._raw_data = {
80+
"name": "@upstash/context7-mcp",
81+
"description": "Test package",
82+
"versions": {"1.0.0": {}},
83+
}
84+
85+
data = collector.get_all_data()
86+
87+
assert Keys.PACKAGE_NAME in data
88+
assert Keys.ORIGINAL_PACKAGE_NAME in data
89+
assert Keys.VERSION_TAG in data
90+
91+
assert data[Keys.PACKAGE_NAME] == "@upstash/context7-mcp"
92+
assert data[Keys.ORIGINAL_PACKAGE_NAME] == "@upstash/context7-mcp@latest"
93+
assert data[Keys.VERSION_TAG] == "latest"
94+
print("✓ test_get_all_data_includes_version_info passed")
95+
96+
97+
def test_edge_case_multiple_at_symbols():
98+
"""Test handling of edge cases with multiple @ symbols."""
99+
collector = NPMCollector("@scope/package@1.0.0@beta")
100+
101+
# Should treat last @ as version separator
102+
assert collector.package_name == "@scope/package@1.0.0"
103+
assert collector.version_tag == "beta"
104+
print("✓ test_edge_case_multiple_at_symbols passed")
105+
106+
107+
def test_whitespace_handling():
108+
"""Test that whitespace is properly handled."""
109+
collector = NPMCollector(" @upstash/context7-mcp@latest ")
110+
111+
assert collector.package_name == "@upstash/context7-mcp"
112+
assert collector.version_tag == "latest"
113+
assert collector.original_package_name == " @upstash/context7-mcp@latest "
114+
print("✓ test_whitespace_handling passed")
115+
116+
117+
def test_api_url_construction():
118+
"""Test that API URLs are constructed with clean package names."""
119+
collector = NPMCollector("@upstash/context7-mcp@latest")
120+
121+
# Test the URL construction logic
122+
expected_registry_url = "https://registry.npmjs.org/@upstash/context7-mcp"
123+
expected_downloads_url = (
124+
"https://api.npmjs.org/downloads/point/last-month/@upstash/context7-mcp"
125+
)
126+
127+
# Simulate the URL construction from fetch_data method
128+
package_url = f"{collector._NPM_REGISTRY_URL}/{collector.package_name}"
129+
downloads_url = (
130+
f"{collector._NPM_DOWNLOADS_API_URL}/last-month/{collector.package_name}"
131+
)
132+
133+
assert package_url == expected_registry_url
134+
assert downloads_url == expected_downloads_url
135+
print("✓ test_api_url_construction passed")
136+
137+
138+
def run_all_tests():
139+
"""Run all tests."""
140+
print("Running NPM version handling tests...")
141+
142+
try:
143+
test_parse_scoped_package_with_version()
144+
test_parse_scoped_package_without_version()
145+
test_parse_regular_package_with_version()
146+
test_parse_regular_package_without_version()
147+
test_parse_scoped_package_with_numeric_version()
148+
test_parse_scoped_package_with_beta_version()
149+
test_get_all_data_includes_version_info()
150+
test_edge_case_multiple_at_symbols()
151+
test_whitespace_handling()
152+
test_api_url_construction()
153+
154+
print("\n🎉 All tests passed!")
155+
return True
156+
except AssertionError as e:
157+
print(f"\n❌ Test failed: {e}")
158+
return False
159+
except Exception as e:
160+
print(f"\n💥 Unexpected error: {e}")
161+
return False
162+
163+
164+
if __name__ == "__main__":
165+
success = run_all_tests()
166+
sys.exit(0 if success else 1)

0 commit comments

Comments
 (0)