admin-governance.spec 此前全 page.route mock /admin-api/muse/**(~20 端点)、前后端契约从未端到端跑;认证注入 localStorage key 硬编码 muse-vben-antd-... 实为 undefined-5.7.0-dev-core-access(VITE_APP_NAMESPACE 未定义→运行时 undefined)、mock 下从不暴露。
命门(实证打通):①认证=与 studio 共用 mock-token Bearer test1(mockSecret test+userId1);②权限=admin 用户原无角色(system_user_role 空)→403,globalSetup 幂等 seed system_user_role(1,1)(super_admin)+DEL redis user_role_ids:1 刷权限缓存→超管跳过 @PreAuthorize;③代理=admin vite 已硬编码 /admin-api→48080、nitroMock 默认关。
基建:新建 global-setup(seed user_role+DEL redis+PG 复位)、playwright.config 加 globalSetup+workers:1、加 pg。spec 改真 localStorage key+token test1+删 routeMuseAdminApis mock(catch-all /admin-api/** 补 tenant-id:1 真打 48080;notify/dict/tenant 平台外壳保留 mock 因切片库无 notify 表返 500)+get-permission-info 走真后端。account 子域真连:真分页 {pageNo,pageSize,total,list} 前端 PageResult 只读 list/total 契约兼容;其他 8 子域 test.skip。独立连跑 15 次绿(含冷启动)。
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
94 lines
4.2 KiB
TypeScript
94 lines
4.2 KiB
TypeScript
import { execFileSync } from 'node:child_process';
|
|
|
|
import { Client } from 'pg';
|
|
|
|
/**
|
|
* Playwright globalSetup —— admin 治理 e2e 转真后端的活体前置(可入库,替代一次性种子脚本)。
|
|
*
|
|
* WHY:admin e2e 关 mock 直连真实 muse-server(vite 代理 /admin-api→48080)+ 真 PostgreSQL(muse_slice_live)。
|
|
* admin 用户(system_users id=1)与 studio 共用 mock-token(Bearer test1 = mockSecret"test"+userId1),
|
|
* 但该用户在 muse_slice_live 里默认**无角色**(system_user_role 空)→ 命中 @PreAuthorize 403。
|
|
* 本前置幂等 seed `system_user_role(id=1,user_id=1,role_id=1)`(role 1=super_admin),令 admin 成超管
|
|
* (PermissionServiceImpl.hasAnyPermissions 超管直接放行,跳过具体权限点),再 DEL redis 权限缓存使其立即生效。
|
|
*
|
|
* 连接凭据从环境变量读取(`MUSE_POSTGRES_*` / `MUSE_REDIS_*`,见 ~/.config/muse-repo/infra.env;**绝不入库**):
|
|
* 跑前先 `set -a; . ~/.config/muse-repo/infra.env; set +a`。缺 PG 凭据时直接报错(本套件仅活体跑,无 DB 不可能真绿)。
|
|
* 如确知库已就绪、想跳过:置 `E2E_SKIP_SEED=1`。
|
|
*
|
|
* 说明:account 子域只读 users/usage-records/purchase-records 三个列表端点,其数据由 muse_slice_live 基础种子
|
|
* 提供(user1 的 account profile / purchase 记录已存在),本前置不重置 account fixture,仅补齐权限前提。
|
|
*/
|
|
async function globalSetup(): Promise<void> {
|
|
if (process.env.E2E_SKIP_SEED === '1') {
|
|
console.log('[admin e2e globalSetup] E2E_SKIP_SEED=1 → 跳过权限前置');
|
|
return;
|
|
}
|
|
|
|
const host = process.env.MUSE_POSTGRES_HOST;
|
|
const password = process.env.MUSE_POSTGRES_PASSWORD;
|
|
if (!host || !password) {
|
|
throw new Error(
|
|
'[admin e2e globalSetup] 缺少 MUSE_POSTGRES_* 环境变量。活体 e2e 需直连真实 PG seed 权限前提,' +
|
|
'请先 `set -a; . ~/.config/muse-repo/infra.env; set +a` 再跑(或置 E2E_SKIP_SEED=1 跳过)。',
|
|
);
|
|
}
|
|
|
|
const client = new Client({
|
|
host,
|
|
port: Number(process.env.MUSE_POSTGRES_PORT ?? '5433'),
|
|
database: process.env.MUSE_POSTGRES_DATABASE ?? 'muse_slice_live',
|
|
user: process.env.MUSE_POSTGRES_USERNAME ?? 'root',
|
|
password,
|
|
ssl: false,
|
|
connectionTimeoutMillis: 10_000,
|
|
});
|
|
|
|
await client.connect();
|
|
try {
|
|
// 幂等 seed:admin(system_users id=1)绑定 super_admin(system_role id=1)。
|
|
// ON CONFLICT DO NOTHING:重跑不重复;若已存在(基础种子已建)则 rowCount=0,无副作用。
|
|
const seed = await client.query(
|
|
`INSERT INTO system_user_role (id, user_id, role_id, tenant_id)
|
|
VALUES (1, 1, 1, 1) ON CONFLICT (id) DO NOTHING`,
|
|
);
|
|
// 复核绑定确实在(防御:若 id 冲突但内容被改过,这里能暴露)。
|
|
const check = await client.query(
|
|
'SELECT user_id, role_id FROM system_user_role WHERE id=1 AND deleted=false',
|
|
);
|
|
if (check.rowCount === 0) {
|
|
console.warn(
|
|
'[admin e2e globalSetup] ⚠️ system_user_role(id=1) 缺失或被软删,admin 可能仍 403',
|
|
);
|
|
}
|
|
console.log(
|
|
`[admin e2e globalSetup] seed system_user_role(super_admin) rowCount=${seed.rowCount}(0=已存在)`,
|
|
);
|
|
} finally {
|
|
await client.end();
|
|
}
|
|
|
|
// DEL redis 权限缓存(yudao 单冒号 key user_role_ids:1),令 seed 立即生效。
|
|
// 本机/活体环境用 redis-cli(redis 在 127.0.0.1:6379,凭据来自 MUSE_REDIS_PASSWORD);
|
|
// 失败不致命(缓存未命中时下次查询会回源 DB),仅告警,避免阻断套件。
|
|
const redisHost = process.env.MUSE_REDIS_HOST ?? '127.0.0.1';
|
|
const redisPort = process.env.MUSE_REDIS_PORT ?? '6379';
|
|
const redisPassword = process.env.MUSE_REDIS_PASSWORD;
|
|
try {
|
|
const args = ['-h', redisHost, '-p', redisPort];
|
|
if (redisPassword) {
|
|
args.push('-a', redisPassword, '--no-auth-warning');
|
|
}
|
|
args.push('DEL', 'user_role_ids:1');
|
|
const out = execFileSync('redis-cli', args, { encoding: 'utf8' }).trim();
|
|
console.log(`[admin e2e globalSetup] DEL redis user_role_ids:1 => ${out}`);
|
|
} catch (error) {
|
|
console.warn(
|
|
`[admin e2e globalSetup] ⚠️ DEL redis 权限缓存失败(非致命,下次查询回源 DB):${
|
|
(error as Error).message
|
|
}`,
|
|
);
|
|
}
|
|
}
|
|
|
|
export default globalSetup;
|