AI Implementation feature(871): Admin Area System: Database Backup/Restore and Danger Zone (#61)

This commit was merged in pull request #61.
This commit is contained in:
2026-07-23 12:10:41 +00:00
parent 6bac67fad7
commit 470ddd30c3
42 changed files with 3996 additions and 55 deletions
@@ -0,0 +1,146 @@
process.env.DATABASE_PATH = ':memory:';
process.env.THEMES_DIR = './themes';
process.env.FRONTEND_DIST = './frontend/dist';
process.env.UPLOAD_SIZE_LIMIT = '5mb';
process.env.NODE_ENV = 'test';
import { Test } from '@nestjs/testing';
import { INestApplication, ValidationPipe } from '@nestjs/common';
import { HttpAdapterHost } from '@nestjs/core';
import cookieParser from 'cookie-parser';
import * as express from 'express';
import request from 'supertest';
import { CookieAccessInfo } from 'cookiejar';
import { AppModule } from '../../backend/src/app.module';
import { GlobalExceptionFilter } from '../../backend/src/common/filters/global-exception.filter';
import { CsrfMiddleware } from '../../backend/src/common/middleware/csrf.middleware';
import { ConfigService } from '@nestjs/config';
import { initDb } from './db-helper';
async function csrfHeader(agent: any): Promise<string> {
const cookies = agent.jar.getCookies(CookieAccessInfo.All);
return (cookies as any[]).find((c: any) => c.name === 'csrf').value as string;
}
async function loginAgent(app: INestApplication, username: string, password: string): Promise<{ agent: any; token: string }> {
const agent = request.agent(app.getHttpServer());
await agent.get('/api/v1/auth/csrf');
const csrf = await csrfHeader(agent);
const res = await agent
.post('/api/v1/auth/login')
.set('X-CSRF-Token', csrf)
.send({ username, password })
.expect(201);
(agent as any).accessToken = res.body.accessToken;
return { agent, token: res.body.accessToken };
}
describe('Job 871: Confirmation-token binding + replay rejection', () => {
let app: INestApplication;
let admin: { agent: any; token: string };
let csrf: string;
beforeAll(async () => {
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
app = moduleRef.createNestApplication();
app.use(cookieParser());
app.use(express.json({ limit: '10mb' }));
const csrfMw = new CsrfMiddleware(app.get(ConfigService));
app.use((req: any, res: any, next: any) => csrfMw.use(req, res, next));
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);
await request(app.getHttpServer())
.post('/api/v1/auth/register-first-admin')
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
.expect(201);
admin = await loginAgent(app, 'admin', 'Sup3rSecret!Pass');
csrf = await csrfHeader(admin.agent);
});
afterAll(async () => {
await app.close();
});
it('issues a confirmation token for reset-scores', async () => {
const res = await admin.agent
.post('/api/v1/admin/system/confirmations')
.set('Authorization', `Bearer ${admin.token}`)
.set('X-CSRF-Token', csrf)
.send({ operation: 'reset-scores', password: 'Sup3rSecret!Pass' })
.expect(200);
expect(res.body.token).toEqual(expect.any(String));
expect(res.body.operation).toBe('reset-scores');
expect(res.body.expiresAt).toEqual(expect.any(String));
});
it('rejects confirmation requests with the wrong password', async () => {
const res = await admin.agent
.post('/api/v1/admin/system/confirmations')
.set('Authorization', `Bearer ${admin.token}`)
.set('X-CSRF-Token', csrf)
.send({ operation: 'reset-scores', password: 'WrongPassword!1' });
expect(res.status).toBe(401);
expect(res.body.code).toBe('SYSTEM_INVALID_CREDENTIALS');
});
it('rejects token requests without authentication', async () => {
const res = await request(app.getHttpServer())
.post('/api/v1/admin/system/confirmations')
.send({ operation: 'reset-scores', password: 'Sup3rSecret!Pass' });
expect([401, 403]).toContain(res.status);
});
it('rejects token reuse on the reset-scores endpoint', async () => {
const issued = await admin.agent
.post('/api/v1/admin/system/confirmations')
.set('Authorization', `Bearer ${admin.token}`)
.set('X-CSRF-Token', csrf)
.send({ operation: 'reset-scores', password: 'Sup3rSecret!Pass' })
.expect(200);
const first = await admin.agent
.post('/api/v1/admin/system/scores/reset')
.set('Authorization', `Bearer ${admin.token}`)
.set('X-CSRF-Token', csrf)
.send({ operation: 'reset-scores', token: issued.body.token })
.expect(200);
expect(first.body.ok).toBe(true);
const second = await admin.agent
.post('/api/v1/admin/system/scores/reset')
.set('Authorization', `Bearer ${admin.token}`)
.set('X-CSRF-Token', csrf)
.send({ operation: 'reset-scores', token: issued.body.token });
expect([400, 409]).toContain(second.status);
expect(['SYSTEM_TOKEN_REUSED', 'SYSTEM_TOKEN_INVALID']).toContain(second.body.code);
});
it('rejects tokens issued for a different operation', async () => {
const issued = await admin.agent
.post('/api/v1/admin/system/confirmations')
.set('Authorization', `Bearer ${admin.token}`)
.set('X-CSRF-Token', csrf)
.send({ operation: 'reset-scores', password: 'Sup3rSecret!Pass' })
.expect(200);
const res = await admin.agent
.post('/api/v1/admin/system/challenges/wipe')
.set('Authorization', `Bearer ${admin.token}`)
.set('X-CSRF-Token', csrf)
.send({ operation: 'wipe-challenges', token: issued.body.token });
expect(res.status).toBe(400);
expect(['SYSTEM_TOKEN_MISMATCH', 'SYSTEM_TOKEN_INVALID']).toContain(res.body.code);
});
it('rejects tokens with a fabricated value', async () => {
const res = await admin.agent
.post('/api/v1/admin/system/scores/reset')
.set('Authorization', `Bearer ${admin.token}`)
.set('X-CSRF-Token', csrf)
.send({ operation: 'reset-scores', token: 'not-a-real-token' });
expect(res.status).toBe(400);
expect(res.body.code).toBe('SYSTEM_TOKEN_INVALID');
});
});