device_events_test.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // device_events_test.go — contrato del endpoint GET /api/device-events
  2. // (issue #184): eventos offline/online de dispositivos.
  3. package httpapi_test
  4. import (
  5. "net/http"
  6. "testing"
  7. )
  8. // TestDeviceEventsProtegido: sin sesión → 401; con sesión → 200 con lista.
  9. func TestDeviceEventsProtegido(t *testing.T) {
  10. srv := makeTestServer(t)
  11. res := get(t, srv.URL, "/api/device-events", "")
  12. if res.StatusCode != http.StatusUnauthorized {
  13. t.Fatalf("sin cookie: got %d want 401", res.StatusCode)
  14. }
  15. body := readJSON(t, res)
  16. if body["error"] != "unauthorized" {
  17. t.Fatalf("error: %v", body)
  18. }
  19. // Login y siembra de 2 eventos (la tabla la crea el schema al arrancar).
  20. _, cookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  21. if _, err := srv.db.Exec(
  22. "INSERT INTO device_events (ts_ms, mac, router_id, state, signal_dbm) VALUES (?,?,?,?,?), (?,?,?,?,?)",
  23. 1000, "AA:BB:CC:DD:EE:01", "rt1", "offline", nil,
  24. 2000, "AA:BB:CC:DD:EE:01", "rt1", "online", -45,
  25. ); err != nil {
  26. t.Fatalf("seed: %v", err)
  27. }
  28. res2 := get(t, srv.URL, "/api/device-events", cookie)
  29. if res2.StatusCode != http.StatusOK {
  30. t.Fatalf("con cookie: got %d want 200", res2.StatusCode)
  31. }
  32. body2 := readJSON(t, res2)
  33. events, ok := body2["events"].([]any)
  34. if !ok || len(events) != 2 {
  35. t.Fatalf("events: %v", body2["events"])
  36. }
  37. // Orden DESC por ts_ms: primero online (2000).
  38. first := events[0].(map[string]any)
  39. if first["state"] != "online" || first["mac"] != "AA:BB:CC:DD:EE:01" {
  40. t.Fatalf("primero: %v", first)
  41. }
  42. }
  43. // TestDeviceEventsFiltros: mac y state filtran; limit acota; sin auth → 401.
  44. func TestDeviceEventsFiltros(t *testing.T) {
  45. srv := makeTestServer(t)
  46. if _, err := srv.db.Exec(
  47. "INSERT INTO device_events (ts_ms, mac, router_id, state) VALUES (?,?,?,?), (?,?,?,?), (?,?,?,?)",
  48. 1000, "AA:BB:CC:DD:EE:01", "rt1", "offline",
  49. 2000, "AA:BB:CC:DD:EE:01", "rt1", "online",
  50. 3000, "AA:BB:CC:DD:EE:02", "rt2", "offline",
  51. ); err != nil {
  52. t.Fatalf("seed: %v", err)
  53. }
  54. _, cookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  55. // Filtro state=online → 1.
  56. res := get(t, srv.URL, "/api/device-events?state=online", cookie)
  57. if res.StatusCode != http.StatusOK {
  58. t.Fatalf("state filter: got %d want 200", res.StatusCode)
  59. }
  60. if events := readJSON(t, res)["events"].([]any); len(events) != 1 {
  61. t.Fatalf("state=online: %d, want 1", len(events))
  62. }
  63. // Filtro mac → 2.
  64. res = get(t, srv.URL, "/api/device-events?mac=AA:BB:CC:DD:EE:01", cookie)
  65. if events := readJSON(t, res)["events"].([]any); len(events) != 2 {
  66. t.Fatalf("mac filter: %d, want 2", len(events))
  67. }
  68. // Filtro router=rt2 → 1.
  69. res = get(t, srv.URL, "/api/device-events?router=rt2", cookie)
  70. if events := readJSON(t, res)["events"].([]any); len(events) != 1 {
  71. t.Fatalf("router filter: %d, want 1", len(events))
  72. }
  73. // limit=1 → 1.
  74. res = get(t, srv.URL, "/api/device-events?limit=1", cookie)
  75. if events := readJSON(t, res)["events"].([]any); len(events) != 1 {
  76. t.Fatalf("limit=1: %d, want 1", len(events))
  77. }
  78. // since=2000 → solo ts>=2000 (2 eventos).
  79. res = get(t, srv.URL, "/api/device-events?since=2000", cookie)
  80. if events := readJSON(t, res)["events"].([]any); len(events) != 2 {
  81. t.Fatalf("since=2000: %d, want 2", len(events))
  82. }
  83. }
  84. // TestDeviceEventsNormalizesMAC: ?mac acepta guiones/minúsculas y rechaza
  85. // valores que no son una MAC.
  86. func TestDeviceEventsNormalizesMAC(t *testing.T) {
  87. srv := makeTestServer(t)
  88. if _, err := srv.db.Exec(
  89. "INSERT INTO device_events (ts_ms, mac, router_id, state) VALUES (?,?,?,?)",
  90. 1000, "AA:BB:CC:DD:EE:01", "rt1", "offline",
  91. ); err != nil {
  92. t.Fatalf("seed: %v", err)
  93. }
  94. _, cookie, _ := loginCookie(t, srv.URL, "admin", "test123456")
  95. res := get(t, srv.URL, "/api/device-events?mac=aa-bb-cc-dd-ee-01", cookie)
  96. if res.StatusCode != http.StatusOK {
  97. t.Fatalf("mac con guiones: got %d want 200", res.StatusCode)
  98. }
  99. events, _ := readJSON(t, res)["events"].([]any)
  100. if len(events) != 1 {
  101. t.Fatalf("esperaba 1 evento, obtuve %v", events)
  102. }
  103. res = get(t, srv.URL, "/api/device-events?mac=nope", cookie)
  104. if res.StatusCode != http.StatusBadRequest {
  105. t.Fatalf("mac inválida: got %d want 400", res.StatusCode)
  106. }
  107. }