package gateway import ( "context" "testing" "github.com/samcharles93/archie-core/internal/domain/access" "github.com/samcharles93/archie-core/internal/tools" ) type allowOnly struct{ action access.Action } func (a allowOnly) Authorize(_ access.Principal, action access.Action, _ access.Resource, _ access.Context) access.Decision { return access.Decision{Allowed: action == a.action} } func TestGuardedToolsRunOnlyForAnAllowedCaller(t *testing.T) { cases := []struct { name string caller bool allow access.Action action access.Action runs bool }{ {"allowed caller", true, access.ActionRead, access.ActionRead, true}, {"denied caller", true, access.ActionRead, access.ActionManageIdentities, false}, {"no caller", false, access.ActionRead, access.ActionRead, false}, } for _, tc := range cases { t.Run(tc.name, func(t *testing.T) { ran := false entry := guarded(allowOnly{tc.allow}, tc.action, access.KindPolicy, nil, tools.ToolEntry{ Name: "probe", Handler: func(context.Context, map[string]any) (any, error) { ran = true; return nil, nil }, }) ctx := context.Background() if tc.caller { ctx = access.WithPrincipal(ctx, access.Principal{IdentityID: "operator"}) } _, err := entry.Handler(ctx, nil) if ran != tc.runs || (err == nil) != tc.runs { t.Fatalf("ran=%v err=%v, want ran=%v", ran, err, tc.runs) } }) } }