feat: implement security logging system

This commit is contained in:
张翔
2026-03-24 10:45:00 +08:00
parent 0d020bc3f8
commit b542f922b2
2 changed files with 156 additions and 0 deletions
+49
View File
@@ -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);
});
});