mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-19 07:27:43 +08:00
fix: fully mask web secret previews
This commit is contained in:
@@ -1,6 +1,9 @@
|
||||
package admin
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
@@ -33,6 +36,53 @@ func TestFieldStringNilToEmpty(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMaskSecretPreviewKeepsOnlyFirstAndLastTwoChars(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"": "",
|
||||
"a": "*",
|
||||
"ab": "**",
|
||||
"abcd": "****",
|
||||
"abcdef": "ab****ef",
|
||||
"abc12345": "ab****45",
|
||||
}
|
||||
|
||||
for input, want := range cases {
|
||||
if got := maskSecretPreview(input); got != want {
|
||||
t.Fatalf("maskSecretPreview(%q)=%q want %q", input, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestGetConfigMasksAccountTokenPreview(t *testing.T) {
|
||||
h := newAdminTestHandler(t, `{
|
||||
"accounts":[{"email":"u@example.com","password":"pwd"}]
|
||||
}`)
|
||||
if err := h.Store.UpdateAccountToken("u@example.com", "abcdefgh"); err != nil {
|
||||
t.Fatalf("seed runtime token: %v", err)
|
||||
}
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/admin/config", nil)
|
||||
rec := httptest.NewRecorder()
|
||||
h.getConfig(rec, req)
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("unexpected status: %d body=%s", rec.Code, rec.Body.String())
|
||||
}
|
||||
|
||||
var payload map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &payload); err != nil {
|
||||
t.Fatalf("decode response failed: %v", err)
|
||||
}
|
||||
accounts, _ := payload["accounts"].([]any)
|
||||
if len(accounts) != 1 {
|
||||
t.Fatalf("expected 1 account, got %d", len(accounts))
|
||||
}
|
||||
first, _ := accounts[0].(map[string]any)
|
||||
if got, _ := first["token_preview"].(string); got != "ab****gh" {
|
||||
t.Fatalf("expected masked token preview, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRunAccountTestsConcurrentlyKeepsInputOrder(t *testing.T) {
|
||||
accounts := []config.Account{
|
||||
{Email: "a@example.com"},
|
||||
|
||||
Reference in New Issue
Block a user