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('Authenticated shell preconditions smoke', () => { let app: INestApplication; const ADMIN_USER = 'admin'; const ADMIN_PASS = 'Sup3rSecret!Pass'; const NEW_PASS = 'N3wSecret!Pass-Y'; 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_USER, password: ADMIN_PASS }) .expect(201); }); afterAll(async () => { await app.close(); }); it('login + /me returns the projected user (id, username, role, rank, points)', async () => { const server = app.getHttpServer(); const agent = request.agent(server); 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_USER, password: ADMIN_PASS }); expect(login.status).toBeGreaterThanOrEqual(200); expect(login.status).toBeLessThan(300); const token = login.body.accessToken as string; expect(typeof token).toBe('string'); const me = await agent .get('/api/v1/auth/me') .set('Authorization', `Bearer ${token}`) .expect(200); expect(me.body.username).toBe(ADMIN_USER); expect(me.body.role).toBe('admin'); expect(typeof me.body.id).toBe('string'); expect(typeof me.body.points).toBe('number'); expect(me.body).toHaveProperty('rank'); }); it('change-password happy path returns 204 and the new password authenticates', async () => { const server = app.getHttpServer(); const agent = request.agent(server); 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_USER, password: ADMIN_PASS }); expect(login.status).toBeGreaterThanOrEqual(200); expect(login.status).toBeLessThan(300); const token = login.body.accessToken as string; await agent .post('/api/v1/auth/change-password') .set('Authorization', `Bearer ${token}`) .set('X-CSRF-Token', csrf.value) .send({ oldPassword: ADMIN_PASS, newPassword: NEW_PASS, confirmNewPassword: NEW_PASS, }) .expect(204); const relogin = await agent .post('/api/v1/auth/login') .set('X-CSRF-Token', csrf.value) .send({ username: ADMIN_USER, password: NEW_PASS }); expect(relogin.status).toBeGreaterThanOrEqual(200); expect(relogin.status).toBeLessThan(300); expect(relogin.body.accessToken).toBeDefined(); }); });