-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
107 lines (93 loc) · 2.45 KB
/
Copy pathmain_test.go
File metadata and controls
107 lines (93 loc) · 2.45 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
package main
import (
"context"
"encoding/json"
"strings"
"testing"
"github.com/mark3labs/mcp-go/mcp"
)
func callTool(t *testing.T, args map[string]any) *mcp.CallToolResult {
t.Helper()
req := mcp.CallToolRequest{}
req.Params.Name = "evaluate_policy"
req.Params.Arguments = args
res, err := evaluatePolicy(context.Background(), req)
if err != nil {
t.Fatalf("evaluatePolicy returned non-nil err: %v", err)
}
return res
}
func resultText(t *testing.T, res *mcp.CallToolResult) string {
t.Helper()
if len(res.Content) == 0 {
t.Fatal("empty result content")
}
tc, ok := mcp.AsTextContent(res.Content[0])
if !ok {
t.Fatalf("first content block is not text: %T", res.Content[0])
}
return tc.Text
}
func TestEvaluatePolicy_Allow(t *testing.T) {
res := callTool(t, map[string]any{
"rego": `package example
default allow := false
allow if {
input.role == "admin"
}`,
"query": "data.example.allow",
"input_json": `{"role": "admin"}`,
})
if res.IsError {
t.Fatalf("expected success, got error: %s", resultText(t, res))
}
var rs []map[string]any
if err := json.Unmarshal([]byte(resultText(t, res)), &rs); err != nil {
t.Fatalf("result is not a JSON array: %v", err)
}
if len(rs) == 0 {
t.Fatal("empty result set; expected one binding with allow=true")
}
exprs, ok := rs[0]["expressions"].([]any)
if !ok || len(exprs) == 0 {
t.Fatalf("missing expressions in result: %#v", rs[0])
}
first := exprs[0].(map[string]any)
if first["value"] != true {
t.Fatalf("expected allow=true, got: %#v", first["value"])
}
}
func TestEvaluatePolicy_Deny(t *testing.T) {
res := callTool(t, map[string]any{
"rego": `package example
default allow := false`,
"query": "data.example.allow",
"input_json": `{}`,
})
if res.IsError {
t.Fatalf("expected success, got error: %s", resultText(t, res))
}
if !strings.Contains(resultText(t, res), `"value": false`) {
t.Fatalf("expected allow=false in output: %s", resultText(t, res))
}
}
func TestEvaluatePolicy_BadRego(t *testing.T) {
res := callTool(t, map[string]any{
"rego": `this is not valid rego`,
"query": "data.example.allow",
})
if !res.IsError {
t.Fatal("expected IsError=true for invalid rego")
}
}
func TestEvaluatePolicy_BadInputJSON(t *testing.T) {
res := callTool(t, map[string]any{
"rego": `package example
allow := true`,
"query": "data.example.allow",
"input_json": `{not json}`,
})
if !res.IsError {
t.Fatal("expected IsError=true for malformed input_json")
}
}