-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathtest_tool_factory.py
More file actions
134 lines (117 loc) · 4.95 KB
/
Copy pathtest_tool_factory.py
File metadata and controls
134 lines (117 loc) · 4.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
"""Tests for MCP tool creation from rules."""
import pytest
from codeguard_mcp.rule_processor import ProcessedRule
from codeguard_mcp.tool_factory import RuleToolFactory
class TestToolFactory:
def setup_method(self):
self.factory = RuleToolFactory()
def test_tool_name_uses_underscores(self):
rule = ProcessedRule(
rule_id="codeguard-1-hardcoded-credentials",
description="No hardcoded creds",
always_apply=True,
content="# Rule content",
filename="codeguard-1-hardcoded-credentials.md",
)
tool = self.factory.create_tool(rule)
assert tool.name == "codeguard_1_hardcoded_credentials"
def test_tool_has_description(self):
rule = ProcessedRule(
rule_id="codeguard-0-logging",
description="Logging security",
languages=["python", "java"],
content="# Logging",
filename="codeguard-0-logging.md",
)
tool = self.factory.create_tool(rule)
assert tool.description == "Logging security"
@pytest.mark.asyncio
async def test_tool_returns_rule_content(self):
rule = ProcessedRule(
rule_id="codeguard-1-test",
description="Test rule",
always_apply=True,
content="# Test\nDo the right thing.",
filename="codeguard-1-test.md",
)
tool = self.factory.create_tool(rule)
result = await tool.fn()
assert "codeguard-1-test" in result
assert "Do the right thing." in result
@pytest.mark.asyncio
async def test_tool_returns_language_metadata(self):
rule = ProcessedRule(
rule_id="codeguard-0-logging",
description="Logging security",
languages=["python", "java"],
content="# Logging",
filename="codeguard-0-logging.md",
tags=["logging"],
)
tool = self.factory.create_tool(rule)
result = await tool.fn()
assert "Languages: python, java" in result
assert "Tags: logging" in result
class TestSearchTool:
def setup_method(self):
self.factory = RuleToolFactory()
self.rules = [
ProcessedRule(
rule_id="codeguard-1-hardcoded-credentials",
description="No hardcoded creds",
always_apply=True,
content="# Creds",
filename="codeguard-1-hardcoded-credentials.md",
tags=["secrets"],
),
ProcessedRule(
rule_id="codeguard-0-input-validation-injection",
description="Input validation and injection prevention",
languages=["python", "java", "javascript"],
content="# Injection",
filename="codeguard-0-input-validation-injection.md",
tags=["web", "input-validation"],
),
ProcessedRule(
rule_id="codeguard-0-authentication-mfa",
description="Authentication and MFA best practices",
languages=["python", "java"],
content="# Auth",
filename="codeguard-0-authentication-mfa.md",
tags=["authentication", "web"],
),
]
self.search_tool = self.factory.create_search_tool(self.rules)
@pytest.mark.asyncio
async def test_search_by_language(self):
result = await self.search_tool.fn(language="javascript")
assert "codeguard-0-input-validation-injection" in result
# always_apply rules should also match
assert "codeguard-1-hardcoded-credentials" in result
# python/java only rule should not match
assert "codeguard-0-authentication-mfa" not in result
@pytest.mark.asyncio
async def test_search_by_tag(self):
result = await self.search_tool.fn(tag="secrets")
assert "codeguard-1-hardcoded-credentials" in result
assert "codeguard-0-input-validation-injection" not in result
@pytest.mark.asyncio
async def test_search_by_keyword(self):
result = await self.search_tool.fn(keyword="injection")
assert "codeguard-0-input-validation-injection" in result
assert "codeguard-1-hardcoded-credentials" not in result
@pytest.mark.asyncio
async def test_search_combined_filters(self):
result = await self.search_tool.fn(language="python", tag="web")
assert "codeguard-0-input-validation-injection" in result
assert "codeguard-0-authentication-mfa" in result
# always_apply but no 'web' tag
assert "codeguard-1-hardcoded-credentials" not in result
@pytest.mark.asyncio
async def test_search_no_matches(self):
result = await self.search_tool.fn(tag="nonexistent-tag")
assert "No rules matched" in result
@pytest.mark.asyncio
async def test_search_no_filters_returns_all(self):
result = await self.search_tool.fn()
assert "Found 3 matching rule(s)" in result