156 lines
4.4 KiB
TypeScript
156 lines
4.4 KiB
TypeScript
#!/usr/bin/env bun
|
|
|
|
/**
|
|
* Simple API test script for the NGINX Proxy Manager Backend
|
|
*/
|
|
|
|
const API_BASE = 'http://10.0.0.122:3000/api';
|
|
|
|
interface TestResult {
|
|
name: string;
|
|
success: boolean;
|
|
message: string;
|
|
data?: any;
|
|
}
|
|
|
|
class APITestSuite {
|
|
private token: string = '';
|
|
private results: TestResult[] = [];
|
|
|
|
async runTests(): Promise<void> {
|
|
console.log('🧪 Running API Test Suite...\n');
|
|
|
|
// Test authentication
|
|
await this.testHealthCheck();
|
|
await this.testLogin();
|
|
await this.testMe();
|
|
|
|
// Test proxy management (requires auth)
|
|
if (this.token) {
|
|
await this.testGetProxies();
|
|
await this.testNginxStatus();
|
|
}
|
|
|
|
// Print results
|
|
this.printResults();
|
|
}
|
|
|
|
private async testHealthCheck(): Promise<void> {
|
|
try {
|
|
const response = await fetch(`${API_BASE}/health`);
|
|
const data = await response.json();
|
|
|
|
this.addResult('Health Check', response.ok && data.success, data.message || 'Failed', data);
|
|
} catch (error: any) {
|
|
this.addResult('Health Check', false, error.message);
|
|
}
|
|
}
|
|
|
|
private async testLogin(): Promise<void> {
|
|
try {
|
|
const response = await fetch(`${API_BASE}/auth/login`, {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
username: 'admin',
|
|
password: 'admin123'
|
|
})
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
if (response.ok && data.success && data.data?.token) {
|
|
this.token = data.data.token;
|
|
this.addResult('Login', true, 'Successfully logged in', { user: data.data.user });
|
|
} else {
|
|
this.addResult('Login', false, data.message || 'Login failed', data);
|
|
}
|
|
} catch (error: any) {
|
|
this.addResult('Login', false, error.message);
|
|
}
|
|
}
|
|
|
|
private async testMe(): Promise<void> {
|
|
if (!this.token) {
|
|
this.addResult('Get Current User', false, 'No token available');
|
|
return;
|
|
}
|
|
|
|
try {
|
|
const response = await fetch(`${API_BASE}/auth/me`, {
|
|
headers: { 'Authorization': `Bearer ${this.token}` }
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
this.addResult('Get Current User', response.ok && data.success, data.message || 'Failed', data.data);
|
|
} catch (error: any) {
|
|
this.addResult('Get Current User', false, error.message);
|
|
}
|
|
}
|
|
|
|
private async testGetProxies(): Promise<void> {
|
|
try {
|
|
const response = await fetch(`${API_BASE}/proxies`, {
|
|
headers: { 'Authorization': `Bearer ${this.token}` }
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
this.addResult('Get Proxies', response.ok && data.success, `Found ${data.data?.length || 0} proxies`, data.data);
|
|
} catch (error: any) {
|
|
this.addResult('Get Proxies', false, error.message);
|
|
}
|
|
}
|
|
|
|
private async testNginxStatus(): Promise<void> {
|
|
try {
|
|
const response = await fetch(`${API_BASE}/proxies/nginx/status`, {
|
|
headers: { 'Authorization': `Bearer ${this.token}` }
|
|
});
|
|
|
|
const data = await response.json();
|
|
|
|
this.addResult('NGINX Status', response.ok, data.message || 'Failed', data.data);
|
|
} catch (error: any) {
|
|
this.addResult('NGINX Status', false, error.message);
|
|
}
|
|
}
|
|
|
|
private addResult(name: string, success: boolean, message: string, data?: any): void {
|
|
this.results.push({ name, success, message, data });
|
|
}
|
|
|
|
private printResults(): void {
|
|
console.log('\n📊 Test Results:');
|
|
console.log('='.repeat(50));
|
|
|
|
const passed = this.results.filter(r => r.success).length;
|
|
const total = this.results.length;
|
|
|
|
this.results.forEach(result => {
|
|
const status = result.success ? '✅ PASS' : '❌ FAIL';
|
|
console.log(`${status} ${result.name}: ${result.message}`);
|
|
|
|
if (result.data && typeof result.data === 'object') {
|
|
console.log(` Data:`, JSON.stringify(result.data, null, 2).split('\n').slice(0, 3).join('\n'));
|
|
}
|
|
});
|
|
|
|
console.log('='.repeat(50));
|
|
console.log(`📈 Results: ${passed}/${total} tests passed`);
|
|
|
|
if (passed === total) {
|
|
console.log('🎉 All tests passed! API is working correctly.');
|
|
} else {
|
|
console.log('⚠️ Some tests failed. Check the logs for details.');
|
|
}
|
|
}
|
|
}
|
|
|
|
// Run tests
|
|
const testSuite = new APITestSuite();
|
|
testSuite.runTests().catch(error => {
|
|
console.error('❌ Test suite failed:', error);
|
|
process.exit(1);
|
|
});
|