package server import ( "encoding/json" "testing" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/core" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/fed" "git.lucasrguerra.dev.br/ciberfauna/urutau/internal/store" "time" ) // host= accepts "all", one name or a comma list, the same way on every endpoint. func TestHostSelectionList(t *testing.T) { mk := func(ip string) store.PeerExport { e := store.PeerExport{V: 1} e.Summary.IPs = []store.IPSummary{{IP: ip, Score: 10}} return e } hub := fed.NewHub(func() []fed.Backend { return []fed.Backend{fakeBackend{name: "p1", exp: mk("198.51.100.1")}, fakeBackend{name: "p2", exp: mk("198.51.100.2")}, fakeBackend{name: "p3", exp: mk("198.51.100.3")}} }) st := store.New(store.Config{Name: "me", Version: "t"}) st.Ingest(core.Event{Time: time.Now(), IP: "203.0.113.9", Cat: core.Scanner, Source: "traefik", Status: 404, Path: "/.env"}) h := New(Options{Token: utok, Store: st, Hub: hub}) total := func(host string) int { code, body := bearerGet(h, "/api/v1/ips?range=24h&min_score=0&host="+host) if code != 200 { t.Fatalf("host=%q: %d %s", host, code, body) } var o struct{ Total int } if err := json.Unmarshal([]byte(body), &o); err != nil { t.Fatal(err) } return o.Total } for host, want := range map[string]int{"": 4, "all": 4, "p1": 1, "p1,p2": 2, "p1,p2,p3": 3, "me,p3": 2, "me": 1, "me,p1,p2,p3": 4, "p2,p2": 1, "p1,all": 4} { if got := total(host); got != want { t.Errorf("host=%q: %d IPs, want %d", host, got, want) } } for _, ep := range []string{"summary", "traffic", "integrity", "geo", "report"} { if code, _ := bearerGet(h, "/api/v1/"+ep+"?host=p1,p2"); code != 200 { t.Errorf("%s host=p1,p2: %d", ep, code) } if code, _ := bearerGet(h, "/api/v1/"+ep+"?host=p1,nope"); code != 404 { t.Errorf("%s host=p1,nope: %d", ep, code) } } for _, ep := range []string{"period", "metrics", "evolution", "egress"} { if code, _ := bearerGet(h, "/api/v1/"+ep+"?host=me,p1"); code != 200 { t.Errorf("%s host=me,p1: %d", ep, code) } if code, _ := bearerGet(h, "/api/v1/"+ep+"?host=p1,nope"); code != 404 { t.Errorf("%s host=p1,nope: %d", ep, code) } } }