Skip to content

Commit f260a25

Browse files
test: add integration tests for agent with guardrails
Add comprehensive integration tests for guardrails at different scopes: - Agent-level guardrails (PII detection) - LLM-level guardrails (Prompt injection) - Tool-level guardrails (Filter, Block, and PII detection) Tests verify that guardrails are properly invoked and block/filter as expected.
1 parent 071863c commit f260a25

File tree

5 files changed

+1823
-0
lines changed

5 files changed

+1823
-0
lines changed

tests/cli/conftest.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
1+
from unittest.mock import patch
2+
13
import pytest
4+
from pydantic import BaseModel
25

36

47
@pytest.fixture
58
def mock_env_vars():
69
return {
710
"UIPATH_URL": "http://example.com",
811
"UIPATH_ACCESS_TOKEN": "***",
12+
"UIPATH_TENANT_ID": "test-tenant-id",
913
}
14+
15+
16+
@pytest.fixture
17+
def mock_guardrails_service():
18+
"""Mock the guardrails service to avoid HTTP errors in tests."""
19+
20+
class MockGuardrailValidationResult(BaseModel):
21+
validation_passed: bool
22+
violations: list[dict[str, object]] = []
23+
reason: str = ""
24+
25+
def mock_evaluate_guardrail(text, guardrail):
26+
"""Mock guardrail evaluation - always passes validation."""
27+
return MockGuardrailValidationResult(validation_passed=True, violations=[])
28+
29+
with patch(
30+
"uipath.platform.guardrails.GuardrailsService.evaluate_guardrail",
31+
side_effect=mock_evaluate_guardrail,
32+
) as mock:
33+
yield mock
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"dependencies": ["."],
3+
"graphs": {
4+
"agent": "./joke_agent_with_guardrails.py:graph"
5+
},
6+
"env": ".env"
7+
}
8+
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
{
2+
"entryPoints": [
3+
{
4+
"filePath": "agent",
5+
"uniqueId": "0afddb15-cecc-4a20-87ef-c1a65a690fcb",
6+
"type": "agent",
7+
"input": {
8+
"type": "object",
9+
"properties": {
10+
"word": {
11+
"type": "string",
12+
"description": "The word to base the joke on"
13+
}
14+
},
15+
"required": ["word"]
16+
},
17+
"output": {
18+
"type": "object",
19+
"properties": {
20+
"joke": {
21+
"type": "string",
22+
"description": "The generated family-friendly joke"
23+
},
24+
"randomName": {
25+
"type": "string",
26+
"description": "A randomly generated name"
27+
},
28+
"analysis": {
29+
"type": "string",
30+
"description": "The analysis result from the SentenceAnalyzer tool"
31+
},
32+
"explanation": {
33+
"type": "string",
34+
"description": "An explanation if a joke couldn't be generated"
35+
}
36+
},
37+
"required": ["joke", "randomName", "analysis"]
38+
}
39+
}
40+
],
41+
"bindings": {
42+
"version": "2.0",
43+
"resources": [],
44+
"guardrails": [
45+
{
46+
"$guardrailType": "custom",
47+
"id": "0dac2299-a8ae-43aa-8703-3eb93c657b2a",
48+
"name": "Guardrail on input for donkey",
49+
"description": "Filters out the word 'donkey' from tool inputs",
50+
"enabledForEvals": true,
51+
"selector": {
52+
"scopes": ["Tool"],
53+
"matchNames": ["Agent _ Sentence Analyzer"]
54+
},
55+
"rules": [
56+
{
57+
"$ruleType": "word",
58+
"fieldSelector": {
59+
"$selectorType": "specific",
60+
"fields": [
61+
{
62+
"path": "sentence",
63+
"source": "input"
64+
}
65+
]
66+
},
67+
"operator": "contains",
68+
"value": "donkey"
69+
}
70+
],
71+
"action": {
72+
"$actionType": "filter",
73+
"fields": [
74+
{
75+
"path": "sentence",
76+
"source": "input"
77+
}
78+
]
79+
}
80+
},
81+
{
82+
"$guardrailType": "builtInValidator",
83+
"id": "3b4d5416-202a-47ab-bba6-89fa8940a5cf",
84+
"name": "PII detection guardrail",
85+
"description": "This validator is designed to detect personally identifiable information",
86+
"validatorType": "pii_detection",
87+
"validatorParameters": [
88+
{
89+
"$parameterType": "enum-list",
90+
"id": "entities",
91+
"value": ["Email", "Address", "Person"]
92+
},
93+
{
94+
"$parameterType": "map-enum",
95+
"id": "entityThresholds",
96+
"value": {
97+
"Email": 0.5,
98+
"Address": 0.5,
99+
"Person": 0.5
100+
}
101+
}
102+
],
103+
"action": {
104+
"$actionType": "block",
105+
"reason": "PII detected"
106+
},
107+
"enabledForEvals": true,
108+
"selector": {
109+
"scopes": ["Agent", "Llm"],
110+
"matchNames": []
111+
}
112+
},
113+
{
114+
"$guardrailType": "builtInValidator",
115+
"id": "255b1220-97f8-4d79-be8e-052a664b2b90",
116+
"name": "Prompt injection guardrail",
117+
"description": "This validator is built to detect malicious attack attempts",
118+
"validatorType": "prompt_injection",
119+
"validatorParameters": [
120+
{
121+
"$parameterType": "number",
122+
"id": "threshold",
123+
"value": 0.5
124+
}
125+
],
126+
"action": {
127+
"$actionType": "block",
128+
"reason": "Prompt Injection detected"
129+
},
130+
"enabledForEvals": true,
131+
"selector": {
132+
"scopes": ["Llm"],
133+
"matchNames": []
134+
}
135+
}
136+
]
137+
}
138+
}
139+

0 commit comments

Comments
 (0)