WIP
This commit is contained in:
98
backend/tests/test-error-handling-simple.js
Normal file
98
backend/tests/test-error-handling-simple.js
Normal file
@ -0,0 +1,98 @@
|
||||
/**
|
||||
* Simple test to verify error handling and logging functionality
|
||||
*/
|
||||
|
||||
const loggingService = require('./src/services/LoggingService');
|
||||
const { AppError, handleDatabaseError, handleJWTError } = require('./src/middleware/errorHandler');
|
||||
|
||||
async function testErrorHandling() {
|
||||
console.log('🧪 Testing Error Handling and Logging System...\n');
|
||||
|
||||
try {
|
||||
// Test 1: Logging Service
|
||||
console.log('1. Testing Logging Service...');
|
||||
await loggingService.info('Test info message', { testData: 'info test' });
|
||||
await loggingService.warn('Test warning message', { testData: 'warning test' });
|
||||
await loggingService.error('Test error message', { testData: 'error test' });
|
||||
await loggingService.debug('Test debug message', { testData: 'debug test' });
|
||||
console.log('✅ Logging service test completed');
|
||||
|
||||
// Test 2: Authentication Event Logging
|
||||
console.log('\n2. Testing Authentication Event Logging...');
|
||||
await loggingService.logAuthEvent('login_success', 'user123', 'test@example.com', {
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'Test Browser'
|
||||
});
|
||||
await loggingService.logAuthEvent('login_failed', 'unknown', 'test@example.com', {
|
||||
ip: '127.0.0.1',
|
||||
userAgent: 'Test Browser'
|
||||
});
|
||||
console.log('✅ Authentication event logging test completed');
|
||||
|
||||
// Test 3: Database Event Logging
|
||||
console.log('\n3. Testing Database Event Logging...');
|
||||
await loggingService.logDatabaseEvent('connection_established', { database: 'test_db' });
|
||||
await loggingService.logDatabaseEvent('query_executed', {
|
||||
query: 'SELECT * FROM users',
|
||||
duration: '15ms'
|
||||
});
|
||||
console.log('✅ Database event logging test completed');
|
||||
|
||||
// Test 4: Security Event Logging
|
||||
console.log('\n4. Testing Security Event Logging...');
|
||||
await loggingService.logSecurityEvent('rate_limit_exceeded', {
|
||||
ip: '127.0.0.1',
|
||||
endpoint: '/api/auth/login',
|
||||
attempts: 10
|
||||
});
|
||||
console.log('✅ Security event logging test completed');
|
||||
|
||||
// Test 5: AppError Class
|
||||
console.log('\n5. Testing AppError Class...');
|
||||
const appError = new AppError('Test application error', 400, 'TEST_ERROR');
|
||||
console.log('AppError created:', {
|
||||
message: appError.message,
|
||||
statusCode: appError.statusCode,
|
||||
code: appError.code,
|
||||
timestamp: appError.timestamp
|
||||
});
|
||||
console.log('✅ AppError class test completed');
|
||||
|
||||
// Test 6: Database Error Handler
|
||||
console.log('\n6. Testing Database Error Handler...');
|
||||
const dbError = { code: '23505', message: 'duplicate key value violates unique constraint' };
|
||||
const handledDbError = handleDatabaseError(dbError);
|
||||
console.log('Database error handled:', {
|
||||
message: handledDbError.message,
|
||||
statusCode: handledDbError.statusCode,
|
||||
code: handledDbError.code
|
||||
});
|
||||
console.log('✅ Database error handler test completed');
|
||||
|
||||
// Test 7: JWT Error Handler
|
||||
console.log('\n7. Testing JWT Error Handler...');
|
||||
const jwtError = { name: 'TokenExpiredError', message: 'jwt expired' };
|
||||
const handledJwtError = handleJWTError(jwtError);
|
||||
console.log('JWT error handled:', {
|
||||
message: handledJwtError.message,
|
||||
statusCode: handledJwtError.statusCode,
|
||||
code: handledJwtError.code
|
||||
});
|
||||
console.log('✅ JWT error handler test completed');
|
||||
|
||||
// Test 8: Log Statistics
|
||||
console.log('\n8. Testing Log Statistics...');
|
||||
const logStats = await loggingService.getLogStats();
|
||||
console.log('Log statistics:', logStats);
|
||||
console.log('✅ Log statistics test completed');
|
||||
|
||||
console.log('\n🎉 All error handling and logging tests completed successfully!');
|
||||
console.log('\n📁 Check the backend/logs directory for generated log files.');
|
||||
|
||||
} catch (error) {
|
||||
console.error('❌ Test failed:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// Run the test
|
||||
testErrorHandling();
|
||||
Reference in New Issue
Block a user