users.test.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /**
  2. * Tests del CRUD de usuarios (/api/users, solo admin):
  3. * - 403 para no-admin y 401 sin sesión.
  4. * - Alta (201), duplicado (409), validación (400).
  5. * - Cambio de password (204, invalida sesiones), borrado (204),
  6. * protección self/último admin (400).
  7. */
  8. import { describe, it, before, after } from 'node:test'
  9. import assert from 'node:assert/strict'
  10. import { makeTestServer, loginCookie } from './helpers.js'
  11. describe('users CRUD', () => {
  12. let srv
  13. let cookie
  14. before(async () => {
  15. srv = await makeTestServer()
  16. const login = await loginCookie(srv.base)
  17. assert.equal(login.status, 204)
  18. cookie = login.cookie
  19. })
  20. after(async () => {
  21. await srv.close()
  22. })
  23. it('requiere auth y rol admin', async () => {
  24. let res = await fetch(`${srv.base}/api/users`)
  25. assert.equal(res.status, 401)
  26. // admin sí
  27. res = await fetch(`${srv.base}/api/users`, { headers: { cookie: `session=${cookie}` } })
  28. assert.equal(res.status, 200)
  29. const { users } = await res.json()
  30. assert.equal(users.length, 1)
  31. assert.equal(users[0].username, 'admin')
  32. assert.equal(users[0].role, 'admin')
  33. })
  34. it('alta, duplicado y validación', async () => {
  35. let res = await fetch(`${srv.base}/api/users`, {
  36. method: 'POST',
  37. headers: { 'Content-Type': 'application/json', cookie: `session=${cookie}` },
  38. body: JSON.stringify({ username: 'ana', password: 'secreto1' }),
  39. })
  40. assert.equal(res.status, 201)
  41. res = await fetch(`${srv.base}/api/users`, {
  42. method: 'POST',
  43. headers: { 'Content-Type': 'application/json', cookie: `session=${cookie}` },
  44. body: JSON.stringify({ username: 'ana', password: 'otra1234' }),
  45. })
  46. assert.equal(res.status, 409)
  47. res = await fetch(`${srv.base}/api/users`, {
  48. method: 'POST',
  49. headers: { 'Content-Type': 'application/json', cookie: `session=${cookie}` },
  50. body: JSON.stringify({ username: 'mal usuario', password: 'x'.repeat(10) }),
  51. })
  52. assert.equal(res.status, 400)
  53. })
  54. it('el usuario creado puede hacer login; cambio de password invalida su sesión', async () => {
  55. let login = await loginCookie(srv.base, 'secreto1', 'ana')
  56. assert.equal(login.status, 204)
  57. const list = await (
  58. await fetch(`${srv.base}/api/users`, { headers: { cookie: `session=${cookie}` } })
  59. ).json()
  60. const ana = list.users.find((u) => u.username === 'ana')
  61. const res = await fetch(`${srv.base}/api/users/${ana.id}/password`, {
  62. method: 'PUT',
  63. headers: { 'Content-Type': 'application/json', cookie: `session=${cookie}` },
  64. body: JSON.stringify({ password: 'nueva-clave-9' }),
  65. })
  66. assert.equal(res.status, 204)
  67. // La sesión vieja de ana quedó invalidada
  68. const me = await fetch(`${srv.base}/api/auth/me`, { headers: { cookie: `session=${login.cookie}` } })
  69. assert.equal(me.status, 401)
  70. // Y entra con la nueva
  71. login = await loginCookie(srv.base, 'nueva-clave-9', 'ana')
  72. assert.equal(login.status, 204)
  73. // Un no-admin no puede listar usuarios
  74. const forbidden = await fetch(`${srv.base}/api/users`, { headers: { cookie: `session=${login.cookie}` } })
  75. assert.equal(forbidden.status, 403)
  76. })
  77. it('no borra self ni el último admin; borra usuarios normales', async () => {
  78. const list = await (
  79. await fetch(`${srv.base}/api/users`, { headers: { cookie: `session=${cookie}` } })
  80. ).json()
  81. const admin = list.users.find((u) => u.username === 'admin')
  82. const ana = list.users.find((u) => u.username === 'ana')
  83. let res = await fetch(`${srv.base}/api/users/${admin.id}`, {
  84. method: 'DELETE',
  85. headers: { cookie: `session=${cookie}` },
  86. })
  87. assert.equal(res.status, 400) // self
  88. res = await fetch(`${srv.base}/api/users/${ana.id}`, {
  89. method: 'DELETE',
  90. headers: { cookie: `session=${cookie}` },
  91. })
  92. assert.equal(res.status, 204)
  93. res = await fetch(`${srv.base}/api/users/${ana.id}`, {
  94. method: 'DELETE',
  95. headers: { cookie: `session=${cookie}` },
  96. })
  97. assert.equal(res.status, 404)
  98. })
  99. })