A key rotated at its vendor had to be removed and added again, which lost its name, its place in the order, its weight, protocol, models, proxy and concurrency, and any gateway key restricted to it. As decided on #1869, in multi-key mode the row now has a Change key action, with a double-click on the row as a shortcut. - provider.ReplaceKey(id, ref, key) sets the key's new value and moves what providers.json keys by the key's fingerprint (AccountModels, AccountProxies, AccountConcurrency) to the new one. An empty value, an unknown key and a value another key of the provider already holds are refused. The same value is a no-op. - access.ReplaceAccount moves gateway keys' Accounts entries from provider/old to provider/new, under the caller-keys lock. - gateway.Server.UnrestAll lifts a rest key and every per-model rest under it. The GUI's keys/replace lifts the key's rest (provider#key, and the provider's own rest key when it is the only key on), so the new value is asked from the next request. - app.js: a pencil beside the key's copy button opens a password field in place of the masked value. Enter saves, Escape leaves it, and a double-click on the row away from its controls opens it too. The button has its own class (.key-edit) with the copy button's look. - i18n: four strings in zh, zh-TW, ja and de. - providers-accounts.md: ReplaceKey in the Keys table. Past requests keep the old fingerprint in history, under the same name. A rest is lifted only in the gateway this magpie serves. Run: - go test ./internal/gateway ./internal/provider ./internal/access ./internal/gui -count=1: ok. New TestReplaceKeyKeepsItsPlace, TestKeysReplaceKeepsTheKeyAndLiftsItsRest and TestUnrestAllLiftsAKeysModels each fail with their change taken out. - gofmt -l clean; go vet darwin, linux -tags nogui, windows. - key-replace.test.cjs (new): en/zh/zh-TW/ja/de, chromium and webkit, 1100 and 420px. Checks the post, order, note, no sideways scroll, and no scroll on change or on double-click plus Escape. - With key-copy, rest-try-now and key-import: 96/96. - 72 key*/routing*/rest* GUI files at concurrency 6: 863/871. The 8 reds were key-copy, whose button.copy locator matched the pencil; that's why it has its own class now, and key-copy passes. - Under load, about one run in 300 saw the editor body go back to the top after the copy button's double-click, with no script scrolling it. That is #1504 (findings added there). The test checks scroll before that step.
75 lines
2.6 KiB
Go
75 lines
2.6 KiB
Go
package gui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/yetone/magpie/internal/access"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// keys/replace (#1869): a key's new value takes its place, a gateway key
|
|
// held to it is held to the new one, and its rests are lifted: by its old
|
|
// fingerprint, and the provider's own while it was the one key on.
|
|
func TestKeysReplaceKeepsTheKeyAndLiftsItsRest(t *testing.T) {
|
|
sandboxHome(t)
|
|
var lifted []string
|
|
old := liftRests
|
|
liftRests = func(keys []string) { lifted = append(lifted, keys...) }
|
|
t.Cleanup(func() { liftRests = old })
|
|
if err := provider.Save(provider.Provider{ID: "relay", Name: "Relay", Chat: "http://127.0.0.1:1/v1", Key: "sk-a", KeyName: "Personal",
|
|
Keys: []provider.KeyAccount{{Name: "Team", Key: "sk-b"}, {Name: "Off", Key: "sk-c", Off: true}}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
a, b := provider.KeyID("sk-a"), provider.KeyID("sk-b")
|
|
if _, err := access.Update("add-key", access.Change{Name: "ci"}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
ks, _ := access.List()
|
|
if _, err := access.Update("accounts-key", access.Change{Key: ks[0].ID, Accounts: []string{"relay/" + b, "relay/" + a}}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mux := http.NewServeMux()
|
|
providerRoutes(mux, nil)
|
|
post := func(body string) *httptest.ResponseRecorder {
|
|
w := httptest.NewRecorder()
|
|
mux.ServeHTTP(w, httptest.NewRequest("POST", "/api/keys/replace", strings.NewReader(body)))
|
|
return w
|
|
}
|
|
if w := post(`{"id":"relay","ref":"` + b + `","key":"sk-b2"}`); w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
p, _ := provider.Find("relay")
|
|
if p.Key != "sk-a" || len(p.Keys) != 2 || p.Keys[0] != (provider.KeyAccount{Name: "Team", Key: "sk-b2"}) {
|
|
t.Fatalf("keys %q %+v", p.Key, p.Keys)
|
|
}
|
|
b2 := provider.KeyID("sk-b2")
|
|
if ks, _ := access.List(); !slices.Equal(ks[0].Accounts, []string{"relay/" + b2, "relay/" + a}) {
|
|
t.Fatalf("gateway key's accounts %v", ks[0].Accounts)
|
|
}
|
|
if !slices.Equal(lifted, []string{"relay#" + b}) {
|
|
t.Fatalf("lifted %v", lifted)
|
|
}
|
|
|
|
// the one key on: the provider rests by its own id, lifted too
|
|
if err := provider.SetKeyOn("relay", b2, false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
lifted = nil
|
|
if w := post(`{"id":"relay","ref":"` + a + `","key":"sk-a2"}`); w.Code != 200 {
|
|
t.Fatalf("%d %s", w.Code, w.Body)
|
|
}
|
|
if !slices.Equal(lifted, []string{"relay#" + a, "relay"}) {
|
|
t.Fatalf("lifted %v", lifted)
|
|
}
|
|
|
|
// another key's value is refused, and nothing is lifted
|
|
lifted = nil
|
|
if w := post(`{"id":"relay","ref":"` + provider.KeyID("sk-c") + `","key":"sk-a2"}`); w.Code == 200 || lifted != nil {
|
|
t.Fatalf("%d %s, lifted %v", w.Code, w.Body, lifted)
|
|
}
|
|
}
|