Files
EasyDNS-Web/internal/server/release_012_test.go
T
Mateusz Gruszczyński a28362b82c
Go quality gates / test (push) Waiting to run
v0.1.2
2026-12-01 00:16:15 +01:00

95 lines
3.4 KiB
Go

package server
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
"git.linuxiarz.pl/gru/EasyDNS-Web/internal/easydns"
)
func TestVersion012WebDashboardControls(t *testing.T) {
s := testPanel(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
switch r.URL.Path {
case "/api/v1/stats":
fmt.Fprint(w, `{"queries_total":100,"cache_hit_rate":0.87,"responses":{"noerror":90}}`)
case "/api/v1/status":
fmt.Fprint(w, `{"mode":"hybrid","version":"0.1.4"}`)
case "/api/v1/health":
fmt.Fprint(w, `{"status":"ok"}`)
case "/api/v1/upstreams":
fmt.Fprint(w, `{"items":[]}`)
case "/api/v1/audit":
fmt.Fprint(w, `{"items":[]}`)
default:
fmt.Fprint(w, `{}`)
}
}))
for _, path := range []string{"/", "/metrics"} {
r := httptest.NewRequest("GET", "http://panel.test"+path, nil)
r.AddCookie(&http.Cookie{Name: sessionCookie, Value: s.auth.makeSession()})
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, r)
if w.Code != 200 {
t.Fatalf("%s: %d %s", path, w.Code, w.Body.String())
}
html := w.Body.String()
for _, required := range []string{"data-global-range", "data-global-step", "data-chart-range", "data-chart-step", "Last 24 hours", "Downsample: Auto", "data-dns-chart=", "Cache traffic"} {
if !strings.Contains(html, required) {
t.Errorf("%s missing %s", path, required)
}
}
if path == "/metrics" {
if strings.Count(html, "data-dns-chart=") < 15 {
t.Error("metrics should contain at least 15 real API-derived chart visualizations")
}
if !strings.Contains(html, "data-telemetry-export") {
t.Error("CSV export unavailable")
}
}
}
}
func TestTimeWindowAppliedByAuthenticatedGoTelemetry(t *testing.T) {
s := testPanel(t, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { fmt.Fprint(w, `{}`) }))
now := time.Now().UTC().Truncate(time.Minute).Add(-4 * time.Minute)
st := easydns.Stats{Responses: map[string]uint64{}, Sources: map[string]uint64{}}
for i := 0; i < 16; i++ {
st.QueriesTotal += 15
s.telemetry.Record(now.Add(time.Duration(i)*15*time.Second), st)
}
get := func(path string) (int, map[string]json.RawMessage) {
r := httptest.NewRequest("GET", "http://panel.test"+path, nil)
r.AddCookie(&http.Cookie{Name: sessionCookie, Value: s.auth.makeSession()})
w := httptest.NewRecorder()
s.Handler().ServeHTTP(w, r)
var body map[string]json.RawMessage
if err := json.Unmarshal(w.Body.Bytes(), &body); err != nil {
t.Fatalf("%s json: %v", path, err)
}
return w.Code, body
}
status, raw := get("/ui/telemetry?range=15m&step=1m")
if status != 200 || string(raw["range"]) != `"15m"` || string(raw["step"]) != `"1m"` || string(raw["stepSeconds"]) != "60" {
t.Fatalf("range/step not applied: %d %v", status, raw)
}
var reduced, legacy []map[string]any
_ = json.Unmarshal(raw["samples"], &reduced)
_, other := get("/ui/telemetry")
_ = json.Unmarshal(other["samples"], &legacy)
if len(reduced) >= len(legacy) || len(reduced) == 0 {
t.Fatalf("downsampling did not reduce real points: %d vs %d", len(reduced), len(legacy))
}
if _, ok := reduced[0]["QueriesTotal"]; ok {
t.Fatal("raw counter leaked to chart JSON")
}
_, bad := get("/ui/telemetry?range=999years&step=bad")
if string(bad["range"]) != `"24h"` || string(bad["step"]) != `"auto"` {
t.Fatal("unsupported range and step should be bounded to safe values")
}
}