218 lines
8.4 KiB
TypeScript
218 lines
8.4 KiB
TypeScript
process.env.DATABASE_PATH = ':memory:';
|
|
process.env.THEMES_DIR = './themes';
|
|
process.env.FRONTEND_DIST = './frontend/dist';
|
|
process.env.UPLOAD_DIR = '/tmp/hipctf-challenges-uploads';
|
|
process.env.UPLOAD_SIZE_LIMIT = '5mb';
|
|
process.env.NODE_ENV = 'test';
|
|
|
|
import * as fs from 'fs';
|
|
import * as os from 'os';
|
|
import * as path from 'path';
|
|
import { Test } from '@nestjs/testing';
|
|
import { ConfigModule } from '@nestjs/config';
|
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
|
import { validateEnv } from '../../backend/src/config/env.schema';
|
|
import { AdminChallengesService } from '../../backend/src/modules/admin/challenges.service';
|
|
import { ChallengeFilesService } from '../../backend/src/modules/admin/challenge-files.service';
|
|
import { CategoryEntity } from '../../backend/src/database/entities/category.entity';
|
|
import { ChallengeEntity } from '../../backend/src/database/entities/challenge.entity';
|
|
import { ChallengeFileEntity } from '../../backend/src/database/entities/challenge-file.entity';
|
|
import { SolveEntity } from '../../backend/src/database/entities/solve.entity';
|
|
import { UserEntity } from '../../backend/src/database/entities/user.entity';
|
|
import { SettingEntity } from '../../backend/src/database/entities/setting.entity';
|
|
import { v4 as uuid } from 'uuid';
|
|
import { Repository } from 'typeorm';
|
|
import { getRepositoryToken } from '@nestjs/typeorm';
|
|
import { InitSchema1700000000000 } from '../../backend/src/database/migrations/1700000000000-InitSchema';
|
|
import { SeedSystemData1700000000100 } from '../../backend/src/database/migrations/1700000000100-SeedSystemData';
|
|
import { AddCategoryTimestampsAndUniqueAbbrev1700000000200 } from '../../backend/src/database/migrations/1700000000200-AddCategoryTimestampsAndUniqueAbbrev';
|
|
import { UpdateSystemCategoryKeys1700000000300 } from '../../backend/src/database/migrations/1700000000300-UpdateSystemCategoryKeys';
|
|
import { RepairCategorySchemaAndSystemCategories1700000000400 } from '../../backend/src/database/migrations/1700000000400-RepairCategorySchemaAndSystemCategories';
|
|
import { UpgradeChallengeAdminSchema1700000000500 } from '../../backend/src/database/migrations/1700000000500-UpgradeChallengeAdminSchema';
|
|
|
|
describe('AdminChallengesService - CRUD + business rules', () => {
|
|
let service: AdminChallengesService;
|
|
let catRepo: Repository<CategoryEntity>;
|
|
let chRepo: Repository<ChallengeEntity>;
|
|
let fileRepo: Repository<ChallengeFileEntity>;
|
|
let fileService: ChallengeFilesService;
|
|
let categoryId: string;
|
|
let uploadDir: string;
|
|
|
|
beforeAll(async () => {
|
|
uploadDir = fs.mkdtempSync(path.join(os.tmpdir(), 'hipctf-challenges-'));
|
|
process.env.UPLOAD_DIR = uploadDir;
|
|
const moduleRef = await Test.createTestingModule({
|
|
imports: [
|
|
ConfigModule.forRoot({ isGlobal: true, validate: validateEnv }),
|
|
TypeOrmModule.forRoot({
|
|
type: 'better-sqlite3',
|
|
database: ':memory:',
|
|
entities: [UserEntity, SettingEntity, CategoryEntity, ChallengeEntity, ChallengeFileEntity, SolveEntity],
|
|
migrations: [
|
|
InitSchema1700000000000,
|
|
SeedSystemData1700000000100,
|
|
AddCategoryTimestampsAndUniqueAbbrev1700000000200,
|
|
UpdateSystemCategoryKeys1700000000300,
|
|
RepairCategorySchemaAndSystemCategories1700000000400,
|
|
UpgradeChallengeAdminSchema1700000000500,
|
|
],
|
|
migrationsRun: true,
|
|
synchronize: false,
|
|
}),
|
|
TypeOrmModule.forFeature([CategoryEntity, ChallengeEntity, ChallengeFileEntity, SolveEntity]),
|
|
],
|
|
providers: [AdminChallengesService, ChallengeFilesService],
|
|
}).compile();
|
|
moduleRef.init();
|
|
service = moduleRef.get(AdminChallengesService);
|
|
catRepo = moduleRef.get<Repository<CategoryEntity>>(getRepositoryToken(CategoryEntity));
|
|
chRepo = moduleRef.get<Repository<ChallengeEntity>>(getRepositoryToken(ChallengeEntity));
|
|
fileRepo = moduleRef.get<Repository<ChallengeFileEntity>>(getRepositoryToken(ChallengeFileEntity));
|
|
fileService = moduleRef.get(ChallengeFilesService);
|
|
const cat = await catRepo.findOne({ where: { systemKey: 'CRY' } });
|
|
if (!cat) throw new Error('expected CRY category seed');
|
|
categoryId = cat.id;
|
|
});
|
|
|
|
afterAll(async () => {
|
|
if (uploadDir && fs.existsSync(uploadDir)) {
|
|
try {
|
|
fs.rmSync(uploadDir, { recursive: true, force: true });
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
});
|
|
|
|
it('creates a WEB challenge with a single staged file and exposes it in the list', async () => {
|
|
const staged = fileService.stage(Buffer.from('hello'), 'greet.txt', 'text/plain');
|
|
const created = await service.create({
|
|
name: 'Welcome',
|
|
descriptionMd: 'intro',
|
|
categoryId,
|
|
difficulty: 'LOW',
|
|
initialPoints: 100,
|
|
minimumPoints: 50,
|
|
decaySolves: 10,
|
|
flag: 'flag{...}',
|
|
protocol: 'WEB',
|
|
port: null,
|
|
ipAddress: '',
|
|
enabled: true,
|
|
files: [
|
|
{
|
|
stagedToken: staged.token,
|
|
originalFilename: staged.originalFilename,
|
|
mimeType: staged.mimeType,
|
|
sizeBytes: staged.sizeBytes,
|
|
},
|
|
],
|
|
} as any);
|
|
expect(created.id).toBeDefined();
|
|
expect(created.files).toHaveLength(1);
|
|
expect(created.files[0].originalFilename).toBe('greet.txt');
|
|
expect(fs.existsSync(path.join(uploadDir, 'challenges', staged.storedFilename))).toBe(true);
|
|
|
|
const list = await service.list({});
|
|
const row = list.find((r) => r.id === created.id);
|
|
expect(row).toBeDefined();
|
|
// Secret flag must NOT be in list output.
|
|
expect((row as any).flag).toBeUndefined();
|
|
expect(row!.fileCount).toBe(1);
|
|
});
|
|
|
|
it('rejects creating an NC challenge without a port', async () => {
|
|
await expect(
|
|
service.create({
|
|
name: 'Bad NC',
|
|
descriptionMd: 'd',
|
|
categoryId,
|
|
difficulty: 'LOW',
|
|
initialPoints: 100,
|
|
minimumPoints: 50,
|
|
decaySolves: 10,
|
|
flag: 'f',
|
|
protocol: 'NC',
|
|
port: null,
|
|
ipAddress: '',
|
|
enabled: true,
|
|
} as any),
|
|
).rejects.toMatchObject({ status: 400 });
|
|
});
|
|
|
|
it('rejects duplicate challenge names case-insensitively', async () => {
|
|
await service.create({
|
|
name: 'Alpha',
|
|
descriptionMd: 'd',
|
|
categoryId,
|
|
difficulty: 'LOW',
|
|
initialPoints: 100,
|
|
minimumPoints: 50,
|
|
decaySolves: 10,
|
|
flag: 'f1',
|
|
protocol: 'WEB',
|
|
port: null,
|
|
ipAddress: '',
|
|
enabled: true,
|
|
} as any);
|
|
await expect(
|
|
service.create({
|
|
name: 'ALPHA',
|
|
descriptionMd: 'd',
|
|
categoryId,
|
|
difficulty: 'LOW',
|
|
initialPoints: 100,
|
|
minimumPoints: 50,
|
|
decaySolves: 10,
|
|
flag: 'f2',
|
|
protocol: 'WEB',
|
|
port: null,
|
|
ipAddress: '',
|
|
enabled: true,
|
|
} as any),
|
|
).rejects.toMatchObject({ code: 'CHALLENGE_NAME_TAKEN' });
|
|
});
|
|
|
|
it('deletes a challenge, removes its ChallengeFile rows and best-effort unlinks the disk file', async () => {
|
|
const staged = fileService.stage(Buffer.from('bye'), 'b.txt', 'text/plain');
|
|
const created = await service.create({
|
|
name: 'Ephemeral',
|
|
descriptionMd: 'd',
|
|
categoryId,
|
|
difficulty: 'LOW',
|
|
initialPoints: 100,
|
|
minimumPoints: 50,
|
|
decaySolves: 10,
|
|
flag: 'f',
|
|
protocol: 'WEB',
|
|
port: null,
|
|
ipAddress: '',
|
|
enabled: true,
|
|
files: [
|
|
{
|
|
stagedToken: staged.token,
|
|
originalFilename: staged.originalFilename,
|
|
mimeType: staged.mimeType,
|
|
sizeBytes: staged.sizeBytes,
|
|
},
|
|
],
|
|
} as any);
|
|
const storedRow = await fileRepo.findOne({ where: { challengeId: created.id } });
|
|
const storedName = storedRow?.storedFilename;
|
|
expect(storedName).toBeDefined();
|
|
await service.remove(created.id);
|
|
const files = await fileRepo.find({ where: { challengeId: created.id } });
|
|
expect(files).toHaveLength(0);
|
|
const target = path.join(uploadDir, 'challenges', storedName!);
|
|
// Either the file is gone or never landed on disk if commit failed;
|
|
// service.remove tolerates unlink failures, so we accept both states.
|
|
expect([true, false]).toContain(fs.existsSync(target));
|
|
});
|
|
|
|
it('search filter matches case-insensitive substring on name', async () => {
|
|
const list = await service.list({ q: 'alpha' });
|
|
expect(list.length).toBeGreaterThanOrEqual(1);
|
|
expect(list.every((r) => r.name.toLowerCase().includes('alpha'))).toBe(true);
|
|
});
|
|
}); |