147 lines
5.8 KiB
JavaScript
147 lines
5.8 KiB
JavaScript
import { describe, it } from 'node:test';
|
|
import assert from 'node:assert/strict';
|
|
import {
|
|
validateSubdomainName,
|
|
validatePort,
|
|
validateUsername,
|
|
validatePassword,
|
|
validateRole,
|
|
sanitizePath
|
|
} from '../../src/utils/validation.util.js';
|
|
|
|
describe('Unit Tests: Validation Utilities', () => {
|
|
describe('Subdomain Validation (validateSubdomainName)', () => {
|
|
it('debe aceptar nombres de subdominios válidos', () => {
|
|
const validNames = ['blog', 'api-v2', 'dashboard-service', 'site123', 'a'];
|
|
for (const name of validNames) {
|
|
assert.equal(validateSubdomainName(name), name);
|
|
}
|
|
});
|
|
|
|
it('debe normalizar letras mayúsculas a minúsculas', () => {
|
|
assert.equal(validateSubdomainName('FastAPI-Service'), 'fastapi-service');
|
|
assert.equal(validateSubdomainName('PORTAL'), 'portal');
|
|
});
|
|
|
|
it('debe rechazar nombres que comiencen o terminen con guión', () => {
|
|
assert.throws(() => validateSubdomainName('-invalid'), /no puede comenzar ni terminar con un guión/);
|
|
assert.throws(() => validateSubdomainName('invalid-'), /no puede comenzar ni terminar con un guión/);
|
|
});
|
|
|
|
it('debe neutralizar intentos de inyección de comandos shell (Command Injection)', () => {
|
|
const injectionAttacks = [
|
|
'test; rm -rf /',
|
|
'api`whoami`',
|
|
'sub$(id)',
|
|
'site&touch hacked',
|
|
'app|cat /etc/passwd',
|
|
'foo;bash -i',
|
|
'sub domain',
|
|
'sub/dir'
|
|
];
|
|
for (const attack of injectionAttacks) {
|
|
assert.throws(
|
|
() => validateSubdomainName(attack),
|
|
/El subdominio solo puede contener letras minúsculas, dígitos y guiones/,
|
|
`Debe rechazar el vector: ${attack}`
|
|
);
|
|
}
|
|
});
|
|
|
|
it('debe rechazar nombres con longitud mayor a 63 caracteres', () => {
|
|
const longName = 'a'.repeat(64);
|
|
assert.throws(() => validateSubdomainName(longName), /longitud del subdominio debe estar entre 1 y 63/);
|
|
});
|
|
|
|
it('debe rechazar entradas vacías, nulas o no textuales', () => {
|
|
assert.throws(() => validateSubdomainName(''), /obligatorio/);
|
|
assert.throws(() => validateSubdomainName(null), /obligatorio/);
|
|
assert.throws(() => validateSubdomainName(undefined), /obligatorio/);
|
|
assert.throws(() => validateSubdomainName(12345), /obligatorio/);
|
|
});
|
|
});
|
|
|
|
describe('Port Validation (validatePort)', () => {
|
|
const reserved = [22, 80, 443, 3000, 3005, 3306, 5101, 5432];
|
|
const active = [8080, 9000];
|
|
|
|
it('debe aceptar puertos válidos en el rango 1024-65535', () => {
|
|
assert.equal(validatePort(3001, reserved, active), 3001);
|
|
assert.equal(validatePort('5000', reserved, active), 5000);
|
|
assert.equal(validatePort(65535, reserved, active), 65535);
|
|
});
|
|
|
|
it('debe rechazar puertos fuera del rango permitido', () => {
|
|
assert.throws(() => validatePort(500, reserved, active), /encontrarse en el rango de puertos no privilegiados/);
|
|
assert.throws(() => validatePort(1023, reserved, active), /encontrarse en el rango de puertos no privilegiados/);
|
|
assert.throws(() => validatePort(65536, reserved, active), /encontrarse en el rango de puertos no privilegiados/);
|
|
assert.throws(() => validatePort(-1, reserved, active), /encontrarse en el rango de puertos no privilegiados/);
|
|
});
|
|
|
|
it('debe rechazar puertos reservados del sistema (Gitea, Panel, SSH, DBs)', () => {
|
|
for (const port of reserved) {
|
|
assert.throws(
|
|
() => validatePort(port, reserved, active),
|
|
/está reservado por el sistema/,
|
|
`Debe bloquear el puerto reservado ${port}`
|
|
);
|
|
}
|
|
});
|
|
|
|
it('debe rechazar puertos ya asignados en otros VirtualHosts activos', () => {
|
|
assert.throws(
|
|
() => validatePort(8080, reserved, active),
|
|
/ya se encuentra asignado a otro subdominio activo/
|
|
);
|
|
});
|
|
|
|
it('debe rechazar entradas no numéricas o flotantes', () => {
|
|
assert.throws(() => validatePort('abc', reserved, active), /número entero válido/);
|
|
assert.throws(() => validatePort(4000.5, reserved, active), /número entero válido/);
|
|
assert.throws(() => validatePort(null, reserved, active), /número entero válido/);
|
|
});
|
|
});
|
|
|
|
describe('User and Password Validation', () => {
|
|
it('debe validar nombres de usuario alfanuméricos', () => {
|
|
assert.equal(validateUsername('admin_user'), 'admin_user');
|
|
assert.equal(validateUsername('dev-jose'), 'dev-jose');
|
|
assert.throws(() => validateUsername('ab'), /entre 3 y 32 caracteres/);
|
|
assert.throws(() => validateUsername('user@domain'), /entre 3 y 32 caracteres/);
|
|
});
|
|
|
|
it('debe validar contraseñas de al menos 8 caracteres', () => {
|
|
assert.equal(validatePassword('SuperSecret123!'), 'SuperSecret123!');
|
|
assert.throws(() => validatePassword('short'), /al menos 8 caracteres/);
|
|
assert.throws(() => validatePassword(''), /obligatoria/);
|
|
});
|
|
|
|
it('debe validar roles permitidos', () => {
|
|
assert.equal(validateRole('admin'), 'admin');
|
|
assert.equal(validateRole('operator'), 'operator');
|
|
assert.throws(() => validateRole('superuser'), /El rol debe ser 'admin' u 'operator'/);
|
|
assert.throws(() => validateRole('guest'), /El rol debe ser 'admin' u 'operator'/);
|
|
});
|
|
});
|
|
|
|
describe('Path Traversal Prevention (sanitizePath)', () => {
|
|
it('debe permitir rutas dentro del directorio base', () => {
|
|
const base = '/var/www';
|
|
const safe = sanitizePath(base, 'portfolio');
|
|
assert.equal(safe, '/var/www/portfolio');
|
|
});
|
|
|
|
it('debe bloquear intentos de escape de directorio (Directory Traversal)', () => {
|
|
const base = '/var/www';
|
|
assert.throws(
|
|
() => sanitizePath(base, '../etc/passwd'),
|
|
/intento de escape de ruta/
|
|
);
|
|
assert.throws(
|
|
() => sanitizePath(base, '../../root'),
|
|
/intento de escape de ruta/
|
|
);
|
|
});
|
|
});
|
|
});
|