Files
HIPCTF2/tests/backend/login-backoff.spec.ts
T

24 lines
955 B
TypeScript

import { LoginBackoffService } from '../../backend/src/common/services/login-backoff.service';
describe('LoginBackoffService', () => {
it('starts unblocked', () => {
const s = new LoginBackoffService();
expect(s.isBlocked('1.1.1.1', 'alice', 1000)).toBe(0);
});
it('blocks after repeated failures and resets on success', () => {
const s = new LoginBackoffService();
const now = 1000;
for (let i = 0; i < 5; i++) s.recordFailure('1.1.1.1', 'alice', now);
expect(s.isBlocked('1.1.1.1', 'alice', now + 100)).toBeGreaterThan(0);
s.reset('1.1.1.1', 'alice');
expect(s.isBlocked('1.1.1.1', 'alice', now + 100)).toBe(0);
});
it('is per (ip, username)', () => {
const s = new LoginBackoffService();
for (let i = 0; i < 6; i++) s.recordFailure('1.1.1.1', 'alice', 1000 + i);
expect(s.isBlocked('1.1.1.1', 'alice', 1100)).toBeGreaterThan(0);
expect(s.isBlocked('1.1.1.1', 'bob', 1100)).toBe(0);
});
});