110 lines
4.2 KiB
TypeScript
110 lines
4.2 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('Admin route protection', () => {
|
|
let app: INestApplication;
|
|
let adminToken: string;
|
|
|
|
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);
|
|
|
|
const login = await request(server).post('/api/v1/auth/login')
|
|
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
|
.expect(201);
|
|
adminToken = login.body.accessToken;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('GET /api/v1/admin/users without a token returns 401', async () => {
|
|
await request(app.getHttpServer()).get('/api/v1/admin/users').expect(401);
|
|
});
|
|
|
|
it('GET /api/v1/admin/users with a player JWT returns 403', async () => {
|
|
const server = app.getHttpServer();
|
|
const agent = request.agent(server);
|
|
|
|
// Prime CSRF cookie first.
|
|
await agent.get('/api/v1/auth/csrf');
|
|
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
|
const csrf = cookies.find((c: any) => c.name === 'csrf');
|
|
|
|
// Create a player via admin endpoint
|
|
await agent.post('/api/v1/admin/users')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.set('X-CSRF-Token', csrf.value)
|
|
.send({ username: 'player1', password: 'Sup3rSecret!Pass', role: 'player' })
|
|
.expect(201);
|
|
|
|
const playerLogin = await agent.post('/api/v1/auth/login')
|
|
.set('X-CSRF-Token', csrf.value)
|
|
.send({ username: 'player1', password: 'Sup3rSecret!Pass' })
|
|
.expect(201);
|
|
const playerToken = playerLogin.body.accessToken;
|
|
|
|
await request(app.getHttpServer())
|
|
.get('/api/v1/admin/users')
|
|
.set('Authorization', `Bearer ${playerToken}`)
|
|
.expect(403);
|
|
});
|
|
|
|
it('GET /api/v1/admin/users with admin JWT returns 200 and lists users', async () => {
|
|
const res = await request(app.getHttpServer())
|
|
.get('/api/v1/admin/users')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.expect(200);
|
|
expect(Array.isArray(res.body)).toBe(true);
|
|
expect(res.body.length).toBeGreaterThanOrEqual(2);
|
|
});
|
|
|
|
it('DELETE /api/v1/admin/users/:id on the only admin returns 409 LAST_ADMIN', async () => {
|
|
const list = await request(app.getHttpServer())
|
|
.get('/api/v1/admin/users')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.expect(200);
|
|
const adminRow = list.body.find((u: any) => u.role === 'admin');
|
|
expect(adminRow).toBeDefined();
|
|
|
|
const server = app.getHttpServer();
|
|
const agent = request.agent(server);
|
|
await agent.get('/api/v1/auth/csrf');
|
|
const cookies: any = agent.jar.getCookies(CookieAccessInfo.All);
|
|
const csrf = cookies.find((c: any) => c.name === 'csrf');
|
|
|
|
await agent.delete(`/api/v1/admin/users/${adminRow.id}`)
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.set('X-CSRF-Token', csrf.value)
|
|
.expect(409);
|
|
});
|
|
}); |