Codex Assistant
1 天以前 58d9f460b2f8c34430285115e2557d18333c5cab
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/**
 * 测试电话号码授权要求功能
 * 验证:
 * 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();