package api_test import ( "net/http" "slices" "strings" "testing" ) func TestAlertGroups(t *testing.T) { e := newEnv(t) owner := e.browser(t) owner.signup("o@"+uniq("ag")+".test", uniq("ag-")) groups := func(b *browser) []any { t.Helper() st, body := b.do("GET", "/api/v1/org/alert-groups", nil) if st != http.StatusOK { t.Fatalf("list groups = %d %v", st, body) } return body["alert_groups"].([]any) } initial := groups(owner) if len(initial) != 1 || initial[0].(map[string]any)["is_default"] != true { t.Fatalf("initial groups = %v", initial) } defaultID := initial[0].(map[string]any)["id"].(string) channel := func(b *browser, name string, enabled bool) string { t.Helper() st, body := b.do("POST", "/api/v1/org/channels", map[string]any{"name": name, "type": "webhook", "url": "https://example.test/hook", "enabled": enabled}) if st != http.StatusCreated { t.Fatalf("create channel = %d %v", st, body) } return body["channel"].(map[string]any)["id"].(string) } selected := channel(owner, "Selected", true) unselected := channel(owner, "Unselected", true) disabled := channel(owner, "Disabled", false) if got := groups(owner)[0].(map[string]any)["channel_ids"].([]any); len(got) != 3 { t.Fatalf("default channels = %v", got) } group := func(name string, channels []string) string { t.Helper() st, body := owner.do("POST", "/api/v1/org/alert-groups", map[string]any{"name": name, "channel_ids": channels}) if st != http.StatusCreated { t.Fatalf("create group = %d %v", st, body) } return body["id"].(string) } a := group("Operations", []string{selected, selected, disabled}) b := group("Escalation", []string{selected}) monitor := map[string]any{"name": "Backup", "type": "heartbeat", "interval_seconds": 3600} st, body := owner.do("POST", "/api/v1/org/monitors", monitor) if st != http.StatusCreated { t.Fatalf("create monitor = %d %v", st, body) } id := body["id"].(string) token := body["target"].(string) if got := body["alert_group_ids"].([]any); len(got) != 1 || got[0] != defaultID { t.Fatalf("default monitor assignments = %v", got) } st, _, html := owner.page("/monitors/" + id + "/edit") if st != http.StatusOK || !strings.Contains(html, `name="alert_group_ids"`) || !strings.Contains(html, "Operations") { t.Fatalf("monitor editor = %d, lacks alert groups", st) } st, _, html = owner.page("/settings/channels") if st != http.StatusOK || !strings.Contains(html, "/org/alert-groups/"+a) { t.Fatalf("settings = %d, lacks alert group editor", st) } monitor["alert_group_ids"] = []string{a, b, a} st, body = owner.do("PUT", "/api/v1/org/monitors/"+id, monitor) if st != http.StatusOK || len(body["alert_group_ids"].([]any)) != 2 { t.Fatalf("assign groups = %d %v", st, body) } delete(monitor, "alert_group_ids") st, body = owner.do("PUT", "/api/v1/org/monitors/"+id, monitor) if st != http.StatusOK || len(body["alert_group_ids"].([]any)) != 2 { t.Fatalf("preserve assignments = %d %v", st, body) } // Exercise actual incident transitions and their outbox, including recovery. for _, suffix := range []string{"/fail", ""} { resp, err := e.srv.Client().Post(e.srv.URL+"/hb/"+token+suffix, "text/plain", strings.NewReader("")) if err != nil { t.Fatal(err) } _ = resp.Body.Close() if resp.StatusCode != http.StatusOK { t.Fatalf("heartbeat %q = %d", suffix, resp.StatusCode) } } count := func(ch string) int { return e.count(t, `SELECT count(*) FROM notifications n JOIN incidents i ON i.id=n.incident_id WHERE i.monitor_id=$1 AND n.channel_id=$2`, id, ch) } if count(selected) != 2 || count(unselected) != 0 || count(disabled) != 0 { t.Fatalf("deliveries selected/unselected/disabled = %d/%d/%d", count(selected), count(unselected), count(disabled)) } other := e.browser(t) other.signup("x@"+uniq("ag")+".test", uniq("agx-")) foreignGroup := groups(other)[0].(map[string]any)["id"].(string) foreignChannel := channel(other, "Foreign", true) monitor["alert_group_ids"] = []string{foreignGroup} st, body = owner.do("PUT", "/api/v1/org/monitors/"+id, monitor) expect(t, "foreign monitor assignment", st, body, http.StatusUnprocessableEntity, "invalid_alert_group") st, body = owner.do("POST", "/api/v1/org/monitors", monitor) expect(t, "foreign assignment on creation", st, body, http.StatusUnprocessableEntity, "invalid_alert_group") st, body = owner.do("PUT", "/api/v1/org/alert-groups/"+a, map[string]any{"name": "Changed", "channel_ids": []string{foreignChannel}}) expect(t, "foreign channel assignment", st, body, http.StatusUnprocessableEntity, "invalid_channel") st, body = other.do("PUT", "/api/v1/org/alert-groups/"+a, map[string]any{"name": "Stolen"}) expect(t, "foreign group update", st, body, http.StatusNotFound, "not_found") for _, g := range groups(owner) { g := g.(map[string]any) if g["id"] == a && (g["name"] != "Operations" || len(g["channel_ids"].([]any)) != 2) { t.Fatalf("failed update changed group: %v", g) } } _, body = owner.do("GET", "/api/v1/org/monitors", nil) monitors := body["monitors"].([]any) if len(monitors) != 1 || len(monitors[0].(map[string]any)["alert_group_ids"].([]any)) != 2 { t.Fatalf("failed assignment changed monitors: %v", monitors) } viewer := e.joinVia(t, owner.invite("v@"+uniq("ag")+".test", owner.roleID("viewer"))) st, body = viewer.do("POST", "/api/v1/org/alert-groups", map[string]any{"name": "Forbidden"}) expect(t, "viewer creates group", st, body, http.StatusForbidden, "forbidden") st, body = owner.do("DELETE", "/api/v1/org/alert-groups/"+defaultID, nil) expect(t, "protected default", st, body, http.StatusNotFound, "alert_group_not_deletable") st, body = owner.do("PUT", "/api/v1/org/alert-groups/"+defaultID, map[string]any{"name": "Standard", "channel_ids": []string{selected}}) if st != http.StatusOK || body["is_default"] != true { t.Fatalf("edit default = %d %v", st, body) } st, body = owner.do("DELETE", "/api/v1/org/alert-groups/"+a, nil) expect(t, "delete custom group", st, body, http.StatusNoContent, "") _, body = owner.do("GET", "/api/v1/org/monitors", nil) assigned := body["monitors"].([]any)[0].(map[string]any)["alert_group_ids"].([]any) if !slices.Equal(assigned, []any{b}) { t.Fatalf("assignments after group deletion = %v", assigned) } monitor["alert_group_ids"] = []string{} st, body = owner.do("PUT", "/api/v1/org/monitors/"+id, monitor) if st != http.StatusOK || len(body["alert_group_ids"].([]any)) != 0 { t.Fatalf("clear assignments = %d %v", st, body) } resp, err := e.srv.Client().Post(e.srv.URL+"/hb/"+token+"/fail", "text/plain", strings.NewReader("")) if err != nil { t.Fatal(err) } _ = resp.Body.Close() if resp.StatusCode != http.StatusOK || count(selected) != 2 { t.Fatalf("unassigned monitor queued notifications, status=%d count=%d", resp.StatusCode, count(selected)) } st, body = owner.do("POST", "/api/v1/org/monitors", monitor) if st != http.StatusCreated || len(body["alert_group_ids"].([]any)) != 0 { t.Fatalf("explicit empty creation = %d %v", st, body) } }