Files
llmgw/internal/store/mcp_test.go
T
Ilker ManapandClaude Opus 5.5 b986ea180d Fixes from review: setup's memory check, MCP secrets, VS Code, bandwidth
- server init run again on a machine already set up is not refused its
  memory (the docs fix a certificate with --host so), and judges count
  only when one will be installed (judgeComes: not without a mirror, nor
  from a bundle without it).
- A server's sealed secrets that it no longer reads are removed when it is
  put or deleted; replacing a credential replaces every header it was
  sent, rather than adding one beside the old.
- llmgw.gatewayUrl is a machine setting and restricted in untrusted
  workspaces: a cloned repository's settings cannot send a sign-in to
  another gateway.
- The bandwidth buffer is at least eight times the CPU's last cache and at
  most a quarter of free memory, else nothing is measured until there is
  room; a registration carries the figure measured so far.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-29 08:23:00 +01:00

109 lines
4.0 KiB
Go

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")
}
}