131 lines
4.4 KiB
JavaScript
131 lines
4.4 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 { UserStore } from '../../src/infrastructure/storage/user.store.js';
|
|
|
|
describe('Unit Tests: User Storage & Persistence (user.store.js)', () => {
|
|
const testDataDir = path.resolve(process.cwd(), '.tmp/test-data');
|
|
const testFile = path.join(testDataDir, 'users.json');
|
|
|
|
beforeEach(() => {
|
|
if (fs.existsSync(testDataDir)) {
|
|
fs.rmSync(testDataDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
afterEach(() => {
|
|
if (fs.existsSync(testDataDir)) {
|
|
fs.rmSync(testDataDir, { recursive: true, force: true });
|
|
}
|
|
});
|
|
|
|
it('debe sembrar automáticamente el superadmin si el archivo no existe', async () => {
|
|
const store = new UserStore(testFile);
|
|
await store.init({ seedAdmin: true });
|
|
|
|
assert.ok(fs.existsSync(testFile), 'El archivo de persistencia debe existir');
|
|
const users = store.findAll();
|
|
assert.equal(users.length, 1);
|
|
assert.equal(users[0].username, 'admin');
|
|
assert.equal(users[0].role, 'admin');
|
|
assert.equal(users[0].active, true);
|
|
assert.equal(typeof users[0].passwordHash, 'undefined', 'passwordHash debe estar despojado en findAll');
|
|
});
|
|
|
|
it('debe crear un nuevo usuario y persistirlo atómicamente en disco', async () => {
|
|
const store = new UserStore(testFile);
|
|
await store.init({ seedAdmin: true });
|
|
|
|
const created = store.create({
|
|
username: 'dev_user',
|
|
passwordHash: 'scrypt$16384$8$1$fake$hash',
|
|
fullName: 'Developer User',
|
|
role: 'operator'
|
|
});
|
|
|
|
assert.ok(created.id.startsWith('u-'));
|
|
assert.equal(created.username, 'dev_user');
|
|
assert.equal(created.role, 'operator');
|
|
|
|
// Comprobar persistencia física en disco
|
|
const rawDiskData = JSON.parse(fs.readFileSync(testFile, 'utf-8'));
|
|
assert.equal(rawDiskData.length, 2);
|
|
assert.equal(rawDiskData[1].username, 'dev_user');
|
|
});
|
|
|
|
it('debe rechazar la creación de usuarios con nombres duplicados', async () => {
|
|
const store = new UserStore(testFile);
|
|
await store.init({ seedAdmin: true });
|
|
|
|
assert.throws(
|
|
() => store.create({ username: 'admin', passwordHash: 'hash', role: 'operator' }),
|
|
/ya está registrado/
|
|
);
|
|
});
|
|
|
|
it('debe buscar usuarios por username de forma insensible a mayúsculas', async () => {
|
|
const store = new UserStore(testFile);
|
|
await store.init({ seedAdmin: true });
|
|
|
|
const found = store.findByUsername('ADMIN');
|
|
assert.ok(found);
|
|
assert.equal(found.username, 'admin');
|
|
});
|
|
|
|
it('debe actualizar los datos de un usuario', async () => {
|
|
const store = new UserStore(testFile);
|
|
await store.init({ seedAdmin: true });
|
|
|
|
const admin = store.findByUsername('admin');
|
|
const updated = store.update(admin.id, { fullName: 'Super Administrador VPS' });
|
|
|
|
assert.equal(updated.fullName, 'Super Administrador VPS');
|
|
const reRead = store.findById(admin.id);
|
|
assert.equal(reRead.fullName, 'Super Administrador VPS');
|
|
});
|
|
|
|
it('debe impedir la eliminación del único administrador del sistema', async () => {
|
|
const store = new UserStore(testFile);
|
|
await store.init({ seedAdmin: true });
|
|
|
|
const admin = store.findByUsername('admin');
|
|
assert.throws(
|
|
() => store.delete(admin.id),
|
|
/No es posible eliminar al único administrador del sistema/
|
|
);
|
|
});
|
|
|
|
it('debe impedir la degradación o desactivación del único administrador del sistema', async () => {
|
|
const store = new UserStore(testFile);
|
|
await store.init({ seedAdmin: true });
|
|
|
|
const admin = store.findByUsername('admin');
|
|
assert.throws(
|
|
() => store.update(admin.id, { role: 'operator' }),
|
|
/No es posible degradar ni desactivar al único administrador activo/
|
|
);
|
|
assert.throws(
|
|
() => store.update(admin.id, { active: false }),
|
|
/No es posible degradar ni desactivar al único administrador activo/
|
|
);
|
|
});
|
|
|
|
it('debe permitir eliminar un administrador si existe otro administrador activo', async () => {
|
|
const store = new UserStore(testFile);
|
|
await store.init({ seedAdmin: true });
|
|
|
|
const secondAdmin = store.create({
|
|
username: 'admin2',
|
|
passwordHash: 'scrypt$fake',
|
|
fullName: 'Segundo Admin',
|
|
role: 'admin'
|
|
});
|
|
|
|
assert.equal(store.countAdmins(), 2);
|
|
const deleted = store.delete(secondAdmin.id);
|
|
assert.equal(deleted, true);
|
|
assert.equal(store.countAdmins(), 1);
|
|
});
|
|
});
|