diff --git a/novalon-manage-web/e2e/global-setup.ts b/novalon-manage-web/e2e/global-setup.ts index fc25aa9..557cb65 100644 --- a/novalon-manage-web/e2e/global-setup.ts +++ b/novalon-manage-web/e2e/global-setup.ts @@ -81,6 +81,9 @@ async function globalSetup(config: FullConfig) { console.log('⏳ 等待后端服务就绪...'); await waitForBackendReady(); + console.log('🧹 清理测试数据...'); + await cleanupTestData(); + console.log('✅ 全局测试环境设置完成'); } @@ -110,4 +113,89 @@ async function waitForBackendReady(): Promise { throw new Error('❌ 后端服务启动超时'); } +async function cleanupTestData(): Promise { + try { + // 登录获取token + const loginResponse = await fetch('http://localhost:8084/api/auth/login', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + username: 'admin', + password: 'Test@123' + }) + }); + + if (!loginResponse.ok) { + console.log('⚠️ 无法登录,跳过数据清理'); + return; + } + + const loginData = await loginResponse.json(); + const token = loginData.token; + + // 获取所有用户 + const usersResponse = await fetch('http://localhost:8084/api/users', { + headers: { + 'Authorization': `Bearer ${token}` + } + }); + + if (usersResponse.ok) { + const users = await usersResponse.json(); + + // 删除测试创建的用户(保留ID 1-10的初始用户) + for (const user of users) { + if (user.id > 10) { + try { + await fetch(`http://localhost:8084/api/users/${user.id}`, { + method: 'DELETE', + headers: { + 'Authorization': `Bearer ${token}` + } + }); + console.log(` 删除用户: ${user.username}`); + } catch (error) { + console.log(` ⚠️ 无法删除用户 ${user.username}`); + } + } + } + } + + // 获取所有角色 + const rolesResponse = await fetch('http://localhost:8084/api/roles', { + headers: { + 'Authorization': `Bearer ${token}` + } + }); + + if (rolesResponse.ok) { + const roles = await rolesResponse.json(); + + // 删除测试创建的角色(保留ID 1-4的初始角色) + for (const role of roles) { + if (role.id > 4) { + try { + await fetch(`http://localhost:8084/api/roles/${role.id}`, { + method: 'DELETE', + headers: { + 'Authorization': `Bearer ${token}` + } + }); + console.log(` 删除角色: ${role.roleName}`); + } catch (error) { + console.log(` ⚠️ 无法删除角色 ${role.roleName}`); + } + } + } + } + + console.log('✅ 测试数据清理完成'); + } catch (error) { + console.log('⚠️ 数据清理失败,继续执行测试'); + console.error('清理错误:', error); + } +} + export default globalSetup;