Files
llmgw/internal/store/toolgrants_test.go
T
Ilker ManapandClaude Opus 5.5 44451bacd9
CI / test (push) Successful in 10m29s
Granting or taking back a server's tools leaves the rest as it was
A tool is seen where every level above that names tools names it (store
Effective.AllowsTool). The console and /admin/mcp wrote a level's list as
given, so a team that saw its organization's hr.* through having no list
was given jira.* and then saw neither; and revoking a level's last server
left [] in the store, closed to every tool, shown nowhere.

store.GrantTools and RevokeTools now do both screens' work: a level's first
list starts from the nearest list above it; taking a server away leaves the
others, and a level left naming nothing goes back to no list, or stays
closed when a level above names tools, so it opens to nothing it was kept
from. GrantTools says which levels above still keep the granted tools out
of reach, and both screens say so. The page shows closed levels and member
scopes by name, and offers no revoke for a * grant it would not change. The
console calls the store instead of keys limits with a list built from what
it had loaded, which overwrote changes made since. Found in review.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-28 22:11:07 +01:00

101 lines
3.4 KiB
Go

package store
import (
"context"
"path/filepath"
"slices"
"testing"
)
func TestGrantingToolsAtALevelKeepsWhatItSawFromAbove(t *testing.T) {
ctx := context.Background()
s, err := OpenSQLite(ctx, filepath.Join(t.TempDir(), "s.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
org, _ := s.CreateOrg(ctx, "Acme")
team, _ := s.CreateTeam(ctx, org, "eng")
tools := func(scope Scope, id string) []string {
l, _ := s.LimitsFor(ctx, scope, id)
return l.AllowedTools
}
sees := func(tool string) bool {
e := Effective{}
for _, l := range []struct {
sc Scope
id string
}{{ScopeOrg, org}, {ScopeTeam, team}} {
if ts := tools(l.sc, l.id); ts != nil {
e.ToolGrants = append(e.ToolGrants, ts)
}
}
return e.AllowsTool(tool)
}
// The organization grants hr's tools; the team names none and sees them.
if _, err := s.GrantTools(ctx, ScopeOrg, org, "hr", []string{"hr.*"}); err != nil {
t.Fatal(err)
}
if !sees("hr.search") {
t.Fatal("the team does not see what its organization grants")
}
// Granting jira's to the team keeps hr's, and says the organization
// still keeps jira's out of reach.
g, err := s.GrantTools(ctx, ScopeTeam, team, "jira", []string{"jira.*"})
if err != nil {
t.Fatal(err)
}
if !sees("hr.search") {
t.Errorf("granting jira's tools to the team took hr's away: %v", tools(ScopeTeam, team))
}
if len(g.BlockedBy) != 1 || g.BlockedBy[0].ID != org {
t.Errorf("the organization, which does not grant jira's tools, should be named: %+v", g)
}
if sees("jira.search") {
t.Error("the team sees jira's tools its organization does not grant")
}
// The organization grants them too: now the team sees both.
_, _ = s.GrantTools(ctx, ScopeOrg, org, "jira", []string{"jira.*"})
if !sees("jira.search") || !sees("hr.get") {
t.Errorf("org %v team %v", tools(ScopeOrg, org), tools(ScopeTeam, team))
}
// Taking jira's back from the team leaves it hr's.
if err := s.RevokeTools(ctx, ScopeTeam, team, "jira"); err != nil {
t.Fatal(err)
}
if sees("jira.search") || !sees("hr.get") {
t.Errorf("after the revoke: team %v", tools(ScopeTeam, team))
}
}
func TestRevokingALevelsLastToolsNeitherClosesNorOpensIt(t *testing.T) {
ctx := context.Background()
s, err := OpenSQLite(ctx, filepath.Join(t.TempDir(), "s.db"))
if err != nil {
t.Fatal(err)
}
defer s.Close()
org, _ := s.CreateOrg(ctx, "Acme")
team, _ := s.CreateTeam(ctx, org, "eng")
// Nothing above names tools: the team's last grant taken leaves it
// naming none at all, not closed to every tool.
_, _ = s.GrantTools(ctx, ScopeTeam, team, "jira", []string{"jira.*"})
_ = s.RevokeTools(ctx, ScopeTeam, team, "jira")
if l, _ := s.LimitsFor(ctx, ScopeTeam, team); l.AllowedTools != nil {
t.Errorf("left %v; want no list", l.AllowedTools)
}
// The organization grants every tool; the team was kept to jira's.
// Taking jira's away must not open it to everything above.
_ = s.PutLimits(ctx, Limits{Scope: ScopeOrg, ScopeID: org, AllowedTools: []string{"*"}})
_ = s.PutLimits(ctx, Limits{Scope: ScopeTeam, ScopeID: team, AllowedTools: []string{"jira.*"}})
_ = s.RevokeTools(ctx, ScopeTeam, team, "jira")
if l, _ := s.LimitsFor(ctx, ScopeTeam, team); l.AllowedTools == nil || len(l.AllowedTools) != 0 {
t.Errorf("left %v; want an empty list, closed", l.AllowedTools)
}
if !slices.Equal(withoutServer([]string{"a.x", "b.*", "a.*"}, "a"), []string{"b.*"}) {
t.Error("withoutServer")
}
}