UNPKG

triostack-audit-sdk

Version:

A server-side audit logging middleware for Node.js applications with Express, Fastify, and Koa support. Includes comprehensive test examples and documentation.

224 lines (183 loc) • 5.91 kB
import fetch from 'node-fetch'; const SERVER_URL = 'http://localhost:3001'; const AUDIT_URL = 'http://localhost:3002'; // Test data const testUsers = [ 'user-001', 'user-002', 'user-003', 'admin-user', 'test-user' ]; const testRoutes = [ '/', '/api/users', '/api/login', '/api/manual-track', '/api/slow', '/api/error' ]; async function makeRequest(url, options = {}) { try { const response = await fetch(url, { headers: { 'Content-Type': 'application/json', 'x-user-id': options.userId || 'curl-test-user', ...options.headers }, ...options }); const data = await response.json().catch(() => ({})); return { status: response.status, ok: response.ok, data, headers: Object.fromEntries(response.headers.entries()) }; } catch (error) { return { status: 0, ok: false, error: error.message, data: {} }; } } async function testServerEndpoints() { console.log('šŸš€ Testing Server Endpoints...\n'); for (const route of testRoutes) { const method = route === '/api/login' ? 'POST' : 'GET'; const userId = testUsers[Math.floor(Math.random() * testUsers.length)]; console.log(`šŸ“” ${method} ${route} (User: ${userId})`); const options = { method, userId }; if (method === 'POST') { options.body = JSON.stringify({ test: true, timestamp: new Date().toISOString() }); } const result = await makeRequest(`${SERVER_URL}${route}`, options); if (result.ok) { console.log(`āœ… Status: ${result.status} - Success`); } else { console.log(`āŒ Status: ${result.status} - ${result.error || 'Failed'}`); } // Small delay between requests await new Promise(resolve => setTimeout(resolve, 200)); } } async function testAuditServer() { console.log('\nšŸ“Š Testing Audit Server...\n'); // Check health console.log('šŸ„ Checking audit server health...'); const health = await makeRequest(`${AUDIT_URL}/health`); if (health.ok) { console.log(`āœ… Health: ${health.data.status}`); console.log(`šŸ“ˆ Events received: ${health.data.eventsReceived}`); } else { console.log(`āŒ Health check failed: ${health.error}`); } // Get all events console.log('\nšŸ“‹ Fetching all audit events...'); const events = await makeRequest(`${AUDIT_URL}/audit`); if (events.ok) { console.log(`āœ… Found ${events.data.totalEvents} events`); if (events.data.events.length > 0) { console.log('\nšŸ“Š Recent Events:'); events.data.events.slice(-3).forEach((event, index) => { console.log(`${index + 1}. ${event.route} - ${event.userId} - ${event.duration}s`); }); } } else { console.log(`āŒ Failed to fetch events: ${events.error}`); } } async function runBulkTest(count = 10) { console.log(`\nšŸ”„ Running bulk test (${count} requests)...\n`); const results = { success: 0, error: 0 }; for (let i = 0; i < count; i++) { const route = testRoutes[Math.floor(Math.random() * testRoutes.length)]; const userId = testUsers[Math.floor(Math.random() * testUsers.length)]; const method = route === '/api/login' ? 'POST' : 'GET'; const options = { method, userId: `${userId}-${i}` }; if (method === 'POST') { options.body = JSON.stringify({ bulk: true, index: i }); } const result = await makeRequest(`${SERVER_URL}${route}`, options); if (result.ok) { results.success++; process.stdout.write('āœ…'); } else { results.error++; process.stdout.write('āŒ'); } // Small delay await new Promise(resolve => setTimeout(resolve, 100)); } console.log(`\n\nšŸ“Š Bulk Test Results:`); console.log(`āœ… Success: ${results.success}`); console.log(`āŒ Errors: ${results.error}`); console.log(`šŸ“ˆ Total: ${results.success + results.error}`); } async function clearAuditEvents() { console.log('\nšŸ—‘ļø Clearing audit events...'); const result = await makeRequest(`${AUDIT_URL}/audit`, { method: 'DELETE' }); if (result.ok) { console.log('āœ… Audit events cleared'); } else { console.log(`āŒ Failed to clear events: ${result.error}`); } } async function main() { console.log('šŸ” Triostack Audit SDK - Curl Test\n'); console.log('=' .repeat(50)); // Check if servers are running console.log('šŸ” Checking server availability...'); const serverCheck = await makeRequest(`${SERVER_URL}/`); const auditCheck = await makeRequest(`${AUDIT_URL}/health`); if (!serverCheck.ok) { console.log('āŒ Main server is not running. Please start it first.'); console.log(' Run: npm run start'); return; } if (!auditCheck.ok) { console.log('āŒ Audit server is not running. Please start it first.'); console.log(' Run: npm run audit'); return; } console.log('āœ… Both servers are running!\n'); // Run tests await testServerEndpoints(); await testAuditServer(); await runBulkTest(15); console.log('\n' + '=' .repeat(50)); console.log('šŸŽ‰ All tests completed!'); console.log('\nšŸ’” Tips:'); console.log(' - Open test-examples/client-test.html for a visual interface'); console.log(' - Run "npm run dev" to start both servers'); console.log(' - Check the console output for detailed audit logs'); } // Handle command line arguments const args = process.argv.slice(2); const command = args[0]; switch (command) { case 'clear': clearAuditEvents(); break; case 'bulk': const count = parseInt(args[1]) || 10; runBulkTest(count); break; case 'health': testAuditServer(); break; default: main(); } export { makeRequest, testServerEndpoints, testAuditServer, runBulkTest, clearAuditEvents };