|
| 1 | +import { describe, it, expect, beforeEach, vi } from 'vitest'; |
| 2 | +import { rateLimit, rateLimitDual, resetRateLimits } from '../rate-limit'; |
| 3 | + |
| 4 | +describe('rateLimit', () => { |
| 5 | + beforeEach(() => { |
| 6 | + resetRateLimits(); |
| 7 | + }); |
| 8 | + |
| 9 | + it('allows requests within limit', () => { |
| 10 | + const res1 = rateLimit('test-key', 2, 60_000); |
| 11 | + expect(res1.allowed).toBe(true); |
| 12 | + expect(res1.retryAfterSeconds).toBe(0); |
| 13 | + |
| 14 | + const res2 = rateLimit('test-key', 2, 60_000); |
| 15 | + expect(res2.allowed).toBe(true); |
| 16 | + }); |
| 17 | + |
| 18 | + it('blocks requests exceeding limit', () => { |
| 19 | + rateLimit('test-key', 2, 60_000); |
| 20 | + rateLimit('test-key', 2, 60_000); |
| 21 | + |
| 22 | + const res3 = rateLimit('test-key', 2, 60_000); |
| 23 | + expect(res3.allowed).toBe(false); |
| 24 | + expect(res3.retryAfterSeconds).toBeGreaterThan(0); |
| 25 | + }); |
| 26 | + |
| 27 | + it('does not record denied hits (does not extend penalty window)', () => { |
| 28 | + vi.useFakeTimers(); |
| 29 | + const now = Date.now(); |
| 30 | + vi.setSystemTime(now); |
| 31 | + |
| 32 | + rateLimit('test-key', 1, 60_000); // 1st hit -> allowed |
| 33 | + rateLimit('test-key', 1, 60_000); // 2nd hit -> blocked |
| 34 | + |
| 35 | + // Advance time 61s |
| 36 | + vi.setSystemTime(now + 61_000); |
| 37 | + |
| 38 | + const res = rateLimit('test-key', 1, 60_000); |
| 39 | + expect(res.allowed).toBe(true); |
| 40 | + |
| 41 | + vi.useRealTimers(); |
| 42 | + }); |
| 43 | +}); |
| 44 | + |
| 45 | +describe('rateLimitDual', () => { |
| 46 | + beforeEach(() => { |
| 47 | + resetRateLimits(); |
| 48 | + }); |
| 49 | + |
| 50 | + it('allows request when both IP and target limits are under thresholds', async () => { |
| 51 | + const res = await rateLimitDual('signin', 'user@example.com', 20, 5, 60_000); |
| 52 | + expect(res.allowed).toBe(true); |
| 53 | + }); |
| 54 | + |
| 55 | + it('blocks request when target limit is exceeded', async () => { |
| 56 | + for (let i = 0; i < 5; i++) { |
| 57 | + await rateLimitDual('signin', 'target@example.com', 20, 5, 60_000); |
| 58 | + } |
| 59 | + |
| 60 | + const res = await rateLimitDual('signin', 'target@example.com', 20, 5, 60_000); |
| 61 | + expect(res.allowed).toBe(false); |
| 62 | + }); |
| 63 | + |
| 64 | + it('blocks IP when IP limit is exceeded before checking target limit', async () => { |
| 65 | + // Fill IP limit (3) |
| 66 | + for (let i = 0; i < 3; i++) { |
| 67 | + await rateLimitDual('signin', `victim${i}@example.com`, 3, 5, 60_000); |
| 68 | + } |
| 69 | + |
| 70 | + // IP blocked attempt against a new target |
| 71 | + const res = await rateLimitDual('signin', 'fresh-target@example.com', 3, 5, 60_000); |
| 72 | + expect(res.allowed).toBe(false); |
| 73 | + |
| 74 | + // Ensure fresh-target bucket was NOT populated/polluted because IP limit blocked it first |
| 75 | + resetRateLimits(); // Clear rate limits (e.g., simulating IP change or reset) |
| 76 | + const freshRes = await rateLimitDual('signin', 'fresh-target@example.com', 3, 5, 60_000); |
| 77 | + expect(freshRes.allowed).toBe(true); |
| 78 | + }); |
| 79 | +}); |
0 commit comments