Files
ai-showcase-platform/scripts/test-profile-update.js

124 lines
3.9 KiB
JavaScript

const bcrypt = require('bcryptjs');
const mysql = require('mysql2/promise');
// 資料庫配置
const dbConfig = {
host: process.env.DB_HOST || 'mysql.theaken.com',
port: parseInt(process.env.DB_PORT || '33306'),
user: process.env.DB_USER || 'AI_Platform',
password: process.env.DB_PASSWORD || 'Aa123456',
database: process.env.DB_NAME || 'db_AI_Platform',
charset: 'utf8mb4',
timezone: '+08:00'
};
async function testProfileUpdate() {
console.log('🧪 測試個人資料更新功能...\n');
try {
const connection = await mysql.createConnection(dbConfig);
console.log('✅ 資料庫連接成功');
// 1. 測試查詢用戶資料(包含新字段)
console.log('1. 測試查詢用戶資料...');
const [users] = await connection.execute(`
SELECT id, name, email, department, role, phone, location, bio, created_at, updated_at
FROM users
WHERE email = 'admin@ai-platform.com'
`);
if (users.length > 0) {
const user = users[0];
console.log('✅ 找到用戶:', {
name: user.name,
email: user.email,
department: user.department,
role: user.role,
phone: user.phone || '未設定',
location: user.location || '未設定',
bio: user.bio || '未設定'
});
} else {
console.log('❌ 未找到用戶');
return;
}
// 2. 測試更新個人資料
console.log('\n2. 測試更新個人資料...');
const userId = users[0].id;
const updateData = {
phone: '0912-345-678',
location: '台北市信義區',
bio: '這是系統管理員的個人簡介,負責管理整個 AI 展示平台。'
};
const [updateResult] = await connection.execute(`
UPDATE users
SET phone = ?, location = ?, bio = ?, updated_at = CURRENT_TIMESTAMP
WHERE id = ?
`, [updateData.phone, updateData.location, updateData.bio, userId]);
console.log('更新結果:', updateResult);
// 3. 驗證更新結果
console.log('\n3. 驗證更新結果...');
const [updatedUsers] = await connection.execute(`
SELECT id, name, email, department, role, phone, location, bio, updated_at
FROM users
WHERE id = ?
`, [userId]);
if (updatedUsers.length > 0) {
const updatedUser = updatedUsers[0];
console.log('✅ 更新後的用戶資料:');
console.log(`- 姓名: ${updatedUser.name}`);
console.log(`- 電子郵件: ${updatedUser.email}`);
console.log(`- 部門: ${updatedUser.department}`);
console.log(`- 角色: ${updatedUser.role}`);
console.log(`- 電話: ${updatedUser.phone}`);
console.log(`- 地點: ${updatedUser.location}`);
console.log(`- 個人簡介: ${updatedUser.bio}`);
console.log(`- 更新時間: ${updatedUser.updated_at}`);
}
// 4. 測試 API 端點
console.log('\n4. 測試 API 端點...');
try {
const response = await fetch('http://localhost:3000/api/auth/profile', {
method: 'PUT',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
userId: userId,
phone: '0987-654-321',
location: '新北市板橋區',
bio: '透過 API 更新的個人簡介'
})
});
if (response.ok) {
const data = await response.json();
console.log('✅ API 更新成功:', {
name: data.user.name,
phone: data.user.phone,
location: data.user.location,
bio: data.user.bio
});
} else {
console.log('❌ API 更新失敗:', response.status, await response.text());
}
} catch (error) {
console.log('❌ API 測試錯誤:', error.message);
}
await connection.end();
console.log('\n🎉 個人資料更新測試完成!');
} catch (error) {
console.error('❌ 測試過程中發生錯誤:', error);
}
}
testProfileUpdate();