AI Implementation feature(892): Admin Area General Settings and Categories 1.10 (#32)
This commit was merged in pull request #32.
This commit is contained in:
@@ -0,0 +1,70 @@
|
||||
process.env.DATABASE_PATH = ':memory:';
|
||||
process.env.THEMES_DIR = './themes';
|
||||
process.env.FRONTEND_DIST = './frontend/dist';
|
||||
process.env.NODE_ENV = 'test';
|
||||
|
||||
import { AdminGeneralService } from '../../backend/src/modules/admin/general.service';
|
||||
|
||||
function makeFakeThemeLoader() {
|
||||
return [
|
||||
{ id: 'classic', name: 'Classic', tokens: {} as any },
|
||||
{ id: 'midnight', name: 'Midnight', tokens: {} as any },
|
||||
];
|
||||
}
|
||||
|
||||
function makeService() {
|
||||
const stored: Record<string, string> = {};
|
||||
const fakeSettings = {
|
||||
get: jest.fn().mockImplementation(async (k: string, d: string) => (k in stored ? stored[k] : d)),
|
||||
set: jest.fn().mockImplementation(async (k: string, v: string) => { stored[k] = v; }),
|
||||
} as any;
|
||||
const fakeHub = { emitEvent: jest.fn() } as any;
|
||||
const fakeThemes: any = { listThemes: () => makeFakeThemeLoader() };
|
||||
const fakeConfig: any = { get: jest.fn().mockReturnValue('./themes') };
|
||||
const svc = new AdminGeneralService(fakeSettings, fakeThemes, fakeHub, fakeConfig);
|
||||
return { svc, fakeHub, fakeSettings };
|
||||
}
|
||||
|
||||
describe('AdminGeneralService.updateSettings - general SSE payload (Job 892)', () => {
|
||||
it('emits a general hub event carrying both themeKey and registrationsEnabled when registrations are enabled', async () => {
|
||||
const { svc, fakeHub } = makeService();
|
||||
await svc.updateSettings({
|
||||
pageTitle: 'Hello',
|
||||
logo: '',
|
||||
welcomeMarkdown: '',
|
||||
themeKey: 'midnight',
|
||||
eventStartUtc: '2026-01-01T00:00:00Z',
|
||||
eventEndUtc: '2026-02-01T00:00:00Z',
|
||||
defaultChallengeIp: '127.0.0.1',
|
||||
registrationsEnabled: true,
|
||||
});
|
||||
expect(fakeHub.emitEvent).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
topic: 'general',
|
||||
themeKey: 'midnight',
|
||||
registrationsEnabled: true,
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('emits a general hub event with registrationsEnabled=false when the admin disables registrations', async () => {
|
||||
const { svc, fakeHub } = makeService();
|
||||
await svc.updateSettings({
|
||||
pageTitle: 'Hello',
|
||||
logo: '',
|
||||
welcomeMarkdown: '',
|
||||
themeKey: 'classic',
|
||||
eventStartUtc: '2026-01-01T00:00:00Z',
|
||||
eventEndUtc: '2026-02-01T00:00:00Z',
|
||||
defaultChallengeIp: '127.0.0.1',
|
||||
registrationsEnabled: false,
|
||||
});
|
||||
expect(fakeHub.emitEvent).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
topic: 'general',
|
||||
themeKey: 'classic',
|
||||
registrationsEnabled: false,
|
||||
}),
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,90 @@
|
||||
process.env.DATABASE_PATH = ':memory:';
|
||||
process.env.THEMES_DIR = './themes';
|
||||
process.env.FRONTEND_DIST = './frontend/dist';
|
||||
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 { 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 { SseHubService } from '../../backend/src/common/services/sse-hub.service';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
import { initDb } from './db-helper';
|
||||
|
||||
describe('GET /api/v1/event/stream forwards general topic (Job 892)', () => {
|
||||
let app: INestApplication;
|
||||
let hub: SseHubService;
|
||||
|
||||
beforeAll(async () => {
|
||||
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
||||
app = moduleRef.createNestApplication();
|
||||
app.use(cookieParser());
|
||||
app.use(express.json({ limit: '1mb' }));
|
||||
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);
|
||||
hub = app.get(SseHubService);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('emits a { topic: "general", themeKey, registrationsEnabled } frame through the public stream', (done) => {
|
||||
const server = app.getHttpServer();
|
||||
const req = request(server).get('/api/v1/event/stream');
|
||||
let received = false;
|
||||
req
|
||||
.buffer(true)
|
||||
.parse((res, cb) => {
|
||||
const chunks: Buffer[] = [];
|
||||
const finalize = () => cb(null, Buffer.concat(chunks));
|
||||
res.on('data', (chunk: Buffer) => {
|
||||
chunks.push(chunk);
|
||||
if (received) return;
|
||||
const text = Buffer.concat(chunks).toString('utf8');
|
||||
const match = /data: (\{[^{]*?"topic":\s*"general"[^{]*\})\n/.exec(text);
|
||||
if (!match) return;
|
||||
try {
|
||||
const payload = JSON.parse(match[1]);
|
||||
expect(payload.topic).toBe('general');
|
||||
expect(payload).toHaveProperty('themeKey');
|
||||
expect(payload).toHaveProperty('registrationsEnabled');
|
||||
received = true;
|
||||
(res as any).destroy();
|
||||
done();
|
||||
} catch (e) {
|
||||
(res as any).destroy();
|
||||
done(e);
|
||||
}
|
||||
});
|
||||
res.on('end', finalize);
|
||||
res.on('error', (err) => cb(err, null));
|
||||
})
|
||||
.end(() => {});
|
||||
|
||||
// Push the hub event shortly after the consumer attaches.
|
||||
setTimeout(() => {
|
||||
hub.emitEvent({ topic: 'general', themeKey: 'midnight', registrationsEnabled: true });
|
||||
}, 150);
|
||||
|
||||
setTimeout(() => {
|
||||
if (!received) {
|
||||
done(new Error('Did not receive a general frame within 5s'));
|
||||
}
|
||||
}, 5000);
|
||||
}, 10_000);
|
||||
});
|
||||
Reference in New Issue
Block a user