fix: harden webui path and account pool compatibility

This commit is contained in:
CJACK
2026-02-16 18:30:27 +08:00
parent 844832f31b
commit 057862f7fb
7 changed files with 170 additions and 9 deletions

View File

@@ -873,7 +873,20 @@ func toStringSlice(v any) ([]string, bool) {
}
func toAccount(m map[string]any) config.Account {
return config.Account{Email: strings.TrimSpace(fmt.Sprintf("%v", m["email"])), Mobile: strings.TrimSpace(fmt.Sprintf("%v", m["mobile"])), Password: strings.TrimSpace(fmt.Sprintf("%v", m["password"])), Token: strings.TrimSpace(fmt.Sprintf("%v", m["token"]))}
return config.Account{
Email: fieldString(m, "email"),
Mobile: fieldString(m, "mobile"),
Password: fieldString(m, "password"),
Token: fieldString(m, "token"),
}
}
func fieldString(m map[string]any, key string) string {
v, ok := m[key]
if !ok || v == nil {
return ""
}
return strings.TrimSpace(fmt.Sprintf("%v", v))
}
func statusOr(v int, d int) int {

View File

@@ -0,0 +1,28 @@
package admin
import "testing"
func TestToAccountMissingFieldsRemainEmpty(t *testing.T) {
acc := toAccount(map[string]any{
"email": "user@example.com",
"password": "secret",
})
if acc.Email != "user@example.com" {
t.Fatalf("unexpected email: %q", acc.Email)
}
if acc.Mobile != "" {
t.Fatalf("expected empty mobile, got %q", acc.Mobile)
}
if acc.Token != "" {
t.Fatalf("expected empty token, got %q", acc.Token)
}
}
func TestFieldStringNilToEmpty(t *testing.T) {
if got := fieldString(map[string]any{"token": nil}, "token"); got != "" {
t.Fatalf("expected empty string for nil field, got %q", got)
}
if got := fieldString(map[string]any{}, "token"); got != "" {
t.Fatalf("expected empty string for missing field, got %q", got)
}
}