package api_test import ( "context" "net/http" "net/http/httptest" "strings" "testing" "time" "github.com/google/uuid" "git.zb-server.de/ZB-Server/uptimesebi/internal/api" "git.zb-server.de/ZB-Server/uptimesebi/internal/store" "git.zb-server.de/ZB-Server/uptimesebi/internal/tenant" ) func TestCheckerActivity(t *testing.T) { e := newEnv(t) ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) go e.api.RunScheduler(ctx) slow := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { time.Sleep(3 * time.Second) })) defer slow.Close() tok, region := e.platformChecker(t, uniq("eu-")) instance, _ := e.startChecker(t, tok) alice, bob := e.browser(t), e.browser(t) alice.signup("a@"+uniq("ca")+".test", uniq("ca-")) bob.signup("b@"+uniq("cb")+".test", uniq("cb-")) create := func(b *browser, name string) string { t.Helper() st, body := b.do("POST", "/api/v1/org/monitors", map[string]any{ "name": name, "type": "http", "target": slow.URL, "interval_seconds": 3600, "timeout_ms": 5000, "regions": []string{region}, }) if st != http.StatusCreated { t.Fatalf("create monitor = %d %v", st, body) } return body["id"].(string) } aliceMonitor := create(alice, "Alice shop") create(bob, "Bob secret service") path := "/api/v1/org/checkers/" + instance.String() var view map[string]any waitFor(t, 10*time.Second, "both checks in progress", func() bool { _, view = alice.do("GET", path, nil) running, _ := view["running"].([]any) return len(running) == 1 && view["running_for_others"] == float64(1) }) if raw := mustJSON(view); strings.Contains(raw, "Bob secret service") || view["live"] != true || view["platform"] != true { t.Errorf("alice's view = %s", raw) } if _, bv := bob.do("GET", path, nil); strings.Contains(mustJSON(bv), "Alice shop") { t.Error("bob sees alice's monitor") } waitFor(t, 10*time.Second, "the finished check listed", func() bool { _, view = alice.do("GET", path, nil) recent, _ := view["recent"].([]any) running, _ := view["running"].([]any) return len(recent) == 1 && len(running) == 0 }) if _, _, page := alice.page("/monitors/" + aliceMonitor); !strings.Contains(page, `href="/checkers/`+instance.String()+`"`) { t.Error("the monitor page does not link the checker that ran the check") } st, _, page := alice.page("/checkers/" + instance.String()) if st != http.StatusOK || !strings.Contains(page, "Alice shop") || strings.Contains(page, "Bob secret service") || !strings.Contains(page, `http-equiv="refresh"`) { t.Errorf("checker page = %d, lacks own monitor, shows others or does not refresh", st) } // Checkers of another organization stay invisible. bobOrg := uuid.MustParse(bob.orgID()) var bobTok string err := tenant.New(e.dbPool).Tx(context.Background(), tenant.System(), func(q *store.Queries) error { r, err := q.CreateRegion(context.Background(), store.CreateRegionParams{OrgID: &bobOrg, Key: uniq("own-"), Name: "Bob DC"}) if err != nil { return err } var params store.CreateAgentParams bobTok, params = api.NewAgentToken(&bobOrg, "checker", "bob-dc", &r.ID) _, err = q.CreateAgent(context.Background(), params) return err }) if err != nil { t.Fatal(err) } bobInstance, _ := e.startChecker(t, bobTok) waitFor(t, 5*time.Second, "bob's checker registered", func() bool { st, _ := bob.do("GET", "/api/v1/org/checkers/"+bobInstance.String(), nil) return st == http.StatusOK }) if st, _ := alice.do("GET", "/api/v1/org/checkers/"+bobInstance.String(), nil); st != http.StatusNotFound { t.Errorf("alice sees bob's own checker: %d", st) } }