package store import ( "context" "errors" "path/filepath" "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) } } func TestAServersSecretsGoWhenItNoLongerReadsThem(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() kept := func(name string) bool { _, err := s.StoredSecret(ctx, name) return err == nil } _ = s.SetClusterSecret(ctx, "mcp/jira/X-Api-Key", "k1") _ = s.SetClusterSecret(ctx, "mcp/jira-old/Authorization", "other server's") if err := s.PutMCPServer(ctx, MCPServer{Name: "jira", URL: "https://j/mcp", HeaderRefs: map[string]string{"X-Api-Key": "store:mcp/jira/X-Api-Key"}}); err != nil { t.Fatal(err) } if !kept("mcp/jira/X-Api-Key") { t.Fatal("a secret the server reads was removed") } // Replaced by a bearer token: the key it was sent before goes. _ = s.SetClusterSecret(ctx, "mcp/jira/Authorization", "Bearer t") _ = s.PutMCPServer(ctx, MCPServer{Name: "jira", URL: "https://j/mcp", HeaderRefs: map[string]string{"Authorization": "store:mcp/jira/Authorization"}}) if kept("mcp/jira/X-Api-Key") || !kept("mcp/jira/Authorization") { t.Error("the replaced key is still kept, or the new one is not") } // Removed: its secrets go with it, another server's of a like name stay. _ = s.DeleteMCPServer(ctx, "jira") if kept("mcp/jira/Authorization") || !kept("mcp/jira-old/Authorization") { t.Error("removing jira left its secret, or took jira-old's") } }