96 lines
3.7 KiB
TypeScript
96 lines
3.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 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';
|
|
|
|
describe('GET /api/v1/events/status (authenticated SSE)', () => {
|
|
let app: INestApplication;
|
|
|
|
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);
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('rejects unauthenticated SSE with 401', async () => {
|
|
await request(app.getHttpServer()).get('/api/v1/events/status').expect(401);
|
|
});
|
|
|
|
it('emits initial status with state + serverNowUtc + eventStartUtc + eventEndUtc when authenticated', (done) => {
|
|
const server = app.getHttpServer();
|
|
const agent = request.agent(server);
|
|
(async () => {
|
|
await agent.get('/api/v1/auth/csrf');
|
|
const csrf = (agent.jar.getCookies(CookieAccessInfo.All) as any).find((c: any) => c.name === 'csrf');
|
|
const login = await agent
|
|
.post('/api/v1/auth/login')
|
|
.set('X-CSRF-Token', csrf.value)
|
|
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
|
.expect(201);
|
|
const token = login.body.accessToken as string;
|
|
|
|
const req = request(server)
|
|
.get('/api/v1/events/status')
|
|
.set('Authorization', `Bearer ${token}`);
|
|
let received = false;
|
|
req
|
|
.buffer(true)
|
|
.parse((res, cb) => {
|
|
const chunks: Buffer[] = [];
|
|
res.on('data', (chunk: Buffer) => {
|
|
chunks.push(chunk);
|
|
if (received) return;
|
|
const text = Buffer.concat(chunks).toString('utf8');
|
|
const match = /data: ({.*?})\n/.exec(text);
|
|
if (match) {
|
|
try {
|
|
const payload = JSON.parse(match[1]);
|
|
expect(payload).toHaveProperty('state');
|
|
expect(payload).toHaveProperty('serverNowUtc');
|
|
expect(payload).toHaveProperty('eventStartUtc');
|
|
expect(payload).toHaveProperty('eventEndUtc');
|
|
expect(['countdown', 'running', 'stopped', 'unconfigured']).toContain(payload.state);
|
|
received = true;
|
|
(res as any).destroy();
|
|
done();
|
|
} catch (e) {
|
|
done(e);
|
|
}
|
|
}
|
|
});
|
|
res.on('end', () => cb(null, Buffer.concat(chunks)));
|
|
res.on('error', (err) => cb(err, null));
|
|
})
|
|
.end(() => {});
|
|
})().catch(done);
|
|
}, 10_000);
|
|
});
|