You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
360 lines
11 KiB
360 lines
11 KiB
// 测试新的身份验证机制
|
|
const WebSocket = require('ws');
|
|
const readline = require('readline');
|
|
const http = require('http');
|
|
|
|
// 创建readline接口用于用户输入
|
|
const rl = readline.createInterface({
|
|
input: process.stdin,
|
|
output: process.stdout
|
|
});
|
|
|
|
// 服务器地址
|
|
const SERVER_URL = 'ws://localhost:3003';
|
|
const API_URL = 'http://localhost:3003/api/managers';
|
|
|
|
// 测试用例数据
|
|
const TEST_DATA = {
|
|
// 有效的客服ID
|
|
validManagerId: '22',
|
|
// 无效的客服ID
|
|
invalidManagerId: '9999',
|
|
// 有效的普通用户ID
|
|
validUserId: 'user_123456',
|
|
// 无效的普通用户ID
|
|
invalidUserId: 'user_999999'
|
|
};
|
|
|
|
console.log('===== 开始测试新的身份验证机制 =====');
|
|
console.log('测试目标:验证新实现的用户和客服身份验证逻辑');
|
|
|
|
/**
|
|
* 测试场景1:有效的普通用户认证
|
|
*/
|
|
async function testValidUserAuthentication() {
|
|
console.log('\n=== 测试场景1: 有效的普通用户认证 ===');
|
|
|
|
return new Promise((resolve) => {
|
|
const ws = new WebSocket(SERVER_URL);
|
|
|
|
ws.on('open', () => {
|
|
console.log('✅ WebSocket连接已建立');
|
|
|
|
// 准备认证消息
|
|
const authMessage = {
|
|
type: 'auth',
|
|
userId: TEST_DATA.validUserId,
|
|
userType: 'customer',
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
console.log(`发送用户认证请求: ${JSON.stringify(authMessage)}`);
|
|
ws.send(JSON.stringify(authMessage));
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('收到服务器响应:', JSON.stringify(message));
|
|
|
|
// 检查认证结果
|
|
if (message.type === 'auth_success') {
|
|
console.log('✅ 测试成功: 有效的普通用户ID正确通过认证');
|
|
resolve({ success: true, scenario: 'valid_user' });
|
|
} else if (message.type === 'auth_error') {
|
|
console.error('❌ 测试失败: 有效的普通用户ID被错误拒绝');
|
|
resolve({ success: false, scenario: 'valid_user', error: message.message });
|
|
}
|
|
|
|
// 关闭连接
|
|
ws.close();
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
console.error('WebSocket错误:', error);
|
|
resolve({ success: false, scenario: 'valid_user', error: error.message });
|
|
});
|
|
|
|
// 设置超时
|
|
setTimeout(() => {
|
|
console.error('❌ 测试超时: 未收到服务器响应');
|
|
ws.close();
|
|
resolve({ success: false, scenario: 'valid_user', error: 'timeout' });
|
|
}, 10000);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 测试场景2:无效的普通用户认证
|
|
*/
|
|
async function testInvalidUserAuthentication() {
|
|
console.log('\n=== 测试场景2: 无效的普通用户认证 ===');
|
|
|
|
return new Promise((resolve) => {
|
|
const ws = new WebSocket(SERVER_URL);
|
|
|
|
ws.on('open', () => {
|
|
console.log('✅ WebSocket连接已建立');
|
|
|
|
// 准备认证消息
|
|
const authMessage = {
|
|
type: 'auth',
|
|
userId: TEST_DATA.invalidUserId,
|
|
userType: 'customer',
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
console.log(`发送无效用户认证请求: ${JSON.stringify(authMessage)}`);
|
|
ws.send(JSON.stringify(authMessage));
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('收到服务器响应:', JSON.stringify(message));
|
|
|
|
// 检查是否收到认证错误消息
|
|
if (message.type === 'auth_error') {
|
|
console.log('✅ 测试成功: 无效的用户ID正确被拒绝认证');
|
|
resolve({ success: true, scenario: 'invalid_user' });
|
|
} else if (message.type === 'auth_success') {
|
|
console.error('❌ 测试失败: 无效的用户ID错误地通过了认证');
|
|
resolve({ success: false, scenario: 'invalid_user', error: 'invalid_user_accepted' });
|
|
}
|
|
|
|
// 关闭连接
|
|
ws.close();
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
console.error('WebSocket错误:', error);
|
|
resolve({ success: false, scenario: 'invalid_user', error: error.message });
|
|
});
|
|
|
|
// 设置超时
|
|
setTimeout(() => {
|
|
console.error('❌ 测试超时: 未收到服务器响应');
|
|
ws.close();
|
|
resolve({ success: false, scenario: 'invalid_user', error: 'timeout' });
|
|
}, 10000);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 测试场景3:有效的客服认证
|
|
*/
|
|
async function testValidManagerAuthentication() {
|
|
console.log('\n=== 测试场景3: 有效的客服认证 ===');
|
|
|
|
return new Promise((resolve) => {
|
|
const ws = new WebSocket(SERVER_URL);
|
|
|
|
ws.on('open', () => {
|
|
console.log('✅ WebSocket连接已建立');
|
|
|
|
// 准备客服认证消息
|
|
const authMessage = {
|
|
type: 'auth',
|
|
managerId: TEST_DATA.validManagerId,
|
|
userType: 'manager',
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
console.log(`发送客服认证请求: ${JSON.stringify(authMessage)}`);
|
|
ws.send(JSON.stringify(authMessage));
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('收到服务器响应:', JSON.stringify(message));
|
|
|
|
// 检查认证结果
|
|
if (message.type === 'auth_success' &&
|
|
(message.payload && message.payload.type === 'manager') ||
|
|
(message.userType === 'manager')) {
|
|
console.log('✅ 测试成功: 有效的客服ID正确通过认证');
|
|
resolve({ success: true, scenario: 'valid_manager' });
|
|
} else if (message.type === 'auth_error') {
|
|
console.error('❌ 测试失败: 有效的客服ID被错误拒绝');
|
|
resolve({ success: false, scenario: 'valid_manager', error: message.message });
|
|
}
|
|
|
|
// 关闭连接
|
|
ws.close();
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
console.error('WebSocket错误:', error);
|
|
resolve({ success: false, scenario: 'valid_manager', error: error.message });
|
|
});
|
|
|
|
// 设置超时
|
|
setTimeout(() => {
|
|
console.error('❌ 测试超时: 未收到服务器响应');
|
|
ws.close();
|
|
resolve({ success: false, scenario: 'valid_manager', error: 'timeout' });
|
|
}, 10000);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 测试场景4:无效的客服认证
|
|
*/
|
|
async function testInvalidManagerAuthentication() {
|
|
console.log('\n=== 测试场景4: 无效的客服认证 ===');
|
|
|
|
return new Promise((resolve) => {
|
|
const ws = new WebSocket(SERVER_URL);
|
|
|
|
ws.on('open', () => {
|
|
console.log('✅ WebSocket连接已建立');
|
|
|
|
// 准备无效客服认证消息
|
|
const authMessage = {
|
|
type: 'auth',
|
|
managerId: TEST_DATA.invalidManagerId,
|
|
userType: 'manager',
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
console.log(`发送无效客服认证请求: ${JSON.stringify(authMessage)}`);
|
|
ws.send(JSON.stringify(authMessage));
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('收到服务器响应:', JSON.stringify(message));
|
|
|
|
// 检查是否收到认证错误消息
|
|
if (message.type === 'auth_error') {
|
|
console.log('✅ 测试成功: 无效的客服ID正确被拒绝认证');
|
|
resolve({ success: true, scenario: 'invalid_manager' });
|
|
} else if (message.type === 'auth_success') {
|
|
console.error('❌ 测试失败: 无效的客服ID错误地通过了认证');
|
|
resolve({ success: false, scenario: 'invalid_manager', error: 'invalid_manager_accepted' });
|
|
}
|
|
|
|
// 关闭连接
|
|
ws.close();
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
console.error('WebSocket错误:', error);
|
|
resolve({ success: false, scenario: 'invalid_manager', error: error.message });
|
|
});
|
|
|
|
// 设置超时
|
|
setTimeout(() => {
|
|
console.error('❌ 测试超时: 未收到服务器响应');
|
|
ws.close();
|
|
resolve({ success: false, scenario: 'invalid_manager', error: 'timeout' });
|
|
}, 10000);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 测试场景5:使用普通userId作为managerId的认证(应该失败)
|
|
*/
|
|
async function testUserIdAsManagerId() {
|
|
console.log('\n=== 测试场景5: 使用普通userId作为managerId的认证 ===');
|
|
console.log('验证:取消了userId作为managerId的容错处理');
|
|
|
|
return new Promise((resolve) => {
|
|
const ws = new WebSocket(SERVER_URL);
|
|
|
|
ws.on('open', () => {
|
|
console.log('✅ WebSocket连接已建立');
|
|
|
|
// 准备使用普通userId作为managerId的认证消息
|
|
const authMessage = {
|
|
type: 'auth',
|
|
managerId: TEST_DATA.validUserId, // 使用普通userId作为managerId
|
|
userType: 'manager',
|
|
timestamp: Date.now()
|
|
};
|
|
|
|
console.log(`发送错误格式认证请求: ${JSON.stringify(authMessage)}`);
|
|
ws.send(JSON.stringify(authMessage));
|
|
});
|
|
|
|
ws.on('message', (data) => {
|
|
const message = JSON.parse(data.toString());
|
|
console.log('收到服务器响应:', JSON.stringify(message));
|
|
|
|
// 检查是否收到认证错误消息
|
|
if (message.type === 'auth_error') {
|
|
console.log('✅ 测试成功: 普通userId作为managerId被正确拒绝');
|
|
resolve({ success: true, scenario: 'userId_as_managerId' });
|
|
} else if (message.type === 'auth_success') {
|
|
console.error('❌ 测试失败: 普通userId作为managerId错误地通过了认证');
|
|
resolve({ success: false, scenario: 'userId_as_managerId', error: 'userId_accepted_as_managerId' });
|
|
}
|
|
|
|
// 关闭连接
|
|
ws.close();
|
|
});
|
|
|
|
ws.on('error', (error) => {
|
|
console.error('WebSocket错误:', error);
|
|
resolve({ success: false, scenario: 'userId_as_managerId', error: error.message });
|
|
});
|
|
|
|
// 设置超时
|
|
setTimeout(() => {
|
|
console.error('❌ 测试超时: 未收到服务器响应');
|
|
ws.close();
|
|
resolve({ success: false, scenario: 'userId_as_managerId', error: 'timeout' });
|
|
}, 10000);
|
|
});
|
|
}
|
|
|
|
/**
|
|
* 运行所有测试
|
|
*/
|
|
async function runAllTests() {
|
|
const results = [];
|
|
|
|
// 按顺序运行测试
|
|
try {
|
|
// 先测试用户认证
|
|
results.push(await testValidUserAuthentication());
|
|
results.push(await testInvalidUserAuthentication());
|
|
|
|
// 再测试客服认证
|
|
results.push(await testValidManagerAuthentication());
|
|
results.push(await testInvalidManagerAuthentication());
|
|
|
|
// 测试特殊场景
|
|
results.push(await testUserIdAsManagerId());
|
|
|
|
} catch (error) {
|
|
console.error('测试过程中发生错误:', error);
|
|
}
|
|
|
|
// 输出测试总结
|
|
console.log('\n===== 测试结果总结 =====');
|
|
|
|
const passedTests = results.filter(r => r.success).length;
|
|
const totalTests = results.length;
|
|
|
|
console.log(`总测试数: ${totalTests}`);
|
|
console.log(`通过测试: ${passedTests}`);
|
|
console.log(`失败测试: ${totalTests - passedTests}`);
|
|
|
|
// 输出失败的测试详情
|
|
const failedTests = results.filter(r => !r.success);
|
|
if (failedTests.length > 0) {
|
|
console.log('\n失败测试详情:');
|
|
failedTests.forEach(test => {
|
|
console.log(`- ${test.scenario}: ${test.error || '未知错误'}`);
|
|
});
|
|
}
|
|
|
|
// 输出最终结果
|
|
const overallResult = passedTests === totalTests;
|
|
console.log('\n===== 最终结果 =====');
|
|
console.log(`整体测试结果: ${overallResult ? '✅ 通过' : '❌ 失败'}`);
|
|
|
|
rl.close();
|
|
}
|
|
|
|
// 启动测试
|
|
runAllTests();
|
|
|