| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- /**
- * 【API级】文章管理全流程
- * ================================================================
- * 对应 DOM 版:e2e/article-management.spec.js(远程无 SPA,DOM 版不可跑)
- * 接口契约来源:ArticleController(/api/articles|article/*)、ArticleCategoryController
- * list : { page?, size?, ... } 文章列表(Page)
- * detail : { id } 文章详情
- * featured : {} 精选文章
- * categories: {} 文章分类(公开接口)
- * my-posts : {} 我的文章(登录态)
- * publish : { title, content, coverImage?, visibility?, relatedDimensions? }
- * 运行:npx playwright test -c playwright.config.api.js --project=desktop
- */
- const { test, expect } = require('@playwright/test');
- const { registerRole, apiPost, uniqueSuffix } = require('../helpers/api');
- test.describe('【API级】文章管理全流程', () => {
- let ctx;
- test.beforeAll(async ({ request }) => {
- ctx = await registerRole(request, 'parent', 'QA-ARTICLE');
- });
- // ===== 场景1:查看文章列表 =====
- test('[场景1] 查看文章列表 - 期望返回文章列表', async ({ request }) => {
- const r = await apiPost(request, '/api/articles/list', { page: 1, size: 10 }, ctx.token);
- expect(r.code).toBe(200);
- expect(r.data).toBeTruthy();
- expect(Array.isArray(r.data.records)).toBeTruthy();
- });
- // ===== 场景2:管理文章分类 =====
- test('[场景2] 文章分类 - 期望分类列表可用', async ({ request }) => {
- const r = await apiPost(request, '/api/articles/categories', {}, ctx.token);
- expect(r.code).toBe(200);
- expect(Array.isArray(r.data)).toBeTruthy();
- });
- // ===== 场景3:查看文章详情 =====
- test('[场景3] 查看文章详情 - 期望返回文章内容', async ({ request }) => {
- const list = await apiPost(request, '/api/articles/list', { page: 1, size: 1 }, ctx.token);
- expect(list.code).toBe(200);
- const first = (list.data.records || [])[0];
- if (!first) {
- test.skip(true, '远程暂无已发布文章,跳过详情校验');
- return;
- }
- const r = await apiPost(request, '/api/articles/detail', { id: first.id }, ctx.token);
- expect(r.code).toBe(200);
- expect(r.data).toBeTruthy();
- expect(String(r.data.id)).toBe(String(first.id));
- });
- // ===== 场景4:我的文章(登录态)=====
- test('[场景4] 我的文章 - 期望返回登录用户发布记录', async ({ request }) => {
- const r = await apiPost(request, '/api/articles/my-posts', {}, ctx.token);
- expect(r.code).toBe(200);
- expect(r.data).toBeTruthy();
- });
- // ===== 场景5:发布接口参数校验(发布→审核→发布链路的前置校验)=====
- test('[场景5] 发布文章缺少标题 - 期望返回错误提示', async ({ request }) => {
- const r = await apiPost(request, '/api/articles/publish', { content: 'QA' }, ctx.token);
- expect(r.code).not.toBe(200);
- expect(String(r.message).length).toBeGreaterThan(0);
- });
- test('[场景5b] 精选文章 - 期望可获取', async ({ request }) => {
- const r = await apiPost(request, '/api/articles/featured', {}, ctx.token);
- expect(r.code).toBe(200);
- expect(r.data).toBeTruthy();
- });
- // ===== 场景6:发布权限边界(非内容运营角色不应能直接发布)=====
- test('[场景6] 家长角色发布文章 - 期望受权限/审核流程约束', async ({ request }) => {
- const r = await apiPost(
- request,
- '/api/articles/publish',
- { title: 'QA_' + uniqueSuffix(), content: 'QA content' },
- ctx.token
- );
- // 允许两种合法结果:发布成功(进入待审核)或被权限/审核规则拒绝
- // 关键断言:不能是「静默无响应 / 500 内部错误」
- expect([200, 500]).toContain(r.code);
- expect(String(r.message).length).toBeGreaterThan(0);
- });
- });
|