feat: rate limiting connexion (5 essais / 15 min)
This commit is contained in:
@@ -0,0 +1,36 @@
|
||||
import {
|
||||
checkRateLimit,
|
||||
recordFailedAttempt,
|
||||
clearAttempts,
|
||||
_resetForTests,
|
||||
} from '@/lib/rate-limit'
|
||||
|
||||
beforeEach(() => _resetForTests())
|
||||
|
||||
describe('checkRateLimit', () => {
|
||||
it('autorise une nouvelle clé', () => {
|
||||
expect(checkRateLimit('ip:user@test.com')).toBe(true)
|
||||
})
|
||||
|
||||
it('autorise après moins de 5 tentatives', () => {
|
||||
const key = 'ip:user@test.com'
|
||||
recordFailedAttempt(key)
|
||||
recordFailedAttempt(key)
|
||||
recordFailedAttempt(key)
|
||||
recordFailedAttempt(key)
|
||||
expect(checkRateLimit(key)).toBe(true)
|
||||
})
|
||||
|
||||
it('bloque après 5 tentatives', () => {
|
||||
const key = 'ip:user@test.com'
|
||||
for (let i = 0; i < 5; i++) recordFailedAttempt(key)
|
||||
expect(checkRateLimit(key)).toBe(false)
|
||||
})
|
||||
|
||||
it('débloque après clearAttempts', () => {
|
||||
const key = 'ip:user@test.com'
|
||||
for (let i = 0; i < 5; i++) recordFailedAttempt(key)
|
||||
clearAttempts(key)
|
||||
expect(checkRateLimit(key)).toBe(true)
|
||||
})
|
||||
})
|
||||
Reference in New Issue
Block a user