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 | null, tokenScope: 'web' })); vi.mock('../src/middleware/auth', async (importOriginal) => { const actual = await importOriginal(); 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(); 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'"); }); });