132 lines
5.1 KiB
TypeScript
132 lines
5.1 KiB
TypeScript
process.env.DATABASE_PATH = ':memory:';
|
|
process.env.THEMES_DIR = './themes';
|
|
process.env.FRONTEND_DIST = './frontend/dist';
|
|
process.env.UPLOAD_SIZE_LIMIT = '5mb';
|
|
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 { 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';
|
|
|
|
async function buildAgent(
|
|
app: INestApplication,
|
|
username: string,
|
|
password: string,
|
|
): Promise<{ agent: any; token: string }> {
|
|
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 res = await agent
|
|
.post('/api/v1/auth/login')
|
|
.set('X-CSRF-Token', csrf.value)
|
|
.send({ username, password })
|
|
.expect(201);
|
|
(agent as any).accessToken = res.body.accessToken as string;
|
|
return { agent, token: res.body.accessToken as string };
|
|
}
|
|
|
|
async function csrfHeaderFor(agent: any): Promise<string> {
|
|
const cookies = agent.jar.getCookies(CookieAccessInfo.All);
|
|
return (cookies as any[]).find((c: any) => c.name === 'csrf').value as string;
|
|
}
|
|
|
|
describe('Job 871: System endpoint authorization', () => {
|
|
let app: INestApplication;
|
|
let adminAgent: any;
|
|
let adminToken: string;
|
|
let playerAgent: any;
|
|
let playerToken: string;
|
|
|
|
beforeAll(async () => {
|
|
const moduleRef = await Test.createTestingModule({ imports: [AppModule] }).compile();
|
|
app = moduleRef.createNestApplication();
|
|
app.use(cookieParser());
|
|
app.use(express.json({ limit: '10mb' }));
|
|
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);
|
|
|
|
adminAgent = request.agent(app.getHttpServer());
|
|
await adminAgent.get('/api/v1/auth/csrf');
|
|
const csrfAdmin = (adminAgent.jar.getCookies(CookieAccessInfo.All) as any).find((c: any) => c.name === 'csrf');
|
|
const adminLogin = await adminAgent
|
|
.post('/api/v1/auth/login')
|
|
.set('X-CSRF-Token', csrfAdmin.value)
|
|
.send({ username: 'admin', password: 'Sup3rSecret!Pass' })
|
|
.expect(201);
|
|
adminToken = adminLogin.body.accessToken;
|
|
(adminAgent as any).accessToken = adminToken;
|
|
|
|
await adminAgent
|
|
.post('/api/v1/admin/users')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.set('X-CSRF-Token', csrfAdmin.value)
|
|
.send({ username: 'player1', password: 'Sup3rSecret!Pass', role: 'player' })
|
|
.expect(201);
|
|
|
|
const playerLogin = await adminAgent
|
|
.post('/api/v1/auth/login')
|
|
.set('X-CSRF-Token', csrfAdmin.value)
|
|
.send({ username: 'player1', password: 'Sup3rSecret!Pass' })
|
|
.expect(201);
|
|
playerToken = playerLogin.body.accessToken;
|
|
|
|
playerAgent = request.agent(app.getHttpServer());
|
|
await playerAgent.get('/api/v1/auth/csrf');
|
|
const csrfPlayer = (playerAgent.jar.getCookies(CookieAccessInfo.All) as any).find((c: any) => c.name === 'csrf');
|
|
await playerAgent
|
|
.post('/api/v1/auth/login')
|
|
.set('X-CSRF-Token', csrfPlayer.value)
|
|
.send({ username: 'player1', password: 'Sup3rSecret!Pass' })
|
|
.expect(201);
|
|
(playerAgent as any).accessToken = playerToken;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
await app.close();
|
|
});
|
|
|
|
it('GET /admin/system/backup without token returns 401', async () => {
|
|
await request(app.getHttpServer()).get('/api/v1/admin/system/backup').expect(401);
|
|
});
|
|
|
|
it('GET /admin/system/backup with player JWT returns 403', async () => {
|
|
await request(app.getHttpServer())
|
|
.get('/api/v1/admin/system/backup')
|
|
.set('Authorization', `Bearer ${playerToken}`)
|
|
.expect(403);
|
|
});
|
|
|
|
it('GET /admin/system/backup with admin JWT returns 200 application/json', async () => {
|
|
const res = await request(app.getHttpServer())
|
|
.get('/api/v1/admin/system/backup')
|
|
.set('Authorization', `Bearer ${adminToken}`)
|
|
.expect(200);
|
|
expect(res.headers['content-type']).toMatch(/application\/json/);
|
|
expect(typeof res.text).toBe('string');
|
|
const parsed = JSON.parse(res.text);
|
|
expect(parsed.format).toBe('hipctf-system-backup');
|
|
expect(parsed.tables).toBeDefined();
|
|
expect(Array.isArray(parsed.uploads)).toBe(true);
|
|
});
|
|
});
|