Files
basic-erp/apps/api/src/permissions/path-matcher.spec.ts
T

27 lines
1.3 KiB
TypeScript
Raw Normal View History

import { isPathAllowed, matchesPattern } from './path-matcher.js';
describe('matchesPattern', () => {
it('"*" permite todo', () => expect(matchesPattern('*', '/lo/que/sea')).toBe(true));
it('"/ventas/*" permite la ruta y sus hijas, no prefijos parecidos', () => {
expect(matchesPattern('/ventas/*', '/ventas')).toBe(true);
expect(matchesPattern('/ventas/*', '/ventas/ordenes')).toBe(true);
expect(matchesPattern('/ventas/*', '/ventas/ordenes/mantenimiento')).toBe(true);
expect(matchesPattern('/ventas/*', '/ventas-x')).toBe(false);
expect(matchesPattern('/ventas/*', '/compras/ordenes')).toBe(false);
});
it('ruta exacta solo coincide consigo misma', () => {
expect(matchesPattern('/ventas/ordenes', '/ventas/ordenes')).toBe(true);
expect(matchesPattern('/ventas/ordenes', '/ventas/ordenes/x')).toBe(false);
});
});
describe('isPathAllowed', () => {
it('null = cualquier sesión, incluso sin permisos', () => expect(isPathAllowed([], null)).toBe(true));
it('sin patrones no hay acceso', () => expect(isPathAllowed([], '/ventas/ordenes')).toBe(false));
it('basta con una de varias rutas requeridas', () => {
expect(isPathAllowed(['/socios/proveedores'], ['/socios/clientes', '/socios/proveedores'])).toBe(true);
expect(isPathAllowed(['/socios/otro'], ['/socios/clientes', '/socios/proveedores'])).toBe(false);
});
});