devices_wan_2183_test.go 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. package httpapi_test
  2. import (
  3. "context"
  4. "net/http"
  5. "testing"
  6. "github.com/gnacho/netpulse/server-go/internal/adapters"
  7. )
  8. // wanSideAdapter añade un vecino del segmento WAN al fixture demo.
  9. type wanSideAdapter struct{ adapters.Snapshotter }
  10. func (w wanSideAdapter) GetDevices(ctx context.Context) []adapters.Device {
  11. return append(w.Snapshotter.GetDevices(ctx), adapters.Device{
  12. ID: "0c-ef-15-00-00-02",
  13. MAC: "0C:EF:15:00:00:02",
  14. Name: "0C:EF:15:00:00:02",
  15. IP: "203.0.113.248",
  16. Online: true,
  17. Band: "cable",
  18. Type: "unknown",
  19. RouterID: "gw",
  20. WanSide: true,
  21. })
  22. }
  23. // #2183: el inventario no lista vecinos del segmento WAN (ISP) y se pueden
  24. // pedir explícitamente con includeWan=1. La topología sigue viéndolos porque
  25. // usa el snapshot del overview, no este endpoint.
  26. func TestDevicesExcludesWanSideNeighbours(t *testing.T) {
  27. ts := makeTestServerWithAdapter(t, false, func(s adapters.Snapshotter) adapters.Snapshotter {
  28. return wanSideAdapter{Snapshotter: s}
  29. })
  30. status, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  31. if status != http.StatusNoContent {
  32. t.Fatalf("login: %d", status)
  33. }
  34. res := get(t, ts.URL, "/api/devices?pageSize=200", cookie)
  35. body := readJSON(t, res)
  36. items := body["items"].([]any)
  37. for _, it := range items {
  38. if it.(map[string]any)["mac"] == "0C:EF:15:00:00:02" {
  39. t.Fatalf("el vecino WAN no puede aparecer en el inventario: %v", it)
  40. }
  41. }
  42. without := int(body["total"].(float64))
  43. res = get(t, ts.URL, "/api/devices?pageSize=200&includeWan=1", cookie)
  44. body = readJSON(t, res)
  45. with := int(body["total"].(float64))
  46. found := false
  47. for _, it := range body["items"].([]any) {
  48. if it.(map[string]any)["mac"] == "0C:EF:15:00:00:02" {
  49. found = true
  50. }
  51. }
  52. if !found {
  53. t.Fatalf("includeWan=1 debe devolver el vecino WAN: %v", body["items"])
  54. }
  55. if with != without+1 {
  56. t.Fatalf("includeWan=1 suma solo el vecino WAN: sin=%d con=%d", without, with)
  57. }
  58. // typeCounts sigue el mismo criterio de visibilidad.
  59. sum := 0
  60. for _, v := range body["typeCounts"].(map[string]any) {
  61. sum += int(v.(float64))
  62. }
  63. if sum != with {
  64. t.Fatalf("typeCounts con includeWan suma %d, total %d", sum, with)
  65. }
  66. }