08ea5fbe98
添加用户管理视图、API和状态管理文件
185 lines
5.8 KiB
TypeScript
185 lines
5.8 KiB
TypeScript
import { test, expect } from '@playwright/test';
|
|
import { MockManager } from './mock-manager';
|
|
|
|
test.describe('Token刷新机制', () => {
|
|
test.beforeEach(async ({ page }) => {
|
|
const mockManager = new MockManager({
|
|
enabled: true,
|
|
mode: 'full',
|
|
mockPaths: [],
|
|
delay: 0,
|
|
logCalls: true,
|
|
validateResponses: true,
|
|
dataSource: 'memory'
|
|
});
|
|
|
|
mockManager.presetTestData({
|
|
menus: [
|
|
{
|
|
id: 1,
|
|
name: '仪表盘',
|
|
code: 'dashboard',
|
|
path: '/dashboard',
|
|
icon: 'DashboardOutlined',
|
|
sortOrder: 1,
|
|
status: 'active',
|
|
parentId: 0,
|
|
createBy: 'system',
|
|
updateBy: 'system',
|
|
createdAt: '2024-01-01T00:00:00.000Z',
|
|
updatedAt: '2024-01-01T00:00:00.000Z',
|
|
children: []
|
|
}
|
|
]
|
|
});
|
|
|
|
await mockManager.interceptAPIRequest(page);
|
|
|
|
await page.goto('/');
|
|
await page.waitForLoadState('networkidle');
|
|
});
|
|
|
|
test('应该能够成功登录并获取token', async ({ page }) => {
|
|
const usernameInput = page.locator('input[placeholder="请输入用户名"]');
|
|
const passwordInput = page.locator('input[placeholder="请输入密码"]');
|
|
const loginButton = page.locator('button[type="submit"]');
|
|
|
|
await usernameInput.fill('admin');
|
|
await passwordInput.fill('admin123');
|
|
await loginButton.click();
|
|
|
|
await page.waitForURL(/.*dashboard/, { timeout: 10000 });
|
|
|
|
const token = await page.evaluate(() => localStorage.getItem('access_token'));
|
|
const refreshToken = await page.evaluate(() => localStorage.getItem('refreshToken'));
|
|
|
|
expect(token).toBeTruthy();
|
|
expect(refreshToken).toBeTruthy();
|
|
expect(token).toBe('mock-token-123456');
|
|
expect(refreshToken).toBe('mock-refresh-token-789012');
|
|
});
|
|
|
|
test('token过期时应该自动刷新', async ({ page }) => {
|
|
const usernameInput = page.locator('input[placeholder="请输入用户名"]');
|
|
const passwordInput = page.locator('input[placeholder="请输入密码"]');
|
|
const loginButton = page.locator('button[type="submit"]');
|
|
|
|
await usernameInput.fill('admin');
|
|
await passwordInput.fill('admin123');
|
|
await loginButton.click();
|
|
|
|
await page.waitForURL(/.*dashboard/, { timeout: 10000 });
|
|
|
|
await page.evaluate(() => {
|
|
localStorage.setItem('access_token', 'expired-token');
|
|
});
|
|
|
|
await page.reload();
|
|
|
|
await page.waitForTimeout(2000);
|
|
|
|
const newToken = await page.evaluate(() => localStorage.getItem('access_token'));
|
|
const newRefreshToken = await page.evaluate(() => localStorage.getItem('refreshToken'));
|
|
|
|
expect(newToken).toBeTruthy();
|
|
expect(newRefreshToken).toBeTruthy();
|
|
});
|
|
|
|
test('刷新token失败时应该跳转到登录页', async ({ page }) => {
|
|
const usernameInput = page.locator('input[placeholder="请输入用户名"]');
|
|
const passwordInput = page.locator('input[placeholder="请输入密码"]');
|
|
const loginButton = page.locator('button[type="submit"]');
|
|
|
|
await usernameInput.fill('admin');
|
|
await passwordInput.fill('admin123');
|
|
await loginButton.click();
|
|
|
|
await page.waitForURL(/.*dashboard/, { timeout: 10000 });
|
|
|
|
await page.evaluate(() => {
|
|
localStorage.setItem('access_token', 'expired-token');
|
|
localStorage.setItem('refreshToken', 'invalid-refresh-token');
|
|
});
|
|
|
|
await page.route('**/sys/auth/refresh', async (route) => {
|
|
await route.fulfill({
|
|
status: 200,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
code: '401',
|
|
message: 'Refresh token已过期',
|
|
data: null
|
|
})
|
|
});
|
|
});
|
|
|
|
await page.reload();
|
|
|
|
await page.waitForTimeout(3000);
|
|
|
|
const currentUrl = page.url();
|
|
expect(currentUrl).toContain('/login');
|
|
});
|
|
|
|
test('没有refresh token时应该跳转到登录页', async ({ page }) => {
|
|
const usernameInput = page.locator('input[placeholder="请输入用户名"]');
|
|
const passwordInput = page.locator('input[placeholder="请输入密码"]');
|
|
const loginButton = page.locator('button[type="submit"]');
|
|
|
|
await usernameInput.fill('admin');
|
|
await passwordInput.fill('admin123');
|
|
await loginButton.click();
|
|
|
|
await page.waitForURL(/.*dashboard/, { timeout: 10000 });
|
|
|
|
await page.evaluate(() => {
|
|
localStorage.removeItem('refreshToken');
|
|
});
|
|
|
|
await page.route('**/sys/user', async (route) => {
|
|
await route.fulfill({
|
|
status: 401,
|
|
contentType: 'application/json',
|
|
body: JSON.stringify({
|
|
code: '401',
|
|
message: 'Token已过期',
|
|
data: null
|
|
})
|
|
});
|
|
});
|
|
|
|
await page.reload();
|
|
|
|
await page.waitForTimeout(3000);
|
|
|
|
const currentUrl = page.url();
|
|
expect(currentUrl).toContain('/login');
|
|
});
|
|
|
|
test('token刷新成功后应该保持用户登录状态', async ({ page }) => {
|
|
const usernameInput = page.locator('input[placeholder="请输入用户名"]');
|
|
const passwordInput = page.locator('input[placeholder="请输入密码"]');
|
|
const loginButton = page.locator('button[type="submit"]');
|
|
|
|
await usernameInput.fill('admin');
|
|
await passwordInput.fill('admin123');
|
|
await loginButton.click();
|
|
|
|
await page.waitForURL(/.*dashboard/, { timeout: 10000 });
|
|
|
|
await page.evaluate(() => {
|
|
localStorage.setItem('access_token', 'expired-token');
|
|
});
|
|
|
|
await page.reload();
|
|
|
|
await page.waitForTimeout(2000);
|
|
|
|
const welcomeMessage = page.locator('text=/欢迎/i');
|
|
await expect(welcomeMessage).toBeVisible({ timeout: 5000 });
|
|
|
|
const newToken = await page.evaluate(() => localStorage.getItem('access_token'));
|
|
expect(newToken).toBeTruthy();
|
|
});
|
|
});
|