AI Implementation feature(820): Project Scaffold and Platform Foundations (#1)
This commit was merged in pull request #1.
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
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';
|
||||
import { DatabaseInitService } from '../../backend/src/database/database-init.service';
|
||||
|
||||
describe('Auth refresh rotation', () => {
|
||||
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);
|
||||
|
||||
const server = app.getHttpServer();
|
||||
await request(server).post('/api/v1/auth/register-first-admin')
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('GET /api/v1/auth/csrf always sets the cookie and returns a non-empty token', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
const res = await agent.get('/api/v1/auth/csrf').expect(200);
|
||||
expect(res.body.csrfToken).toBeDefined();
|
||||
expect(res.body.csrfToken.length).toBeGreaterThan(0);
|
||||
const cookies = res.headers['set-cookie'] as unknown as string[];
|
||||
expect(cookies?.some((c) => c.startsWith('csrf='))).toBe(true);
|
||||
});
|
||||
|
||||
it('login returns an access token and sets the rt refresh cookie', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
|
||||
const res = await agent.post('/api/v1/auth/login')
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
expect(res.body.accessToken).toBeDefined();
|
||||
const cookies = res.headers['set-cookie'] as unknown as string[];
|
||||
const rt = cookies.find((c) => c.startsWith('rt='));
|
||||
expect(rt).toBeDefined();
|
||||
expect(rt).toMatch(/HttpOnly/);
|
||||
});
|
||||
|
||||
it('refresh rotates the refresh cookie and returns a new access token', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
|
||||
await agent.post('/api/v1/auth/login')
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
|
||||
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
||||
const rt = cookies.find((c: any) => c.name === 'rt');
|
||||
const csrf = cookies.find((c: any) => c.name === 'csrf');
|
||||
expect(rt).toBeDefined();
|
||||
expect(csrf).toBeDefined();
|
||||
|
||||
const refresh = await agent.post('/api/v1/auth/refresh')
|
||||
.set('X-CSRF-Token', csrf.value)
|
||||
.set('Cookie', `rt=${rt.value}`)
|
||||
.send({})
|
||||
.expect(200);
|
||||
expect(refresh.body.accessToken).toBeDefined();
|
||||
expect(refresh.body.user.username).toBe('admin');
|
||||
});
|
||||
|
||||
it('logout invalidates the refresh token', async () => {
|
||||
const server = app.getHttpServer();
|
||||
const agent = request.agent(server);
|
||||
|
||||
await agent.post('/api/v1/auth/login')
|
||||
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
||||
.expect(201);
|
||||
|
||||
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
||||
const rt = cookies.find((c: any) => c.name === 'rt');
|
||||
const csrf = cookies.find((c: any) => c.name === 'csrf');
|
||||
|
||||
await agent.post('/api/v1/auth/logout')
|
||||
.set('X-CSRF-Token', csrf.value)
|
||||
.set('Cookie', `rt=${rt.value}`)
|
||||
.send({})
|
||||
.expect(204);
|
||||
|
||||
// Reusing the same refresh cookie must fail
|
||||
await agent.post('/api/v1/auth/refresh')
|
||||
.set('X-CSRF-Token', csrf.value)
|
||||
.set('Cookie', `rt=${rt.value}`)
|
||||
.send({})
|
||||
.expect(401);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user