TestBuildExplainContext tests the context building function.
(t *testing.T)
| 213 | |
| 214 | // TestBuildExplainContext tests the context building function. |
| 215 | func 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 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…