|
|
@@ -257,3 +257,97 @@ func TestDeviceLinkPutDelete(t *testing.T) {
|
|
|
t.Fatalf("unlink de nuevo = %d, want 404", rec.Code)
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+// TestDeviceLinkPutRejectsInvalidMAC verifica que PUT /api/devices/{mac}/link
|
|
|
+// rechaza MACs inválidas en el path o en target con 400 invalid_mac.
|
|
|
+func TestDeviceLinkPutRejectsInvalidMAC(t *testing.T) {
|
|
|
+ s := newOverrideTestServer(t)
|
|
|
+
|
|
|
+ t.Run("path con MAC inválida", func(t *testing.T) {
|
|
|
+ req := httptest.NewRequest(http.MethodPut, "/api/devices/no-es-mac/link", strings.NewReader(`{"target":"02:00:00:00:00:01"}`))
|
|
|
+ req.SetPathValue("mac", "no-es-mac")
|
|
|
+ rec := httptest.NewRecorder()
|
|
|
+ s.handleDeviceLinkPut(rec, req)
|
|
|
+ if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "invalid_mac") {
|
|
|
+ t.Fatalf("esperaba 400 invalid_mac, obtuve %d: %s", rec.Code, rec.Body.String())
|
|
|
+ }
|
|
|
+ })
|
|
|
+
|
|
|
+ t.Run("target con MAC inválida", func(t *testing.T) {
|
|
|
+ req := httptest.NewRequest(http.MethodPut, "/api/devices/02:00:00:00:00:01/link", strings.NewReader(`{"target":"basura"}`))
|
|
|
+ req.SetPathValue("mac", "02:00:00:00:00:01")
|
|
|
+ rec := httptest.NewRecorder()
|
|
|
+ s.handleDeviceLinkPut(rec, req)
|
|
|
+ if rec.Code != http.StatusBadRequest || !strings.Contains(rec.Body.String(), "invalid_mac") {
|
|
|
+ t.Fatalf("esperaba 400 invalid_mac, obtuve %d: %s", rec.Code, rec.Body.String())
|
|
|
+ }
|
|
|
+ })
|
|
|
+}
|
|
|
+
|
|
|
+// TestDeviceLinkDeleteLegacyNonMAC garantiza que DELETE /api/devices/{mac}/link
|
|
|
+// sigue siendo permisivo para poder retirar filas heredadas no-MAC.
|
|
|
+func TestDeviceLinkDeleteLegacyNonMAC(t *testing.T) {
|
|
|
+ s := newOverrideTestServer(t)
|
|
|
+ if _, err := s.db.Exec("INSERT INTO device_links (mac, canonical) VALUES (?, ?)", "LEGACY:ALIAS", "02:00:00:00:00:01"); err != nil {
|
|
|
+ t.Fatalf("sembrar device_links: %v", err)
|
|
|
+ }
|
|
|
+ del := func(raw string) int {
|
|
|
+ req := httptest.NewRequest(http.MethodDelete, "/api/devices/x/link", nil)
|
|
|
+ req.SetPathValue("mac", raw)
|
|
|
+ rec := httptest.NewRecorder()
|
|
|
+ s.handleDeviceLinkDelete(rec, req)
|
|
|
+ return rec.Code
|
|
|
+ }
|
|
|
+ if code := del("no-existe"); code != http.StatusNotFound {
|
|
|
+ t.Fatalf("clave desconocida: esperaba 404, obtuve %d", code)
|
|
|
+ }
|
|
|
+ if code := del("legacy-alias"); code != http.StatusNoContent {
|
|
|
+ t.Fatalf("fila heredada: esperaba 204, obtuve %d", code)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// TestDeviceDeleteLegacyNonMAC garantiza que DELETE /api/devices/{mac} sigue
|
|
|
+// borrando filas heredadas no-MAC de device_seen con 204.
|
|
|
+func TestDeviceDeleteLegacyNonMAC(t *testing.T) {
|
|
|
+ s := panelTestServer(t)
|
|
|
+ if _, err := s.db.Exec("INSERT INTO device_seen (mac, first_seen, last_seen, name) VALUES ('legacy-non-mac', 1, 2, 'legacy')"); err != nil {
|
|
|
+ t.Fatalf("sembrar device_seen: %v", err)
|
|
|
+ }
|
|
|
+ r := httptest.NewRequest(http.MethodDelete, "/api/devices/legacy-non-mac", nil)
|
|
|
+ r.SetPathValue("mac", "legacy-non-mac")
|
|
|
+ rec := httptest.NewRecorder()
|
|
|
+ s.handleDeviceDelete(rec, r)
|
|
|
+ if rec.Code != http.StatusNoContent {
|
|
|
+ t.Fatalf("esperaba 204, obtuve %d (%s)", rec.Code, rec.Body.String())
|
|
|
+ }
|
|
|
+ var n int
|
|
|
+ if err := s.db.QueryRow("SELECT COUNT(*) FROM device_seen WHERE mac = 'legacy-non-mac'").Scan(&n); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ if n != 0 {
|
|
|
+ t.Fatalf("filas restantes = %d, esperaba 0", n)
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+// TestDeviceDeleteLegacyNonMACNoGarbageTombstone verifica que borrar una fila
|
|
|
+// heredada no-MAC de device_seen no inserta una lápida basura en deleted_clients.
|
|
|
+func TestDeviceDeleteLegacyNonMACNoGarbageTombstone(t *testing.T) {
|
|
|
+ s := panelTestServer(t)
|
|
|
+ if _, err := s.db.Exec("INSERT INTO device_seen (mac, first_seen, last_seen, name) VALUES ('legacy-non-mac', 1, 2, 'legacy')"); err != nil {
|
|
|
+ t.Fatalf("sembrar device_seen: %v", err)
|
|
|
+ }
|
|
|
+ r := httptest.NewRequest(http.MethodDelete, "/api/devices/legacy-non-mac", nil)
|
|
|
+ r.SetPathValue("mac", "legacy-non-mac")
|
|
|
+ rec := httptest.NewRecorder()
|
|
|
+ s.handleDeviceDelete(rec, r)
|
|
|
+ if rec.Code != http.StatusNoContent {
|
|
|
+ t.Fatalf("esperaba 204, obtuve %d (%s)", rec.Code, rec.Body.String())
|
|
|
+ }
|
|
|
+ var tombs int
|
|
|
+ if err := s.db.QueryRow("SELECT COUNT(*) FROM deleted_clients").Scan(&tombs); err != nil {
|
|
|
+ t.Fatal(err)
|
|
|
+ }
|
|
|
+ if tombs != 0 {
|
|
|
+ t.Fatalf("no debía guardarse lápida para clave no-MAC, obtuve %d filas en deleted_clients", tombs)
|
|
|
+ }
|
|
|
+}
|