Files
magpie/internal/access/legacy_fallback_test.go
T
yetone 46f03154ff gateway: a gateway key can be held to some models (#882, LCorleone) — a key could reach every model magpie serves. A key now keeps an optional list of models (caller-keys.json "models", none = every model, so older files read as before): <provider>/<model> or <provider>/*, matched against the provider that serves the call, so a bare model name is resolved first and OpenRouter's "anthropic/x" is not Anthropic's. Such a key sees only its models in /v1/models, /v1/models/{id} and the Anthropic (Claude Desktop) and Gemini lists, a routing group only when every member is its; asking for another, by id, bare name or through a group with a member it may not use, is refused before any provider is asked with a 403 in the API's own error shape (permission_error) that names the key and its models, logged as a rejected call like an over-limit key's 429; a fallback it may not use is skipped; embeddings, images and videos it names are held too. magpie's own calls for it (a web search, a picture described, Codex titles with the model Settings name) are marked in their context, not by a User-Agent a client could send, and go on. Set it with the new badge on the key's row in the Gateway page (the app's menu: All models, every model of a provider, each model; sent when the menu closes) or magpie gateway-key models <id> openai/gpt-5 anthropic/* (all takes it off); gateway-key list has a MODELS column. The app's menu, fitting neither below nor above its button, now goes on the roomier side and scrolls instead of running off the window. en/zh/ja/de. TestGatewayKeyModelsHoldAKey (lists, 403s by id, bare name and group, Anthropic shape, fallback skipped, magpie's own call, a free key) fails without the change; TestModelPatterns, TestKeyModelsKept, TestGatewayKeyModelsCLI, TestCallerKeyModelsRoutes pass; gateway-key-models.test.cjs (Chromium and WebKit, en and zh: no native select, menu inside a 420px window, picks sent on close, All models, no scroll, no left border, 560px fits) passes with gateway-connect-keys, gateway-key-limit, gateway-caller-keys and click-scroll. go vet, go build (darwin, GOOS=linux nogui, GOOS=windows) and go test -tags nogui ./... under a temp HOME pass.
2026-10-05 16:52:23 +08:00

103 lines
3.3 KiB
Go

package access
import (
"errors"
"os"
"reflect"
"testing"
"github.com/yetone/magpie/internal/settings"
)
func TestLegacyLANFallbackBeforeMigration(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
s := settings.Settings{LAN: true, LANKey: "sk-magpie-old-client"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
before, ok := Authenticate(s.LANKey)
if !ok || before.KeyID == "" || before.KeyName != "Magpie" {
t.Fatal("unmigrated legacy key lost access", before, ok)
}
if _, err := os.Stat(Path()); !errors.Is(err, os.ErrNotExist) {
t.Fatal("authentication must not create a key store", err)
}
for _, secret := range []string{"", "wrong", "magpie", s.LANKey + "x"} {
if _, ok := Authenticate(secret); ok {
t.Fatal("legacy fallback accepted another token")
}
}
if err := MigrateLegacyLANKey(); err != nil {
t.Fatal(err)
}
if after, ok := Authenticate(s.LANKey); !ok || !reflect.DeepEqual(after, before) {
t.Fatal("migration split the legacy caller's usage identity", before, after, ok)
}
}
func TestLegacyLANFallbackDoesNotBypassRevocation(t *testing.T) {
for _, tc := range []struct {
name string
cfg settings.Settings
keys []Key
}{
{"not-shared", settings.Settings{LANKey: "legacy"}, nil},
{"completed", settings.Settings{LAN: true, LANKey: "legacy", LANKeyID: "removed"}, nil},
{"revoked-mirror", settings.Settings{LAN: true, LANKey: revokedLANPrefix + "old"}, nil},
{"disabled", settings.Settings{LAN: true, LANKey: "legacy"}, []Key{{ID: "default", LAN: true, Secret: "legacy", Off: true}}},
{"rotated", settings.Settings{LAN: true, LANKey: "legacy"}, []Key{{ID: "default", LAN: true, Secret: "new"}}},
{"disabled-matching-key", settings.Settings{LAN: true, LANKey: "legacy"}, []Key{{ID: "other", Secret: "legacy", Off: true}}},
} {
t.Run(tc.name, func(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
if err := settings.Save(tc.cfg); err != nil {
t.Fatal(err)
}
if tc.keys != nil {
if err := Restore(tc.keys); err != nil {
t.Fatal(err)
}
}
if _, ok := Authenticate(tc.cfg.LANKey); ok {
t.Fatal("fallback accepted a revoked or inactive legacy credential")
}
})
}
}
func TestLegacyLANFallbackWithReadOnlyDirectory(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
s := settings.Settings{LAN: true, LANKey: "sk-magpie-readonly"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if err := os.Chmod(settings.Dir(), 0o500); err != nil {
t.Fatal(err)
}
t.Cleanup(func() { os.Chmod(settings.Dir(), 0o700) })
if err := MigrateLegacyLANKey(); err == nil {
t.Skip("configuration directory remains writable")
} else if !errors.Is(err, os.ErrPermission) {
t.Fatal(err)
}
for range 3 {
if who, ok := Authenticate(s.LANKey); !ok || who.KeyID == "" || who.KeyName != "Magpie" {
t.Fatal("failed key-store creation broke legacy access", who, ok)
}
}
}
func TestLegacyLANFallbackRefusesCorruptStore(t *testing.T) {
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
s := settings.Settings{LAN: true, LANKey: "legacy"}
if err := settings.Save(s); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(Path(), []byte("broken"), 0o600); err != nil {
t.Fatal(err)
}
if _, ok := Authenticate(s.LANKey); ok {
t.Fatal("an unreadable revocation state must not enable legacy fallback")
}
}