Files
magpie/internal/provider/factory_key_test.go
T
yetone bb5e8a2b61 factory: the Gateway page says when an older magpie serves the gateway, and a Factory account can be added by its API key (#506, 黄瑞 and aohun: Factory 403 "Factory refused this account the request; check that droid… sign in to it again" on v0.1.630; aohun: support FACTORY_API_KEY as droid2api does) — the 403's wording quoted in #506 was removed in 5523eb17 (first in v0.1.551; git grep finds it in v0.1.550, not in v0.1.630), so those requests were served by a magpie at v0.1.550 or older still holding :3425 (a magpie serve, a second copy, one that didn't quit): serveGateway leaves the port to any magpie answering GET / and the page said only "running · served by another magpie", so none of the later Factory fixes (droid's opening line on /api/llm/o in v0.1.630 among them) reached their requests. gateway.ServedBy reads the serving magpie's version from its GET / answer (Serving wraps it); the providers state carries it with older when it is below this one's, the Gateway card then says "running · served by magpie <v>, older than this one" with an amber dot and that agents' requests go through it without this version's fixes until it quits, when this one takes over within 15 s (zh too, wrapped, never cut off), and the log says so. A Factory API key (fk-…) is added from the import box (Factory's accounts list "Add an account by API key…", and "Use an API key instead…" on the warning before a sign-in): as droid's om() takes FACTORY_API_KEY, the key is the bearer with droid's headers, never renewed, and sends no X-Factory-Org-Id (droid's active org comes from a sign-in, st() is null for a key); whoami asked with the key says whose it is and its region/host, as droid's yP checks a key ("invalid-api-key" when refused), and a refused key isn't added. factoryFresh hands a key account on as it is (no WorkOS, no whoami for an org) and factoryMendOrg leaves it alone; when Factory has moved to its plugin the key isn't offered, the built-in being what keeps it. No request was made to Factory, no real key or account was tried, and whether Factory serves a key's requests from agents other than droid is not known. TestGatewayServedByAnOlderMagpie (fake magpie answering 0.1.550/0.1.630/0.1.640/no version), TestFactoryAPIKey (fake Factory: import, bearer and headers, no org, no WorkOS, refused key, "exists" again; fails with a 403 without the key handling in factoryFresh) and gateway-older.test.cjs, factory-key.test.cjs (Chromium and WebKit, en and zh) pass; login-import, factory-sub, plugin-moved-signin, gateway-fold pass in WebKit (Chromium's channel launch lacks ICU here). go vet ./internal/..., go build -tags nogui (darwin, GOOS=linux), GOOS=windows go build, go test -tags nogui -count=1 ./... under a temp HOME pass.
2026-10-02 14:58:49 +08:00

88 lines
3.1 KiB
Go

package provider
import (
"context"
"io"
"net/http"
"strings"
"sync"
"testing"
)
// #506: a Factory account added by its API key, as droid takes
// FACTORY_API_KEY: whoami asked with the key says whose it is; the account's
// requests carry the key as the bearer with droid's headers and no active
// org (droid's comes from a sign-in), and nothing is renewed with WorkOS. A
// key whoami refuses isn't added; the same key again is "exists".
func TestFactoryAPIKey(t *testing.T) {
signIn(t)
const good, bad = "fk-good_0123456789abcdef", "fk-bad_0123456789abcdef"
var mu sync.Mutex
var sent []http.Header
factorySite(t, func(w http.ResponseWriter, r *http.Request) {
auth := r.Header.Get("Authorization")
switch {
case strings.HasPrefix(r.URL.Path, "/wos/"):
t.Errorf("WorkOS asked for a key account: %s", r.URL.Path)
w.WriteHeader(400)
case r.URL.Path == "/api/cli/whoami":
if auth != "Bearer "+good {
factoryJSON(w, 401, map[string]any{"detail": "Invalid API key"})
return
}
factoryJSON(w, 200, map[string]any{"userId": "user_k", "orgId": "fac_K", "email": "key@example.com"})
case r.URL.Path == "/api/llm/a/v1/messages":
mu.Lock()
sent = append(sent, r.Header.Clone())
mu.Unlock()
// a key's org isn't one droid sends: the header refused, as
// Factory refuses an org the caller didn't pick
if auth != "Bearer "+good || r.Header.Get("X-Factory-Org-Id") != "" {
w.WriteHeader(403)
io.WriteString(w, `{"detail":"Forbidden"}`)
return
}
io.WriteString(w, `{"type":"message"}`)
default:
t.Errorf("unexpected %s %s", r.Method, r.URL.Path)
w.WriteHeader(404)
}
})
res, err := ImportFactoryKeys(context.Background(), []string{"FACTORY_API_KEY=" + good + "\n" + bad + "\n" + good})
if err != nil {
t.Fatal(err)
}
if len(res) != 2 || res[0].User != "key@example.com" || res[0].Status != "added" || res[1].Status != "failed" || strings.Contains(res[1].User, "0123456789") {
t.Fatalf("imported %+v", res)
}
if c := factoryKept(t, "key@example.com"); !c.Key || c.Access != good || c.Refresh != "" || c.Active != "" {
t.Fatalf("kept %+v", c)
}
p, ok := find(Accounts(), "factory")
if !ok || p.Account.User != "key@example.com" {
t.Fatalf("account: %v %+v", ok, p.Account)
}
if code, b := factorySend(t, p); code != 200 {
t.Fatalf("a key account's request: %d %s", code, b)
}
mu.Lock()
h := sent[len(sent)-1]
mu.Unlock()
if h.Get("Authorization") != "Bearer "+good || h.Get("X-Factory-Client") != "cli" || h.Get("User-Agent") != "factory-cli/"+factoryVersion || h.Get("X-Factory-Org-Id") != "" {
t.Fatalf("sent %v", h)
}
// still no org after the request, and no whoami asked for one
if c := factoryKept(t, "key@example.com"); c.Active != "" {
t.Fatalf("a key account got an org: %q", c.Active)
}
res, err = ImportFactoryKeys(context.Background(), []string{good})
if err != nil || len(res) != 1 || res[0].Status != "exists" {
t.Fatalf("again: %+v %v", res, err)
}
if _, err := ImportFactoryKeys(context.Background(), []string{"sk-not-factory"}); err == nil {
t.Fatal("no key in it, and no error")
}
}