mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-21 16:37:47 +08:00
fix: harden webui path and account pool compatibility
This commit is contained in:
@@ -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 {
|
||||
|
||||
28
internal/admin/handler_test.go
Normal file
28
internal/admin/handler_test.go
Normal 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user