dns_api_test.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // dns_api_test.go — contrato de los endpoints de DNS por dispositivo y red (#2078).
  2. package httpapi_test
  3. import (
  4. "encoding/json"
  5. "io"
  6. "net/http"
  7. "testing"
  8. "time"
  9. )
  10. type dnsDomain struct {
  11. Domain string `json:"domain"`
  12. Queries int64 `json:"queries"`
  13. Blocked int64 `json:"blocked"`
  14. }
  15. type dnsBody struct {
  16. Status string `json:"status"`
  17. Window string `json:"window"`
  18. GapTs int64 `json:"gap_ts"`
  19. TotalQueries int64 `json:"total_queries"`
  20. TotalBlocked int64 `json:"total_blocked"`
  21. Top []dnsDomain `json:"top"`
  22. NewDomains []struct {
  23. Domain string `json:"domain"`
  24. MAC string `json:"mac"`
  25. } `json:"new_domains"`
  26. }
  27. func getDNS(t *testing.T, ts *testServer, path string) (int, dnsBody) {
  28. t.Helper()
  29. _, cookie, _ := loginCookie(t, ts.URL, "admin", "test123456")
  30. req, _ := http.NewRequest("GET", ts.URL+path, nil)
  31. req.Header.Set("Cookie", "session="+cookie)
  32. res, err := http.DefaultClient.Do(req)
  33. if err != nil {
  34. t.Fatalf("GET %s: %v", path, err)
  35. }
  36. defer res.Body.Close()
  37. body, _ := io.ReadAll(res.Body)
  38. var out dnsBody
  39. if res.StatusCode == http.StatusOK {
  40. if err := json.Unmarshal(body, &out); err != nil {
  41. t.Fatalf("decode %s: %v (%s)", path, err, body)
  42. }
  43. }
  44. return res.StatusCode, out
  45. }
  46. func seedDNS(t *testing.T, ts *testServer) time.Time {
  47. t.Helper()
  48. now := time.Now()
  49. hour := now.Truncate(time.Hour)
  50. // La ingesta guarda la MAC tal como está en device_seen (mayúsculas);
  51. // la ruta del endpoint llega en minúsculas.
  52. _, err := ts.db.Exec(`INSERT INTO dns_activity (mac, domain, hour_ts, queries, blocked, first_seen, last_seen)
  53. VALUES (?, ?, ?, ?, ?, ?, ?)`, "AA:BB:CC:DD:EE:01", "video.example", hour.UnixMilli(), 12, 3,
  54. now.Add(-2*time.Minute).UnixMilli(), now.UnixMilli())
  55. if err != nil {
  56. t.Fatalf("seed dns_activity: %v", err)
  57. }
  58. _, err = ts.db.Exec(`INSERT INTO dns_domain_first (domain, first_seen, mac) VALUES (?, ?, ?)`,
  59. "video.example", now.Add(-time.Minute).UnixMilli(), "AA:BB:CC:DD:EE:01")
  60. if err != nil {
  61. t.Fatalf("seed dns_domain_first: %v", err)
  62. }
  63. if _, err := ts.db.Exec(`INSERT INTO kv (key, value) VALUES ('adguard_querylog_state', 'ok')
  64. ON CONFLICT(key) DO UPDATE SET value = 'ok'`); err != nil {
  65. t.Fatalf("seed state: %v", err)
  66. }
  67. return now
  68. }
  69. func TestDeviceDNSContract(t *testing.T) {
  70. ts := makeTestServer(t)
  71. seedDNS(t, ts)
  72. code, body := getDNS(t, ts, "/api/devices/aa:bb:cc:dd:ee:01/dns?window=1h")
  73. if code != http.StatusOK {
  74. t.Fatalf("status = %d", code)
  75. }
  76. if body.Status != "ok" || body.Window != "1h" {
  77. t.Errorf("status/window = %q/%q", body.Status, body.Window)
  78. }
  79. if body.TotalQueries != 12 || body.TotalBlocked != 3 {
  80. t.Errorf("totales = %d/%d", body.TotalQueries, body.TotalBlocked)
  81. }
  82. if len(body.Top) != 1 || body.Top[0].Domain != "video.example" || body.Top[0].Queries != 12 {
  83. t.Errorf("top = %+v", body.Top)
  84. }
  85. }
  86. func TestDeviceDNSEmptyIsNotAnError(t *testing.T) {
  87. ts := makeTestServer(t)
  88. code, body := getDNS(t, ts, "/api/devices/aa:bb:cc:dd:ee:09/dns?window=24h")
  89. if code != http.StatusOK {
  90. t.Fatalf("status = %d", code)
  91. }
  92. if body.Top == nil || len(body.Top) != 0 {
  93. t.Errorf("top = %+v, want lista vacía (no null)", body.Top)
  94. }
  95. if body.Status != "unavailable" {
  96. t.Errorf("status = %q, want unavailable sin ingesta", body.Status)
  97. }
  98. }
  99. func TestDeviceDNSRejectsBadMAC(t *testing.T) {
  100. ts := makeTestServer(t)
  101. if code, _ := getDNS(t, ts, "/api/devices/no-es-una-mac/dns"); code != http.StatusBadRequest {
  102. t.Errorf("status = %d, want 400", code)
  103. }
  104. }
  105. func TestDNSOverviewContract(t *testing.T) {
  106. ts := makeTestServer(t)
  107. seedDNS(t, ts)
  108. code, body := getDNS(t, ts, "/api/dns/overview?window=24h")
  109. if code != http.StatusOK {
  110. t.Fatalf("status = %d", code)
  111. }
  112. if len(body.Top) != 1 || body.Top[0].Domain != "video.example" {
  113. t.Errorf("top = %+v", body.Top)
  114. }
  115. if len(body.NewDomains) != 1 || body.NewDomains[0].Domain != "video.example" {
  116. t.Errorf("new_domains = %+v", body.NewDomains)
  117. }
  118. if body.NewDomains[0].MAC != "AA:BB:CC:DD:EE:01" {
  119. t.Errorf("mac del dominio nuevo = %q", body.NewDomains[0].MAC)
  120. }
  121. }