feat: implement security logging system
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import { SecurityLogger, SecurityEventType } from './logger';
|
||||
|
||||
describe('Security Logging System', () => {
|
||||
let logger: SecurityLogger;
|
||||
|
||||
beforeEach(() => {
|
||||
logger = new SecurityLogger();
|
||||
});
|
||||
|
||||
test('should log security events', () => {
|
||||
logger.logEvent({
|
||||
type: SecurityEventType.CAPTCHA_FAILED,
|
||||
ip: '192.168.1.1',
|
||||
email: 'test@example.com',
|
||||
details: { reason: 'Invalid answer' },
|
||||
});
|
||||
|
||||
const logs = logger.getRecentLogs(10);
|
||||
expect(logs.length).toBe(1);
|
||||
expect(logs[0].type).toBe(SecurityEventType.CAPTCHA_FAILED);
|
||||
});
|
||||
|
||||
test('should detect suspicious activity', () => {
|
||||
for (let i = 0; i < 6; i++) {
|
||||
logger.logEvent({
|
||||
type: SecurityEventType.CAPTCHA_FAILED,
|
||||
ip: '192.168.1.2',
|
||||
email: 'suspicious@example.com',
|
||||
details: {},
|
||||
});
|
||||
}
|
||||
|
||||
const suspiciousIPs = logger.getSuspiciousIPs();
|
||||
expect(suspiciousIPs).toContain('192.168.1.2');
|
||||
});
|
||||
|
||||
test('should clear old logs', () => {
|
||||
logger.logEvent({
|
||||
type: SecurityEventType.RATE_LIMIT_EXCEEDED,
|
||||
ip: '192.168.1.3',
|
||||
details: {},
|
||||
});
|
||||
|
||||
logger.clearOldLogs(0);
|
||||
|
||||
const logs = logger.getRecentLogs(10);
|
||||
expect(logs.length).toBe(0);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user