MCPcopy Index your code
hub / github.com/apache/casbin / TestBuildExplainContext

Function TestBuildExplainContext

ai_api_test.go:215–250  ·  view source on GitHub ↗

TestBuildExplainContext tests the context building function.

(t *testing.T)

Source from the content-addressed store, hash-verified

213
214// TestBuildExplainContext tests the context building function.
215func TestBuildExplainContext(t *testing.T) {
216 e, err := NewEnforcer("examples/basic_model.conf", "examples/basic_policy.csv")
217 if err != nil {
218 t.Fatal(err)
219 }
220
221 // Test with matched rules
222 rvals := []interface{}{"alice", "data1", "read"}
223 result := true
224 matchedRules := []string{"alice, data1, read"}
225
226 context := e.buildExplainContext(rvals, result, matchedRules)
227
228 // Verify context contains expected elements
229 if !strings.Contains(context, "alice") {
230 t.Error("Context should contain subject 'alice'")
231 }
232 if !strings.Contains(context, "data1") {
233 t.Error("Context should contain object 'data1'")
234 }
235 if !strings.Contains(context, "read") {
236 t.Error("Context should contain action 'read'")
237 }
238 if !strings.Contains(context, "true") {
239 t.Error("Context should contain result 'true'")
240 }
241 if !strings.Contains(context, "alice, data1, read") {
242 t.Error("Context should contain matched rule")
243 }
244
245 // Test with no matched rules
246 context2 := e.buildExplainContext(rvals, false, []string{})
247 if !strings.Contains(context2, "No policy rules matched") {
248 t.Error("Context should indicate no matched rules")
249 }
250}

Callers

nothing calls this directly

Calls 3

buildExplainContextMethod · 0.95
NewEnforcerFunction · 0.85
ErrorMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…