dns_api_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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. _, err := ts.db.Exec(`INSERT INTO dns_activity (mac, domain, hour_ts, queries, blocked, first_seen, last_seen)
  51. VALUES (?, ?, ?, ?, ?, ?, ?)`, "aa:bb:cc:dd:ee:01", "video.example", hour.UnixMilli(), 12, 3,
  52. now.Add(-2*time.Minute).UnixMilli(), now.UnixMilli())
  53. if err != nil {
  54. t.Fatalf("seed dns_activity: %v", err)
  55. }
  56. _, err = ts.db.Exec(`INSERT INTO dns_domain_first (domain, first_seen, mac) VALUES (?, ?, ?)`,
  57. "video.example", now.Add(-time.Minute).UnixMilli(), "aa:bb:cc:dd:ee:01")
  58. if err != nil {
  59. t.Fatalf("seed dns_domain_first: %v", err)
  60. }
  61. if _, err := ts.db.Exec(`INSERT INTO kv (key, value) VALUES ('adguard_querylog_state', 'ok')
  62. ON CONFLICT(key) DO UPDATE SET value = 'ok'`); err != nil {
  63. t.Fatalf("seed state: %v", err)
  64. }
  65. return now
  66. }
  67. func TestDeviceDNSContract(t *testing.T) {
  68. ts := makeTestServer(t)
  69. seedDNS(t, ts)
  70. code, body := getDNS(t, ts, "/api/devices/aa:bb:cc:dd:ee:01/dns?window=1h")
  71. if code != http.StatusOK {
  72. t.Fatalf("status = %d", code)
  73. }
  74. if body.Status != "ok" || body.Window != "1h" {
  75. t.Errorf("status/window = %q/%q", body.Status, body.Window)
  76. }
  77. if body.TotalQueries != 12 || body.TotalBlocked != 3 {
  78. t.Errorf("totales = %d/%d", body.TotalQueries, body.TotalBlocked)
  79. }
  80. if len(body.Top) != 1 || body.Top[0].Domain != "video.example" || body.Top[0].Queries != 12 {
  81. t.Errorf("top = %+v", body.Top)
  82. }
  83. }
  84. func TestDeviceDNSEmptyIsNotAnError(t *testing.T) {
  85. ts := makeTestServer(t)
  86. code, body := getDNS(t, ts, "/api/devices/aa:bb:cc:dd:ee:09/dns?window=24h")
  87. if code != http.StatusOK {
  88. t.Fatalf("status = %d", code)
  89. }
  90. if body.Top == nil || len(body.Top) != 0 {
  91. t.Errorf("top = %+v, want lista vacía (no null)", body.Top)
  92. }
  93. if body.Status != "unavailable" {
  94. t.Errorf("status = %q, want unavailable sin ingesta", body.Status)
  95. }
  96. }
  97. func TestDeviceDNSRejectsBadMAC(t *testing.T) {
  98. ts := makeTestServer(t)
  99. if code, _ := getDNS(t, ts, "/api/devices/no-es-una-mac/dns"); code != http.StatusBadRequest {
  100. t.Errorf("status = %d, want 400", code)
  101. }
  102. }
  103. func TestDNSOverviewContract(t *testing.T) {
  104. ts := makeTestServer(t)
  105. seedDNS(t, ts)
  106. code, body := getDNS(t, ts, "/api/dns/overview?window=24h")
  107. if code != http.StatusOK {
  108. t.Fatalf("status = %d", code)
  109. }
  110. if len(body.Top) != 1 || body.Top[0].Domain != "video.example" {
  111. t.Errorf("top = %+v", body.Top)
  112. }
  113. if len(body.NewDomains) != 1 || body.NewDomains[0].Domain != "video.example" {
  114. t.Errorf("new_domains = %+v", body.NewDomains)
  115. }
  116. if body.NewDomains[0].MAC != "aa:bb:cc:dd:ee:01" {
  117. t.Errorf("mac del dominio nuevo = %q", body.NewDomains[0].MAC)
  118. }
  119. }