Files
tokens-reef/backend/internal/handler/admin/backup_restore_limiter_test.go
pham 649eb23091
Some checks failed
CI / test (push) Has been cancelled
CI / golangci-lint (push) Has been cancelled
Security Scan / backend-security (push) Has been cancelled
Security Scan / frontend-security (push) Has been cancelled
fix confirmed deployment risks
2026-04-25 09:24:17 +08:00

57 lines
1.4 KiB
Go

package admin
import (
"testing"
"time"
)
func TestRestoreAttemptLimiterBlocksAfterMaxFailures(t *testing.T) {
limiter := newRestoreAttemptLimiter(2, time.Minute)
now := time.Unix(1700000000, 0)
if limited, _ := limiter.allow(1, now); !limited {
t.Fatalf("first attempt should be allowed")
}
limiter.recordFailure(1, now)
if limited, _ := limiter.allow(1, now.Add(time.Second)); !limited {
t.Fatalf("second attempt should still be allowed")
}
limiter.recordFailure(1, now.Add(2*time.Second))
allowed, retryAfter := limiter.allow(1, now.Add(3*time.Second))
if allowed {
t.Fatal("limiter should block after hitting max failures")
}
if retryAfter <= 0 {
t.Fatalf("retryAfter should be positive, got %v", retryAfter)
}
}
func TestRestoreAttemptLimiterResetsAfterSuccess(t *testing.T) {
limiter := newRestoreAttemptLimiter(2, time.Minute)
now := time.Unix(1700000000, 0)
limiter.recordFailure(1, now)
limiter.recordSuccess(1)
allowed, retryAfter := limiter.allow(1, now.Add(time.Second))
if !allowed {
t.Fatalf("limiter should reset after success, retryAfter=%v", retryAfter)
}
}
func TestRestoreAttemptLimiterExpiresBlockWindow(t *testing.T) {
limiter := newRestoreAttemptLimiter(1, time.Minute)
now := time.Unix(1700000000, 0)
limiter.recordFailure(1, now)
allowed, _ := limiter.allow(1, now.Add(61*time.Second))
if !allowed {
t.Fatal("limiter should allow attempts after block window expires")
}
}