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

389 lines
10 KiB
JavaScript
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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 API_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 responseData = '';
res.on('data', (chunk) => {
responseData += chunk;
});
res.on('end', () => {
try {
const result = {
statusCode: res.statusCode,
headers: res.headers,
data: responseData ? JSON.parse(responseData) : null
};
resolve(result);
} catch (error) {
reject(new Error(`解析响应失败: ${error.message}`));
}
});
});
req.on('error', (error) => {
reject(error);
});
if (data) {
req.write(JSON.stringify(data));
}
req.end();
});
}
// 登录获取认证令牌
async function login() {
console.log('🔐 正在登录...');
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/auth/login',
method: 'POST',
headers: {
'Content-Type': 'application/json'
}
};
const loginData = {
username: 'admin',
password: 'Admin123456'
};
try {
const response = await makeRequest(options, loginData);
if (response.statusCode === 200 && response.data.success) {
authToken = response.data.data.token;
console.log('✅ 登录成功');
return true;
} else {
console.error('❌ 登录失败:', response.data.message);
return false;
}
} catch (error) {
console.error('❌ 登录请求失败:', error.message);
return false;
}
}
// 测试获取项目列表
async function testGetProjects() {
console.log('\n📋 测试获取项目列表...');
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/projects',
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
};
try {
const response = await makeRequest(options);
if (response.statusCode === 200 && response.data.success) {
console.log('✅ 获取项目列表成功');
console.log(` 项目数量: ${response.data.data.projects.length}`);
console.log(` 总数量: ${response.data.data.pagination.total}`);
// 显示前3个项目的基本信息
const projects = response.data.data.projects.slice(0, 3);
projects.forEach((project, index) => {
console.log(` 项目${index + 1}: ${project.name} (${project.status}) - ${project.farmName}`);
});
return true;
} else {
console.error('❌ 获取项目列表失败:', response.data.message);
return false;
}
} catch (error) {
console.error('❌ 获取项目列表请求失败:', error.message);
return false;
}
}
// 测试获取项目统计
async function testGetProjectStats() {
console.log('\n📊 测试获取项目统计...');
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/projects/stats',
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
};
try {
const response = await makeRequest(options);
if (response.statusCode === 200 && response.data.success) {
console.log('✅ 获取项目统计成功');
const stats = response.data.data;
console.log(` 总项目数: ${stats.total}`);
console.log(` 监管中: ${stats.supervision}`);
console.log(` 已结项: ${stats.completed}`);
console.log(` 总监管金额: ${stats.totalAmount?.toFixed(2) || 0}`);
console.log(` 总监管数量: ${stats.totalQuantity || 0}`);
return true;
} else {
console.error('❌ 获取项目统计失败:', response.data.message);
return false;
}
} catch (error) {
console.error('❌ 获取项目统计请求失败:', error.message);
return false;
}
}
// 测试创建项目
async function testCreateProject() {
console.log('\n 测试创建项目...');
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/projects',
method: 'POST',
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
};
const projectData = {
name: '测试项目',
status: 'supervision',
farmName: '测试养殖场',
supervisionObject: '牛',
supervisionQuantity: 20,
supervisionPeriod: '30天',
supervisionAmount: 50000.00,
startTime: '2024-01-01',
endTime: '2024-12-31',
earTag: 10,
collar: 10,
host: 1,
loanOfficer: '测试专员',
description: '这是一个测试项目'
};
try {
const response = await makeRequest(options, projectData);
if (response.statusCode === 201 && response.data.success) {
console.log('✅ 创建项目成功');
console.log(` 项目ID: ${response.data.data.id}`);
console.log(` 项目名称: ${response.data.data.name}`);
return response.data.data.id;
} else {
console.error('❌ 创建项目失败:', response.data.message);
return null;
}
} catch (error) {
console.error('❌ 创建项目请求失败:', error.message);
return null;
}
}
// 测试获取项目详情
async function testGetProjectById(projectId) {
console.log('\n🔍 测试获取项目详情...');
const options = {
hostname: 'localhost',
port: 5351,
path: `/api/projects/${projectId}`,
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
};
try {
const response = await makeRequest(options);
if (response.statusCode === 200 && response.data.success) {
console.log('✅ 获取项目详情成功');
const project = response.data.data;
console.log(` 项目名称: ${project.name}`);
console.log(` 养殖场: ${project.farmName}`);
console.log(` 监管对象: ${project.supervisionObject}`);
console.log(` 监管数量: ${project.supervisionQuantity}`);
console.log(` 监管金额: ${project.supervisionAmount}`);
return true;
} else {
console.error('❌ 获取项目详情失败:', response.data.message);
return false;
}
} catch (error) {
console.error('❌ 获取项目详情请求失败:', error.message);
return false;
}
}
// 测试更新项目
async function testUpdateProject(projectId) {
console.log('\n✏ 测试更新项目...');
const options = {
hostname: 'localhost',
port: 5351,
path: `/api/projects/${projectId}`,
method: 'PUT',
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
};
const updateData = {
supervisionQuantity: 25,
supervisionAmount: 60000.00,
description: '更新后的项目描述'
};
try {
const response = await makeRequest(options, updateData);
if (response.statusCode === 200 && response.data.success) {
console.log('✅ 更新项目成功');
const project = response.data.data;
console.log(` 更新后监管数量: ${project.supervisionQuantity}`);
console.log(` 更新后监管金额: ${project.supervisionAmount}`);
return true;
} else {
console.error('❌ 更新项目失败:', response.data.message);
return false;
}
} catch (error) {
console.error('❌ 更新项目请求失败:', error.message);
return false;
}
}
// 测试删除项目
async function testDeleteProject(projectId) {
console.log('\n🗑 测试删除项目...');
const options = {
hostname: 'localhost',
port: 5351,
path: `/api/projects/${projectId}`,
method: 'DELETE',
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
};
try {
const response = await makeRequest(options);
if (response.statusCode === 200 && response.data.success) {
console.log('✅ 删除项目成功');
return true;
} else {
console.error('❌ 删除项目失败:', response.data.message);
return false;
}
} catch (error) {
console.error('❌ 删除项目请求失败:', error.message);
return false;
}
}
// 测试搜索项目
async function testSearchProjects() {
console.log('\n🔍 测试搜索项目...');
const options = {
hostname: 'localhost',
port: 5351,
path: '/api/projects?search=张洪彬&status=completed',
method: 'GET',
headers: {
'Authorization': `Bearer ${authToken}`,
'Content-Type': 'application/json'
}
};
try {
const response = await makeRequest(options);
if (response.statusCode === 200 && response.data.success) {
console.log('✅ 搜索项目成功');
console.log(` 搜索结果数量: ${response.data.data.projects.length}`);
const projects = response.data.data.projects;
projects.forEach((project, index) => {
console.log(` 结果${index + 1}: ${project.name} - ${project.farmName}`);
});
return true;
} else {
console.error('❌ 搜索项目失败:', response.data.message);
return false;
}
} catch (error) {
console.error('❌ 搜索项目请求失败:', error.message);
return false;
}
}
// 主测试函数
async function runTests() {
console.log('🚀 开始测试项目清单API接口...\n');
// 1. 登录
const loginSuccess = await login();
if (!loginSuccess) {
console.log('❌ 登录失败,无法继续测试');
return;
}
// 2. 测试获取项目列表
await testGetProjects();
// 3. 测试获取项目统计
await testGetProjectStats();
// 4. 测试搜索项目
await testSearchProjects();
// 5. 测试创建项目
const projectId = await testCreateProject();
if (projectId) {
// 6. 测试获取项目详情
await testGetProjectById(projectId);
// 7. 测试更新项目
await testUpdateProject(projectId);
// 8. 测试删除项目
await testDeleteProject(projectId);
}
console.log('\n🎉 项目清单API接口测试完成');
}
// 运行测试
runTests().catch(console.error);