package grants import ( "encoding/json" "strings" "testing" "git.novox.be/novox/mesh-sdk/go/seatdef" ) type defsGate struct{ defs map[string]seatdef.Definition } func (defsGate) MayAct(...string) bool { return true } func (g defsGate) Definitions() map[string]seatdef.Definition { return g.defs } type canCall struct { allowed bool asked map[string]any } func (c *canCall) Call(key string, body any) (json.RawMessage, error) { c.asked = body.(map[string]any) return json.Marshal(map[string]any{"allowed": c.allowed}) } // grants_callers and grants_why_refused read the definitions' callers (design 47 G §83): every verb takes its class's // default (§610: no verb names its callers; an act's are the operator, sessions and invokers, a read's invokers, persons // and sessions), and a refused account is told the call subject it lacks and the callers the definition admits. func TestCallersAndWhyRefused(t *testing.T) { defs := map[string]seatdef.Definition{"mesh-wants": {Seat: "mesh-wants", Scope: "mesh", Verbs: []seatdef.Verb{ {Name: "upgrade", Class: seatdef.ClassAct}, {Name: "entry", Class: seatdef.ClassRead}, }}} lines := CallerLines(defs, "mesh-wants", "") if len(lines) != 2 || lines[0].Verb != "entry" || lines[0].Named || lines[1].Named || strings.Join(lines[0].Callers, ",") != "invokers,persons,sessions" || strings.Join(lines[1].Callers, ",") != "operator,sessions,invokers" { t.Fatalf("lines: %+v", lines) } can := &canCall{} p := &Part{SeatList: defsGate{defs}, Call: can} got, err := p.WhyRefused("one.shop", "mesh-wants.upgrade", "") if err != nil { t.Fatal(err) } out := got.(map[string]any) if out["lacks"] != "mesh.seat.mesh-wants.call.upgrade.one~shop" || can.asked["subject"] != out["lacks"] || !strings.Contains(out["says"].(string), "operator, sessions, invokers") { t.Errorf("a refused caller: %+v", out) } can.allowed = true got, _ = p.WhyRefused("one.shop", "mesh-wants.upgrade", "") if _, lacks := got.(map[string]any)["lacks"]; lacks { t.Errorf("an allowed caller lacks a grant: %+v", got) } if _, err := p.WhyRefused("one.shop", "mesh-wants.nothing", ""); err == nil { t.Error("a verb no definition names was explained") } }