362 lines
12 KiB
JavaScript
362 lines
12 KiB
JavaScript
/**
|
|
* 附件功能完整性测试脚本
|
|
*
|
|
* 测试目标:
|
|
* 1. 验证附件上传API是否正常工作
|
|
* 2. 验证附件查看、下载、删除功能
|
|
* 3. 验证工单与附件的关联关系
|
|
* 4. 验证权限控制是否正确
|
|
*/
|
|
|
|
const axios = require('axios');
|
|
const FormData = require('form-data');
|
|
const fs = require('fs');
|
|
const path = require('path');
|
|
const colors = require('colors');
|
|
|
|
// 配置
|
|
const config = {
|
|
baseURL: 'http://localhost/dev-api',
|
|
timeout: 30000,
|
|
clientId: 'e5cd7e4891bf95d1d19206ce24a7b32e'
|
|
};
|
|
|
|
/**
|
|
* 获取Token(登录)
|
|
*/
|
|
async function getToken() {
|
|
try {
|
|
const { getToken } = require('./quick-token-getter');
|
|
return await getToken('admin', 'admin123');
|
|
} catch (error) {
|
|
console.error('获取Token失败:', error.message);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试创建临时工单
|
|
*/
|
|
async function createTestOrder(token) {
|
|
console.log('\n📋 创建测试工单'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
try {
|
|
const orderData = {
|
|
title: '附件功能测试工单',
|
|
description: '这是一个专门用于测试附件功能的工单',
|
|
repairLocation: '测试地点-附件功能验证',
|
|
categoryId: 1,
|
|
priorityLevel: 3,
|
|
reporterName: '测试用户',
|
|
reporterPhone: '13800138000',
|
|
reporterDept: '技术部'
|
|
};
|
|
|
|
const response = await axios.post(`${config.baseURL}/pm/repairOrder`, orderData, {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'Content-Type': 'application/json;charset=utf-8',
|
|
'clientid': config.clientId
|
|
},
|
|
timeout: config.timeout
|
|
});
|
|
|
|
if (response.data.code === 200) {
|
|
const orderId = response.data.data || response.data.id;
|
|
console.log('✅ 测试工单创建成功'.green);
|
|
console.log(` 工单ID: ${orderId}`);
|
|
return orderId;
|
|
} else {
|
|
console.log('❌ 测试工单创建失败'.red);
|
|
console.log(` 错误信息: ${response.data.msg}`);
|
|
return null;
|
|
}
|
|
} catch (error) {
|
|
console.log('❌ 测试工单创建异常'.red);
|
|
console.log(` 错误信息: ${error.message}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 创建测试文件
|
|
*/
|
|
function createTestFile() {
|
|
const testFilePath = path.join(__dirname, 'test-attachment.txt');
|
|
const testContent = `附件功能测试文件
|
|
创建时间: ${new Date().toLocaleString()}
|
|
测试内容: 这是一个用于测试附件上传功能的文本文件
|
|
文件大小: 适中,符合上传要求
|
|
测试目的: 验证文件上传、存储和下载功能是否正常
|
|
`;
|
|
|
|
fs.writeFileSync(testFilePath, testContent, 'utf8');
|
|
return testFilePath;
|
|
}
|
|
|
|
/**
|
|
* 测试单文件上传
|
|
*/
|
|
async function testSingleFileUpload(token, orderId) {
|
|
console.log('\n📤 测试单文件上传'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
try {
|
|
// 创建测试文件
|
|
const testFilePath = createTestFile();
|
|
|
|
// 准备FormData
|
|
const formData = new FormData();
|
|
formData.append('orderId', orderId);
|
|
formData.append('uploadStage', '1');
|
|
formData.append('description', '测试附件文件描述');
|
|
formData.append('file', fs.createReadStream(testFilePath));
|
|
|
|
const response = await axios.post(`${config.baseURL}/pm/attachment/upload`, formData, {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'clientid': config.clientId,
|
|
...formData.getHeaders()
|
|
},
|
|
timeout: config.timeout
|
|
});
|
|
|
|
// 清理测试文件
|
|
fs.unlinkSync(testFilePath);
|
|
|
|
if (response.data.code === 200) {
|
|
console.log('✅ 单文件上传成功'.green);
|
|
console.log(` 附件ID: ${response.data.data.id}`);
|
|
console.log(` 文件名: ${response.data.data.fileName}`);
|
|
console.log(` 文件大小: ${response.data.data.fileSize} 字节`);
|
|
console.log(` 存储路径: ${response.data.data.filePath}`);
|
|
return response.data.data;
|
|
} else {
|
|
console.log('❌ 单文件上传失败'.red);
|
|
console.log(` 错误信息: ${response.data.msg}`);
|
|
return null;
|
|
}
|
|
} catch (error) {
|
|
console.log('❌ 单文件上传异常'.red);
|
|
console.log(` 错误信息: ${error.message}`);
|
|
return null;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试获取附件列表
|
|
*/
|
|
async function testGetAttachmentList(token, orderId) {
|
|
console.log('\n📋 测试获取附件列表'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
try {
|
|
const response = await axios.get(`${config.baseURL}/pm/repairOrder/${orderId}/attachments`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'clientid': config.clientId
|
|
},
|
|
timeout: config.timeout
|
|
});
|
|
|
|
if (response.data.code === 200) {
|
|
const attachments = response.data.data || [];
|
|
console.log('✅ 获取附件列表成功'.green);
|
|
console.log(` 附件总数: ${attachments.length}`);
|
|
|
|
attachments.forEach((attachment, index) => {
|
|
console.log(` 附件${index + 1}:`);
|
|
console.log(` ID: ${attachment.id}`);
|
|
console.log(` 文件名: ${attachment.fileName}`);
|
|
console.log(` 文件类型: ${attachment.fileType}`);
|
|
console.log(` 上传人: ${attachment.uploaderName}`);
|
|
console.log(` 创建时间: ${attachment.createTime}`);
|
|
});
|
|
|
|
return attachments;
|
|
} else {
|
|
console.log('❌ 获取附件列表失败'.red);
|
|
console.log(` 错误信息: ${response.data.msg}`);
|
|
return [];
|
|
}
|
|
} catch (error) {
|
|
console.log('❌ 获取附件列表异常'.red);
|
|
console.log(` 错误信息: ${error.message}`);
|
|
return [];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 测试删除附件
|
|
*/
|
|
async function testDeleteAttachment(token, attachmentId) {
|
|
console.log('\n🗑️ 测试删除附件'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
try {
|
|
const response = await axios.delete(`${config.baseURL}/pm/attachment/file/${attachmentId}`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'clientid': config.clientId
|
|
},
|
|
timeout: config.timeout
|
|
});
|
|
|
|
if (response.data.code === 200) {
|
|
console.log('✅ 删除附件成功'.green);
|
|
console.log(` 附件ID: ${attachmentId}`);
|
|
return true;
|
|
} else {
|
|
console.log('❌ 删除附件失败'.red);
|
|
console.log(` 错误信息: ${response.data.msg}`);
|
|
return false;
|
|
}
|
|
} catch (error) {
|
|
console.log('❌ 删除附件异常'.red);
|
|
console.log(` 错误信息: ${error.message}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 清理测试数据
|
|
*/
|
|
async function cleanupTestData(token, orderId) {
|
|
console.log('\n🧹 清理测试数据'.blue.bold);
|
|
console.log('='.repeat(50).blue);
|
|
|
|
try {
|
|
// 删除测试工单
|
|
const response = await axios.delete(`${config.baseURL}/pm/repairOrder/${orderId}`, {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
'clientid': config.clientId
|
|
},
|
|
timeout: config.timeout
|
|
});
|
|
|
|
if (response.data.code === 200) {
|
|
console.log('✅ 测试工单删除成功'.green);
|
|
} else {
|
|
console.log('⚠️ 测试工单删除失败'.yellow);
|
|
console.log(` 错误信息: ${response.data.msg}`);
|
|
}
|
|
} catch (error) {
|
|
console.log('⚠️ 清理测试数据异常'.yellow);
|
|
console.log(` 错误信息: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 主测试函数
|
|
*/
|
|
async function runAttachmentTests() {
|
|
console.log('🚀 开始附件功能完整性测试'.cyan.bold);
|
|
console.log('测试时间:', new Date().toLocaleString());
|
|
console.log('测试环境:', config.baseURL);
|
|
|
|
let testResults = {
|
|
token: false,
|
|
createOrder: false,
|
|
uploadFile: false,
|
|
getAttachments: false,
|
|
deleteAttachment: false,
|
|
cleanup: false
|
|
};
|
|
|
|
try {
|
|
// 1. 获取Token
|
|
console.log('\n🔐 获取登录Token'.blue.bold);
|
|
const token = await getToken();
|
|
if (!token) {
|
|
console.log('❌ 无法获取Token,测试终止'.red.bold);
|
|
return;
|
|
}
|
|
testResults.token = true;
|
|
console.log('✅ Token获取成功'.green);
|
|
|
|
// 2. 创建测试工单
|
|
const orderId = await createTestOrder(token);
|
|
if (!orderId) {
|
|
console.log('❌ 无法创建测试工单,测试终止'.red.bold);
|
|
return;
|
|
}
|
|
testResults.createOrder = true;
|
|
|
|
// 3. 测试文件上传
|
|
const uploadedFile = await testSingleFileUpload(token, orderId);
|
|
if (uploadedFile) {
|
|
testResults.uploadFile = true;
|
|
}
|
|
|
|
// 4. 测试获取附件列表
|
|
const attachments = await testGetAttachmentList(token, orderId);
|
|
if (attachments.length > 0) {
|
|
testResults.getAttachments = true;
|
|
}
|
|
|
|
// 5. 测试删除附件
|
|
if (uploadedFile) {
|
|
const deleteSuccess = await testDeleteAttachment(token, uploadedFile.id);
|
|
if (deleteSuccess) {
|
|
testResults.deleteAttachment = true;
|
|
}
|
|
}
|
|
|
|
// 6. 清理测试数据
|
|
await cleanupTestData(token, orderId);
|
|
testResults.cleanup = true;
|
|
|
|
} catch (error) {
|
|
console.log('\n❌ 测试过程中发生异常'.red.bold);
|
|
console.log('错误信息:', error.message);
|
|
}
|
|
|
|
// 输出测试结果
|
|
console.log('\n📊 测试结果汇总'.cyan.bold);
|
|
console.log('='.repeat(60).cyan);
|
|
|
|
const results = [
|
|
{ name: 'Token获取', status: testResults.token },
|
|
{ name: '创建工单', status: testResults.createOrder },
|
|
{ name: '文件上传', status: testResults.uploadFile },
|
|
{ name: '获取附件', status: testResults.getAttachments },
|
|
{ name: '删除附件', status: testResults.deleteAttachment },
|
|
{ name: '数据清理', status: testResults.cleanup }
|
|
];
|
|
|
|
results.forEach(result => {
|
|
const status = result.status ? '✅ 通过'.green : '❌ 失败'.red;
|
|
console.log(`${result.name.padEnd(10)} : ${status}`);
|
|
});
|
|
|
|
const passedCount = results.filter(r => r.status).length;
|
|
const totalCount = results.length;
|
|
|
|
console.log('\n📈 测试统计'.cyan.bold);
|
|
console.log(`通过测试: ${passedCount}/${totalCount}`);
|
|
console.log(`成功率: ${((passedCount / totalCount) * 100).toFixed(1)}%`);
|
|
|
|
if (passedCount === totalCount) {
|
|
console.log('\n🎉 所有测试通过!附件功能运行正常!'.green.bold);
|
|
} else {
|
|
console.log('\n⚠️ 部分测试失败,请检查相关功能!'.yellow.bold);
|
|
}
|
|
}
|
|
|
|
// 运行测试
|
|
if (require.main === module) {
|
|
runAttachmentTests().catch(error => {
|
|
console.error('测试运行失败:', error);
|
|
process.exit(1);
|
|
});
|
|
}
|
|
|
|
module.exports = {
|
|
runAttachmentTests,
|
|
createTestOrder,
|
|
testSingleFileUpload,
|
|
testGetAttachmentList,
|
|
testDeleteAttachment
|
|
};
|