Files
jiebanke/test-admin-api.js

105 lines
3.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 axios = require('axios');
// API基础配置
const BASE_URL = 'http://localhost:3100';
// 测试管理员登录
async function testAdminLogin() {
try {
console.log('🔐 测试管理员登录接口...');
const response = await axios.post(`${BASE_URL}/api/v1/admin/login`, {
username: 'admin',
password: 'admin123' // 默认密码
});
console.log('✅ 登录成功:', response.data);
return response.data.data.token;
} catch (error) {
console.error('❌ 登录失败:', error.response?.data || error.message);
return null;
}
}
// 测试获取管理员信息
async function testGetAdminProfile(token) {
if (!token) return;
try {
console.log('\n👤 测试获取管理员信息接口...');
const response = await axios.get(`${BASE_URL}/api/v1/admin/profile`, {
headers: { Authorization: `Bearer ${token}` }
});
console.log('✅ 获取管理员信息成功:', response.data);
} catch (error) {
console.error('❌ 获取管理员信息失败:', error.response?.data || error.message);
}
}
// 测试获取用户列表
async function testGetUsers(token) {
if (!token) return;
try {
console.log('\n👥 测试获取用户列表接口...');
const response = await axios.get(`${BASE_URL}/api/v1/users`, {
headers: { Authorization: `Bearer ${token}` },
params: { page: 1, limit: 10 }
});
console.log('✅ 获取用户列表成功:');
console.log(' 总用户数:', response.data.data.total);
console.log(' 当前页用户数:', response.data.data.items.length);
} catch (error) {
console.error('❌ 获取用户列表失败:', error.response?.data || error.message);
}
}
// 测试获取系统信息
async function testGetSystemInfo(token) {
if (!token) return;
try {
console.log('\n💻 测试获取系统信息接口...');
const response = await axios.get(`${BASE_URL}/api/v1/admin/system/info`, {
headers: { Authorization: `Bearer ${token}` }
});
console.log('✅ 获取系统信息成功:');
console.log(' 系统版本:', response.data.data.version);
console.log(' 运行环境:', response.data.data.environment);
} catch (error) {
console.error('❌ 获取系统信息失败:', error.response?.data || error.message);
}
}
// 主测试函数
async function main() {
console.log('🎯 结伴客系统管理员API测试工具');
console.log('='.repeat(60));
// 检查后端服务是否运行
try {
await axios.get(`${BASE_URL}/health`);
console.log('✅ 后端服务正常运行');
} catch (error) {
console.log('⚠️ 后端服务未启动,请先启动后端服务');
console.log(' 运行命令: cd backend && npm start');
return;
}
const token = await testAdminLogin();
await testGetAdminProfile(token);
await testGetUsers(token);
await testGetSystemInfo(token);
console.log('\n' + '='.repeat(60));
console.log('🎉 API测试完成');
}
// 运行测试
main().catch(console.error);