feat: add authztest package for authorization test helpers with failure context - #1758
feat: add authztest package for authorization test helpers with failure context#1758tlyyxjz wants to merge 1 commit into
Conversation
…re context Implements the test helpers proposed in apache#1624 as a new sub-package: AssertAllow/AssertDeny thin wrappers around Enforce, plus Diagnose/Explain near-miss attribution that reports, position by position, why a request was denied (or which policy line allowed an unexpected allow). Role inheritance via g rules is credited rather than falsely reported as a subject mismatch, domain-aware models check links in the policy line's own domain, and wildcard idiom (p.obj == '*') is recognized. Enforce errors are surfaced as errors, not misattributed denies.
|
Ran The header says Why the two disagree. allowed := misses[0].FullyMatched()
The documented safeguard for this isn't implemented. The fix I'd suggest keeps the signature. allowed, explain, err := e.EnforceEx(rvals...)
I'd avoid threading the verdict in as a parameter ( Test. Method note: measured by executing the package at |
Closes #1624 — implements the authorization test helpers proposed there.
What this adds
A new
authztestsub-package with a deliberately small API surface, per the "thin wrappers aroundEnforce" design note:The value is in failure context. When an assertion fails, instead of a bare
false, you get a position-by-position near-miss attribution of the closest policies:There are also two exported escape hatches for people who want the diagnosis without
testing.TBcoupling:Diagnose(e, rvals...)(human-readable string, shown above) andExplain(e, rvals...)(structured[]NearMiss).Design decisions
g = _, _, _models, role links are checked in the policy line's own domain, so multi-tenant failures are unambiguous.p.obj == "*"idiom is recognized; arity mismatches between policy lines and the request definition are surfaced.Enforceerrors are errors. Wrong-arity requests report the error instead of being misattributed as denies.casbincore imports it.Tis a minimal interface (Helper+Errorf), so*testing.T,*testing.B,*testing.Fand custom fakes all work.Targeting the v3 line since that's where
masterdevelopment happens; the API has no v3-specific surface and can be backported to v2 if maintainers prefer. If the intended home for this is a separate repository under the casbin org rather than a sub-package (the open question from the issue thread), the code transfers as-is.Test evidence
ExampleDiagnosein the package, covering: pass/fail paths for both assertions, RBAC role-inheritance attribution, domain-scoped attribution, wildcard idiom, near-miss ranking, arity mismatch, andEnforceerror reporting.go test ./...passes for the whole module;go vetandgofmtclean.