feat: marca Uriel Jareth Consulting, API segura para agentes, aprobacion humana y webhooks HMAC
This commit is contained in:
@@ -0,0 +1,182 @@
|
||||
import { describe, it } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { validarTransicionEstado } from "../src/lib/aprobacion-core";
|
||||
import { coincideContacto, hashSecret, type ActorContext } from "../src/lib/agent-auth-core";
|
||||
|
||||
const adminHumano: ActorContext = {
|
||||
type: "user",
|
||||
userId: "u-admin",
|
||||
email: "[email protected]",
|
||||
name: "Uriel Jareth",
|
||||
role: "admin",
|
||||
};
|
||||
|
||||
const asesorHumano: ActorContext = {
|
||||
type: "user",
|
||||
userId: "u-asesor",
|
||||
email: "[email protected]",
|
||||
name: "Asesor",
|
||||
role: "asesor",
|
||||
};
|
||||
|
||||
const salesAgent: ActorContext = {
|
||||
type: "agent",
|
||||
keyId: "k-sales",
|
||||
nombre: "sales-agent",
|
||||
prefijo: "ujc_live_abc1",
|
||||
scopes: [
|
||||
"catalogo:leer",
|
||||
"clientes:leer",
|
||||
"clientes:escribir",
|
||||
"cotizaciones:leer",
|
||||
"cotizaciones:borrador",
|
||||
"cotizaciones:enviar",
|
||||
],
|
||||
rateLimitPerMin: 60,
|
||||
};
|
||||
|
||||
describe("Guardia de aprobacion humana y transiciones de estado", () => {
|
||||
it("bloquea con 403 cuando un agente intenta aprobar una cotizacion", () => {
|
||||
for (const via of ["aprobar", "put", "estado"] as const) {
|
||||
const res = validarTransicionEstado({
|
||||
actual: "borrador",
|
||||
nuevo: "aprobada",
|
||||
actor: salesAgent,
|
||||
viaEndpoint: via,
|
||||
});
|
||||
assert.equal(res.ok, false);
|
||||
assert.equal(res.status, 403);
|
||||
}
|
||||
});
|
||||
|
||||
it("bloquea con 403 cuando un asesor (no admin) intenta aprobar", () => {
|
||||
const res = validarTransicionEstado({
|
||||
actual: "borrador",
|
||||
nuevo: "aprobada",
|
||||
actor: asesorHumano,
|
||||
viaEndpoint: "aprobar",
|
||||
});
|
||||
assert.equal(res.ok, false);
|
||||
assert.equal(res.status, 403);
|
||||
});
|
||||
|
||||
it("bloquea con 409 cuando un agente intenta enviar una cotizacion aun en borrador", () => {
|
||||
const res = validarTransicionEstado({
|
||||
actual: "borrador",
|
||||
nuevo: "enviada",
|
||||
actor: salesAgent,
|
||||
viaEndpoint: "enviar",
|
||||
aprobadaAt: null,
|
||||
});
|
||||
assert.equal(res.ok, false);
|
||||
assert.equal(res.status, 409);
|
||||
});
|
||||
|
||||
it("bloquea con 403 cuando un agente intenta cambiar a enviada por PUT en lugar de /enviar", () => {
|
||||
const res = validarTransicionEstado({
|
||||
actual: "aprobada",
|
||||
nuevo: "enviada",
|
||||
actor: salesAgent,
|
||||
viaEndpoint: "put",
|
||||
aprobadaAt: new Date(),
|
||||
});
|
||||
assert.equal(res.ok, false);
|
||||
assert.equal(res.status, 403);
|
||||
});
|
||||
|
||||
it("bloquea envio cuando verificarContacto no coincide con el cliente de la cotizacion", () => {
|
||||
const clienteCot = { email: "[email protected]", telefono: "+52 55 1111 2222" };
|
||||
assert.equal(coincideContacto("[email protected]", clienteCot), false);
|
||||
assert.equal(coincideContacto("5599998888", clienteCot), false);
|
||||
assert.equal(coincideContacto("[email protected]", clienteCot), true);
|
||||
});
|
||||
|
||||
it("permite al admin humano aprobar borrador -> aprobada", () => {
|
||||
const res = validarTransicionEstado({
|
||||
actual: "borrador",
|
||||
nuevo: "aprobada",
|
||||
actor: adminHumano,
|
||||
viaEndpoint: "aprobar",
|
||||
});
|
||||
assert.equal(res.ok, true);
|
||||
});
|
||||
|
||||
it("simula enlace firmado de un solo uso: aprueba una vez, rechaza reuso y rechaza expirado", () => {
|
||||
const rawToken = "a".repeat(64);
|
||||
const tokenStore = {
|
||||
tokenHash: hashSecret(rawToken),
|
||||
expiraAt: new Date(Date.now() + 60_000),
|
||||
usadoAt: null as Date | null,
|
||||
cotizacionEstado: "borrador" as string,
|
||||
};
|
||||
|
||||
function consumirToken(candidato: string, ahora: Date) {
|
||||
if (hashSecret(candidato) !== tokenStore.tokenHash) {
|
||||
return { ok: false, status: 404 };
|
||||
}
|
||||
if (tokenStore.usadoAt) {
|
||||
return { ok: false, status: 410 };
|
||||
}
|
||||
if (tokenStore.expiraAt <= ahora) {
|
||||
return { ok: false, status: 410 };
|
||||
}
|
||||
const trans = validarTransicionEstado({
|
||||
actual: tokenStore.cotizacionEstado,
|
||||
nuevo: "aprobada",
|
||||
actor: { type: "enlace_firmado" },
|
||||
viaEndpoint: "enlace_firmado",
|
||||
});
|
||||
if (!trans.ok) return trans;
|
||||
tokenStore.usadoAt = ahora;
|
||||
tokenStore.cotizacionEstado = "aprobada";
|
||||
return { ok: true, status: 200 };
|
||||
}
|
||||
|
||||
// Primer uso valido
|
||||
const first = consumirToken(rawToken, new Date());
|
||||
assert.equal(first.ok, true);
|
||||
assert.equal(tokenStore.cotizacionEstado, "aprobada");
|
||||
|
||||
// Segundo intento con el mismo token -> 410 Gone
|
||||
const second = consumirToken(rawToken, new Date());
|
||||
assert.equal(second.ok, false);
|
||||
assert.equal(second.status, 410);
|
||||
|
||||
// Token expirado -> 410 Gone
|
||||
tokenStore.usadoAt = null;
|
||||
tokenStore.cotizacionEstado = "borrador";
|
||||
const expired = consumirToken(rawToken, new Date(Date.now() + 120_000));
|
||||
assert.equal(expired.ok, false);
|
||||
assert.equal(expired.status, 410);
|
||||
});
|
||||
|
||||
it("permite al agente con cotizaciones:enviar pasar de aprobada -> enviada via /enviar", () => {
|
||||
const res = validarTransicionEstado({
|
||||
actual: "aprobada",
|
||||
nuevo: "enviada",
|
||||
actor: salesAgent,
|
||||
viaEndpoint: "enviar",
|
||||
aprobadaAt: new Date(),
|
||||
});
|
||||
assert.equal(res.ok, true);
|
||||
});
|
||||
|
||||
it("impide que un agente marque aceptada/rechazada/vencida, pero lo permite a staff humano tras enviada", () => {
|
||||
const agentAttempt = validarTransicionEstado({
|
||||
actual: "enviada",
|
||||
nuevo: "aceptada",
|
||||
actor: salesAgent,
|
||||
viaEndpoint: "estado",
|
||||
});
|
||||
assert.equal(agentAttempt.ok, false);
|
||||
assert.equal(agentAttempt.status, 403);
|
||||
|
||||
const humanAccept = validarTransicionEstado({
|
||||
actual: "enviada",
|
||||
nuevo: "aceptada",
|
||||
actor: adminHumano,
|
||||
viaEndpoint: "estado",
|
||||
});
|
||||
assert.equal(humanAccept.ok, true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user