56 lines
2.1 KiB
TypeScript
56 lines
2.1 KiB
TypeScript
import { describe, expect, it, vi } from 'vitest'
|
|
import type { Request, Response } from 'express'
|
|
import { ROLES_EDITAN_PLAN, requireEditaPlan, resolverFiltroMios } from './planes.controller.js'
|
|
|
|
describe('resolverFiltroMios (?mios=1)', () => {
|
|
it('sin ?mios=1 no filtra nada', () => {
|
|
expect(resolverFiltroMios(undefined, { sub: 'u1', rol: 'vendedor' })).toBeUndefined()
|
|
expect(resolverFiltroMios('0', { sub: 'u1', rol: 'vendedor' })).toBeUndefined()
|
|
})
|
|
|
|
it('vendedor con ?mios=1 → filtra por su sub', () => {
|
|
expect(resolverFiltroMios('1', { sub: 'u1', rol: 'vendedor' })).toEqual({ vendedorId: 'u1' })
|
|
})
|
|
|
|
it('admin y coordinador ven todo aunque pidan ?mios=1', () => {
|
|
expect(resolverFiltroMios('1', { sub: 'u1', rol: 'admin' })).toBeUndefined()
|
|
expect(resolverFiltroMios('1', { sub: 'u1', rol: 'coordinador' })).toBeUndefined()
|
|
})
|
|
|
|
it('sin token (AUTH_ENABLED=false) el filtro no aplica', () => {
|
|
expect(resolverFiltroMios('1', undefined)).toBeUndefined()
|
|
})
|
|
})
|
|
|
|
const reqCon = (rol?: string): Request =>
|
|
({
|
|
body: {},
|
|
params: { id: 'plan-1' },
|
|
...(rol ? { usuarioToken: { sub: 'u-1', username: 'x', rol } } : {}),
|
|
}) as unknown as Request
|
|
|
|
describe('requireEditaPlan (PATCH /planes/:id)', () => {
|
|
it('admin, manager, dirección y sus roles legacy pasan', () => {
|
|
for (const rol of ROLES_EDITAN_PLAN) {
|
|
const next = vi.fn()
|
|
requireEditaPlan(reqCon(rol), {} as Response, next)
|
|
expect(next).toHaveBeenCalled()
|
|
}
|
|
expect(ROLES_EDITAN_PLAN).toEqual(
|
|
expect.arrayContaining(['admin', 'manager', 'direccion_general', 'director_ventas']),
|
|
)
|
|
})
|
|
|
|
it('los roles de cobranza que no son dirección quedan fuera', () => {
|
|
for (const rol of ['vendedor', 'administracion_venta', 'escrituracion']) {
|
|
expect(() => requireEditaPlan(reqCon(rol), {} as Response, vi.fn())).toThrow(
|
|
'Solo admin, manager o dirección pueden modificar el plan',
|
|
)
|
|
}
|
|
})
|
|
|
|
it('sin token también rechaza (403)', () => {
|
|
expect(() => requireEditaPlan(reqCon(), {} as Response, vi.fn())).toThrow()
|
|
})
|
|
})
|