- T1 资金/库存/报价/自采 P1(BIZ-1/2/3/4/10/13/14) - T2 主系统反向事件墓碑/改派/孤儿处理(BIZ-6/8/9) - T3 选分公司 ticket、trust proxy、角色权限收口(SEC-2/3/4) - T4 缺列/RLS/tenant 名单/FK/迁移执行器(DATA-1..8) - T5 401 登出/分页/映射口径/blob/ZIP 中文名(FE-1..4/BIZ-15) - T6 测试脚本目标守卫/config fail-fast/容器配置(OPS-2..8) - T7 前后端测试与 lint 门禁清零 - T8 应付卡片按关联订单日(口径 A) - T9 存储对象全链路归属校验、敏感列禁写、审计 target_id 超长修复
92 lines
3.6 KiB
TypeScript
92 lines
3.6 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import { join } from 'node:path';
|
||
import request from 'supertest';
|
||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||
|
||
/**
|
||
* T9 SEC-6(2026-10-19):/api/health/details 仅 super_admin/运维角色可读;
|
||
* 普通登录用户 403(此前任意登录用户都能读桶名/endpoint/热点表/在线连接数/锁统计)。
|
||
* SEC-8:全局 error handler 统一 JSON(不返回堆栈)。
|
||
*/
|
||
|
||
const H = vi.hoisted(() => ({ ctx: null as Record<string, unknown> | null, tokenScope: 'web' }));
|
||
|
||
vi.mock('../src/middleware/auth', async (importOriginal) => {
|
||
const actual = await importOriginal<typeof import('../src/middleware/auth')>();
|
||
return {
|
||
...actual,
|
||
authMiddleware: (req: { userId?: string; tokenScope?: string }, _res: unknown, next: () => void) => {
|
||
req.userId = 'u1';
|
||
req.tokenScope = H.tokenScope;
|
||
next();
|
||
},
|
||
};
|
||
});
|
||
|
||
vi.mock('../src/services/tenantScope', async (importOriginal) => {
|
||
const actual = await importOriginal<typeof import('../src/services/tenantScope')>();
|
||
return { ...actual, loadTenantContext: async () => H.ctx };
|
||
});
|
||
|
||
const { app } = await import('../src/index');
|
||
|
||
beforeEach(() => {
|
||
H.ctx = { userId: 'u1', roles: ['operator'], permissions: [], branchCode: 'BR002', isSuperAdmin: false };
|
||
H.tokenScope = 'web';
|
||
});
|
||
|
||
describe('SEC-6:完整诊断仅超管/运维', () => {
|
||
it('普通登录用户 ⇒ 403', async () => {
|
||
const res = await request(app).get('/api/health/details');
|
||
expect(res.status).toBe(403);
|
||
expect(JSON.stringify(res.body)).not.toContain('storage');
|
||
});
|
||
|
||
it('ops 运维角色 ⇒ 200', async () => {
|
||
H.ctx = { userId: 'u1', roles: ['ops'], permissions: [], branchCode: 'BR002', isSuperAdmin: false };
|
||
const res = await request(app).get('/api/health/details');
|
||
expect(res.status).toBe(200);
|
||
expect(res.body.status).toBe('ok');
|
||
});
|
||
|
||
it('super_admin ⇒ 200', async () => {
|
||
H.ctx = { userId: 'u1', roles: ['super_admin'], permissions: [], branchCode: null, isSuperAdmin: true };
|
||
const res = await request(app).get('/api/health/details');
|
||
expect(res.status).toBe(200);
|
||
});
|
||
|
||
it('mobile 受限 token 即使 super_admin 也 403', async () => {
|
||
H.ctx = { userId: 'u1', roles: ['super_admin'], permissions: [], branchCode: 'BR002', isSuperAdmin: true };
|
||
H.tokenScope = 'mobile';
|
||
const res = await request(app).get('/api/health/details');
|
||
expect(res.status).toBe(403);
|
||
});
|
||
|
||
it('未知 /api 路由 ⇒ JSON 404(不是 Express HTML)', async () => {
|
||
const res = await request(app).get('/api/definitely-not-a-route');
|
||
expect(res.status).toBe(404);
|
||
expect(res.type).toContain('json');
|
||
expect(res.body.error?.code).toBe('not_found');
|
||
});
|
||
|
||
it('公开 /api/health 仍 200(部署探针不回归)', async () => {
|
||
const res = await request(app).get('/api/health');
|
||
expect(res.status).toBe(200);
|
||
expect(res.body.status).toBe('ok');
|
||
});
|
||
});
|
||
|
||
describe('SEC-8:全局 error handler(源码契约)', () => {
|
||
it('index.ts 末尾挂 4 参错误中间件,5xx 不返回堆栈', () => {
|
||
const src = readFileSync(join(__dirname, '../src/index.ts'), 'utf8');
|
||
const idx = src.indexOf('[express-error]');
|
||
expect(idx).toBeGreaterThan(-1);
|
||
const block = src.slice(Math.max(0, idx - 400), idx + 600);
|
||
expect(block).toMatch(/app\.use\(\(err: any, req: express\.Request, res: express\.Response, _next: express\.NextFunction\)/);
|
||
expect(block).toContain('服务器内部错误');
|
||
// headersSent 时不得裸 return(SSE/流式会悬挂)
|
||
expect(block).toContain('res.destroy()');
|
||
expect(block).toContain("'internal_error'");
|
||
});
|
||
});
|