af3c24275d
- Backend: NestJS 10 + TypeORM (better-sqlite3) feature-modular layout.
- Entities: user, setting, category, challenge, challenge_file, solve,
refresh_token, blog_post. Auto-run migrations + idempotent 6-system-
category seed on startup.
- Argon2id password hashing with policy check; JWT access + rotating
refresh tokens (HttpOnly cookie); CSRF middleware (SameSite + custom
X-CSRF-Token header); global JWT auth guard with @Public() opt-out;
per-IP login backoff + per-IP registration rate limit.
- Endpoints: auth (login/refresh/logout/csrf), users (first-admin
registration), system (bootstrap/event status/SSE), admin (guarded
user CRUD with last-admin invariant), frontend module (uploads +
SPA fallback).
- Security: helmet+CSP+HSTS-gated-by-TLS, CORS allowlist, structured
global exception filter, Zod request validation pipes, OpenAPI 3.1
served at /api/docs and /api/docs-json, 10 canonical themes under
backend/themes/.
- Frontend: Angular 17 standalone components, lazy-loaded feature routes,
signals, functional HttpInterceptorFn (csrf + auth), functional
CanActivateFn auth guard, HttpOnly-cookie-based auth service.
- Tests: Jest + supertest, 46 tests across 13 suites covering
migrations, env schema, theme loader, event status, login backoff,
registration rate limit, ApiError shape, bootstrap integration,
auth refresh rotation, admin guard, last-admin invariant, SSE flat
payloads. Single-command runner: `npm test`.
71 lines
2.6 KiB
TypeScript
71 lines
2.6 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';
|
|
|
|
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();
|
|
});
|
|
|
|
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);
|
|
});
|
|
}); |