Files
nxxmdata/bank-backend/test-api-integration.js

292 lines
8.0 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const http = require('http');
// 测试配置
const BASE_URL = 'http://localhost:5351';
let authToken = '';
// 辅助函数发送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 result = JSON.parse(body);
resolve({ status: res.statusCode, data: result });
} catch (error) {
resolve({ status: res.statusCode, data: body });
}
});
});
req.on('error', (error) => {
reject(error);
});
if (data) {
req.write(JSON.stringify(data));
}
req.end();
});
}
// 测试登录
async function testLogin() {
console.log('🔐 测试登录...');
try {
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/auth/login',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const result = await makeRequest(options, {
username: 'admin',
password: 'Admin123456'
});
if (result.status === 200 && result.data.success) {
authToken = result.data.data.token;
console.log('✅ 登录成功');
return true;
} else {
console.log('❌ 登录失败:', result.data.message);
return false;
}
} catch (error) {
console.log('❌ 登录请求失败:', error.message);
return false;
}
}
// 测试仪表盘统计
async function testDashboardStats() {
console.log('📊 测试仪表盘统计...');
try {
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/dashboard',
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`
}
};
const result = await makeRequest(options);
if (result.status === 200 && result.data.success) {
console.log('✅ 仪表盘统计获取成功');
console.log(' - 总用户数:', result.data.data.overview?.totalUsers || 0);
console.log(' - 总账户数:', result.data.data.overview?.totalAccounts || 0);
console.log(' - 今日交易数:', result.data.data.today?.transactionCount || 0);
return true;
} else {
console.log('❌ 仪表盘统计获取失败:', result.data.message);
return false;
}
} catch (error) {
console.log('❌ 仪表盘统计请求失败:', error.message);
return false;
}
}
// 测试用户列表
async function testUsersList() {
console.log('👥 测试用户列表...');
try {
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/users?page=1&pageSize=10',
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`
}
};
const result = await makeRequest(options);
if (result.status === 200 && result.data.success) {
console.log('✅ 用户列表获取成功');
console.log(' - 用户数量:', result.data.data.users?.length || 0);
console.log(' - 分页信息:', result.data.data.pagination);
return true;
} else {
console.log('❌ 用户列表获取失败:', result.data.message);
return false;
}
} catch (error) {
console.log('❌ 用户列表请求失败:', error.message);
return false;
}
}
// 测试账户列表
async function testAccountsList() {
console.log('🏦 测试账户列表...');
try {
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/accounts?page=1&pageSize=10',
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`
}
};
const result = await makeRequest(options);
if (result.status === 200 && result.data.success) {
console.log('✅ 账户列表获取成功');
console.log(' - 账户数量:', result.data.data.accounts?.length || 0);
console.log(' - 分页信息:', result.data.data.pagination);
return true;
} else {
console.log('❌ 账户列表获取失败:', result.data.message);
return false;
}
} catch (error) {
console.log('❌ 账户列表请求失败:', error.message);
return false;
}
}
// 测试交易记录列表
async function testTransactionsList() {
console.log('💳 测试交易记录列表...');
try {
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/transactions?page=1&pageSize=10',
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`
}
};
const result = await makeRequest(options);
if (result.status === 200 && result.data.success) {
console.log('✅ 交易记录列表获取成功');
console.log(' - 交易数量:', result.data.data.transactions?.length || 0);
console.log(' - 分页信息:', result.data.data.pagination);
return true;
} else {
console.log('❌ 交易记录列表获取失败:', result.data.message);
return false;
}
} catch (error) {
console.log('❌ 交易记录列表请求失败:', error.message);
return false;
}
}
// 测试员工列表
async function testEmployeesList() {
console.log('👨‍💼 测试员工列表...');
try {
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/employees?page=1&pageSize=10',
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`
}
};
const result = await makeRequest(options);
if (result.status === 200 && result.data.success) {
console.log('✅ 员工列表获取成功');
console.log(' - 员工数量:', result.data.data.employees?.length || 0);
console.log(' - 分页信息:', result.data.data.pagination);
return true;
} else {
console.log('❌ 员工列表获取失败:', result.data.message);
return false;
}
} catch (error) {
console.log('❌ 员工列表请求失败:', error.message);
return false;
}
}
// 测试贷款产品列表
async function testLoanProductsList() {
console.log('💰 测试贷款产品列表...');
try {
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/loan-products?page=1&pageSize=10',
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`
}
};
const result = await makeRequest(options);
if (result.status === 200 && result.data.success) {
console.log('✅ 贷款产品列表获取成功');
console.log(' - 产品数量:', result.data.data.products?.length || 0);
console.log(' - 分页信息:', result.data.data.pagination);
return true;
} else {
console.log('❌ 贷款产品列表获取失败:', result.data.message);
return false;
}
} catch (error) {
console.log('❌ 贷款产品列表请求失败:', error.message);
return false;
}
}
// 主测试函数
async function runTests() {
console.log('🚀 开始API集成测试...\n');
const tests = [
{ name: '登录', fn: testLogin },
{ name: '仪表盘统计', fn: testDashboardStats },
{ name: '用户列表', fn: testUsersList },
{ name: '账户列表', fn: testAccountsList },
{ name: '交易记录列表', fn: testTransactionsList },
{ name: '员工列表', fn: testEmployeesList },
{ name: '贷款产品列表', fn: testLoanProductsList }
];
let passed = 0;
let total = tests.length;
for (const test of tests) {
try {
const success = await test.fn();
if (success) {
passed++;
}
} catch (error) {
console.log(`${test.name}测试异常:`, error.message);
}
console.log(''); // 空行分隔
}
console.log('📋 测试结果汇总:');
console.log(`✅ 通过: ${passed}/${total}`);
console.log(`❌ 失败: ${total - passed}/${total}`);
if (passed === total) {
console.log('🎉 所有测试通过API集成正常');
} else {
console.log('⚠️ 部分测试失败请检查相关API接口');
}
}
// 运行测试
runTests().catch(console.error);