auth.test.js 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * Tests de auth (contrato + skill):
  3. * - login incorrecto → 401 invalid_credentials
  4. * - login correcto → 204 + Set-Cookie httpOnly; /api/auth/me con cookie → 200
  5. * - overview sin cookie → 401
  6. * - rate-limit SQLite: 5º fallo bloquea 5 min → siguiente intento 429 rate_limited
  7. * - logout → 204 y la sesión queda revocada
  8. */
  9. import { describe, it, before, after } from 'node:test'
  10. import assert from 'node:assert/strict'
  11. import { makeTestServer, loginCookie } from './helpers.js'
  12. describe('auth', () => {
  13. let srv
  14. before(async () => {
  15. srv = await makeTestServer()
  16. })
  17. after(async () => {
  18. await srv.close()
  19. })
  20. it('rechaza login incorrecto con 401 invalid_credentials', async () => {
  21. const { status, cookie } = await loginCookie(srv.base, 'wrong-pass')
  22. assert.equal(status, 401)
  23. assert.equal(cookie, null)
  24. })
  25. it('acepta login correcto: 204 + Set-Cookie httpOnly, y me devuelve user/mode', async () => {
  26. const { status, cookie, setCookie } = await loginCookie(srv.base)
  27. assert.equal(status, 204)
  28. assert.ok(cookie, 'debe devolver cookie de sesión')
  29. assert.match(setCookie, /HttpOnly/)
  30. assert.match(setCookie, /SameSite=Lax/)
  31. const me = await fetch(`${srv.base}/api/auth/me`, { headers: { cookie: `session=${cookie}` } })
  32. assert.equal(me.status, 200)
  33. const body = await me.json()
  34. assert.deepEqual(body, { user: 'admin', role: 'admin', language: 'auto', mode: 'demo' })
  35. })
  36. it('protege /api/overview sin cookie (401) y la sirve con cookie (200)', async () => {
  37. const unauth = await fetch(`${srv.base}/api/overview`)
  38. assert.equal(unauth.status, 401)
  39. const { cookie } = await loginCookie(srv.base)
  40. const auth = await fetch(`${srv.base}/api/overview`, { headers: { cookie: `session=${cookie}` } })
  41. assert.equal(auth.status, 200)
  42. })
  43. it('logout: 204 y la sesión queda revocada', async () => {
  44. const { cookie } = await loginCookie(srv.base)
  45. const out = await fetch(`${srv.base}/api/auth/logout`, {
  46. method: 'POST',
  47. headers: { cookie: `session=${cookie}` },
  48. })
  49. assert.equal(out.status, 204)
  50. const after_ = await fetch(`${srv.base}/api/overview`, { headers: { cookie: `session=${cookie}` } })
  51. assert.equal(after_.status, 401)
  52. })
  53. it('rate-limit: bloquea tras el 5º fallo (429 rate_limited con retryAfterSec)', async () => {
  54. const ip = '10.9.9.9'
  55. const headers = { 'Content-Type': 'application/json', 'x-forwarded-for': ip }
  56. const bad = () =>
  57. fetch(`${srv.base}/api/auth/login`, {
  58. method: 'POST',
  59. headers,
  60. body: JSON.stringify({ username: 'admin', password: 'nope' }),
  61. })
  62. for (let i = 1; i <= 5; i++) {
  63. const res = await bad()
  64. assert.equal(res.status, 401, `fallo ${i} debe ser 401`)
  65. }
  66. // 6º intento: ya bloqueado (5 min)
  67. const blocked = await bad()
  68. assert.equal(blocked.status, 429)
  69. const body = await blocked.json()
  70. assert.equal(body.error, 'rate_limited')
  71. assert.ok(body.retryAfterSec > 0 && body.retryAfterSec <= 300)
  72. // Incluso con la password correcta sigue bloqueado
  73. const goodWhileLocked = await fetch(`${srv.base}/api/auth/login`, {
  74. method: 'POST',
  75. headers,
  76. body: JSON.stringify({ username: 'admin', password: 'test1234' }),
  77. })
  78. assert.equal(goodWhileLocked.status, 429)
  79. })
  80. it('el rate-limit persiste en SQLite (no solo memoria)', async () => {
  81. const row = srv.dbHandle.db.prepare('SELECT * FROM login_attempts WHERE ip = ?').get('10.9.9.9')
  82. assert.ok(row, 'debe existir fila en login_attempts')
  83. assert.ok(row.attempts >= 5)
  84. assert.ok(row.locked_until > Date.now())
  85. })
  86. })