chitfund/backend/simple-test.js

146 lines
4.4 KiB
JavaScript

const http = require('http');
function makeRequest(options, data = null) {
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
let body = '';
res.on('data', (chunk) => {
body += chunk;
});
res.on('end', () => {
try {
const response = JSON.parse(body);
resolve({ status: res.statusCode, data: response });
} catch (e) {
resolve({ status: res.statusCode, data: body });
}
});
});
req.on('error', (err) => {
reject(err);
});
if (data) {
req.write(JSON.stringify(data));
}
req.end();
});
}
async function testAPI() {
console.log('🔍 Testing LuckyChit API...\n');
try {
// Test health endpoint
console.log('1. Testing health endpoint...');
const healthResponse = await makeRequest({
hostname: 'localhost',
port: 3000,
path: '/health',
method: 'GET'
});
console.log('✅ Health check passed:', healthResponse.data.message);
console.log('');
// Test login endpoint
console.log('2. Testing login endpoint...');
const loginResponse = await makeRequest({
hostname: 'localhost',
port: 3000,
path: '/api/auth/login',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
}, {
mobile_number: '9876543210',
password: 'password123'
});
if (loginResponse.status === 200) {
console.log('✅ Login successful');
const token = loginResponse.data.data.token;
console.log('Token received:', token ? 'Yes' : 'No');
console.log('');
// Test chit groups endpoint with token
console.log('3. Testing chit groups endpoint with token...');
const groupsResponse = await makeRequest({
hostname: 'localhost',
port: 3000,
path: '/api/chit-groups/manager',
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (groupsResponse.status === 200) {
console.log('✅ Chit groups endpoint working');
const groups = groupsResponse.data.data?.chitGroups || [];
console.log('Groups found:', groups.length);
if (groups.length > 0) {
const groupId = groups[0].id;
console.log('Testing with group ID:', groupId);
console.log('');
// Test stats endpoint
console.log('4. Testing stats endpoint...');
const statsResponse = await makeRequest({
hostname: 'localhost',
port: 3000,
path: `/api/chit-groups/${groupId}/stats`,
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (statsResponse.status === 200) {
console.log('✅ Stats endpoint working');
console.log('Stats data received');
} else {
console.log('❌ Stats endpoint failed:', statsResponse.status, statsResponse.data.message);
}
// Test financial data endpoint
console.log('5. Testing financial data endpoint...');
const financialResponse = await makeRequest({
hostname: 'localhost',
port: 3000,
path: `/api/chit-groups/${groupId}/financial-data`,
method: 'GET',
headers: {
'Authorization': `Bearer ${token}`
}
});
if (financialResponse.status === 200) {
console.log('✅ Financial data endpoint working');
const financialData = financialResponse.data.data?.financial_data || [];
console.log('Financial entries:', financialData.length);
} else {
console.log('❌ Financial data endpoint failed:', financialResponse.status, financialResponse.data.message);
}
} else {
console.log('💡 No chit groups found. Create a chit group first.');
}
} else {
console.log('❌ Chit groups endpoint failed:', groupsResponse.status, groupsResponse.data.message);
}
} else {
console.log('❌ Login failed:', loginResponse.status, loginResponse.data.message);
console.log('💡 You may need to create a test user first');
}
} catch (error) {
console.error('❌ Test failed:', error.message);
}
console.log('\n🎉 API testing completed!');
}
testAPI();