备份:修复前完整项目快照 2026-04-19
This commit is contained in:
@@ -0,0 +1,401 @@
|
||||
const http = require('http');
|
||||
const fs = require('fs');
|
||||
const path = require('path');
|
||||
|
||||
// 测试基础URL
|
||||
const BASE_URL = 'http://localhost:3005';
|
||||
|
||||
// 测试文件路径
|
||||
const TEST_IMAGE_PATH = path.join(__dirname, 'uploads', 'test-image.png');
|
||||
|
||||
// 测试数据
|
||||
const testUser = {
|
||||
username: 'admin',
|
||||
password: 'X123c321@'
|
||||
};
|
||||
|
||||
let authToken = '';
|
||||
|
||||
// HTTP请求函数
|
||||
function request(method, url, data = null, headers = {}, file = null) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const options = {
|
||||
method,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...headers
|
||||
}
|
||||
};
|
||||
|
||||
if (file) {
|
||||
// 处理文件上传
|
||||
const boundary = '--------------------------' + Date.now().toString(16);
|
||||
options.headers['Content-Type'] = 'multipart/form-data; boundary=' + boundary;
|
||||
}
|
||||
|
||||
const req = http.request(url, options, (res) => {
|
||||
let data = '';
|
||||
res.on('data', (chunk) => {
|
||||
data += chunk;
|
||||
});
|
||||
res.on('end', () => {
|
||||
try {
|
||||
resolve(JSON.parse(data));
|
||||
} catch (e) {
|
||||
resolve({ success: false, error: 'Invalid JSON response' });
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
req.on('error', (error) => {
|
||||
reject(error);
|
||||
});
|
||||
|
||||
if (data) {
|
||||
req.write(JSON.stringify(data));
|
||||
}
|
||||
|
||||
req.end();
|
||||
});
|
||||
}
|
||||
|
||||
// 测试套件
|
||||
async function runTests() {
|
||||
console.log('=== 开始财务流程测试 ===\n');
|
||||
|
||||
try {
|
||||
// 1. 登录系统...
|
||||
console.log('1. 登录系统...');
|
||||
const loginResponse = await request('POST', `${BASE_URL}/api/auth/login`, testUser);
|
||||
if (loginResponse.success) {
|
||||
console.log('✓ 登录成功');
|
||||
} else {
|
||||
console.log('✗ 登录失败:', loginResponse.error);
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. 测试预支申请流程
|
||||
console.log('\n2. 测试预支申请流程...');
|
||||
await testAdvanceFlow();
|
||||
|
||||
// 3. 测试报销申请流程
|
||||
console.log('\n3. 测试报销申请流程...');
|
||||
await testReimbursementFlow();
|
||||
|
||||
// 4. 测试付款申请流程
|
||||
console.log('\n4. 测试付款申请流程...');
|
||||
await testPaymentFlow();
|
||||
|
||||
// 5. 测试核销申请流程
|
||||
console.log('\n5. 测试核销申请流程...');
|
||||
await testVerificationFlow();
|
||||
|
||||
// 6. 测试执行记录查询
|
||||
console.log('\n6. 测试执行记录查询...');
|
||||
await testExecutionQueries();
|
||||
|
||||
console.log('\n=== 测试完成 ===');
|
||||
|
||||
} catch (error) {
|
||||
console.error('测试过程中发生错误:', error);
|
||||
}
|
||||
}
|
||||
|
||||
// 测试预支申请流程
|
||||
async function testAdvanceFlow() {
|
||||
console.log(' - 创建预支申请...');
|
||||
const advanceResponse = await request('POST', `${BASE_URL}/api/advances`, {
|
||||
advance_date: '2026-03-24',
|
||||
amount: 5000,
|
||||
currency: 'CNY',
|
||||
reason: '项目差旅预支',
|
||||
expense_type: 'project',
|
||||
project_id: '1',
|
||||
applicant: '测试用户',
|
||||
attachments: []
|
||||
});
|
||||
|
||||
if (advanceResponse.success) {
|
||||
const advanceId = advanceResponse.data.id;
|
||||
console.log(' ✓ 预支申请创建成功');
|
||||
|
||||
console.log(' - 提交预支申请到审批...');
|
||||
const submitResponse = await request('PUT', `${BASE_URL}/api/advances/${advanceId}/submit`, {});
|
||||
|
||||
if (submitResponse.success) {
|
||||
console.log(' ✓ 预支申请提交成功');
|
||||
|
||||
console.log(' - 审批预支申请...');
|
||||
const approveResponse = await request('PUT', `${BASE_URL}/api/advances/${advanceId}/approve`, {
|
||||
approver: '系统管理员'
|
||||
});
|
||||
|
||||
if (approveResponse.success) {
|
||||
console.log(' ✓ 预支申请审批成功');
|
||||
|
||||
console.log(' - 执行预支付款...');
|
||||
const executeResponse = await request('POST', `${BASE_URL}/api/executions`, {
|
||||
apply_id: advanceId,
|
||||
apply_type: 'advance',
|
||||
action: 'execute',
|
||||
execute_method: '银行转账',
|
||||
voucher_no: 'VCH' + Date.now(),
|
||||
remark: '测试执行'
|
||||
});
|
||||
|
||||
if (executeResponse.success) {
|
||||
console.log(' ✓ 预支付款执行成功');
|
||||
} else {
|
||||
console.log(' ✗ 预支付款执行失败:', executeResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 预支申请审批失败:', approveResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 预支申请提交失败:', submitResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 预支申请创建失败:', advanceResponse.error);
|
||||
}
|
||||
}
|
||||
|
||||
// 测试报销申请流程
|
||||
async function testReimbursementFlow() {
|
||||
console.log(' - 创建报销申请...');
|
||||
const reimbursementResponse = await request('POST', `${BASE_URL}/api/reimbursements`, {
|
||||
reimbursement_date: '2026-03-24',
|
||||
amount: 3500,
|
||||
currency: 'CNY',
|
||||
reason: '项目差旅报销',
|
||||
expense_type: 'project',
|
||||
project_id: '1',
|
||||
applicant: '测试用户',
|
||||
detail_items: [
|
||||
{
|
||||
description: '住宿费',
|
||||
amount: 2000,
|
||||
category: 'accommodation',
|
||||
attachments: []
|
||||
},
|
||||
{
|
||||
description: '餐饮费',
|
||||
amount: 1500,
|
||||
category: 'food',
|
||||
attachments: []
|
||||
}
|
||||
],
|
||||
attachments: []
|
||||
});
|
||||
|
||||
if (reimbursementResponse.success) {
|
||||
const reimbursementId = reimbursementResponse.data.id;
|
||||
console.log(' ✓ 报销申请创建成功');
|
||||
|
||||
console.log(' - 提交报销申请到审批...');
|
||||
const submitResponse = await request('PUT', `${BASE_URL}/api/reimbursements/${reimbursementId}/submit`, {});
|
||||
|
||||
if (submitResponse.success) {
|
||||
console.log(' ✓ 报销申请提交成功');
|
||||
|
||||
console.log(' - 审批报销申请...');
|
||||
const approveResponse = await request('PUT', `${BASE_URL}/api/reimbursements/${reimbursementId}/approve`, {
|
||||
approver: '系统管理员'
|
||||
});
|
||||
|
||||
if (approveResponse.success) {
|
||||
console.log(' ✓ 报销申请审批成功');
|
||||
|
||||
console.log(' - 执行报销付款...');
|
||||
const executeResponse = await request('POST', `${BASE_URL}/api/executions`, {
|
||||
apply_id: reimbursementId,
|
||||
apply_type: 'reimbursement',
|
||||
action: 'execute',
|
||||
execute_method: '银行转账',
|
||||
voucher_no: 'VCH' + Date.now(),
|
||||
remark: '测试执行'
|
||||
});
|
||||
|
||||
if (executeResponse.success) {
|
||||
console.log(' ✓ 报销付款执行成功');
|
||||
} else {
|
||||
console.log(' ✗ 报销付款执行失败:', executeResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 报销申请审批失败:', approveResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 报销申请提交失败:', submitResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 报销申请创建失败:', reimbursementResponse.error);
|
||||
}
|
||||
}
|
||||
|
||||
// 测试付款申请流程
|
||||
async function testPaymentFlow() {
|
||||
console.log(' - 创建付款申请...');
|
||||
const paymentResponse = await request('POST', `${BASE_URL}/api/payment-requests`, {
|
||||
payment_date: '2026-03-24',
|
||||
payee: '测试供应商',
|
||||
bank_account: '1234567890',
|
||||
bank_name: '测试银行',
|
||||
currency: 'CNY',
|
||||
reason: '设备采购款',
|
||||
detail_items: [
|
||||
{
|
||||
description: '设备款',
|
||||
amount: 50000,
|
||||
attachments: []
|
||||
}
|
||||
],
|
||||
applicant: '测试用户',
|
||||
attachments: []
|
||||
});
|
||||
|
||||
if (paymentResponse.success) {
|
||||
const paymentId = paymentResponse.data.id;
|
||||
console.log(' ✓ 付款申请创建成功');
|
||||
|
||||
console.log(' - 提交付款申请到审批...');
|
||||
const submitResponse = await request('PUT', `${BASE_URL}/api/payments/${paymentId}/submit`, {});
|
||||
|
||||
if (submitResponse.success) {
|
||||
console.log(' ✓ 付款申请提交成功');
|
||||
|
||||
console.log(' - 审批付款申请...');
|
||||
const approveResponse = await request('PUT', `${BASE_URL}/api/payments/${paymentId}/approve`, {
|
||||
approver: '系统管理员'
|
||||
});
|
||||
|
||||
if (approveResponse.success) {
|
||||
console.log(' ✓ 付款申请审批成功');
|
||||
|
||||
console.log(' - 执行付款...');
|
||||
const executeResponse = await request('POST', `${BASE_URL}/api/executions`, {
|
||||
apply_id: paymentId,
|
||||
apply_type: 'payment',
|
||||
action: 'execute',
|
||||
execute_method: '银行转账',
|
||||
voucher_no: 'VCH' + Date.now(),
|
||||
remark: '测试执行'
|
||||
});
|
||||
|
||||
if (executeResponse.success) {
|
||||
console.log(' ✓ 付款执行成功');
|
||||
} else {
|
||||
console.log(' ✗ 付款执行失败:', executeResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 付款申请审批失败:', approveResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 付款申请提交失败:', submitResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 付款申请创建失败:', paymentResponse.error);
|
||||
}
|
||||
}
|
||||
|
||||
// 测试核销申请流程
|
||||
async function testVerificationFlow() {
|
||||
console.log(' - 创建核销申请...');
|
||||
const verificationResponse = await request('POST', `${BASE_URL}/api/verifications`, {
|
||||
verification_date: '2026-03-24',
|
||||
advance_id: '1',
|
||||
actual_amount: 4500,
|
||||
balance: 500,
|
||||
reason: '项目差旅核销',
|
||||
applicant: '测试用户',
|
||||
detail_items: [
|
||||
{
|
||||
description: '住宿费',
|
||||
amount: 2500,
|
||||
category: 'accommodation',
|
||||
attachments: []
|
||||
},
|
||||
{
|
||||
description: '餐饮费',
|
||||
amount: 2000,
|
||||
category: 'food',
|
||||
attachments: []
|
||||
}
|
||||
],
|
||||
attachments: []
|
||||
});
|
||||
|
||||
if (verificationResponse.success) {
|
||||
const verificationId = verificationResponse.data.id;
|
||||
console.log(' ✓ 核销申请创建成功');
|
||||
|
||||
console.log(' - 提交核销申请到审批...');
|
||||
const submitResponse = await request('PUT', `${BASE_URL}/api/verifications/${verificationId}/submit`, {});
|
||||
|
||||
if (submitResponse.success) {
|
||||
console.log(' ✓ 核销申请提交成功');
|
||||
|
||||
console.log(' - 审批核销申请...');
|
||||
const approveResponse = await request('PUT', `${BASE_URL}/api/verifications/${verificationId}/approve`, {
|
||||
approver: '系统管理员'
|
||||
});
|
||||
|
||||
if (approveResponse.success) {
|
||||
console.log(' ✓ 核销申请审批成功');
|
||||
|
||||
console.log(' - 执行核销...');
|
||||
const executeResponse = await request('POST', `${BASE_URL}/api/executions`, {
|
||||
apply_id: verificationId,
|
||||
apply_type: 'verification',
|
||||
action: 'execute',
|
||||
execute_method: '银行转账',
|
||||
voucher_no: 'VCH' + Date.now(),
|
||||
remark: '测试执行'
|
||||
});
|
||||
|
||||
if (executeResponse.success) {
|
||||
console.log(' ✓ 核销执行成功');
|
||||
} else {
|
||||
console.log(' ✗ 核销执行失败:', executeResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 核销申请审批失败:', approveResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 核销申请提交失败:', submitResponse.error);
|
||||
}
|
||||
} else {
|
||||
console.log(' ✗ 核销申请创建失败:', verificationResponse.error);
|
||||
}
|
||||
}
|
||||
|
||||
// 测试执行记录查询
|
||||
async function testExecutionQueries() {
|
||||
console.log(' - 查询执行记录列表...');
|
||||
const historyResponse = await request('GET', `${BASE_URL}/api/executions`);
|
||||
|
||||
if (historyResponse.success) {
|
||||
console.log(' ✓ 执行记录查询成功,共', historyResponse.data.length, '条记录');
|
||||
} else {
|
||||
console.log(' ✗ 执行记录查询失败:', historyResponse.error);
|
||||
}
|
||||
|
||||
console.log(' - 查询待执行列表...');
|
||||
const pendingResponse = await request('GET', `${BASE_URL}/api/executions/pending`);
|
||||
|
||||
if (pendingResponse.success) {
|
||||
console.log(' ✓ 待执行列表查询成功,共', pendingResponse.data.length, '条记录');
|
||||
} else {
|
||||
console.log(' ✗ 待执行列表查询失败:', pendingResponse.error);
|
||||
}
|
||||
|
||||
console.log(' - 查询已执行列表...');
|
||||
const executedResponse = await request('GET', `${BASE_URL}/api/executions/executed`);
|
||||
|
||||
if (executedResponse.success) {
|
||||
console.log(' ✓ 已执行列表查询成功,共', executedResponse.data.length, '条记录');
|
||||
} else {
|
||||
console.log(' ✗ 已执行列表查询失败:', executedResponse.error);
|
||||
}
|
||||
}
|
||||
|
||||
// 运行测试
|
||||
runTests();
|
||||
Reference in New Issue
Block a user