Files
CLIProxyAPI/internal/api/handlers/management/handler_test.go
T
Luis Pater e50cabac4b chore: upgrade CLIProxyAPI dependency to v7 across the project
- Updated all references from v6 to v7 for `github.com/router-for-me/CLIProxyAPI`.
- Ensured consistency in imports within core libraries, tests, and integration tests.
- Added missing tests for new features in Redis Protocol integration.
2026-05-08 11:46:46 +08:00

39 lines
1.2 KiB
Go

package management
import (
"net/http"
"strings"
"testing"
"github.com/router-for-me/CLIProxyAPI/v7/internal/config"
)
func TestAuthenticateManagementKey_LocalhostIPBan_BlocksCorrectKeyDuringBan(t *testing.T) {
h := &Handler{
cfg: &config.Config{},
failedAttempts: make(map[string]*attemptInfo),
envSecret: "test-secret",
}
for i := 0; i < 5; i++ {
allowed, statusCode, errMsg := h.AuthenticateManagementKey("127.0.0.1", true, "wrong-secret")
if allowed {
t.Fatalf("expected auth to be denied at attempt %d", i+1)
}
if statusCode != http.StatusUnauthorized || errMsg != "invalid management key" {
t.Fatalf("unexpected auth failure at attempt %d: status=%d msg=%q", i+1, statusCode, errMsg)
}
}
allowed, statusCode, errMsg := h.AuthenticateManagementKey("127.0.0.1", true, "test-secret")
if allowed {
t.Fatalf("expected correct key to be denied while banned")
}
if statusCode != http.StatusForbidden {
t.Fatalf("expected forbidden status while banned, got %d", statusCode)
}
if !strings.HasPrefix(errMsg, "IP banned due to too many failed attempts. Try again in") {
t.Fatalf("unexpected banned message: %q", errMsg)
}
}