package server import ( "encoding/json" "testing" ) var opsAuth = map[string]string{"Authorization": "Bearer user-token-0123456789-abcdefghijklmnop"} func TestIntegrityAPI(t *testing.T) { h, _ := setup(t) if w := do(h, "GET", "/api/v1/integrity?range=24h", nil, nil); w.Code != 401 && w.Code != 403 { t.Fatalf("closed without token: %d", w.Code) } w := do(h, "GET", "/api/v1/integrity?range=24h", copyHdr(opsAuth), nil) if w.Code != 200 { t.Fatalf("%d %s", w.Code, w.Body) } var o struct { Hosts []struct { Name string `json:"name"` Online bool `json:"online"` Hourly []uint32 `json:"hourly"` } `json:"hosts"` Peers []any `json:"peers"` } if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } if len(o.Hosts) != 1 || o.Hosts[0].Name != "oracle" || !o.Hosts[0].Online || len(o.Hosts[0].Hourly) < 24 { t.Fatalf("%+v", o) } var sum uint32 for _, v := range o.Hosts[0].Hourly { sum += v } if sum == 0 { t.Fatal("the ingested event must show in the hourly series") } if w := do(h, "GET", "/api/v1/integrity?host=nope", copyHdr(opsAuth), nil); w.Code != 404 { t.Fatalf("unknown host: %d", w.Code) } } func TestReportAPI(t *testing.T) { h, _ := setup(t) w := do(h, "GET", "/api/v1/report?range=7d&format=json", copyHdr(opsAuth), nil) if w.Code != 200 { t.Fatalf("%d %s", w.Code, w.Body) } var o struct { Hosts []string `json:"hosts"` Summary struct { Attacks uint64 `json:"attacks"` } `json:"summary"` TopIPs []struct { IP string `json:"ip"` } `json:"top_ips"` } if err := json.Unmarshal(w.Body.Bytes(), &o); err != nil { t.Fatal(err) } if o.Summary.Attacks != 1 || len(o.TopIPs) != 1 || len(o.Hosts) != 1 { t.Fatalf("%s", w.Body) } if w := do(h, "GET", "/api/v1/report?format=pdf", copyHdr(opsAuth), nil); w.Code != 400 { t.Fatalf("format: %d", w.Code) } if w := do(h, "GET", "/api/v1/report?host=nope", copyHdr(opsAuth), nil); w.Code != 404 { t.Fatalf("host: %d", w.Code) } } func copyHdr(m map[string]string) map[string]string { c := map[string]string{} for k, v := range m { c[k] = v } return c }