Files
magpie/internal/davsync/keep_secrets_test.go
T
yetone 8792ee063d provider: a Volcengine or Tencent access key is kept through sync, restore, a stale editor and a copy, and a plan card without one says what to give (#1821, Edward4226)
The reporter's Volcengine Agent Plan card vanished: providers.json had
lost accessKeyID/secretAccessKey, and volcPlanQuotas skipped a provider
without them before building any card, so nothing said why.

Writers that dropped the access key, fixed at the cause:
- Sync and restore (Mirror, Restore, davsync take) kept this machine's
  access key and balance token only when the incoming provider had no
  key at all. A magpie before v0.1.1142 (aa82288f), or one the access
  key was never given on, sends the same key with no access key, and
  that replaced it here and on the server. KeepSecrets now keeps them
  when the incoming has the same key and none of its own; another key is
  another account and still brings its own (TestBackupBalanceToken
  unchanged). A keyless copy also keeps this machine's secret headers,
  which withoutKeys strips and nothing put back. The secret-name regex
  moves to provider.SecretHeader; backup.Secret delegates to it.
- The GUI save kept the access key ID only when the field was left out;
  the editor always sends it, so an editor opened before the key was
  given saved "" over it (the Secret was kept). Blank now keeps it; only
  clearAccessKey drops it.
- AddCopy skipped the Secret when the copy's form sent the source's ID.
- read() and validProviders didn't strip a UTF-8 BOM (lastgood.JSON
  does): a BOM'd providers.json fell back to its .bak, an older
  generation.
Import's replace mode replaces providers on purpose and is unchanged.

The Usage page: a plan endpoint (Volcengine Coding/Agent Plan, Tencent
Token Plan Enterprise) whose providers have no access key is a card
with no windows, an error naming AccessKey ID and Secret (SecretId and
SecretKey for Tencent), needsAccessKey, and an "Open settings" button to
the provider's editor; one card per plan, none when another provider on
the plan has the key. Strings in en, zh, zh-TW, ja, de.

Run:
- go test ./internal/provider ./internal/davsync ./internal/backup
  ./internal/gui -count=1, twice, green (private GOCACHE, sandbox HOME).
- Broke each by hand and saw red: KeepSecrets same-key keep
  (TestSyncKeepsAccessKey, TestTakeKeepsAccessKey), header keep, BOM
  strip (TestProvidersFileWithBOM), GUI blank-ID keep
  (TestProviderSaveKeepsVolcengineSecret), AddCopy
  (TestAddCopyKeepsAccessKeySecret), the Volcengine missing card
  (TestPlanCardWithoutAccessKey).
- GUI: usage-access-key-card 20/20 (chromium + webkit, 5 languages, 900
  and 420px; red with the button removed and with the ja string
  changed); i18n-keys, volc-access-key, tencent-access-key,
  tencent-cloud, quota-balance-error, usage-card-refresh,
  usage-remote-cards green.
- gofmt -l clean; go vet darwin, linux -tags nogui, windows clean for
  provider, davsync, backup, gui, cli.

Not verified: which writer dropped the reporter's key is inferred from
their 10-10 to 10-11 snapshots, not reproduced on their machine (most
likely sync from another computer). Not tried against real Volcengine or
Tencent accounts. The tray panel shows the error text with no button.
2026-10-12 23:45:06 +08:00

44 lines
1.7 KiB
Go

package davsync
import (
"testing"
"github.com/yetone/magpie/internal/backup"
"github.com/yetone/magpie/internal/provider"
)
// #1821: an upload with keys from a magpie that knows no access key (one
// before v0.1.1142, or one it was never given on) has the provider's key
// and no AccessKey ID or Secret. The server's are kept while the key is
// the same, so the next computer to sync doesn't lose them; a keyless
// upload keeps the server's secret headers as it keeps its keys.
func TestTakeKeepsAccessKey(t *testing.T) {
server := provider.Provider{ID: "volcengine", Name: "Old", Chat: "https://ark.cn-beijing.volces.com/api/coding/v3",
Key: "ark-k", AccessKeyID: "AK-server", SecretAccessKey: "SK-server",
Headers: map[string]string{"X-Api-Token": "hdr-server"}}
for _, c := range []struct {
name string
keys bool
incoming provider.Provider
wantAK, wantHd string
}{
{"same key, no access key", true, provider.Provider{Key: "ark-k"}, "AK-server", ""},
{"another key", true, provider.Provider{Key: "ark-other"}, "", ""},
{"keyless upload", false, provider.Provider{}, "AK-server", "hdr-server"},
} {
t.Run(c.name, func(t *testing.T) {
in := c.incoming
in.ID, in.Name, in.Chat = "volcengine", "New", server.Chat
to := backup.Bundle{Keys: true, Providers: []provider.Provider{server}}
take(&to, backup.Bundle{Keys: c.keys, Providers: []provider.Provider{in}}, "providers")
got := to.Providers[0]
if got.AccessKeyID != c.wantAK || (c.wantAK != "") != (got.SecretAccessKey == "SK-server") || got.Name != "New" {
t.Errorf("merged %+v, want access key %q", got, c.wantAK)
}
if got.Headers["X-Api-Token"] != c.wantHd {
t.Errorf("headers %v, want %q", got.Headers, c.wantHd)
}
})
}
}