test(web): add percentage checking of characters displaying in APIKey

This commit is contained in:
lc6464
2026-03-24 13:54:04 +08:00
parent 66d2efc9d1
commit 1ef2b6903d
2 changed files with 20 additions and 1 deletions
+1 -1
View File
@@ -311,7 +311,7 @@ func (h *Handler) handleSetDefaultModel(w http.ResponseWriter, r *http.Request)
// Keys 9-12 chars show prefix + last 2 chars: "sk-****cd".
// Shorter keys are fully masked as "****".
// Empty keys return empty string.
// Ensure at least 40% of the key is masked.
// Ensure at least 40% of the key will not be displayed.
func maskAPIKey(key string) string {
if key == "" {
return ""
+19
View File
@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"time"
@@ -365,6 +366,24 @@ func TestMaskAPIKey(t *testing.T) {
if got != tc.want {
t.Fatalf("maskAPIKey(%q) = %q, want %q", tc.key, got, tc.want)
}
if tc.key != "" {
displayed := strings.Replace(tc.want, "****", "", 1)
if len(tc.key) <= 8 {
if displayed != "" {
t.Fatalf("maskAPIKey(%q) displayed part = %q, want empty", tc.key, displayed)
}
} else {
if len(displayed)*10 > len(tc.key)*6 {
t.Fatalf(
"maskAPIKey(%q) displayed length = %d, want at most 60%% of %d",
tc.key,
len(displayed),
len(tc.key),
)
}
}
}
})
}
}