chore: sync local latest state and repository cleanup
This commit is contained in:
@@ -1,59 +1,79 @@
|
||||
import { test, expect } from '@playwright/test';
|
||||
|
||||
/**
|
||||
* 简化版E2E测试 - API可用性验证
|
||||
* 验证后端服务是否正常运行
|
||||
* E2E测试 - API可用性验证
|
||||
* 支持两种模式:
|
||||
* - E2E_STRICT=false (默认): 连通性模式,401/403视为可达
|
||||
* - E2E_STRICT=true: 严格业务模式,需要真实凭证
|
||||
*/
|
||||
|
||||
test.describe('🦟 蚊子项目 E2E测试 - API可用性验证', () => {
|
||||
|
||||
|
||||
const API_BASE_URL = process.env.API_BASE_URL || 'http://localhost:8080';
|
||||
|
||||
const E2E_STRICT = process.env.E2E_STRICT === 'true';
|
||||
const E2E_USER_TOKEN = process.env.E2E_USER_TOKEN;
|
||||
|
||||
test('后端健康检查', async ({ request }) => {
|
||||
const response = await request.get(`${API_BASE_URL}/actuator/health`);
|
||||
|
||||
|
||||
expect(response.ok()).toBeTruthy();
|
||||
|
||||
|
||||
const body = await response.json();
|
||||
expect(body.status).toBe('UP');
|
||||
|
||||
|
||||
console.log('✅ 后端服务健康检查通过');
|
||||
});
|
||||
|
||||
test('活动列表API可用性', async ({ request }) => {
|
||||
const response = await request.get(`${API_BASE_URL}/api/v1/activities`, {
|
||||
headers: {
|
||||
'X-API-Key': 'test',
|
||||
'Authorization': 'Bearer test',
|
||||
},
|
||||
});
|
||||
|
||||
// API需要认证,401是预期的安全行为
|
||||
// 我们验证API端点存在且响应格式正确即可
|
||||
expect([200, 401]).toContain(response.status());
|
||||
|
||||
console.log(`✅ 活动列表API端点可访问,状态码: ${response.status()}`);
|
||||
|
||||
if (response.status() === 200) {
|
||||
const body = await response.json();
|
||||
expect(body.code).toBe(200);
|
||||
console.log(` 返回 ${body.data?.length || 0} 个活动`);
|
||||
test('活动列表API可达性验证', async ({ request }) => {
|
||||
// 活动列表API需要认证
|
||||
const response = await request.get(`${API_BASE_URL}/api/v1/activities`);
|
||||
|
||||
const status = response.status();
|
||||
|
||||
// 严格模式下必须有真实凭证,无凭证则失败
|
||||
if (E2E_STRICT) {
|
||||
if (!E2E_USER_TOKEN) {
|
||||
throw new Error('严格模式需要E2E_USER_TOKEN环境变量,但未提供,测试失败');
|
||||
}
|
||||
|
||||
// 严格模式:必须返回200
|
||||
if (status === 401 || status === 403) {
|
||||
throw new Error(`严格模式下活动列表API需要认证但未提供有效凭证(HTTP ${status})`);
|
||||
}
|
||||
if (status >= 400 && status < 500) {
|
||||
throw new Error(`活动列表API客户端错误(HTTP ${status})`);
|
||||
}
|
||||
if (status >= 500) {
|
||||
throw new Error(`活动列表API服务器错误(HTTP ${status}),服务异常`);
|
||||
}
|
||||
expect(status).toBe(200);
|
||||
console.log(`✅ 严格模式:活动列表API业务成功,HTTP状态码: ${status}`);
|
||||
} else {
|
||||
console.log(' API需要有效认证(这是预期的安全行为)');
|
||||
// 连通性模式:401/403表示API可达但需要认证
|
||||
if (status === 404) {
|
||||
throw new Error(`活动列表API不存在(HTTP 404),端点路径可能错误`);
|
||||
}
|
||||
if (status >= 500) {
|
||||
throw new Error(`活动列表API服务器错误(HTTP ${status}),服务异常`);
|
||||
}
|
||||
|
||||
// 连通性模式允许401/403代表API可达
|
||||
expect([401, 403, 200]).toContain(status);
|
||||
console.log(`✅ 连通性模式:活动列表API可达,HTTP状态码: ${status}`);
|
||||
}
|
||||
});
|
||||
|
||||
test('前端服务可访问', async ({ page }) => {
|
||||
const FRONTEND_URL = process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:5175';
|
||||
|
||||
test('前端服务可访问', async ({ page }, testInfo) => {
|
||||
const FRONTEND_URL = process.env.PLAYWRIGHT_BASE_URL || 'http://localhost:5173';
|
||||
|
||||
await page.goto(FRONTEND_URL);
|
||||
|
||||
|
||||
// 验证页面加载
|
||||
await expect(page).toHaveTitle(/./);
|
||||
|
||||
// 截图记录
|
||||
await page.screenshot({ path: 'e2e-report/frontend-check.png' });
|
||||
|
||||
|
||||
// 截图记录到 Playwright 输出目录,避免污染仓库根目录
|
||||
await page.screenshot({ path: testInfo.outputPath('frontend-check.png') });
|
||||
|
||||
console.log('✅ 前端服务可访问');
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user