Files
magpie/internal/plugin/plugin_uid_test.go
T
yetone 261117363e plugin host: a sign-in again joins its account when an older plugin named it another way (#4473)
officeace-auth 0.1.14 to 0.1.17 kept an account as accountId = the masked
phone ("189****8051") and uid = the Huawei domain id. The CodeArts
plugin's OfficeAce card (plugins#522) names a new sign-in by the domain
id, uid the same. settle joined a new sign-in to an old record only on an
equal account id, so signing in again added a second row (fakeco#xxxxxx)
and left the old one "Sign-in required", holding the user's name, proxy
and gateway keys.

settle now also joins two records with the same uid when one of them is
named by that uid (renamedBy): then the uid is the account's own id. The
joined record keeps the old record's accountId, since the user's name,
proxy and gateway keys are kept by it (#4416), as the CodeArts plugin's
renewals already keep a stored accountId.

The issue suggested joining on any equal uid. That is not safe: Kiro
keeps how an account was signed in to as its uid ("google"), so two
different Google sign-ins share one. The test signs in two such accounts
and checks they stay apart; with a uid-only rule they were joined.

Not covered: a re-sign-in where neither record is named by its uid (a
WorkBuddy nickname changed at the vendor) still adds a row; nothing in
the records proves those are one account.

Run:
- Reproduced first on origin/main's host.js: TestPluginReSignInJoinsAnOldIDShape,
  with the old record as officeace-auth 0.1.14's callback and
  credential.mjs write it (read from the npm tarball), was red: the
  sign-in again went to "fakeco#9oncv2" beside the old record.
- Broken by hand: renamedBy on uid alone -> red (two Google accounts of
  uid "google" joined); joined without keeping the old accountId -> red
  (accountId became the domain id). Green with the fix, and
  TestPluginSameNameOtherUID (#413) still green.
- Full go test ./... -timeout 45m on mote was still running at push
  (pushed without waiting, per the owner's rule); a red it shows that
  isn't on origin/main gets its own issue.

Not tried: a real Huawei Cloud sign-in in a running magpie; the CodeArts
plugin with the OfficeAce card is not on npm yet (plugins#522 is open).
2026-10-31 01:11:31 +08:00

129 lines
4.1 KiB
Go

package plugin
import (
"context"
"encoding/json"
"os"
"path/filepath"
"testing"
"time"
)
// Two accounts of one name that the plugin tells apart by a uid (WorkBuddy's
// nickname and uid) are both kept: the second took the first one's place
// (#413). The same uid again replaces its own sign-in.
func TestPluginSameNameOtherUID(t *testing.T) {
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/fake/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
signIn := func(team string) Saved {
t.Helper()
a, err := Authorize(ctx, "fakeco", 1, map[string]string{"where": "work", "team": team}, NewAccount)
if err != nil {
t.Fatal(err)
}
s, err := Finish(ctx, a.Session, "good")
if err != nil {
t.Fatal(err)
}
return s
}
saved := func() map[string]map[string]any {
t.Helper()
var m map[string]map[string]any
b, _ := os.ReadFile(AuthPath())
json.Unmarshal(b, &m)
return m
}
first, second := signIn("pat/u1"), signIn("pat/u2")
if first.Account == second.Account {
t.Fatalf("the second took the first's place: %+v %+v", first, second)
}
if m := saved(); len(m) != 2 || m[first.Account]["uid"] != "u1" || m[second.Account]["uid"] != "u2" {
t.Fatalf("saved %v", m)
}
if again := signIn("pat/u1"); again != first {
t.Fatalf("u1 again as %+v, not %+v", again, first)
}
if m := saved(); len(m) != 2 {
t.Fatalf("after u1 again %v", m)
}
}
// A sign-in again to an account an earlier version of its plugin named
// another way replaces that account's record (#4473). officeace-auth
// 0.1.14 to 0.1.17 named an account by its masked phone and kept the
// Huawei domain id as its uid; the CodeArts plugin's OfficeAce card names
// a new sign-in by the domain id, uid the same. The record is joined by
// that uid, since the new one is named by it, and keeps the account id the
// user's name, proxy and gateway keys are kept by. Two accounts that only
// share a uid, as Kiro's two Google sign-ins share "google", stay apart.
func TestPluginReSignInJoinsAnOldIDShape(t *testing.T) {
sandbox(t)
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel()
abs, _ := filepath.Abs("testdata/fake/index.js")
if _, err := Add(ctx, abs); err != nil {
t.Fatal(err)
}
const domain = "01a1642ad2ae7f93bf6387ad2fc5f09e"
// as officeace-auth 0.1.14's callback and credential.mjs left it
old := map[string]any{
"fakeco": map[string]any{
"type": "oauth",
"refresh": "eyJ.dead.refresh",
"access": `{"access":"HSTA","securityToken":"hQpj","refreshToken":"eyJ.dead.refresh","accountId":"` + domain + `","user_phone":"189****8051"}`,
"expires": 1792841892266,
"accountId": "189****8051",
"uid": domain,
},
}
b, _ := json.Marshal(old)
if err := os.MkdirAll(filepath.Dir(AuthPath()), 0o700); err != nil {
t.Fatal(err)
}
if err := os.WriteFile(AuthPath(), b, 0o600); err != nil {
t.Fatal(err)
}
signIn := func(team string) Saved {
t.Helper()
a, err := Authorize(ctx, "fakeco", 1, map[string]string{"where": "work", "team": team}, NewAccount)
if err != nil {
t.Fatal(err)
}
s, err := Finish(ctx, a.Session, "good")
if err != nil {
t.Fatal(err)
}
return s
}
saved := func() map[string]map[string]any {
t.Helper()
var m map[string]map[string]any
b, _ := os.ReadFile(AuthPath())
json.Unmarshal(b, &m)
return m
}
if s := signIn("=" + domain); s.Account != "fakeco" {
t.Fatalf("the sign-in again went to %q, beside the old record: %v", s.Account, saved())
}
m := saved()
if len(m) != 1 {
t.Fatalf("saved %v", m)
}
if r := m["fakeco"]; r["refresh"] != "r-="+domain || r["accountId"] != "189****8051" || r["uid"] != domain {
t.Fatalf("the old record wasn't replaced as that account: %v", r)
}
// Kiro keeps how an account was signed in to as its uid: two Google
// sign-ins share "google" and are two accounts.
a, b2 := signIn("ann/google"), signIn("bob/google")
if a.Account == b2.Account || len(saved()) != 3 {
t.Fatalf("two accounts of one uid were joined: %+v %+v %v", a, b2, saved())
}
}