Files
HIPCTF2/tests/backend/bootstrap.integration.spec.ts
T
OpenVelo Agent ac6c834525 Harden scaffold (reviewer followups)
- main.ts awaits DatabaseInitService.init() before app.listen(), ensuring
  migrations + seed run before the HTTP server accepts traffic.
- AppModule now uses NestModule with consumer.apply() no longer needed for
  CSRF (registered globally via app.use after body parsers in main.ts).
- JwtAuthGuard extended from AuthGuard('jwt') so protected endpoints
  actually validate the bearer token.
- Admin controller now uses Zod-validated DTOs for body/path/query:
  createUser, updateUserRole, userIdParam, listUsersQuery; with @Public
  / @Roles decorators and AdminGuard applied.
- Multer upload module + controller (POST /api/v1/uploads/category-icon
  and /challenge-file) with safe-filename strategy, configured
  UPLOAD_SIZE_LIMIT, admin-only via AdminGuard, served via /uploads
  static handler.
- ThemeLoaderService now requires all 10 canonical theme ids at startup,
  backfilling missing themes from built-ins with a warning; validates the
  configured themeKey setting and falls back to 'classic' if invalid;
  gracefully tolerates missing setting table during early boot.
- Test suite expanded to 76 tests / 17 suites; new specs:
  admin-validation, uploads, theme-required, database-init.
2026-07-21 14:04:27 +00:00

73 lines
2.7 KiB
TypeScript

process.env.DATABASE_PATH = ':memory:';
process.env.THEMES_DIR = './themes';
process.env.FRONTEND_DIST = './frontend/dist';
import { Test } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import { AppModule } from '../../backend/src/app.module';
import { GlobalExceptionFilter } from '../../backend/src/common/filters/global-exception.filter';
import { csrfClient, primeCsrf } from './csrf-client';
import { initDb } from './db-helper';
describe('Bootstrap integration', () => {
let app: INestApplication;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
app = moduleRef.createNestApplication();
app.useGlobalPipes(new ValidationPipe({ whitelist: true, forbidNonWhitelisted: false, transform: true }));
const httpAdapterHost = app.get(HttpAdapterHost);
app.useGlobalFilters(new GlobalExceptionFilter(httpAdapterHost));
await app.init();
await initDb(app);
});
afterAll(async () => {
await app.close();
});
it('a: bootstrap before any admin', async () => {
const c = csrfClient(app);
const res = await c.get('/api/v1/bootstrap').expect(200);
expect(res.body.initialized).toBe(false);
expect(res.body.theme).toBeDefined();
expect(res.body.theme.tokens.primary).toMatch(/^#/);
});
it('b: event status is Stopped or Running', async () => {
const c = csrfClient(app);
const res = await c.get('/api/v1/event/status').expect(200);
expect(['Stopped', 'Running']).toContain(res.body.status);
expect(typeof res.body.countdownMs).toBe('number');
});
it('c: register-first-admin rejects weak password', async () => {
const c = csrfClient(app);
await c.post('/api/v1/auth/register-first-admin')
.send({ username: 'weak', password: '123' })
.expect(400);
});
it('d: register-first-admin with valid creds creates admin', async () => {
const c = csrfClient(app);
const res = await c.post('/api/v1/auth/register-first-admin')
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
.expect(201);
expect(res.body.accessToken).toBeDefined();
expect(res.body.user.role).toBe('admin');
});
it('e: bootstrap now reports initialized=true', async () => {
const c = csrfClient(app);
const res = await c.get('/api/v1/bootstrap').expect(200);
expect(res.body.initialized).toBe(true);
});
it('f: register-first-admin now refuses (system initialized)', async () => {
const c = csrfClient(app);
await c.post('/api/v1/auth/register-first-admin')
.send({ username: 'second', password: 'Sup3rSecret!Pass' })
.expect(409);
});
});