feat(statuspage): add nested status page groups and optional component details
ci / image (pull_request) Successful in 4m23s
ci / lint (pull_request) Canceled after 6m9s
ci / test (pull_request) Canceled after 6m8s
ci / build (pull_request) Canceled after 6m7s

This commit is contained in:
Sebastian Unterschütz committed 2026-11-22 20:07:30 +01:00
1 parent 1c31003baf
commit 9b26b4150e
26 files changed
+4566 -760

No files matched your search

+214
View File
@@ -213,3 +213,217 @@ func verifyDomain(t *testing.T, owner *browser, pageID string, record map[string
st, body := owner.do("POST", "/api/v1/org/statuspages/"+pageID+"/domain/verify", nil)
expect(t, "verify domain", st, body, http.StatusNoContent, "")
}
func TestStatusPageGroupsAndDetails(t *testing.T) {
e := newEnv(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go e.api.RunScheduler(ctx)
up := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
defer up.Close()
down := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusInternalServerError)
}))
defer down.Close()
tok, region := e.platformChecker(t, uniq("eu-"))
e.startChecker(t, tok)
owner := e.browser(t)
owner.signup("o@"+uniq("spg")+".test", uniq("spg-"))
// Create monitor groups to test importing the hierarchy later.
st, mgRoot := owner.do("POST", "/api/v1/org/groups", map[string]any{"name": "Core"})
if st != http.StatusCreated {
t.Fatalf("create monitor root group = %d %v, want 201", st, mgRoot)
}
st, mgChild := owner.do("POST", "/api/v1/org/groups", map[string]any{
"name": "Edge",
"parent_id": mgRoot["id"],
})
if st != http.StatusCreated {
t.Fatalf("create monitor child group = %d %v, want 201", st, mgChild)
}
newMonitor := func(name, target string, groupID any) string {
t.Helper()
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{
"name": name,
"type": "http",
"target": target,
"interval_seconds": 60,
"confirmations": 1,
"regions": []string{region},
"group_id": groupID,
})
if st != http.StatusCreated {
t.Fatalf("create monitor %q = %d %v, want 201", name, st, body)
}
return body["id"].(string)
}
webID := newMonitor("web", up.URL, mgChild["id"])
apiID := newMonitor("api", down.URL, mgRoot["id"])
slug := uniq("nested-")
st, pageBody := owner.do("POST", "/api/v1/org/statuspages", map[string]any{
"slug": slug,
"title": "Nested Status",
"published": true,
"show_component_details": false,
})
if st != http.StatusCreated {
t.Fatalf("create status page = %d %v, want 201", st, pageBody)
}
pageID := pageBody["id"].(string)
// Nested status page groups CRUD + cycle & depth validation.
st, gParent := owner.do("POST", "/api/v1/org/statuspages/"+pageID+"/groups", map[string]any{
"name": "Platform",
"position": 0,
})
if st != http.StatusCreated {
t.Fatalf("create page parent group = %d %v, want 201", st, gParent)
}
parentID := gParent["id"].(string)
st, gChild := owner.do("POST", "/api/v1/org/statuspages/"+pageID+"/groups", map[string]any{
"name": "Public Services",
"parent_id": parentID,
"position": 0,
})
if st != http.StatusCreated {
t.Fatalf("create page child group = %d %v, want 201", st, gChild)
}
childID := gChild["id"].(string)
// Cycle rejection.
st, body := owner.do("PUT", "/api/v1/org/statuspages/"+pageID+"/groups/"+parentID, map[string]any{
"name": "Platform",
"parent_id": childID,
})
expect(t, "group cycle", st, body, http.StatusUnprocessableEntity, "group_cycle")
// Assign monitors into nested groups.
st, body = owner.do("PUT", "/api/v1/org/statuspages/"+pageID+"/monitors", map[string]any{
"monitors": []map[string]any{
{"monitor_id": webID, "group_id": childID, "label": "Website"},
{"monitor_id": apiID, "group_id": parentID, "label": "API"},
},
})
expect(t, "assign grouped monitors", st, body, http.StatusNoContent, "")
waitFor(t, 10*time.Second, "incident for failing monitor", func() bool {
_, b := owner.do("GET", "/api/v1/org/incidents", nil)
return len(b["incidents"].([]any)) == 1
})
platform := e.statuspageClient(t, nil)
snap, err := platform.Page(context.Background(), "", slug)
if err != nil {
t.Fatalf("Page() error = %v", err)
}
if snap.ShowDetails {
t.Fatalf("snap.ShowDetails = true, want false")
}
if len(snap.Sections) != 1 || snap.Sections[0].Name != "Platform" ||
snap.Sections[0].Total != 2 || snap.Sections[0].Status != agentapi.PageDegraded ||
len(snap.Sections[0].Children) != 1 || snap.Sections[0].Children[0].Name != "Public Services" ||
snap.Sections[0].Children[0].Status != agentapi.PageOperational {
t.Fatalf("nested sections = %+v, want Platform(degraded,2) -> Public Services(operational,1)", snap.Sections)
}
if snap.Sections[0].Components[0].Details != nil {
t.Errorf("component Details populated when show_component_details=false")
}
// Enable show_component_details and verify snapshot details & privacy.
st, body = owner.do("PUT", "/api/v1/org/statuspages/"+pageID, map[string]any{
"slug": slug,
"title": "Nested Status",
"published": true,
"show_component_details": true,
})
if st != http.StatusOK || body["show_component_details"] != true {
t.Fatalf("enable show_component_details = %d %v, want 200 with true", st, body)
}
snap, err = platform.Page(context.Background(), "", slug)
if err != nil {
t.Fatalf("Page(with details) error = %v", err)
}
if !snap.ShowDetails {
t.Fatalf("snap.ShowDetails = false, want true")
}
comp, ok := snap.ComponentByID(apiID)
if !ok || comp.Details == nil {
t.Fatalf("ComponentByID(%s) = (%+v, %t), want component with Details", apiID, comp, ok)
}
if len(comp.Details.Hours) != 24 || len(comp.Details.Regions) != 1 {
t.Errorf("component details hours/regions = %d/%d, want 24/1", len(comp.Details.Hours), len(comp.Details.Regions))
}
raw, _ := json.Marshal(snap)
for _, secret := range []string{up.URL, down.URL, "unexpected status", "HTTP 500"} {
if strings.Contains(string(raw), secret) {
t.Errorf("snapshot with details leaks %q", secret)
}
}
// Verify live status page server renders component detail page.
cat, _ := i18n.Load()
spSrv, err := statuspage.New(platform, cat, slog.New(slog.DiscardHandler), statuspage.Options{Hosts: []string{"status.test"}})
if err != nil {
t.Fatalf("statuspage.New() error = %v", err)
}
gin.SetMode(gin.TestMode)
router := gin.New()
spSrv.Mount(router)
req := httptest.NewRequest(http.MethodGet, "/"+slug+"/components/"+apiID, nil)
req.Host = "status.test"
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
if rec.Code != http.StatusOK || !strings.Contains(rec.Body.String(), "API") || !strings.Contains(rec.Body.String(), "Last 24 hours") {
t.Errorf("GET detail page = %d, want 200 with component details", rec.Code)
}
// Static export has no broken /components/ links and embeds inline details.
resp, err := owner.hc.Get(owner.base + "/api/v1/org/statuspages/" + pageID + "/export")
if err != nil {
t.Fatalf("export request error = %v", err)
}
exported, _ := io.ReadAll(resp.Body)
_ = resp.Body.Close()
if resp.StatusCode != http.StatusOK || strings.Contains(string(exported), "/components/") || !strings.Contains(string(exported), "Show details") {
t.Errorf("export status = %d, want inline details without /components/ links", resp.StatusCode)
}
// Delete child group: monitor moves up to parent group.
st, body = owner.do("DELETE", "/api/v1/org/statuspages/"+pageID+"/groups/"+childID, nil)
expect(t, "delete child group", st, body, http.StatusNoContent, "")
snap, err = platform.Page(context.Background(), "", slug)
if err != nil {
t.Fatalf("Page(after delete) error = %v", err)
}
if len(snap.Sections) != 1 || len(snap.Sections[0].Components) != 2 || len(snap.Sections[0].Children) != 0 {
t.Errorf("after deleting child group, sections = %+v, want 2 monitors under Platform", snap.Sections)
}
// Import monitor groups copies Core -> Edge and re-assigns monitors.
st, imported := owner.do("POST", "/api/v1/org/statuspages/"+pageID+"/groups/import", nil)
if st != http.StatusOK || len(imported["groups"].([]any)) != 2 {
t.Fatalf("import groups = %d %v, want 200 with 2 groups", st, imported)
}
snap, err = platform.Page(context.Background(), "", slug)
if err != nil {
t.Fatalf("Page(after import) error = %v", err)
}
if len(snap.Sections) != 1 || snap.Sections[0].Name != "Core" ||
len(snap.Sections[0].Children) != 1 || snap.Sections[0].Children[0].Name != "Edge" {
t.Errorf("imported sections = %+v, want Core -> Edge", snap.Sections)
}
// Console editor shows new controls.
st, _, html := owner.page("/statuspages/" + pageID)
for _, want := range []string{`name="show_component_details"`, `/groups/import`, `data-field="group_id"`} {
if st != http.StatusOK || !strings.Contains(html, want) {
t.Errorf("editor HTML = %d, missing %q", st, want)
}
}
}