368b1d5b47
Task 12 + Task 7 quality-review carryovers: Part A - password hashing & controlled initial admin: - src/auth/password.ts: hashPassword/verifyPassword over @node-rs/argon2 (napi prebuilt, argon2id default); empty plain rejected at the door - src/scripts/seed-admin.ts: idempotent admin seed (ADMIN_ACCOUNT/ ADMIN_PASSWORD env, walk-up root .env load); existing account -> skip, never overwrites the password; no audit_logs (system op, no actor) - pnpm seed script (build + node dist/scripts/seed-admin.js); .env.example dev defaults admin/admin-secret-change-me - tests: test/password.spec.ts (5), test/seed.spec.ts real-DB idempotency (4) Part B - Task 7 carryovers (all 8): - ApiException(apiCode, message, httpStatus, details?) recognized first by the filter; unmapped-status rule: >=500 -> INTERNAL_ERROR + generic message, <500 -> VALIDATION_FAILED keeping original status (warn-logged) - 5xx/INTERNAL_ERROR responses forced to generic message (leak fix) - pino redact adds req.headers.cookie / res.headers.set-cookie - x-request-id echo capped at 128 chars (fresh UUID beyond, no log) - LOG_LEVEL validated against pino levels at module wiring -> fail fast - shared/ constants (API_PREFIX), request-with-id, log-level (+spec) - jest ts-jest reuses tsconfig.json; unit testMatch includes test/*.spec.ts - build:watch script (no concurrently dep; dev = build:watch + node --watch) api: 35 unit + 10 e2e tests green; root lint/typecheck/test green. Co-Authored-By: Claude Code <noreply@anthropic.com>
60 lines
2.2 KiB
TypeScript
60 lines
2.2 KiB
TypeScript
// services/api/test/health.e2e-spec.ts
|
||
// e2e 冒烟:验证 e2e 工装(Nest testing module + supertest + 根 .env 加载)可用。
|
||
// 不监听端口:app.init() 后直接对 app.getHttpServer() 发请求。
|
||
// 全局前缀与 main-cn.ts 共用 src/shared/constants 的 API_PREFIX(Task 7 遗留项⑥)。
|
||
import { type INestApplication } from '@nestjs/common';
|
||
import { Test } from '@nestjs/testing';
|
||
import request from 'supertest';
|
||
import { AppCnModule } from '../src/app-cn.module';
|
||
import { API_PREFIX } from '../src/shared/constants';
|
||
import { resetDb } from './reset';
|
||
|
||
describe(`GET /${API_PREFIX}/health (e2e)`, () => {
|
||
let app: INestApplication;
|
||
|
||
beforeAll(async () => {
|
||
await resetDb();
|
||
const moduleRef = await Test.createTestingModule({
|
||
imports: [AppCnModule],
|
||
}).compile();
|
||
app = moduleRef.createNestApplication();
|
||
app.setGlobalPrefix(API_PREFIX);
|
||
await app.init();
|
||
});
|
||
|
||
afterAll(async () => {
|
||
await app.close();
|
||
});
|
||
|
||
it('responds 200 {"status":"ok"}', async () => {
|
||
const res = await request(app.getHttpServer()).get(`/${API_PREFIX}/health`);
|
||
expect(res.status).toBe(200);
|
||
expect(res.body).toEqual({ status: 'ok' });
|
||
});
|
||
|
||
it('sets x-request-id response header (generated when absent)', async () => {
|
||
const res = await request(app.getHttpServer()).get(`/${API_PREFIX}/health`);
|
||
const requestId = res.headers['x-request-id'];
|
||
expect(typeof requestId).toBe('string');
|
||
expect(requestId.length ?? 0).toBeGreaterThan(0);
|
||
});
|
||
|
||
it('echoes incoming x-request-id header', async () => {
|
||
const res = await request(app.getHttpServer())
|
||
.get(`/${API_PREFIX}/health`)
|
||
.set('x-request-id', 'e2e-fixed-request-id');
|
||
expect(res.headers['x-request-id']).toBe('e2e-fixed-request-id');
|
||
});
|
||
|
||
it('replaces an over-long incoming x-request-id (>128 chars) with a fresh one', async () => {
|
||
const overlong = 'z'.repeat(200);
|
||
const res = await request(app.getHttpServer())
|
||
.get(`/${API_PREFIX}/health`)
|
||
.set('x-request-id', overlong);
|
||
const requestId = res.headers['x-request-id'];
|
||
expect(typeof requestId).toBe('string');
|
||
expect(requestId).not.toBe(overlong);
|
||
expect((requestId as string).length).toBeLessThanOrEqual(128);
|
||
});
|
||
});
|