File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ package util
2+
3+ import (
4+ "context"
5+ "testing"
6+ "time"
7+ )
8+
9+ func TestPasswordRateLimiter_Configuration (t * testing.T ) {
10+ // Test that different configurations work
11+ tests := []struct {
12+ name string
13+ maxAttempts int
14+ windowSize time.Duration
15+ attempts int
16+ shouldBlock bool
17+ }{
18+ {
19+ name : "strict limit - 2 attempts per minute" ,
20+ maxAttempts : 2 ,
21+ windowSize : time .Minute ,
22+ attempts : 2 ,
23+ shouldBlock : true ,
24+ },
25+ {
26+ name : "lenient limit - 10 attempts per minute" ,
27+ maxAttempts : 10 ,
28+ windowSize : time .Minute ,
29+ attempts : 5 ,
30+ shouldBlock : false ,
31+ },
32+ {
33+ name : "very strict - 1 attempt per minute" ,
34+ maxAttempts : 1 ,
35+ windowSize : time .Minute ,
36+ attempts : 1 ,
37+ shouldBlock : true ,
38+ },
39+ }
40+
41+ for _ , tt := range tests {
42+ t .Run (tt .name , func (t * testing.T ) {
43+ rl := NewPasswordRateLimiter (tt .maxAttempts , tt .windowSize )
44+ defer rl .Close ()
45+
46+ ctx := context .Background ()
47+ ip := "192.168.1.100"
48+
49+ // Record the specified number of attempts
50+ for i := 0 ; i < tt .attempts ; i ++ {
51+ rl .RecordFailedAttempt (ctx , ip )
52+ }
53+
54+ // Check if next attempt should be blocked
55+ allowed := rl .IsAllowed (ctx , ip )
56+ blocked := ! allowed
57+
58+ if blocked != tt .shouldBlock {
59+ t .Errorf ("Expected blocked=%v but got blocked=%v" , tt .shouldBlock , blocked )
60+ }
61+ })
62+ }
63+ }
You can’t perform that action at this time.
0 commit comments