package store import ( "context" "errors" "testing" "time" ) // A company tool is granted, never assumed: none anywhere is none, and a // level that names any narrows what the levels above it gave. func TestACompanyToolIsTheCallersOnlyWhereGrantedAllTheWayDown(t *testing.T) { s := openDB(t) ctx := context.Background() project := tree(t, s) _, k := mustKey(t, s, "key1", project) e, err := s.Resolve(ctx, k.ID) if err != nil { t.Fatal(err) } if e.AllowsTool("jira.search") { t.Fatal("a tool nobody granted is allowed") } must(t, s.PutLimits(ctx, Limits{Scope: ScopeTeam, ScopeID: "team1", AllowedTools: []string{"jira.*", "wiki.read"}})) e, _ = s.Resolve(ctx, k.ID) if !e.AllowsTool("jira.search") || !e.AllowsTool("wiki.read") || e.AllowsTool("db.query") || e.AllowsTool("wiki.write") { t.Errorf("the team's grant: %v", e.ToolGrants) } // The key narrows it; it cannot add what the team withheld. must(t, s.PutLimits(ctx, Limits{Scope: ScopeKey, ScopeID: k.ID, AllowedTools: []string{"jira.search", "db.query"}})) e, _ = s.Resolve(ctx, k.ID) if !e.AllowsTool("jira.search") || e.AllowsTool("jira.create") || e.AllowsTool("db.query") { t.Errorf("narrowed by the key: %v", e.ToolGrants) } // Read back as written. l, err := s.LimitsFor(ctx, ScopeKey, k.ID) if err != nil || len(l.AllowedTools) != 2 { t.Errorf("limits %+v %v", l, err) } all, _ := s.AllLimits(ctx) if got := all["team:team1"].AllowedTools; len(got) != 2 { t.Errorf("AllLimits: %v", got) } } func TestACompanyMCPServerIsKeptReplacedAndRemoved(t *testing.T) { s := openDB(t) ctx := context.Background() m := MCPServer{Name: "jira", URL: "https://jira.internal/mcp", Description: "tickets", Headers: map[string]string{"X-Org": "acme"}, HeaderRefs: map[string]string{"Authorization": "store:mcp/jira/Authorization"}, Enabled: true, Timeout: 30 * time.Second, CreatedBy: "ilker"} must(t, s.PutMCPServer(ctx, m)) got, err := s.MCPServer(ctx, "jira") if err != nil || got.URL != m.URL || got.Headers["X-Org"] != "acme" || got.HeaderRefs["Authorization"] == "" || !got.Enabled || got.Timeout != 30*time.Second || got.CreatedBy != "ilker" { t.Fatalf("%+v %v", got, err) } m.Enabled, m.URL = false, "https://jira2.internal/mcp" must(t, s.PutMCPServer(ctx, m)) list, _ := s.MCPServers(ctx) if len(list) != 1 || list[0].Enabled || list[0].URL != m.URL { t.Errorf("replaced: %+v", list) } must(t, s.DeleteMCPServer(ctx, "jira")) if _, err := s.MCPServer(ctx, "jira"); !errors.Is(err, ErrNotFound) { t.Errorf("after removal: %v", err) } if err := s.DeleteMCPServer(ctx, "jira"); !errors.Is(err, ErrNotFound) { t.Errorf("removing twice: %v", err) } }