#1981 refused every quota route to a gateway key held to a budget, models
or accounts, so a Remote magpie set up with such a key showed none of the
providers it may use. A key's owner can now let it be told them:
access.Key.Quotas, set from the key's new "Quotas" badge on the Gateway
page or `magpie gateway-key quotas <id> on|off`. Off stays the default,
so #1981's answer is unchanged for every key until its owner says so.
With it on, /v1/magpie/quotas (and ?kept=1), /quotas/history,
/quotas/cards and POST /quotas/refresh go through scopeQuotas
(internal/gateway/quotas_scope.go):
- a provider is told only when the key may use every model the catalog
lists of it: a key with some of a provider's models is told nothing of
it, since the models it may not use spend the same quota;
- of those, only cards of accounts and keys AllowsAccount lets it use,
matched by the card's User against AccountIDs (a name two accounts
share needs both; a card no account is known by only where the key's
list names none of that provider's); a provider's only key's card,
named by no one, is that key's;
- cards read from another magpie (From) are never told;
- a refresh of a card outside the scope is 403 gateway_key_held, one
naming no card reads again only the scope's cards, not ReadAllCards;
- a scope with no card answers the cards routes 403 gateway_key_held, so
the remote shows the key's own card rather than "nothing read".
/v1/magpie/concurrency and /v1/magpie/usage still refuse a held key.
Run at the reviewed head:
- gofmt -l: clean; go vet ./... (darwin), GOOS=linux -tags nogui,
GOOS=windows: clean.
- go test -tags nogui -timeout 45m ./... in a sandbox HOME, after the
rebase onto 7be68467f (#2227's several limits; conflicts in access,
the CLI usage and test, keys.js and providers-accounts.md resolved by
keeping both): every package ok but internal/gateway, whose
TestTranslatedSilenceStillTimesOut went red once at load ~95 (its
400ms deadline also covers the response headers; filed #2248) and
passed 5 of 5 alone. Before the rebase the whole suite was ok, with
agent and gateway rerun at -timeout 40m after the 10m timeout under
load.
- TestHeldKeyToldItsOwnQuotas (new): a provider-wide key sees A only,
a key with A/m1 sees nothing of A, an account-held key sees only its
key's card, a provider's only key, a key told nothing gets 403 from
the cards route, Quotas-off still 403 everywhere, refreshes outside
the scope 403. Broken by hand, each red: scope returned nil (every
card told), every[id] forced true (partial models), the account check
skipped, the refresh check removed, the only-key rule removed, and the
told-none 403 removed. TestHeldKeyToldNoAccounts (#1981) still green.
- TestRemoteMagpieShowsHeldKeyItsQuotas (new): a magpie with this one as
its Remote magpie provider, through a key held to alpha's Two, shows
office/alpha's Two card beside the key's own; a key told none shows its
own card alone. Red with the told-none 403 removed ("nothing read on
that magpie yet").
- TestGatewayKeyQuotasCLI (new): show, on, off, bad values refused.
- GUI: gateway-key-quotas.test.cjs (new) in chromium and webkit, at 1100
and 420, in en, zh, zh-TW, ja, de: 20/20; red when the badge sends the
value it already has.
- The badge is a gateway key row's: off, it folds to no width until the
row is hovered (the row had no room left at 390px: gateway-connect-keys
went red in en until it did), and on a phone it is shown like the
row's other controls (phone-rows went red until its rule won).
- GUI, 119 files matching gateway|key|usage|quota|remote|i18n|caller|
web-|phone|narrow, both engines, concurrency 2: 1349/1385. The reds
besides phone-rows (since fixed, above): quota-count-wrap and
quota-name-translated webkit (#2228), usage-agents-hide (#2211) and
usage-fold chromium en/zh (#2163), each red alike on a fresh
origin/main. gateway-key-quotas, gateway-connect-keys, phone-rows and
gateway-key-groups rerun: 38/38; after the rebase every gateway-*,
phone-rows and key-limit file, both engines: 210/210.
Not tried: a real Remote magpie against a second machine with a held key;
the GUI badge in the Linux and Windows apps.
440 lines
16 KiB
Go
440 lines
16 KiB
Go
package cli
|
|
|
|
import (
|
|
"bytes"
|
|
"os"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/budget"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
"github.com/yetone/magpie/internal/settings"
|
|
)
|
|
|
|
func TestGatewayKeyCLI(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
call := func(args ...string) string {
|
|
t.Helper()
|
|
var out bytes.Buffer
|
|
if err := gatewayKeysTo(&out, append([]string{"gateway-key"}, args...)); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return strings.TrimSpace(out.String())
|
|
}
|
|
if got := call("list"); !strings.Contains(got, "GATEWAY KEY") {
|
|
t.Fatal(got)
|
|
}
|
|
secret := call("add", "Remote laptop")
|
|
keys, _ := access.List()
|
|
if len(keys) != 1 {
|
|
t.Fatal(keys)
|
|
}
|
|
id := keys[0].ID
|
|
if who, ok := access.Authenticate(secret); !ok || who.KeyID != id || who.KeyName != "Remote laptop" {
|
|
t.Fatal(who, ok)
|
|
}
|
|
if got := call(); strings.Contains(got, secret) || !strings.Contains(got, id) || !strings.Contains(got, "Remote laptop") {
|
|
t.Fatal("list must mask credentials", got)
|
|
}
|
|
next := call("rotate", id)
|
|
if next == secret {
|
|
t.Fatal("rotation reused the secret")
|
|
}
|
|
if _, ok := access.Authenticate(secret); ok {
|
|
t.Fatal("old credential works after rotation")
|
|
}
|
|
if who, ok := access.Authenticate(next); !ok || who.KeyID != id {
|
|
t.Fatal("rotation changed identity", who, ok)
|
|
}
|
|
call("remove", id)
|
|
if _, ok := access.Authenticate(next); ok {
|
|
t.Fatal("removed credential still works")
|
|
}
|
|
for _, args := range [][]string{{"add"}, {"add", ""}, {"list", "extra"}, {"rotate", "missing"}, {"remove", "missing"}, {"unknown", "x"}} {
|
|
if err := gatewayKeysTo(&bytes.Buffer{}, append([]string{"gateway-key"}, args...)); err == nil {
|
|
t.Error("accepted", args)
|
|
}
|
|
}
|
|
}
|
|
|
|
// add --key keeps a value clients already send (love1sbug on X); "-"
|
|
// takes it from stdin.
|
|
func TestGatewayKeyCLIOwnValue(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
var out bytes.Buffer
|
|
if err := gatewayKeysTo(&out, []string{"gateway-key", "add", "Family", "--key", "cpa-family-key-0042"}); err != nil || strings.TrimSpace(out.String()) != "cpa-family-key-0042" {
|
|
t.Fatal(out.String(), err)
|
|
}
|
|
r, w, err := os.Pipe()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w.WriteString("cpa-friend-key-0007\n")
|
|
w.Close()
|
|
stdin := os.Stdin
|
|
os.Stdin = r
|
|
defer func() { os.Stdin = stdin }()
|
|
if err := gatewayKeysTo(&bytes.Buffer{}, []string{"gateway-key", "add", "Friend", "--key", "-"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, k := range []string{"cpa-family-key-0042", "cpa-friend-key-0007"} {
|
|
if _, ok := access.Authenticate(k); !ok {
|
|
t.Fatal("not a key:", k)
|
|
}
|
|
}
|
|
out.Reset()
|
|
if err := gatewayKeysTo(&out, []string{"gateway-key", "list"}); err != nil || strings.Contains(out.String(), "cpa-family") || !strings.Contains(out.String(), "…0042") {
|
|
t.Fatal("list must mask an own value", out.String(), err)
|
|
}
|
|
for _, args := range [][]string{{"add", "X", "--key"}, {"add", "X", "--key", "short"}, {"add", "X", "--key", "cpa-family-key-0042"}, {"rotate", "x", "--key", "cpa-other-key-0001"}} {
|
|
if err := gatewayKeysTo(&bytes.Buffer{}, append([]string{"gateway-key"}, args...)); err == nil {
|
|
t.Error("accepted", args)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestGatewayKeyListWithReadOnlyMigration(t *testing.T) {
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
s := settings.Settings{LAN: true, LANKey: "sk-magpie-fixture-cli"}
|
|
if err := settings.Save(s); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.Chmod(settings.Path(), 0o400); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { os.Chmod(settings.Path(), 0o600) })
|
|
if err := settings.Save(s); err == nil {
|
|
t.Skip("settings.json remains writable")
|
|
}
|
|
var out bytes.Buffer
|
|
if err := gatewayKeysTo(&out, []string{"gateway-key", "list"}); err != nil || !strings.Contains(out.String(), "Magpie") {
|
|
t.Fatal("read-only migration blocked CLI list", out.String(), err)
|
|
}
|
|
}
|
|
|
|
// magpie gateway-key limit sets, shows and takes off a key's limit, and
|
|
// list says what each key has used of its own (#585).
|
|
func TestGatewayKeyLimitCLI(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
call := func(args ...string) (string, error) {
|
|
var out bytes.Buffer
|
|
err := gatewayKeysTo(&out, append([]string{"gateway-key"}, args...))
|
|
return out.String(), err
|
|
}
|
|
if _, err := call("add", "Phone"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, _ := access.List()
|
|
id := keys[0].ID
|
|
if got, err := call("limit", id); err != nil || !strings.Contains(got, "no limit") {
|
|
t.Fatal(got, err)
|
|
}
|
|
got, err := call("limit", id, "week", "--tokens", "2m", "--cost=5", "--cache-reads")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, _ = access.List()
|
|
if l := keys[0].Limit; l == nil || l.Period != "week" || l.Tokens != 2_000_000 || l.Cost != 5 || !l.CacheReads {
|
|
t.Fatalf("limit kept as %+v", keys[0].Limit)
|
|
}
|
|
for _, want := range []string{"0 used of 2000000, 2000000 left", "cache reads", "$0.00 used of $5.00", "State", "open"} {
|
|
if !strings.Contains(got, want) {
|
|
t.Fatalf("limit shows %q, without %q", got, want)
|
|
}
|
|
}
|
|
if got, _ := call("list"); !strings.Contains(got, "LIMIT") || !strings.Contains(got, "0/2000000 tokens $0.00/$5.00 per week") {
|
|
t.Fatal(got)
|
|
}
|
|
for _, args := range [][]string{{"limit"}, {"limit", id, "hour", "--tokens", "5"}, {"limit", id, "day"}, {"limit", id, "day", "--tokens", "lots"},
|
|
{"limit", id, "day", "--cost"}, {"limit", id, "day", "--what"}, {"limit", "missing", "day", "--tokens", "5"}, {"limit", id, "off", "extra"},
|
|
{"limit", id, "0d", "--tokens", "5"}, {"limit", id, "3651d", "--tokens", "5"}, {"limit", id, "xd", "--tokens", "5"}, {"limit", id, "days", "--tokens", "5"}, {"limit", id, "reset", "extra"}} {
|
|
if _, err := call(args...); err == nil {
|
|
t.Error("accepted", args)
|
|
}
|
|
}
|
|
// every 10 days (#1509), and Reset
|
|
if got, err = call("limit", id, "10d", "--cost", "800"); err != nil || !strings.Contains(got, "this 10-day cycle") {
|
|
t.Fatal(got, err)
|
|
}
|
|
keys, _ = access.List()
|
|
if l := keys[0].Limit; l == nil || l.Period != "days" || l.Days != 10 || l.Cost != 800 || l.Since.IsZero() {
|
|
t.Fatalf("limit kept as %+v", keys[0].Limit)
|
|
}
|
|
if got, _ := call("list"); !strings.Contains(got, "$0.00/$800.00 every 10 days") {
|
|
t.Fatal(got)
|
|
}
|
|
since := keys[0].Limit.Since
|
|
if got, err = call("limit", id, "reset"); err != nil || !strings.Contains(got, "this 10-day cycle") {
|
|
t.Fatal(got, err)
|
|
}
|
|
if keys, _ = access.List(); keys[0].Limit.Since.Before(since) || keys[0].Limit.Cost != 800 {
|
|
t.Fatalf("reset kept %+v", keys[0].Limit)
|
|
}
|
|
// every 5 hours (#2102): a new cycle from now, then Reset starts another
|
|
if got, err = call("limit", id, "5h", "--tokens", "200k"); err != nil || !strings.Contains(got, "this 5-hour cycle") {
|
|
t.Fatal(got, err)
|
|
}
|
|
keys, _ = access.List()
|
|
if l := keys[0].Limit; l == nil || l.Period != "hours" || l.Hours != 5 || l.Days != 0 || l.Tokens != 200_000 || l.Since.IsZero() || l.Since.Before(since) {
|
|
t.Fatalf("limit kept as %+v", keys[0].Limit)
|
|
}
|
|
if st := budget.Of(keys[0], keys[0].Limit.Since.Add(time.Minute)); st == nil || st.Reset.Sub(st.Start) != 5*time.Hour || st.Hours != 5 {
|
|
t.Fatalf("a 5-hour key's window: %+v", st)
|
|
}
|
|
if got, _ := call("list"); !strings.Contains(got, "0/200000 tokens every 5 hours") {
|
|
t.Fatal(got)
|
|
}
|
|
if got, err = call("limit", id, "reset"); err != nil || !strings.Contains(got, "this 5-hour cycle") {
|
|
t.Fatal(got, err)
|
|
}
|
|
for _, args := range [][]string{{"limit", id, "0h", "--tokens", "5"}, {"limit", id, "8761h", "--tokens", "5"}, {"limit", id, "xh", "--tokens", "5"},
|
|
{"limit", id, "1.5h", "--tokens", "5"}, {"limit", id, "hours", "--tokens", "5"}} {
|
|
if _, err := call(args...); err == nil {
|
|
t.Error("accepted", args)
|
|
}
|
|
}
|
|
if got, err := call("limit", id, "off"); err != nil || !strings.Contains(got, "no limit") {
|
|
t.Fatal(got, err)
|
|
}
|
|
if keys, _ = access.List(); keys[0].Limit != nil {
|
|
t.Fatal("off kept", keys[0].Limit)
|
|
}
|
|
if _, err := call("limit", id, "reset"); err == nil {
|
|
t.Fatal("reset a key with no limit")
|
|
}
|
|
}
|
|
|
|
func TestGatewayKeyModelsCLI(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
call := func(args ...string) (string, error) {
|
|
var out bytes.Buffer
|
|
err := gatewayKeysTo(&out, append([]string{"gateway-key"}, args...))
|
|
return out.String(), err
|
|
}
|
|
if _, err := call("add", "Phone"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, _ := access.List()
|
|
id := keys[0].ID
|
|
if got, err := call("models", id); err != nil || got != "Phone: every model\n" {
|
|
t.Fatal(got, err)
|
|
}
|
|
if got, err := call("models", id, "openai/gpt-5", "anthropic/*"); err != nil || got != "Phone: only openai/gpt-5, anthropic/*\n" {
|
|
t.Fatal(got, err)
|
|
}
|
|
if keys, _ = access.List(); !slices.Equal(keys[0].Models, []string{"openai/gpt-5", "anthropic/*"}) {
|
|
t.Fatal("kept", keys[0].Models)
|
|
}
|
|
if got, _ := call("list"); !strings.Contains(got, "MODELS") || !strings.Contains(got, "openai/gpt-5,anthropic/*") {
|
|
t.Fatal(got)
|
|
}
|
|
for _, args := range [][]string{{"models"}, {"models", id, "gpt-5"}, {"models", "missing", "a/b"}, {"models", "missing"}} {
|
|
if _, err := call(args...); err == nil {
|
|
t.Error("accepted", args)
|
|
}
|
|
}
|
|
if got, err := call("models", id, "all"); err != nil || got != "Phone: every model\n" {
|
|
t.Fatal(got, err)
|
|
}
|
|
if keys, _ = access.List(); keys[0].Models != nil {
|
|
t.Fatal("all kept", keys[0].Models)
|
|
}
|
|
}
|
|
|
|
// magpie gateway-key accounts sets, shows and takes off a key's account
|
|
// whitelist, and refuses an account no provider has (#905). An account's
|
|
// stable id, named by who is signed in, is exercised against a real
|
|
// sign-in in the gateway's tests; a key's fingerprint is one here.
|
|
func TestGatewayKeyAccountsCLI(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: "http://127.0.0.1:1/v1", Keys: []provider.KeyAccount{{Key: "sk-1", Name: "First"}, {Key: "sk-2", Name: "Second"}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
k1, k2 := provider.KeyID("sk-1"), provider.KeyID("sk-2")
|
|
call := func(args ...string) (string, error) {
|
|
var out bytes.Buffer
|
|
err := gatewayKeysTo(&out, append([]string{"gateway-key"}, args...))
|
|
return out.String(), err
|
|
}
|
|
if _, err := call("add", "Phone"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, _ := access.List()
|
|
id := keys[0].ID
|
|
if got, err := call("accounts", id); err != nil || got != "Phone: every account\n" {
|
|
t.Fatal(got, err)
|
|
}
|
|
// named by the name it is shown by, kept by its fingerprint
|
|
if got, err := call("accounts", id, "relay/First", "relay/"+k2); err != nil || got != "Phone: only relay/First, relay/Second\n" {
|
|
t.Fatal(got, err)
|
|
}
|
|
if keys, _ = access.List(); !slices.Equal(keys[0].Accounts, []string{"relay/" + k1, "relay/" + k2}) {
|
|
t.Fatal("kept", keys[0].Accounts)
|
|
}
|
|
if got, _ := call("list"); !strings.Contains(got, "ACCOUNTS") || !strings.Contains(got, "relay/First,relay/Second") {
|
|
t.Fatal(got)
|
|
}
|
|
for _, args := range [][]string{{"accounts"}, {"accounts", id, "sk-1"}, {"accounts", id, "relay/" + provider.KeyID("sk-9")}, {"accounts", "missing", "relay/First"}, {"accounts", "missing"}} {
|
|
if _, err := call(args...); err == nil {
|
|
t.Error("accepted", args)
|
|
}
|
|
}
|
|
if got, err := call("accounts", id, "all"); err != nil || got != "Phone: every account\n" {
|
|
t.Fatal(got, err)
|
|
}
|
|
if keys, _ = access.List(); keys[0].Accounts != nil {
|
|
t.Fatal("all kept accounts", keys[0].Accounts)
|
|
}
|
|
}
|
|
|
|
// magpie gateway-key limit adds, lists, changes and removes a key's
|
|
// several limits (#2227); the single-limit form still sets an only limit.
|
|
func TestGatewayKeySeveralLimitsCLI(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
call := func(args ...string) (string, error) {
|
|
var out bytes.Buffer
|
|
err := gatewayKeysTo(&out, append([]string{"gateway-key"}, args...))
|
|
return out.String(), err
|
|
}
|
|
if _, err := call("add", "Team"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, _ := access.List()
|
|
id := keys[0].ID
|
|
limits := func() []*access.Limit {
|
|
keys, _ := access.List()
|
|
return keys[0].Limits()
|
|
}
|
|
if _, err := call("limit", id, "add", "5h", "--cost", "10"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, err := call("limit", id, "add", "week", "--cost", "50")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, want := range []string{"Limit 1", "every 5 hours", "Limit 2", "per week", "this 5-hour cycle", "this week", "$10.00", "$50.00"} {
|
|
if !strings.Contains(got, want) {
|
|
t.Fatalf("two limits show %q, without %q", got, want)
|
|
}
|
|
}
|
|
if list, err := call("limit", id, "list"); err != nil || list != got {
|
|
t.Fatal("list:", list, err)
|
|
}
|
|
// the plain form changes the one with that window
|
|
if _, err := call("limit", id, "week", "--cost", "60"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ls := limits(); len(ls) != 2 || ls[1].Cost != 60 || ls[0].Cost != 10 {
|
|
t.Fatalf("after changing the week: %+v %+v", ls[0], ls[1])
|
|
}
|
|
for _, args := range [][]string{{"limit", id, "day", "--tokens", "5"}, {"limit", id, "add", "week", "--tokens", "5"}, {"limit", id, "remove", "3"},
|
|
{"limit", id, "remove"}, {"limit", id, "remove", "x"}, {"limit", id, "reset", "0"}, {"limit", id, "list", "extra"}, {"limit", id, "add"}} {
|
|
if _, err := call(args...); err == nil {
|
|
t.Fatal("accepted", args)
|
|
}
|
|
}
|
|
if ls := limits(); len(ls) != 2 {
|
|
t.Fatalf("a refused command left %d limits", len(ls))
|
|
}
|
|
if _, err := call("limit", id, "reset", "2"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := call("limit", id, "remove", "1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ls := limits(); len(ls) != 1 || ls[0].Period != "week" {
|
|
t.Fatalf("after removing the first: %+v", ls)
|
|
}
|
|
if got, err := call("limit", id); err != nil || strings.Contains(got, "Limit 1") || !strings.Contains(got, "this week") {
|
|
t.Fatal("one limit shows", got, err)
|
|
}
|
|
// the single form, with one limit, replaces it as before
|
|
if _, err := call("limit", id, "day", "--tokens", "5"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if ls := limits(); len(ls) != 1 || ls[0].Period != "day" {
|
|
t.Fatalf("the single form: %+v", ls)
|
|
}
|
|
if _, err := call("limit", id, "remove", "1"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got, err := call("limit", id); err != nil || !strings.Contains(got, "no limit") {
|
|
t.Fatal(got, err)
|
|
}
|
|
}
|
|
|
|
// #2216: whether a key held to a budget, models or accounts is told the
|
|
// quotas of what it may use; off, as every key was (#1981), it is told none.
|
|
func TestGatewayKeyQuotasCLI(t *testing.T) {
|
|
t.Setenv("HOME", t.TempDir())
|
|
t.Setenv("USERPROFILE", os.Getenv("HOME"))
|
|
t.Setenv("XDG_CONFIG_HOME", t.TempDir())
|
|
if err := settings.Save(settings.Settings{LAN: true}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
call := func(args ...string) (string, error) {
|
|
var out bytes.Buffer
|
|
err := gatewayKeysTo(&out, append([]string{"gateway-key"}, args...))
|
|
return out.String(), err
|
|
}
|
|
if _, err := call("add", "Phone"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
keys, _ := access.List()
|
|
id := keys[0].ID
|
|
off, on := "Phone: told no quotas when held to a budget, models or accounts\n", "Phone: told the quotas of the providers and accounts it may use\n"
|
|
if got, err := call("quotas", id); err != nil || got != off {
|
|
t.Fatal(got, err)
|
|
}
|
|
if got, err := call("quotas", id, "on"); err != nil || got != on {
|
|
t.Fatal(got, err)
|
|
}
|
|
if keys, _ = access.List(); !keys[0].Quotas {
|
|
t.Fatal("not kept on")
|
|
}
|
|
for _, args := range [][]string{{"quotas"}, {"quotas", id, "yes"}, {"quotas", id, "on", "off"}, {"quotas", "missing", "on"}, {"quotas", "missing"}} {
|
|
if _, err := call(args...); err == nil {
|
|
t.Error("accepted", args)
|
|
}
|
|
}
|
|
if keys, _ = access.List(); !keys[0].Quotas {
|
|
t.Fatal("a refused call turned it off")
|
|
}
|
|
if got, err := call("quotas", id, "off"); err != nil || got != off {
|
|
t.Fatal(got, err)
|
|
}
|
|
if keys, _ = access.List(); keys[0].Quotas {
|
|
t.Fatal("not kept off")
|
|
}
|
|
}
|