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) } } func TestAlertGroupQuietHours(t *testing.T) { e := newEnv(t) owner := e.browser(t) owner.signup("q@"+uniq("agq")+".test", uniq("agq-")) channel := func(name string) string { t.Helper() req := map[string]any{ "name": name, "type": "webhook", "url": "https://example.test/hook", "enabled": true, } st, body := owner.do("POST", "/api/v1/org/channels", req) if st != http.StatusCreated { t.Fatalf("POST /api/v1/org/channels = %d %v, want %d", st, body, http.StatusCreated) } return body["channel"].(map[string]any)["id"].(string) } quietCh := channel("QuietOnly") sharedCh := channel("Shared") activeCh := channel("ActiveOnly") // Invalid quiet hours are rejected. st, body := owner.do("POST", "/api/v1/org/alert-groups", map[string]any{ "name": "BadTZ", "channel_ids": []string{quietCh}, "quiet_hours": []map[string]any{{"start": "22:00", "end": "07:00", "time_zone": "Not/AZone"}}, }) expect(t, "invalid timezone", st, body, http.StatusUnprocessableEntity, "invalid_time_zone") st, body = owner.do("POST", "/api/v1/org/alert-groups", map[string]any{ "name": "BadRange", "channel_ids": []string{quietCh}, "quiet_hours": []map[string]any{{"start": "12:00", "end": "12:00", "time_zone": "UTC"}}, }) expect(t, "equal non-zero times", st, body, http.StatusUnprocessableEntity, "invalid_quiet_hours") // Create a group that is muted 24/7 (00:00-00:00 every day) and an active group. st, body = owner.do("POST", "/api/v1/org/alert-groups", map[string]any{ "name": "Muted", "channel_ids": []string{quietCh, sharedCh}, "quiet_hours": []map[string]any{{"weekdays": []string{}, "start": "00:00", "end": "00:00", "time_zone": "UTC"}}, }) if st != http.StatusCreated { t.Fatalf("create muted group = %d %v", st, body) } mutedGroup := body["id"].(string) if qh := body["quiet_hours"].([]any); len(qh) != 1 { t.Fatalf("quiet_hours = %v", qh) } // Updating without quiet_hours preserves existing quiet hours. st, body = owner.do("PUT", "/api/v1/org/alert-groups/"+mutedGroup, map[string]any{ "name": "Muted Renamed", "channel_ids": []string{quietCh, sharedCh}, }) if st != http.StatusOK || len(body["quiet_hours"].([]any)) != 1 { t.Fatalf("preserve quiet_hours = %d %v", st, body) } // Settings page shows the quiet hours inputs and active badge. st, _, html := owner.page("/settings/channels") if st != http.StatusOK || !strings.Contains(html, `name="quiet_hours.0.start"`) || !strings.Contains(html, "Quiet hours active") { t.Fatalf("settings page = %d, lacks quiet hours UI", st) } st, body = owner.do("POST", "/api/v1/org/alert-groups", map[string]any{ "name": "AlwaysOn", "channel_ids": []string{activeCh, sharedCh}, "quiet_hours": []map[string]any{}, }) if st != http.StatusCreated { t.Fatalf("create active group = %d %v", st, body) } activeGroup := body["id"].(string) // Monitor assigned only to Muted: incident opens & resolves, 0 notifications sent. st, body = owner.do("POST", "/api/v1/org/monitors", map[string]any{ "name": "SilentBackup", "type": "heartbeat", "interval_seconds": 3600, "alert_group_ids": []string{mutedGroup}, }) if st != http.StatusCreated { t.Fatalf("create monitor = %d %v", st, body) } monID := body["id"].(string) token := body["target"].(string) ping := func(suffix string) { t.Helper() 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 { const q = `SELECT count(*) FROM notifications n JOIN incidents i ON i.id = n.incident_id WHERE i.monitor_id = $1 AND n.channel_id = $2` return e.count(t, q, monID, ch) } ping("/fail") ping("") if inc := e.count(t, `SELECT count(*) FROM incidents WHERE monitor_id=$1`, monID); inc != 1 { t.Fatalf("incidents = %d, want 1", inc) } if q, s := count(quietCh), count(sharedCh); q != 0 || s != 0 { t.Fatalf("muted notifications quiet/shared = %d/%d, want 0/0", q, s) } // Assign both Muted and AlwaysOn: only AlwaysOn's channels (activeCh + sharedCh) notify, // while quietCh stays silent. st, body = owner.do("PUT", "/api/v1/org/monitors/"+monID, map[string]any{ "name": "SilentBackup", "type": "heartbeat", "interval_seconds": 3600, "alert_group_ids": []string{mutedGroup, activeGroup}, }) if st != http.StatusOK { t.Fatalf("PUT /api/v1/org/monitors/%s = %d %v, want %d", monID, st, body, http.StatusOK) } ping("/fail") ping("") if q, a, s := count(quietCh), count(activeCh), count(sharedCh); q != 0 || a != 2 || s != 2 { t.Fatalf("mixed deliveries quiet/active/shared = %d/%d/%d, want 0/2/2", q, a, s) } // Explicitly clearing quiet_hours re-enables notifications on quietCh. st, body = owner.do("PUT", "/api/v1/org/alert-groups/"+mutedGroup, map[string]any{ "name": "Muted Renamed", "channel_ids": []string{quietCh, sharedCh}, "quiet_hours": []map[string]any{}, }) if st != http.StatusOK || len(body["quiet_hours"].([]any)) != 0 { t.Fatalf("clear quiet_hours = %d %v, want 200 with empty quiet_hours", st, body) } ping("/fail") ping("") if q, a, s := count(quietCh), count(activeCh), count(sharedCh); q != 2 || a != 4 || s != 4 { t.Fatalf("after clearing quiet hours quiet/active/shared = %d/%d/%d, want 2/4/4", q, a, s) } }