A storm of alerts used to mean opening every incident to acknowledge it. The queue now has a checkbox per row and a bar to acknowledge, snooze or assign everything ticked, with an Undo on the confirmation. On the server this is one POST per action under /api/incidents (acknowledge, unacknowledge, snooze, unsnooze, assign), each taking up to 100 ids and answering per id: ok, noop, conflict (resolved) or not_found (missing, or not in the caller's teams, which look the same on purpose). Partial success rather than all-or-nothing, since one resolved incident in a selection should not block the other nine. Each incident runs in its own transaction, row-locked, so the update, the escalation stop and the timeline event land together and carry the same actor as the single-incident endpoints. Those are unchanged; only the snooze time parsing moved into a helper both now use. Resolve is left out of bulk deliberately: it is terminal, and a mis-ticked row is hard to take back. Undoing a bulk assign restores the previous assignee but cannot unassign, as no endpoint does that. The snooze and assign sheets moved to js/pickers.js so the incident page and the queue share them.
228 lines
7.5 KiB
Go
228 lines
7.5 KiB
Go
package api_test
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"testing"
|
|
|
|
"git.ryuvia.com/niklas/terdut-server/internal/models"
|
|
)
|
|
|
|
type bulkResp struct {
|
|
Results []struct {
|
|
ID int `json:"id"`
|
|
Status string `json:"status"`
|
|
Incident map[string]any `json:"incident"`
|
|
} `json:"results"`
|
|
Summary map[string]int `json:"summary"`
|
|
}
|
|
|
|
// fireN opens n incidents (ids 1..n), one per distinct group key.
|
|
func fireN(t *testing.T, s *ts, n int) {
|
|
t.Helper()
|
|
for i := 1; i <= n; i++ {
|
|
name := fmt.Sprintf("Bulk%d", i)
|
|
postWebhook(t, s, []map[string]any{
|
|
amAlert("fp-"+name, name, "firing", "2026-05-20T10:00:00Z", zeroTime, nil),
|
|
}, fmt.Sprintf("{}:{alertname=%q}", name))
|
|
}
|
|
}
|
|
|
|
func bulkPost(t *testing.T, s *ts, path string, body any) (int, bulkResp) {
|
|
t.Helper()
|
|
resp := s.req(t, http.MethodPost, "/api/incidents/"+path, body)
|
|
code := resp.StatusCode
|
|
var out bulkResp
|
|
if code == http.StatusOK {
|
|
decode(t, resp, &out)
|
|
} else {
|
|
resp.Body.Close()
|
|
}
|
|
return code, out
|
|
}
|
|
|
|
func statuses(r bulkResp) map[int]string {
|
|
m := map[int]string{}
|
|
for _, res := range r.Results {
|
|
m[res.ID] = res.Status
|
|
}
|
|
return m
|
|
}
|
|
|
|
func TestBulk_AcknowledgeMixedOutcomes(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 4)
|
|
s.req(t, http.MethodPost, "/api/incidents/2/acknowledge", nil).Body.Close() // already acked
|
|
s.req(t, http.MethodPost, "/api/incidents/3/resolve", nil).Body.Close() // terminal
|
|
|
|
code, out := bulkPost(t, s, "acknowledge", map[string]any{"ids": []int{1, 2, 3, 4, 999, 1}})
|
|
if code != http.StatusOK {
|
|
t.Fatalf("bulk acknowledge returned %d", code)
|
|
}
|
|
got := statuses(out)
|
|
want := map[int]string{1: "ok", 2: "noop", 3: "conflict", 4: "ok", 999: "not_found"}
|
|
for id, st := range want {
|
|
if got[id] != st {
|
|
t.Errorf("incident %d: got %q, want %q", id, got[id], st)
|
|
}
|
|
}
|
|
if len(out.Results) != 5 {
|
|
t.Errorf("duplicate id should be collapsed: got %d results", len(out.Results))
|
|
}
|
|
if out.Summary["ok"] != 2 || out.Summary["noop"] != 1 || out.Summary["conflict"] != 1 || out.Summary["not_found"] != 1 {
|
|
t.Errorf("unexpected summary %v", out.Summary)
|
|
}
|
|
|
|
// One acknowledged event each, with the actor; the no-op added none.
|
|
for _, id := range []int{1, 4} {
|
|
if inc := getIncident(t, s, id); inc["status"] != "acknowledged" || inc["acknowledged_by"] == nil {
|
|
t.Errorf("incident %d not acknowledged by a user: %v", id, inc["status"])
|
|
}
|
|
n := 0
|
|
for _, typ := range eventTypes(timeline(t, s, id)) {
|
|
if typ == "acknowledged" {
|
|
n++
|
|
}
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("incident %d: %d acknowledged events, want 1", id, n)
|
|
}
|
|
}
|
|
n := 0
|
|
for _, typ := range eventTypes(timeline(t, s, 2)) {
|
|
if typ == "acknowledged" {
|
|
n++
|
|
}
|
|
}
|
|
if n != 1 {
|
|
t.Errorf("no-op ack wrote an extra event: %d", n)
|
|
}
|
|
}
|
|
|
|
func TestBulk_Validation(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 1)
|
|
tooMany := make([]int, 101)
|
|
for i := range tooMany {
|
|
tooMany[i] = i + 1
|
|
}
|
|
cases := map[string]any{
|
|
"empty": map[string]any{"ids": []int{}},
|
|
"missing": map[string]any{},
|
|
"over100": map[string]any{"ids": tooMany},
|
|
"zero id": map[string]any{"ids": []int{0}},
|
|
}
|
|
for name, body := range cases {
|
|
if code, _ := bulkPost(t, s, "acknowledge", body); code != http.StatusBadRequest {
|
|
t.Errorf("%s: got %d, want 400", name, code)
|
|
}
|
|
}
|
|
if code, _ := bulkPost(t, s, "snooze", map[string]any{"ids": []int{1}}); code != http.StatusBadRequest {
|
|
t.Errorf("snooze without until/duration: got %d, want 400", code)
|
|
}
|
|
if code, _ := bulkPost(t, s, "snooze", map[string]any{"ids": []int{1}, "duration": "-1h"}); code != http.StatusBadRequest {
|
|
t.Errorf("snooze in the past: got %d, want 400", code)
|
|
}
|
|
if code, _ := bulkPost(t, s, "assign", map[string]any{"ids": []int{1}}); code != http.StatusBadRequest {
|
|
t.Errorf("assign without user_id: got %d, want 400", code)
|
|
}
|
|
if code, _ := bulkPost(t, s, "assign", map[string]any{"ids": []int{1}, "user_id": 99}); code != http.StatusNotFound {
|
|
t.Errorf("assign to unknown user: got %d, want 404", code)
|
|
}
|
|
}
|
|
|
|
func TestBulk_SnoozeAndUndo(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 2)
|
|
code, out := bulkPost(t, s, "snooze", map[string]any{"ids": []int{1, 2}, "duration": "1h"})
|
|
if code != http.StatusOK || out.Summary["ok"] != 2 {
|
|
t.Fatalf("bulk snooze: %d %v", code, out.Summary)
|
|
}
|
|
if inc := getIncident(t, s, 1); inc["snoozed_until"] == nil {
|
|
t.Error("incident 1 should be snoozed")
|
|
}
|
|
_, out = bulkPost(t, s, "unsnooze", map[string]any{"ids": []int{1, 2}})
|
|
if out.Summary["ok"] != 2 {
|
|
t.Errorf("unsnooze summary %v", out.Summary)
|
|
}
|
|
_, out = bulkPost(t, s, "unsnooze", map[string]any{"ids": []int{1}})
|
|
if out.Summary["noop"] != 1 {
|
|
t.Errorf("second unsnooze should be a no-op: %v", out.Summary)
|
|
}
|
|
types := eventTypes(timeline(t, s, 1))
|
|
if !contains(types, "snoozed") || !contains(types, "unsnoozed") {
|
|
t.Errorf("timeline missing snooze events: %v", types)
|
|
}
|
|
}
|
|
|
|
func TestBulk_UnacknowledgeOnlyTouchesAcknowledged(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 2)
|
|
bulkPost(t, s, "acknowledge", map[string]any{"ids": []int{1}})
|
|
_, out := bulkPost(t, s, "unacknowledge", map[string]any{"ids": []int{1, 2}})
|
|
got := statuses(out)
|
|
if got[1] != "ok" || got[2] != "noop" {
|
|
t.Errorf("got %v", got)
|
|
}
|
|
if inc := getIncident(t, s, 1); inc["status"] != "triggered" {
|
|
t.Errorf("incident 1 status %v", inc["status"])
|
|
}
|
|
}
|
|
|
|
func TestBulk_Assign(t *testing.T) {
|
|
s := newTS(t)
|
|
fireN(t, s, 2)
|
|
s.req(t, http.MethodPost, "/api/users",
|
|
map[string]string{"username": "alice", "email": "alice@test.com"}).Body.Close()
|
|
|
|
_, out := bulkPost(t, s, "assign", map[string]any{"ids": []int{1, 2}, "user_id": 2})
|
|
if out.Summary["ok"] != 2 {
|
|
t.Fatalf("assign summary %v", out.Summary)
|
|
}
|
|
if inc := getIncident(t, s, 1); inc["assigned_to"] != "alice" {
|
|
t.Errorf("assigned_to = %v", inc["assigned_to"])
|
|
}
|
|
if e := lastEvent(t, timeline(t, s, 1), "assigned"); e["actor_user_id"] == nil {
|
|
t.Errorf("assigned event should record the actor: %v", e)
|
|
}
|
|
_, out = bulkPost(t, s, "assign", map[string]any{"ids": []int{1}, "user_id": 2})
|
|
if out.Summary["noop"] != 1 {
|
|
t.Errorf("re-assigning the same user should be a no-op: %v", out.Summary)
|
|
}
|
|
}
|
|
|
|
func TestBulk_ServiceAccountScopedToItsTeam(t *testing.T) {
|
|
s := newTS(t)
|
|
instanceKey := createServiceAccount(t, s, s.key, "operator", models.ServiceAccountScopeInstance, 0)
|
|
teamA := createTeamAs(t, s, instanceKey, "team-a")
|
|
teamB := createTeamAs(t, s, instanceKey, "team-b")
|
|
keyA := createServiceAccount(t, s, instanceKey, "team-a-sa", models.ServiceAccountScopeTeam, teamA)
|
|
|
|
var ia, ib struct {
|
|
Key string `json:"key"`
|
|
}
|
|
decode(t, s.reqAs(t, instanceKey, http.MethodPost, "/api/teams/"+id64(teamA)+"/integrations",
|
|
map[string]string{"name": "a"}), &ia)
|
|
decode(t, s.reqAs(t, instanceKey, http.MethodPost, "/api/teams/"+id64(teamB)+"/integrations",
|
|
map[string]string{"name": "b"}), &ib)
|
|
postToIntegration(t, s, ia.Key, "fp-a", "A") // incident 1, team A
|
|
postToIntegration(t, s, ib.Key, "fp-b", "B") // incident 2, team B
|
|
|
|
resp := s.reqAs(t, keyA, http.MethodPost, "/api/incidents/acknowledge", map[string]any{"ids": []int{1, 2}})
|
|
if resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("service account bulk acknowledge: %d", resp.StatusCode)
|
|
}
|
|
var out bulkResp
|
|
decode(t, resp, &out)
|
|
got := statuses(out)
|
|
if got[1] != "ok" || got[2] != "not_found" {
|
|
t.Errorf("got %v; the other team's incident must look missing", got)
|
|
}
|
|
// The bootstrap human is not in team A, so read the incident as its own account.
|
|
var inc map[string]any
|
|
decode(t, s.reqAs(t, keyA, http.MethodGet, "/api/incidents/1", nil), &inc)
|
|
if inc["acknowledged_by_service_account_id"] == nil {
|
|
t.Error("expected the service account recorded as the actor")
|
|
}
|
|
}
|