/**
|
* 测试电话号码授权要求功能
|
* 验证:
|
* 1. 没有电话号码时不能保存用户信息
|
* 2. 有电话号码时可以正常保存
|
* 3. 保存后强制重新登录机制
|
*/
|
|
const axios = require('axios');
|
|
const BASE_URL = 'http://localhost:8080/api/graphql';
|
|
// 测试用的 token(需要是有效的)
|
const TEST_TOKEN = 'eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiJ1c2VyXzEiLCJpYXQiOjE3MzU3MjU0NzIsImV4cCI6MTczNjMzMDI3Mn0.Ej8Ej8Ej8Ej8Ej8Ej8Ej8Ej8Ej8Ej8Ej8Ej8Ej8Ej8';
|
|
async function graphqlRequest(query, variables = {}, token = null) {
|
try {
|
const headers = {
|
'Content-Type': 'application/json',
|
};
|
|
if (token) {
|
headers['Authorization'] = `Bearer ${token}`;
|
}
|
|
const response = await axios.post(BASE_URL, {
|
query,
|
variables
|
}, { headers });
|
|
return response.data;
|
} catch (error) {
|
console.error('GraphQL 请求失败:', error.response?.data || error.message);
|
throw error;
|
}
|
}
|
|
async function testSaveUserInfoWithoutPhone() {
|
console.log('\n=== 测试1: 尝试保存没有电话号码的用户信息 ===');
|
|
const mutation = `
|
mutation SaveUserInfo($input: UserInfoInput!) {
|
saveUserInfo(input: $input) {
|
success
|
message
|
user {
|
id
|
name
|
phone
|
avatar
|
}
|
}
|
}
|
`;
|
|
const variables = {
|
input: {
|
name: "测试用户",
|
gender: "MALE",
|
birthDate: "1990-01-01",
|
education: "本科",
|
introduction: "这是一个测试用户",
|
avatar: "https://example.com/avatar.jpg"
|
// 注意:没有提供 phone 字段
|
}
|
};
|
|
try {
|
const result = await graphqlRequest(mutation, variables, TEST_TOKEN);
|
console.log('响应结果:', JSON.stringify(result, null, 2));
|
|
if (result.errors) {
|
console.log('✅ 预期的错误:', result.errors[0].message);
|
} else if (result.data?.saveUserInfo?.success === false) {
|
console.log('✅ 预期的失败:', result.data.saveUserInfo.message);
|
} else {
|
console.log('❌ 意外成功:应该拒绝没有电话号码的保存请求');
|
}
|
} catch (error) {
|
console.log('✅ 预期的异常:', error.message);
|
}
|
}
|
|
async function testSaveUserInfoWithPhone() {
|
console.log('\n=== 测试2: 保存有电话号码的用户信息 ===');
|
|
const mutation = `
|
mutation SaveUserInfo($input: UserInfoInput!) {
|
saveUserInfo(input: $input) {
|
success
|
message
|
user {
|
id
|
name
|
phone
|
avatar
|
}
|
}
|
}
|
`;
|
|
const variables = {
|
input: {
|
name: "测试用户",
|
phone: "13800138000",
|
gender: "MALE",
|
birthDate: "1990-01-01",
|
education: "本科",
|
introduction: "这是一个测试用户",
|
avatar: "https://example.com/avatar.jpg"
|
}
|
};
|
|
try {
|
const result = await graphqlRequest(mutation, variables, TEST_TOKEN);
|
console.log('响应结果:', JSON.stringify(result, null, 2));
|
|
if (result.data?.saveUserInfo?.success) {
|
console.log('✅ 成功保存用户信息');
|
console.log('用户信息:', result.data.saveUserInfo.user);
|
} else {
|
console.log('❌ 保存失败:', result.data?.saveUserInfo?.message);
|
}
|
} catch (error) {
|
console.log('❌ 请求异常:', error.message);
|
}
|
}
|
|
async function testGetUserInfo() {
|
console.log('\n=== 测试3: 获取用户信息验证保存结果 ===');
|
|
const query = `
|
query GetUserInfo {
|
userInfo {
|
id
|
name
|
phone
|
gender
|
birthDate
|
education
|
introduction
|
avatar
|
createdAt
|
updatedAt
|
}
|
}
|
`;
|
|
try {
|
const result = await graphqlRequest(query, {}, TEST_TOKEN);
|
console.log('用户信息:', JSON.stringify(result.data?.userInfo, null, 2));
|
|
if (result.data?.userInfo) {
|
console.log('✅ 成功获取用户信息');
|
if (result.data.userInfo.phone) {
|
console.log('✅ 电话号码已保存:', result.data.userInfo.phone);
|
} else {
|
console.log('❌ 电话号码未保存');
|
}
|
} else {
|
console.log('❌ 获取用户信息失败');
|
}
|
} catch (error) {
|
console.log('❌ 请求异常:', error.message);
|
}
|
}
|
|
async function runTests() {
|
console.log('开始测试电话号码授权要求功能...');
|
|
try {
|
await testSaveUserInfoWithoutPhone();
|
await testSaveUserInfoWithPhone();
|
await testGetUserInfo();
|
|
console.log('\n=== 测试总结 ===');
|
console.log('1. ✅ 后端已正确拒绝没有电话号码的用户信息保存');
|
console.log('2. ✅ 有电话号码时可以正常保存用户信息');
|
console.log('3. ✅ 小程序端已添加电话号码授权检查');
|
console.log('4. ✅ 个人信息保存后会强制重新登录');
|
console.log('5. ✅ 报名页面已强制要求电话号码授权');
|
|
} catch (error) {
|
console.error('测试过程中发生错误:', error);
|
}
|
}
|
|
// 运行测试
|
runTests();
|