Files
mesh-delivery/modules/mesh-grants/internal/grants/callers_test.go
T
jschoubben 6182730923 GR2: grants_callers and grants_why_refused
The first reads the callers each verb and accepted kind of the seat definitions
admits, the second says which call subject an account lacks (the bus's own
nats_user_can) beside the definition's callers.

Builds: design 47 G §83, §391, §395

Claude-Session: https://claude.ai/code/session_01NcbgfGKaxw984XyAPfLm4h
2026-11-12 05:58:25 +01:00

58 lines
2.0 KiB
Go

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): a verb that names none takes its
// class's default, 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", Callers: &seatdef.Callers{List: []string{"seat:mesh-pipeline-runner", "operator"}}},
}}}
lines := CallerLines(defs, "mesh-wants", "")
if len(lines) != 2 || lines[0].Verb != "entry" || !lines[0].Named || lines[1].Named ||
strings.Join(lines[1].Callers, ",") != "operator,sessions" {
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") {
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")
}
}