199 lines
7.3 KiB
JavaScript
199 lines
7.3 KiB
JavaScript
import { describe, it, beforeEach, afterEach } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import fs from 'node:fs';
|
|
import path from 'node:path';
|
|
import { MockApacheDriver } from '../../src/infrastructure/apache/mock.apache.driver.js';
|
|
import { SiteService } from '../../src/services/site.service.js';
|
|
import { TemplateService } from '../../src/services/template.service.js';
|
|
import config from '../../src/config/env.js';
|
|
|
|
describe('Unit Tests: Subdomain Risk Validation & Protection Policies', () => {
|
|
const testTmpDir = path.resolve(process.cwd(), '.tmp/test-risk');
|
|
let mockDriver;
|
|
let siteService;
|
|
|
|
beforeEach(() => {
|
|
if (fs.existsSync(testTmpDir)) {
|
|
fs.rmSync(testTmpDir, { recursive: true, force: true });
|
|
}
|
|
mockDriver = new MockApacheDriver(testTmpDir);
|
|
siteService = new SiteService(mockDriver, new TemplateService());
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (fs.existsSync(testTmpDir)) {
|
|
fs.rmSync(testTmpDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
describe('1. assertNotProtected(name, action)', () => {
|
|
it("para 'delete': debe proteger estrictamente '000-default', 'default-ssl', 'panel' y 'git'", () => {
|
|
const protectedNames = ['000-default', 'default-ssl', 'panel', 'git'];
|
|
for (const name of protectedNames) {
|
|
assert.throws(
|
|
() => siteService.assertNotProtected(name, 'delete'),
|
|
(err) => {
|
|
assert.equal(err.status, 403);
|
|
assert.equal(err.code, 'SITE_PROTECTED');
|
|
return true;
|
|
},
|
|
`Debe bloquear 'delete' sobre ${name}`
|
|
);
|
|
}
|
|
});
|
|
|
|
it("para 'update': NO debe bloquear 'panel' ni 'git'", () => {
|
|
assert.doesNotThrow(() => {
|
|
siteService.assertNotProtected('panel', 'update');
|
|
});
|
|
assert.doesNotThrow(() => {
|
|
siteService.assertNotProtected('git', 'update');
|
|
});
|
|
});
|
|
|
|
it("para 'update': SOLO debe bloquear '000-default' y 'default-ssl'", () => {
|
|
assert.throws(
|
|
() => siteService.assertNotProtected('000-default', 'update'),
|
|
(err) => {
|
|
assert.equal(err.status, 403);
|
|
assert.equal(err.code, 'SITE_PROTECTED');
|
|
return true;
|
|
}
|
|
);
|
|
assert.throws(
|
|
() => siteService.assertNotProtected('default-ssl', 'update'),
|
|
(err) => {
|
|
assert.equal(err.status, 403);
|
|
assert.equal(err.code, 'SITE_PROTECTED');
|
|
return true;
|
|
}
|
|
);
|
|
});
|
|
});
|
|
|
|
describe('2. validateSubdomainRisk(name, { type, port })', () => {
|
|
it('debe detectar riesgo crítico de auto-bloqueo si es panel y se cambia a estático', async () => {
|
|
await mockDriver.createVirtualHost(
|
|
'panel',
|
|
`<VirtualHost *:80>
|
|
ServerName panel.${config.baseDomain}
|
|
ProxyPass / http://127.0.0.1:${config.port}/
|
|
</VirtualHost>`
|
|
);
|
|
|
|
const risk = await siteService.validateSubdomainRisk('panel', { type: 'static' });
|
|
|
|
assert.equal(risk.subdomain, 'panel');
|
|
assert.equal(risk.isSelfPanel, true);
|
|
assert.equal(risk.hasRisk, true);
|
|
assert.equal(risk.requiresConfirmation, true);
|
|
assert.ok(risk.risks.some((r) => r.code === 'SELF_PANEL_STATIC' && r.level === 'critical'));
|
|
});
|
|
|
|
it('debe detectar riesgo crítico de auto-bloqueo si es panel y se cambia su puerto', async () => {
|
|
await mockDriver.createVirtualHost(
|
|
'panel',
|
|
`<VirtualHost *:80>
|
|
ServerName panel.${config.baseDomain}
|
|
ProxyPass / http://127.0.0.1:${config.port}/
|
|
</VirtualHost>`
|
|
);
|
|
|
|
const risk = await siteService.validateSubdomainRisk('panel', { type: 'proxy', port: 8080 });
|
|
|
|
assert.equal(risk.isSelfPanel, true);
|
|
assert.equal(risk.hasRisk, true);
|
|
assert.equal(risk.requiresConfirmation, true);
|
|
assert.ok(risk.risks.some((r) => r.code === 'SELF_PANEL_PORT' && r.level === 'critical'));
|
|
});
|
|
|
|
it('debe detectar riesgo crítico si es git y se intenta cambiar a estático', async () => {
|
|
const risk = await siteService.validateSubdomainRisk('git', { type: 'static' });
|
|
|
|
assert.equal(risk.isGitea, true);
|
|
assert.equal(risk.hasRisk, true);
|
|
assert.equal(risk.requiresConfirmation, true);
|
|
assert.ok(risk.risks.some((r) => r.code === 'GITEA_STATIC' && r.level === 'critical'));
|
|
});
|
|
|
|
it('debe detectar riesgo si es git y se intenta cambiar su puerto de 3000 a otro', async () => {
|
|
const risk = await siteService.validateSubdomainRisk('git', { type: 'proxy', port: 4000 });
|
|
|
|
assert.equal(risk.isGitea, true);
|
|
assert.equal(risk.hasRisk, true);
|
|
assert.equal(risk.requiresConfirmation, true);
|
|
assert.ok(risk.risks.some((r) => r.code === 'GITEA_PORT' && r.level === 'high'));
|
|
});
|
|
|
|
it('debe detectar cambio de modo arquitectónico de Static a Proxy', async () => {
|
|
await siteService.createSubdomain({ name: 'blog', type: 'static' });
|
|
|
|
const risk = await siteService.validateSubdomainRisk('blog', { type: 'proxy', port: 8080 });
|
|
|
|
assert.equal(risk.currentType, 'static');
|
|
assert.equal(risk.targetType, 'proxy');
|
|
assert.equal(risk.hasRisk, true);
|
|
assert.equal(risk.requiresConfirmation, true);
|
|
assert.ok(risk.risks.some((r) => r.code === 'ARCHITECTURE_MODE_CHANGE'));
|
|
});
|
|
|
|
it('debe detectar colisión con puertos reservados del sistema', async () => {
|
|
await siteService.createSubdomain({ name: 'api-service', type: 'proxy', port: 8080 });
|
|
|
|
// Intentar cambiar a puerto 3306 (MySQL reservado)
|
|
const risk = await siteService.validateSubdomainRisk('api-service', { type: 'proxy', port: 3306 });
|
|
|
|
assert.equal(risk.hasRisk, true);
|
|
assert.ok(risk.risks.some((r) => r.code === 'PORT_RESERVED'));
|
|
});
|
|
|
|
it('debe detectar colisión con puertos ya en uso por otros subdominios', async () => {
|
|
await siteService.createSubdomain({ name: 'app1', type: 'proxy', port: 5000 });
|
|
await siteService.createSubdomain({ name: 'app2', type: 'proxy', port: 6000 });
|
|
|
|
// Intentar cambiar app2 al puerto 5000 usado por app1
|
|
const risk = await siteService.validateSubdomainRisk('app2', { type: 'proxy', port: 5000 });
|
|
|
|
assert.equal(risk.hasRisk, true);
|
|
assert.ok(risk.risks.some((r) => r.code === 'PORT_IN_USE'));
|
|
});
|
|
});
|
|
|
|
describe('3. updateSubdomain con confirmación atómica', () => {
|
|
it('debe rechazar con 428 Precondition Required si requiresConfirmation y !forceConfirmed', async () => {
|
|
await siteService.createSubdomain({ name: 'service-a', type: 'static' });
|
|
|
|
let caught = null;
|
|
try {
|
|
await siteService.updateSubdomain('service-a', {
|
|
type: 'proxy',
|
|
port: 7000,
|
|
forceConfirmed: false
|
|
});
|
|
} catch (err) {
|
|
caught = err;
|
|
}
|
|
|
|
assert.ok(caught);
|
|
assert.equal(caught.status, 428);
|
|
assert.equal(caught.code, 'PRECONDITION_REQUIRED');
|
|
assert.ok(caught.riskReport);
|
|
assert.equal(caught.riskReport.requiresConfirmation, true);
|
|
});
|
|
|
|
it('debe proceder exitosamente si forceConfirmed === true', async () => {
|
|
await siteService.createSubdomain({ name: 'service-b', type: 'static' });
|
|
|
|
const updated = await siteService.updateSubdomain('service-b', {
|
|
type: 'proxy',
|
|
port: 7001,
|
|
forceConfirmed: true
|
|
});
|
|
|
|
assert.equal(updated.name, 'service-b');
|
|
assert.equal(updated.type, 'proxy');
|
|
assert.equal(updated.port, 7001);
|
|
});
|
|
});
|
|
});
|