Files
digital-psychology/apps/admin-h5/e2e/admin-ops-smoke.spec.ts
T
jackyu66gitandCursor b5a05941d9 feat(ECR-013A): Admin RBAC 实现并 Closed
角色权限、RequirePermission、/me permissions 与 migration 000015;
Reviewer Approve → Closed。Next:ECR-013B Contract Definition。

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-08-07 17:19:45 +08:00

184 lines
6.0 KiB
TypeScript

import { test, expect } from '@playwright/test'
/** Admin ops console: login → dashboard → users → detail grant → audit (API mocked). */
test('admin login users grant and audit with mocked API', async ({ page }) => {
const userId = '11111111-1111-1111-1111-111111111111'
let granted = false
await page.route('**/api/v1/admin/**', async (route) => {
const req = route.request()
const url = req.url()
const method = req.method()
const ok = (data: unknown) =>
route.fulfill({
status: 200,
contentType: 'application/json',
body: JSON.stringify({ code: 0, message: 'success', data }),
})
if (url.includes('/auth/login') && method === 'POST') {
await ok({
token: 'adm_e2e_token',
expires_at: new Date(Date.now() + 3600_000).toISOString(),
admin: {
id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
username: 'admin',
role: 'super_admin',
permissions: [
'admin.users.read',
'admin.users.membership.grant',
'admin.users.ask_quota.grant',
'admin.orders.read',
'admin.audit.read',
'admin.analytics.read',
'admin.content.write',
'admin.roles.read',
'admin.roles.write',
],
},
})
return
}
if (url.includes('/auth/logout') && method === 'POST') {
await ok({ ok: true })
return
}
if (url.includes('/stats')) {
await ok({
users_total: 1,
membership_active: granted ? 1 : 0,
orders_today: 0,
paid_cents_today: 0,
ask_replies_today: 0,
profiles_total: 1,
reports_total: 2,
series: [
{ day: '2026-08-01', new_users: 0, orders: 1, paid_cents: 990, ask_replies: 2 },
{ day: '2026-08-02', new_users: 1, orders: 0, paid_cents: 0, ask_replies: 1 },
{ day: '2026-08-03', new_users: 0, orders: 2, paid_cents: 1980, ask_replies: 3 },
{ day: '2026-08-04', new_users: 0, orders: 0, paid_cents: 0, ask_replies: 0 },
{ day: '2026-08-05', new_users: 0, orders: 1, paid_cents: 2500, ask_replies: 4 },
{ day: '2026-08-06', new_users: 0, orders: 0, paid_cents: 0, ask_replies: 2 },
{ day: '2026-08-07', new_users: 0, orders: 1, paid_cents: 990, ask_replies: 1 },
],
reports_by_type: [
{ type: 'portrait', count: 1 },
{ type: 'star', count: 1 },
],
})
return
}
if (url.endsWith('/me') || url.includes('/me?')) {
await ok({
id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
username: 'admin',
role: 'super_admin',
permissions: [
'admin.users.read',
'admin.users.membership.grant',
'admin.users.ask_quota.grant',
'admin.orders.read',
'admin.audit.read',
'admin.analytics.read',
'admin.content.write',
'admin.roles.read',
'admin.roles.write',
],
})
return
}
if (url.includes(`/users/${userId}/ask-quota/grant`) && method === 'POST') {
await ok({ ok: true, ask_paid_quota_left: 40 })
return
}
if (url.includes(`/users/${userId}/membership/grant`) && method === 'POST') {
granted = true
await ok({ ok: true })
return
}
if (url.includes(`/users/${userId}`) && method === 'GET') {
await ok({
id: userId,
status: 'active',
phone: '13800000000',
nickname: '测试用户',
ask_paid_quota_left: 0,
created_at: '2026-08-01T00:00:00Z',
profiles: [{ id: 'p1', relation: 'self', display_name: '我', birth_date: '1990-01-01' }],
reports: [],
membership: {
active: granted,
plan: granted ? 'month' : undefined,
status: granted ? 'active' : 'none',
expires_at: granted ? '2026-09-01T00:00:00Z' : undefined,
ask_quota_left: granted ? 100 : 0,
},
recent_orders: [],
})
return
}
if (url.includes('/users') && method === 'GET') {
await ok({
items: [
{
id: userId,
status: 'active',
phone: '13800000000',
nickname: '测试用户',
ask_paid_quota_left: 0,
profile_count: 1,
membership_active: granted,
membership_plan: granted ? 'month' : null,
created_at: '2026-08-01T00:00:00Z',
},
],
})
return
}
if (url.includes('/orders')) {
await ok({ items: [] })
return
}
if (url.includes('/audit-logs')) {
await ok({
items: granted
? [
{
id: 'aud1',
admin_id: 'aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa',
action: 'membership.grant',
target_type: 'user',
target_id: userId,
meta: { plan: 'month' },
created_at: '2026-08-06T00:00:00Z',
},
]
: [],
})
return
}
await ok({})
})
await page.goto('/login')
await expect(page.getByRole('heading', { name: '愈心谷' })).toBeVisible()
await page.locator('input[type="password"]').fill('change-me')
await page.getByRole('button', { name: '登录' }).click()
await expect(page.getByRole('heading', { name: '概览' })).toBeVisible()
await page.getByRole('link', { name: '用户', exact: true }).click()
await expect(page.getByRole('heading', { name: '用户' })).toBeVisible()
await expect(page.getByText('测试用户')).toBeVisible()
await page.getByRole('link', { name: '测试用户' }).click()
await expect(page.getByRole('heading', { name: '用户详情' })).toBeVisible()
await page.getByRole('button', { name: '授予 / 延长' }).click()
await expect(page.getByText('会员已授予')).toBeVisible()
await page.getByRole('link', { name: '审计' }).click()
await expect(page.getByRole('heading', { name: '审计' })).toBeVisible()
await expect(page.getByText('membership.grant')).toBeVisible()
})