package api_test
import (
"context"
"net/http"
"strings"
"testing"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
)
func TestIncidentUpdates(t *testing.T) {
e := newEnv(t)
ctx := context.Background()
owner := e.browser(t)
owner.signup("o@"+uniq("iu")+".test", uniq("iu-"))
_, m := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Login", "type": "heartbeat", "interval_seconds": 3600})
monitorID := m["id"].(string)
slug := uniq("iup-")
_, page := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Acme"})
pageID := page["id"].(string)
owner.do("PUT", "/api/v1/org/statuspages/"+pageID, map[string]any{"slug": slug, "title": "Acme", "published": true})
if st, body := owner.do("PUT", "/api/v1/org/statuspages/"+pageID+"/monitors", map[string]any{"monitors": []map[string]any{{"monitor_id": monitorID}}}); st != http.StatusNoContent && st != http.StatusOK {
t.Fatalf("list monitor on page = %d %v", st, body)
}
var incident uuid.UUID
if err := e.asSystem(t, func(tx pgx.Tx) error {
return tx.QueryRow(ctx, `INSERT INTO incidents (org_id, monitor_id, started_at, cause)
SELECT org_id, id, now() - interval '10 minutes', '{}' FROM monitors WHERE id = $1 RETURNING id`, monitorID).Scan(&incident)
}); err != nil {
t.Fatal(err)
}
base := "/api/v1/org/incidents/" + incident.String()
status := func() string {
_, body := owner.do("GET", base, nil)
s, _ := body["status"].(string)
return s
}
if s := status(); s != "" {
t.Errorf("status before any update = %q", s)
}
// Updates with a status, public or internal, and a title.
for _, bad := range []map[string]any{
{"status": "panicking", "message": "x"},
{"status": "investigating", "message": " "},
} {
st, body := owner.do("POST", base+"/updates", bad)
expect(t, "invalid update", st, body, http.StatusUnprocessableEntity, "invalid_input")
}
if st, body := owner.do("POST", base+"/updates", map[string]any{"status": "investigating", "message": "We are **looking** into it."}); st != http.StatusCreated {
t.Fatalf("post update = %d %v", st, body)
}
if st, body := owner.do("PATCH", base, map[string]any{"title": "Sign-in is slow"}); st != http.StatusNoContent {
t.Fatalf("set title = %d %v", st, body)
}
_, note := owner.do("POST", base+"/updates", map[string]any{"status": "identified", "message": "DB failover, ask Bob", "public": false})
if s := status(); s != "identified" {
t.Errorf("status = %q, want the latest update's", s)
}
_, list := owner.do("GET", "/api/v1/org/incidents", nil)
if first := list["incidents"].([]any)[0].(map[string]any); first["title"] != "Sign-in is slow" || first["status"] != "identified" {
t.Errorf("incident list = %v", first)
}
// Status pages show the title, the status and only public updates.
snap, err := e.statuspageClient(t, nil).Page(ctx, "", slug)
if err != nil {
t.Fatal(err)
}
var shown agentapi.PageIncident
for _, i := range snap.Incidents {
if i.Component == "Login" {
shown = i
}
}
if shown.Title != "Sign-in is slow" || shown.Status != "identified" || len(shown.Updates) != 1 || !strings.Contains(shown.Updates[0].Message, "looking") {
t.Errorf("status page incident = %+v", shown)
}
// The console page tells the story, internal notes marked.
_, _, html := owner.page("/incidents/" + incident.String())
for _, want := range []string{"Sign-in is slow", "looking", "DB failover", ">internal<", "Post update", "Outage began"} {
if !strings.Contains(html, want) {
t.Errorf("incident page lacks %q", want)
}
}
// Readers read, writers write.
viewer := e.joinVia(t, owner.invite("v@"+uniq("iu")+".test", owner.roleID("viewer")))
if st, _ := viewer.do("GET", base, nil); st != http.StatusOK {
t.Errorf("viewer reads incident = %d", st)
}
st, body := viewer.do("POST", base+"/updates", map[string]any{"status": "monitoring", "message": "x"})
expect(t, "viewer posts update", st, body, http.StatusForbidden, "forbidden")
if _, _, html := viewer.page("/incidents/" + incident.String()); strings.Contains(html, "Post update") {
t.Error("the viewer is offered the update form")
}
// Deleting an update falls back to the previous status; the monitor
// recovering resolves the incident whatever was said.
if st, _ := owner.do("DELETE", base+"/updates/"+note["id"].(string), nil); st != http.StatusNoContent || status() != "investigating" {
t.Errorf("after deleting the latest update status = %q", status())
}
if err := e.asSystem(t, func(tx pgx.Tx) error {
_, err := tx.Exec(ctx, `UPDATE incidents SET resolved_at = $2 WHERE id = $1`, incident, time.Now())
return err
}); err != nil {
t.Fatal(err)
}
if s := status(); s != "resolved" {
t.Errorf("status after recovery = %q", s)
}
if st, _ := owner.do("GET", "/api/v1/org/incidents/"+uuid.NewString(), nil); st != http.StatusNotFound {
t.Errorf("unknown incident = %d", st)
}
}
func TestCustomIncidentStatuses(t *testing.T) {
e := newEnv(t)
ctx := context.Background()
owner := e.browser(t)
owner.signup("o@"+uniq("cis")+".test", uniq("cis-"))
_, m := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Shop", "type": "heartbeat", "interval_seconds": 3600})
monitorID := m["id"].(string)
slug := uniq("cisp-")
_, page := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Acme"})
owner.do("PUT", "/api/v1/org/statuspages/"+page["id"].(string), map[string]any{"slug": slug, "title": "Acme", "published": true})
owner.do("PUT", "/api/v1/org/statuspages/"+page["id"].(string)+"/monitors", map[string]any{"monitors": []map[string]any{{"monitor_id": monitorID}}})
var incident uuid.UUID
if err := e.asSystem(t, func(tx pgx.Tx) error {
return tx.QueryRow(ctx, `INSERT INTO incidents (org_id, monitor_id, started_at, cause)
SELECT org_id, id, now() - interval '10 minutes', '{}' FROM monitors WHERE id = $1 RETURNING id`, monitorID).Scan(&incident)
}); err != nil {
t.Fatal(err)
}
for _, bad := range []map[string]any{{"name": "x", "color": "red"}, {"name": " ", "color": "#7c3aed"}} {
st, body := owner.do("POST", "/api/v1/org/incident-statuses", bad)
expect(t, "invalid status", st, body, http.StatusUnprocessableEntity, "invalid_input")
}
st, status := owner.do("POST", "/api/v1/org/incident-statuses", map[string]any{"name": "Waiting for provider", "color": "#7C3AED"})
if st != http.StatusCreated || status["color"] != "#7c3aed" {
t.Fatalf("create status = %d %v", st, status)
}
id := status["id"].(string)
st, body := owner.do("POST", "/api/v1/org/incident-statuses", map[string]any{"name": "Waiting for provider", "color": "#000000"})
expect(t, "duplicate name", st, body, http.StatusConflict, "status_name_taken")
viewer := e.joinVia(t, owner.invite("v@"+uniq("cis")+".test", owner.roleID("viewer")))
st, body = viewer.do("POST", "/api/v1/org/incident-statuses", map[string]any{"name": "Mine", "color": "#000000"})
expect(t, "viewer adds a status", st, body, http.StatusForbidden, "forbidden")
// Updates use it by its ID; it shows with name and color.
base := "/api/v1/org/incidents/" + incident.String()
if st, body := owner.do("POST", base+"/updates", map[string]any{"status": id, "message": "Provider is on it."}); st != http.StatusCreated || body["status_name"] != "Waiting for provider" {
t.Fatalf("update with own status = %d %v", st, body)
}
st, body = owner.do("POST", base+"/updates", map[string]any{"status": uuid.NewString(), "message": "x"})
expect(t, "update with an unknown status", st, body, http.StatusUnprocessableEntity, "invalid_input")
if _, inc := owner.do("GET", base, nil); inc["status"] != "custom" || inc["status_id"] != id || inc["status_color"] != "#7c3aed" {
t.Errorf("incident = %v", inc)
}
snap, err := e.statuspageClient(t, nil).Page(ctx, "", slug)
if err != nil {
t.Fatal(err)
}
if len(snap.Incidents) == 0 || snap.Incidents[0].StatusName != "Waiting for provider" || snap.Incidents[0].Updates[0].StatusColor != "#7c3aed" {
t.Errorf("status page incidents = %+v", snap.Incidents)
}
_, _, html := owner.page("/incidents/" + incident.String())
if !strings.Contains(html, "background-color:#7c3aed") || !strings.Contains(html, `