package.json test:e2e:prepare 现在是 migrate reset --force && prisma generate && seed 为 seed 运行时补充 JS Prisma client 生成器: schema.prisma 修复 seed 在 ESM/CJS 下的 Prisma 导入兼容: seed.mjs 修复 Jest 环境未加载 .env 导致连到 127.0.0.1 的问题: e2e-app.helper.ts 修复夹具依赖“名称”导致被组织测试改名后失效的问题(改为按 seed openId 反查): e2e-fixtures.helper.ts 修复组织测试的状态污染与清理逻辑,并收敛 afterAll 资源释放: organization.e2e-spec.ts e2e-context.helper.ts
33 lines
1.2 KiB
TypeScript
33 lines
1.2 KiB
TypeScript
import type { Response } from 'supertest';
|
|
import { E2E_ROLE_LIST, type E2ERole } from '../fixtures/e2e-roles.js';
|
|
import type { E2EAccessTokenMap } from './e2e-auth.helper.js';
|
|
|
|
interface RoleMatrixCase {
|
|
name: string;
|
|
tokens: E2EAccessTokenMap;
|
|
expectedStatusByRole: Record<E2ERole, number>;
|
|
sendAsRole: (role: E2ERole, token: string) => Promise<Response>;
|
|
sendWithoutToken: () => Promise<Response>;
|
|
expectedStatusWithoutToken?: number;
|
|
}
|
|
|
|
export async function assertRoleMatrix(matrixCase: RoleMatrixCase) {
|
|
for (const role of E2E_ROLE_LIST) {
|
|
const response = await matrixCase.sendAsRole(role, matrixCase.tokens[role]);
|
|
const expectedStatus = matrixCase.expectedStatusByRole[role];
|
|
const isSuccess = expectedStatus >= 200 && expectedStatus < 300;
|
|
|
|
expect(response.status).toBe(expectedStatus);
|
|
expect(response.body.code).toBe(isSuccess ? 0 : expectedStatus);
|
|
}
|
|
|
|
const unauthorizedResponse = await matrixCase.sendWithoutToken();
|
|
const unauthorizedStatus = matrixCase.expectedStatusWithoutToken ?? 401;
|
|
expect(unauthorizedResponse.status).toBe(unauthorizedStatus);
|
|
expect(unauthorizedResponse.body.code).toBe(
|
|
unauthorizedStatus >= 200 && unauthorizedStatus < 300
|
|
? 0
|
|
: unauthorizedStatus,
|
|
);
|
|
}
|