369 lines
12 KiB
JavaScript
369 lines
12 KiB
JavaScript
const http = require('http');
|
|
|
|
// 测试基础URL
|
|
const BASE_URL = 'http://localhost:3005';
|
|
|
|
// 测试结果
|
|
const testResults = [];
|
|
|
|
// HTTP请求函数
|
|
function httpRequest(options, postData = null) {
|
|
return new Promise((resolve, reject) => {
|
|
const req = http.request(options, (res) => {
|
|
let data = '';
|
|
res.on('data', (chunk) => {
|
|
data += chunk;
|
|
});
|
|
res.on('end', () => {
|
|
resolve({ status: res.statusCode, data: JSON.parse(data) });
|
|
});
|
|
});
|
|
|
|
req.on('error', (e) => {
|
|
reject(e);
|
|
});
|
|
|
|
if (postData) {
|
|
req.write(JSON.stringify(postData));
|
|
}
|
|
|
|
req.end();
|
|
});
|
|
}
|
|
|
|
// 测试创建付款申请
|
|
async function testCreatePayment() {
|
|
console.log('\n💰 测试创建付款申请...');
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/payment-requests',
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
};
|
|
|
|
const paymentData = {
|
|
payment_date: '2026-03-24',
|
|
payee: '测试供应商',
|
|
bank_account: '1234567890123456789',
|
|
bank_name: '测试银行',
|
|
currency: 'CNY',
|
|
reason: '设备采购款',
|
|
detail_items: [
|
|
{
|
|
description: '设备1',
|
|
amount: 10000,
|
|
attachments: []
|
|
},
|
|
{
|
|
description: '设备2',
|
|
amount: 20000,
|
|
attachments: []
|
|
}
|
|
],
|
|
attachments: [],
|
|
applicant: '测试用户'
|
|
};
|
|
|
|
try {
|
|
const response = await httpRequest(options, paymentData);
|
|
console.log('创建付款申请响应状态:', response.status);
|
|
console.log('创建付款申请响应数据:', JSON.stringify(response.data, null, 2));
|
|
|
|
if (response.status === 200 && response.data.success) {
|
|
testResults.push({ test: '创建付款申请', status: '✅ 成功', paymentId: response.data.data.id });
|
|
console.log('✅ 付款申请创建成功,ID:', response.data.data.id);
|
|
return response.data.data.id;
|
|
} else {
|
|
testResults.push({ test: '创建付款申请', status: '❌ 失败', message: response.data.message || '创建失败' });
|
|
console.log('❌ 付款申请创建失败:', response.data.message);
|
|
return null;
|
|
}
|
|
} catch (error) {
|
|
testResults.push({ test: '创建付款申请', status: '❌ 失败', message: error.message });
|
|
console.log('❌ 付款申请创建失败:', error.message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
// 测试提交付款申请
|
|
async function testSubmitPayment(paymentId) {
|
|
console.log('\n📤 测试提交付款申请...');
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: `/api/payment-requests/${paymentId}/submit`,
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
};
|
|
|
|
try {
|
|
const response = await httpRequest(options);
|
|
console.log('提交付款申请响应状态:', response.status);
|
|
console.log('提交付款申请响应数据:', JSON.stringify(response.data, null, 2));
|
|
|
|
if (response.status === 200 && response.data.success) {
|
|
testResults.push({ test: '提交付款申请', status: '✅ 成功' });
|
|
console.log('✅ 付款申请提交成功');
|
|
return true;
|
|
} else {
|
|
testResults.push({ test: '提交付款申请', status: '❌ 失败', message: response.data.message || '提交失败' });
|
|
console.log('❌ 付款申请提交失败:', response.data.message);
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
testResults.push({ test: '提交付款申请', status: '❌ 失败', message: error.message });
|
|
console.log('❌ 付款申请提交失败:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 测试审批付款申请
|
|
async function testApprovePayment(paymentId) {
|
|
console.log('\n✅ 测试审批付款申请...');
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: `/api/payment-requests/${paymentId}/approve`,
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
};
|
|
|
|
try {
|
|
const response = await httpRequest(options, { remark: '审批通过' });
|
|
console.log('审批付款申请响应状态:', response.status);
|
|
console.log('审批付款申请响应数据:', JSON.stringify(response.data, null, 2));
|
|
|
|
if (response.status === 200 && response.data.success) {
|
|
testResults.push({ test: '审批付款申请', status: '✅ 成功' });
|
|
console.log('✅ 付款申请审批成功');
|
|
return true;
|
|
} else {
|
|
testResults.push({ test: '审批付款申请', status: '❌ 失败', message: response.data.message || '审批失败' });
|
|
console.log('❌ 付款申请审批失败:', response.data.message);
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
testResults.push({ test: '审批付款申请', status: '❌ 失败', message: error.message });
|
|
console.log('❌ 付款申请审批失败:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 测试执行付款申请
|
|
async function testExecutePayment(paymentId) {
|
|
console.log('\n💳 测试执行付款申请...');
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/executions',
|
|
method: 'POST',
|
|
headers: {
|
|
'Content-Type': 'application/json'
|
|
}
|
|
};
|
|
|
|
const executionData = {
|
|
apply_id: paymentId,
|
|
apply_type: 'payment',
|
|
action: 'execute',
|
|
execute_method: '银行转账',
|
|
voucher_no: 'VCH' + Date.now(),
|
|
remark: '测试执行'
|
|
};
|
|
|
|
try {
|
|
const response = await httpRequest(options, executionData);
|
|
console.log('执行付款申请响应状态:', response.status);
|
|
console.log('执行付款申请响应数据:', JSON.stringify(response.data, null, 2));
|
|
|
|
if (response.status === 200 && response.data.success) {
|
|
testResults.push({ test: '执行付款申请', status: '✅ 成功' });
|
|
console.log('✅ 付款申请执行成功');
|
|
return true;
|
|
} else {
|
|
testResults.push({ test: '执行付款申请', status: '❌ 失败', message: response.data.message || '执行失败' });
|
|
console.log('❌ 付款申请执行失败:', response.data.message);
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
testResults.push({ test: '执行付款申请', status: '❌ 失败', message: error.message });
|
|
console.log('❌ 付款申请执行失败:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 测试获取付款申请列表
|
|
async function testGetPayments() {
|
|
console.log('\n📋 测试获取付款申请列表...');
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/payment-requests',
|
|
method: 'GET'
|
|
};
|
|
|
|
try {
|
|
const response = await httpRequest(options);
|
|
console.log('获取付款申请列表响应状态:', response.status);
|
|
console.log('获取付款申请列表响应数据:', JSON.stringify(response.data, null, 2));
|
|
|
|
if (response.status === 200 && response.data.success) {
|
|
testResults.push({ test: '获取付款申请列表', status: '✅ 成功', message: `找到 ${response.data.data.length} 条记录` });
|
|
console.log('✅ 付款申请列表获取成功,找到', response.data.data.length, '条记录');
|
|
return true;
|
|
} else {
|
|
testResults.push({ test: '获取付款申请列表', status: '❌ 失败', message: response.data.message || '获取失败' });
|
|
console.log('❌ 付款申请列表获取失败:', response.data.message);
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
testResults.push({ test: '获取付款申请列表', status: '❌ 失败', message: error.message });
|
|
console.log('❌ 付款申请列表获取失败:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 测试待执行列表
|
|
async function testGetPendingExecutions() {
|
|
console.log('\n⏳ 测试获取待执行列表...');
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/executions/pending',
|
|
method: 'GET'
|
|
};
|
|
|
|
try {
|
|
const response = await httpRequest(options);
|
|
console.log('获取待执行列表响应状态:', response.status);
|
|
console.log('获取待执行列表响应数据:', JSON.stringify(response.data, null, 2));
|
|
|
|
if (response.status === 200 && response.data.success) {
|
|
testResults.push({ test: '获取待执行列表', status: '✅ 成功', message: `找到 ${response.data.data.length} 条记录` });
|
|
console.log('✅ 待执行列表获取成功,找到', response.data.data.length, '条记录');
|
|
return true;
|
|
} else {
|
|
testResults.push({ test: '获取待执行列表', status: '❌ 失败', message: response.data.message || '获取失败' });
|
|
console.log('❌ 待执行列表获取失败:', response.data.message);
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
testResults.push({ test: '获取待执行列表', status: '❌ 失败', message: error.message });
|
|
console.log('❌ 待执行列表获取失败:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 测试已执行列表
|
|
async function testGetExecutedExecutions() {
|
|
console.log('\n✅ 测试获取已执行列表...');
|
|
|
|
const options = {
|
|
hostname: 'localhost',
|
|
port: 3005,
|
|
path: '/api/executions/executed',
|
|
method: 'GET'
|
|
};
|
|
|
|
try {
|
|
const response = await httpRequest(options);
|
|
console.log('获取已执行列表响应状态:', response.status);
|
|
console.log('获取已执行列表响应数据:', JSON.stringify(response.data, null, 2));
|
|
|
|
if (response.status === 200 && response.data.success) {
|
|
testResults.push({ test: '获取已执行列表', status: '✅ 成功', message: `找到 ${response.data.data.length} 条记录` });
|
|
console.log('✅ 已执行列表获取成功,找到', response.data.data.length, '条记录');
|
|
return true;
|
|
} else {
|
|
testResults.push({ test: '获取已执行列表', status: '❌ 失败', message: response.data.message || '获取失败' });
|
|
console.log('❌ 已执行列表获取失败:', response.data.message);
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
testResults.push({ test: '获取已执行列表', status: '❌ 失败', message: error.message });
|
|
console.log('❌ 已执行列表获取失败:', error.message);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
// 运行所有测试
|
|
async function runAllTests() {
|
|
console.log('🚀 开始执行付款申请流程测试\n');
|
|
|
|
// 1. 获取付款申请列表
|
|
await testGetPayments();
|
|
|
|
// 2. 创建付款申请
|
|
const paymentId = await testCreatePayment();
|
|
|
|
if (paymentId) {
|
|
// 3. 提交付款申请
|
|
const submitSuccess = await testSubmitPayment(paymentId);
|
|
|
|
if (submitSuccess) {
|
|
// 4. 审批付款申请
|
|
const approveSuccess = await testApprovePayment(paymentId);
|
|
|
|
if (approveSuccess) {
|
|
// 5. 测试待执行列表
|
|
await testGetPendingExecutions();
|
|
|
|
// 6. 执行付款申请
|
|
const executeSuccess = await testExecutePayment(paymentId);
|
|
|
|
if (executeSuccess) {
|
|
// 7. 测试已执行列表
|
|
await testGetExecutedExecutions();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// 输出测试结果
|
|
console.log('\n📋 测试结果汇总:');
|
|
console.log('=============================================');
|
|
|
|
let successCount = 0;
|
|
let failureCount = 0;
|
|
|
|
testResults.forEach(result => {
|
|
console.log(`${result.test}: ${result.status}`);
|
|
if (result.status.includes('✅')) {
|
|
successCount++;
|
|
} else {
|
|
failureCount++;
|
|
}
|
|
});
|
|
|
|
console.log('=============================================');
|
|
console.log(`总测试数: ${testResults.length}`);
|
|
console.log(`成功: ${successCount}`);
|
|
console.log(`失败: ${failureCount}`);
|
|
|
|
if (failureCount === 0) {
|
|
console.log('\n🎉 所有测试通过!付款申请流程正常。');
|
|
} else {
|
|
console.log('\n⚠️ 部分测试失败,需要检查问题。');
|
|
}
|
|
|
|
console.log('\n测试完成。');
|
|
}
|
|
|
|
// 启动测试
|
|
runAllTests().catch(error => {
|
|
console.error('测试过程中出现错误:', error);
|
|
});
|