08ea5fbe98
添加用户管理视图、API和状态管理文件
174 lines
4.7 KiB
TypeScript
174 lines
4.7 KiB
TypeScript
import { test, expect, APIRequestContext } from '@playwright/test';
|
|
|
|
test.describe('API 集成测试 - 用户管理', () => {
|
|
let apiContext: APIRequestContext;
|
|
let authToken: string;
|
|
|
|
test.beforeAll(async ({ playwright }) => {
|
|
apiContext = await playwright.request.newContext({
|
|
baseURL: process.env.API_BASE_URL || 'http://localhost:8080',
|
|
extraHTTPHeaders: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
});
|
|
|
|
test.afterAll(async () => {
|
|
await apiContext.dispose();
|
|
});
|
|
|
|
test('健康检查 - 服务应正常运行', async () => {
|
|
const response = await apiContext.get('/actuator/health');
|
|
expect(response.ok()).toBeTruthy();
|
|
|
|
const health = await response.json();
|
|
expect(health.status).toBe('UP');
|
|
});
|
|
|
|
test('用户注册 - 应成功创建新用户', async () => {
|
|
const timestamp = Date.now();
|
|
const response = await apiContext.post('/api/sys/auth/register', {
|
|
data: {
|
|
username: `testuser_${timestamp}`,
|
|
password: 'Test@123456',
|
|
email: `test_${timestamp}@example.com`,
|
|
phone: `138${timestamp.toString().slice(-8)}`,
|
|
},
|
|
});
|
|
|
|
expect(response.status()).toBe(200);
|
|
|
|
const user = await response.json();
|
|
expect(user).toHaveProperty('id');
|
|
expect(user.username).toContain('testuser_');
|
|
});
|
|
|
|
test('用户登录 - 应成功获取认证令牌', async () => {
|
|
const timestamp = Date.now();
|
|
const username = `loginuser_${timestamp}`;
|
|
|
|
await apiContext.post('/api/sys/auth/register', {
|
|
data: {
|
|
username,
|
|
password: 'Test@123456',
|
|
email: `login_${timestamp}@example.com`,
|
|
phone: `139${timestamp.toString().slice(-8)}`,
|
|
},
|
|
});
|
|
|
|
const response = await apiContext.post('/api/sys/auth/login', {
|
|
data: {
|
|
username,
|
|
password: 'Test@123456',
|
|
},
|
|
});
|
|
|
|
expect(response.ok()).toBeTruthy();
|
|
|
|
const loginResult = await response.json();
|
|
expect(loginResult).toHaveProperty('token');
|
|
authToken = loginResult.token;
|
|
});
|
|
|
|
test('获取用户信息 - 需要认证', async () => {
|
|
const timestamp = Date.now();
|
|
const username = `infouser_${timestamp}`;
|
|
|
|
await apiContext.post('/api/sys/auth/register', {
|
|
data: {
|
|
username,
|
|
password: 'Test@123456',
|
|
email: `info_${timestamp}@example.com`,
|
|
phone: `137${timestamp.toString().slice(-8)}`,
|
|
},
|
|
});
|
|
|
|
const loginResponse = await apiContext.post('/api/sys/auth/login', {
|
|
data: {
|
|
username,
|
|
password: 'Test@123456',
|
|
},
|
|
});
|
|
|
|
const { token } = await loginResponse.json();
|
|
|
|
const response = await apiContext.get('/api/sys/user/info', {
|
|
headers: {
|
|
'Authorization': `Bearer ${token}`,
|
|
},
|
|
});
|
|
|
|
expect(response.ok()).toBeTruthy();
|
|
|
|
const userInfo = await response.json();
|
|
expect(userInfo.username).toBe(username);
|
|
});
|
|
|
|
test('无认证访问 - 应返回401', async () => {
|
|
const response = await apiContext.get('/api/sys/user/info');
|
|
expect(response.status()).toBe(401);
|
|
});
|
|
});
|
|
|
|
test.describe('API 集成测试 - 角色管理', () => {
|
|
let apiContext: APIRequestContext;
|
|
let adminToken: string;
|
|
|
|
test.beforeAll(async ({ playwright }) => {
|
|
apiContext = await playwright.request.newContext({
|
|
baseURL: process.env.API_BASE_URL || 'http://localhost:8080',
|
|
extraHTTPHeaders: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
});
|
|
|
|
test.afterAll(async () => {
|
|
await apiContext.dispose();
|
|
});
|
|
|
|
test('查询角色列表 - 需要管理员权限', async () => {
|
|
const response = await apiContext.get('/api/sys/role/list', {
|
|
headers: {
|
|
'Authorization': `Bearer ${adminToken}`,
|
|
},
|
|
});
|
|
|
|
if (response.ok()) {
|
|
const roles = await response.json();
|
|
expect(Array.isArray(roles)).toBeTruthy();
|
|
} else {
|
|
expect(response.status()).toBe(401);
|
|
}
|
|
});
|
|
});
|
|
|
|
test.describe('API 集成测试 - 菜单管理', () => {
|
|
let apiContext: APIRequestContext;
|
|
|
|
test.beforeAll(async ({ playwright }) => {
|
|
apiContext = await playwright.request.newContext({
|
|
baseURL: process.env.API_BASE_URL || 'http://localhost:8080',
|
|
extraHTTPHeaders: {
|
|
'Content-Type': 'application/json',
|
|
},
|
|
});
|
|
});
|
|
|
|
test.afterAll(async () => {
|
|
await apiContext.dispose();
|
|
});
|
|
|
|
test('查询菜单树 - 需要认证', async () => {
|
|
const response = await apiContext.get('/api/sys/menu/tree');
|
|
|
|
if (response.status() === 401) {
|
|
expect(response.status()).toBe(401);
|
|
} else {
|
|
expect(response.ok()).toBeTruthy();
|
|
const menus = await response.json();
|
|
expect(Array.isArray(menus)).toBeTruthy();
|
|
}
|
|
});
|
|
});
|