feat: PWA instalable, cambio de contraseña y dashboards por rol
- POST /api/auth/cambiar-password (bcrypt 12, política ≥8+1 número) + modal en las 6 apps con auth - PWA: manifest + íconos de marca en las 7 apps (sin service worker a propósito, documentado); instalable en celular - Dashboards: 'Mi tablero' para vendedor (sus KPIs y funnel) y sección Equipo para coordinador/corporativo/admin - mi-dia: equipo.porVendedor gana leadsNuevosSemana - Tests: api 271, cotizador 88, alta 88, cobranza 81, agente-ia 19, dashboards 15, mi-dia 11, landing 4
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import {
|
||||
ErrorLogin,
|
||||
cambiarPassword,
|
||||
conAuth,
|
||||
getSesion,
|
||||
getToken,
|
||||
@@ -122,3 +123,37 @@ describe('manejar401', () => {
|
||||
expect(getSesion()).not.toBeNull()
|
||||
})
|
||||
})
|
||||
|
||||
describe('cambiarPassword', () => {
|
||||
it('204: resuelve sin tocar la sesión y manda Bearer + ambas contraseñas', async () => {
|
||||
localStorage.setItem('frasa_auth', JSON.stringify(sesionOk))
|
||||
const fetchMock = vi.fn().mockResolvedValue(new Response(null, { status: 204 }))
|
||||
vi.stubGlobal('fetch', fetchMock)
|
||||
|
||||
await cambiarPassword('Frasa2026', 'NuevaPass2026')
|
||||
|
||||
const [url, init] = fetchMock.mock.calls[0] as [string, RequestInit]
|
||||
expect(url).toContain('/auth/cambiar-password')
|
||||
expect(init.method).toBe('POST')
|
||||
expect(init.headers).toMatchObject({ Authorization: 'Bearer jwt-de-prueba' })
|
||||
expect(JSON.parse(init.body as string)).toEqual({ actual: 'Frasa2026', nueva: 'NuevaPass2026' })
|
||||
expect(getSesion()).not.toBeNull()
|
||||
})
|
||||
|
||||
it('400 del API: lanza Error con el mensaje del API (p. ej. actual incorrecta)', async () => {
|
||||
vi.stubGlobal(
|
||||
'fetch',
|
||||
vi.fn().mockResolvedValue(
|
||||
new Response('{"error":"La contraseña actual no es correcta"}', { status: 400 }),
|
||||
),
|
||||
)
|
||||
await expect(cambiarPassword('mala', 'NuevaPass2026')).rejects.toThrow(
|
||||
'La contraseña actual no es correcta',
|
||||
)
|
||||
})
|
||||
|
||||
it('sin red: Error de conexión', async () => {
|
||||
vi.stubGlobal('fetch', vi.fn().mockRejectedValue(new TypeError('fetch failed')))
|
||||
await expect(cambiarPassword('a', 'b12345678')).rejects.toThrow('No se pudo conectar')
|
||||
})
|
||||
})
|
||||
|
||||
@@ -75,3 +75,28 @@ export function manejar401(res: Response): void {
|
||||
logout()
|
||||
if (typeof window !== 'undefined') window.location.reload()
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /auth/cambiar-password. Lanza Error con el mensaje del API cuando
|
||||
* falla (p. ej. "La contraseña actual no es correcta"); no toca la sesión.
|
||||
*/
|
||||
export async function cambiarPassword(actual: string, nueva: string): Promise<void> {
|
||||
let res: Response
|
||||
try {
|
||||
res = await fetch(
|
||||
`${API_URL}/auth/cambiar-password`,
|
||||
conAuth({
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ actual, nueva }),
|
||||
}),
|
||||
)
|
||||
} catch {
|
||||
throw new Error('No se pudo conectar con el servidor')
|
||||
}
|
||||
manejar401(res)
|
||||
if (!res.ok) {
|
||||
const body = (await res.json().catch(() => null)) as { error?: string } | null
|
||||
throw new Error(body?.error ?? `Error ${res.status}`)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user