08ea5fbe98
添加用户管理视图、API和状态管理文件
139 lines
5.0 KiB
TypeScript
139 lines
5.0 KiB
TypeScript
import { test, expect, Page } from '@playwright/test';
|
|
|
|
const BASE_URL = process.env.ADMIN_BASE_URL || 'http://localhost:5173';
|
|
|
|
test.describe('真实后端API认证测试', () => {
|
|
|
|
test.beforeEach(async ({ page }) => {
|
|
await page.goto(`${BASE_URL}/login`);
|
|
await page.waitForLoadState('networkidle');
|
|
});
|
|
|
|
test('登录页面 - 应正确显示', async ({ page }) => {
|
|
await expect(page.locator('[data-testid="username-input"]')).toBeVisible();
|
|
await expect(page.locator('[data-testid="password-input"]')).toBeVisible();
|
|
await expect(page.locator('[data-testid="login-button"]')).toBeVisible();
|
|
|
|
await expect(page.locator('text=/admin.*admin123/')).toBeVisible();
|
|
});
|
|
|
|
test('用户登录 - 成功', async ({ page }) => {
|
|
await page.fill('[data-testid="username-input"]', 'admin');
|
|
await page.fill('[data-testid="password-input"]', 'admin123');
|
|
|
|
await page.click('[data-testid="login-button"]');
|
|
|
|
await expect(page).toHaveURL(/.*\//, { timeout: 15000 });
|
|
|
|
const token = await page.evaluate(() => localStorage.getItem('token'));
|
|
expect(token).toBeTruthy();
|
|
|
|
const userInfo = await page.evaluate(() => localStorage.getItem('userInfo'));
|
|
expect(userInfo).toBeTruthy();
|
|
});
|
|
|
|
test('用户登录 - 错误密码应显示错误', async ({ page }) => {
|
|
await page.fill('[data-testid="username-input"]', 'admin');
|
|
await page.fill('[data-testid="password-input"]', 'wrongpassword');
|
|
|
|
await page.click('[data-testid="login-button"]');
|
|
|
|
await expect(page.locator('.ant-message-error, [role="alert"]')).toBeVisible({ timeout: 5000 });
|
|
|
|
await expect(page).toHaveURL(/.*login/, { timeout: 5000 });
|
|
});
|
|
|
|
test('用户登录 - 空用户名应显示验证错误', async ({ page }) => {
|
|
await page.fill('[data-testid="password-input"]', 'admin123');
|
|
|
|
await page.click('[data-testid="login-button"]');
|
|
|
|
await expect(page.locator('.ant-form-item-explain-error, [role="alert"]')).toBeVisible();
|
|
});
|
|
|
|
test('用户登录 - 空密码应显示验证错误', async ({ page }) => {
|
|
await page.fill('[data-testid="username-input"]', 'admin');
|
|
|
|
await page.click('[data-testid="login-button"]');
|
|
|
|
await expect(page.locator('.ant-form-item-explain-error, [role="alert"]')).toBeVisible();
|
|
});
|
|
|
|
test('记住我功能 - 应保存用户名', async ({ page }) => {
|
|
await page.fill('[data-testid="username-input"]', 'testuser');
|
|
await page.check('[data-testid="remember-me"]');
|
|
await page.fill('[data-testid="password-input"]', 'admin123');
|
|
|
|
await page.click('[data-testid="login-button"]');
|
|
|
|
const remembered = await page.evaluate(() => localStorage.getItem('rememberedUsername'));
|
|
expect(remembered).toBe('testuser');
|
|
});
|
|
});
|
|
|
|
test.describe('认证状态管理', () => {
|
|
|
|
test('已登录用户访问登录页应重定向到首页', async ({ page }) => {
|
|
await page.goto(`${BASE_URL}/login`);
|
|
await page.fill('[data-testid="username-input"]', 'admin');
|
|
await page.fill('[data-testid="password-input"]', 'admin123');
|
|
await page.click('[data-testid="login-button"]');
|
|
|
|
await expect(page).toHaveURL(/.*\//, { timeout: 15000 });
|
|
|
|
await page.goto(`${BASE_URL}/login`);
|
|
|
|
await expect(page).not.toHaveURL(/.*login/, { timeout: 5000 });
|
|
});
|
|
|
|
test('未登录用户访问受保护页面应重定向到登录页', async ({ page, context }) => {
|
|
await context.clearCookies();
|
|
await page.evaluate(() => localStorage.clear());
|
|
|
|
await page.goto(`${BASE_URL}/system/user`);
|
|
|
|
await expect(page).toHaveURL(/.*login/, { timeout: 10000 });
|
|
});
|
|
|
|
test('用户登出 - 应清除认证状态', async ({ page }) => {
|
|
await page.goto(`${BASE_URL}/login`);
|
|
await page.fill('[data-testid="username-input"]', 'admin');
|
|
await page.fill('[data-testid="password-input"]', 'admin123');
|
|
await page.click('[data-testid="login-button"]');
|
|
|
|
await expect(page).toHaveURL(/.*\//, { timeout: 15000 });
|
|
|
|
await page.click('.ant-dropdown-link');
|
|
await page.click('[data-testid="logout-button"]');
|
|
|
|
await expect(page).toHaveURL(/.*login/, { timeout: 10000 });
|
|
|
|
const token = await page.evaluate(() => localStorage.getItem('token'));
|
|
expect(token).toBeNull();
|
|
});
|
|
});
|
|
|
|
test.describe('Token 刷新机制', () => {
|
|
|
|
test('Token 过期后应自动刷新', async ({ page, context }) => {
|
|
await page.goto(`${BASE_URL}/login`);
|
|
await page.fill('[data-testid="username-input"]', 'admin');
|
|
await page.fill('[data-testid="password-input"]', 'admin123');
|
|
await page.click('[data-testid="login-button"]');
|
|
|
|
await expect(page).toHaveURL(/.*\//, { timeout: 15000 });
|
|
|
|
const originalToken = await page.evaluate(() => localStorage.getItem('token'));
|
|
|
|
await page.evaluate(() => {
|
|
localStorage.setItem('token', 'invalid_token_for_test');
|
|
});
|
|
|
|
await page.reload();
|
|
|
|
const newToken = await page.evaluate(() => localStorage.getItem('token'));
|
|
|
|
expect(newToken).toBeTruthy();
|
|
});
|
|
});
|