device_overrides_test.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. // device_overrides_test.go — issue #797: nombre visible y tipo de dispositivo
  2. // como overrides persistidos (además del icono existente, #437).
  3. package httpapi
  4. import (
  5. "encoding/json"
  6. "net/http/httptest"
  7. "strings"
  8. "testing"
  9. "github.com/gnacho/netpulse/server-go/internal/db"
  10. )
  11. func newOverrideTestServer(t *testing.T) *server {
  12. t.Helper()
  13. dataDir := t.TempDir()
  14. d, err := db.Open(dataDir)
  15. if err != nil {
  16. t.Fatalf("db: %v", err)
  17. }
  18. t.Cleanup(func() { _ = d.Close() })
  19. return &server{db: d}
  20. }
  21. func doOverridePut(t *testing.T, s *server, mac, body string) *httptest.ResponseRecorder {
  22. t.Helper()
  23. req := httptest.NewRequest("PUT", "/api/devices/"+mac+"/override", strings.NewReader(body))
  24. req.SetPathValue("mac", mac)
  25. rec := httptest.NewRecorder()
  26. s.handleDeviceOverridePut(rec, req)
  27. return rec
  28. }
  29. func doOverrideGet(t *testing.T, s *server, mac string) map[string]any {
  30. t.Helper()
  31. req := httptest.NewRequest("GET", "/api/devices/"+mac+"/override", nil)
  32. req.SetPathValue("mac", mac)
  33. rec := httptest.NewRecorder()
  34. s.handleDeviceOverrideGet(rec, req)
  35. var out map[string]any
  36. if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
  37. t.Fatalf("get decode: %v", err)
  38. }
  39. return out
  40. }
  41. func TestDeviceOverrideNameTypeRoundtrip(t *testing.T) {
  42. s := newOverrideTestServer(t)
  43. mac := "aa:bb:cc:dd:ee:ff"
  44. rec := doOverridePut(t, s, mac, `{"icon":"tv","name":"TV Salón","type":"tv"}`)
  45. if rec.Code != 200 {
  46. t.Fatalf("put: %d %s", rec.Code, rec.Body.String())
  47. }
  48. got := doOverrideGet(t, s, mac)
  49. if got["icon"] != "tv" || got["name"] != "TV Salón" || got["type"] != "tv" {
  50. t.Errorf("roundtrip incompleto: %+v", got)
  51. }
  52. }
  53. func TestDeviceOverridePartialPutPreservesOthers(t *testing.T) {
  54. s := newOverrideTestServer(t)
  55. mac := "aa:bb:cc:dd:ee:ff"
  56. doOverridePut(t, s, mac, `{"icon":"tv","name":"TV Salón","type":"tv"}`)
  57. // Cliente antiguo que solo envía icon (#772): name/type no se tocan.
  58. rec := doOverridePut(t, s, mac, `{"icon":"speaker"}`)
  59. if rec.Code != 200 {
  60. t.Fatalf("put parcial: %d", rec.Code)
  61. }
  62. got := doOverrideGet(t, s, mac)
  63. if got["icon"] != "speaker" || got["name"] != "TV Salón" || got["type"] != "tv" {
  64. t.Errorf("el put parcial pisó otros campos: %+v", got)
  65. }
  66. // Limpiar solo el nombre.
  67. doOverridePut(t, s, mac, `{"name":""}`)
  68. got = doOverrideGet(t, s, mac)
  69. if got["name"] != "" || got["icon"] != "speaker" || got["type"] != "tv" {
  70. t.Errorf("limpiar name no funcionó: %+v", got)
  71. }
  72. }
  73. func TestDeviceOverrideClearAllDeletesRow(t *testing.T) {
  74. s := newOverrideTestServer(t)
  75. mac := "aa:bb:cc:dd:ee:ff"
  76. doOverridePut(t, s, mac, `{"icon":"tv","name":"TV","type":"tv"}`)
  77. rec := doOverridePut(t, s, mac, `{"icon":"","name":"","type":""}`)
  78. if rec.Code != 200 {
  79. t.Fatalf("clear: %d", rec.Code)
  80. }
  81. var n int
  82. if err := s.db.QueryRow("SELECT COUNT(*) FROM device_overrides WHERE mac = ?", mac).Scan(&n); err != nil {
  83. t.Fatalf("count: %v", err)
  84. }
  85. if n != 0 {
  86. t.Errorf("la fila debió borrarse con los tres campos vacíos")
  87. }
  88. }
  89. func TestDeviceOverrideValidation(t *testing.T) {
  90. s := newOverrideTestServer(t)
  91. mac := "aa:bb:cc:dd:ee:ff"
  92. if rec := doOverridePut(t, s, mac, `{"type":"router-magico"}`); rec.Code != 400 {
  93. t.Errorf("tipo inválido debió dar 400, dio %d", rec.Code)
  94. }
  95. if rec := doOverridePut(t, s, mac, `{"icon":"icono-falso"}`); rec.Code != 400 {
  96. t.Errorf("icon inválido debió dar 400, dio %d", rec.Code)
  97. }
  98. if rec := doOverridePut(t, s, "not-a-mac", `{"name":"x"}`); rec.Code != 400 {
  99. t.Errorf("mac inválida debió dar 400, dio %d", rec.Code)
  100. }
  101. // Nombre con salto de línea: rechazado.
  102. if rec := doOverridePut(t, s, mac, `{"name":"a\nb"}`); rec.Code != 400 {
  103. t.Errorf("name con newline debió dar 400, dio %d", rec.Code)
  104. }
  105. // Tipo válido del clasificador: aceptado.
  106. if rec := doOverridePut(t, s, mac, `{"type":"movil"}`); rec.Code != 200 {
  107. t.Errorf("tipo válil debió dar 200, dio %d", rec.Code)
  108. }
  109. }