Files
uptimesebi/internal/api/console_test.go
T
Sebastian UnterschützandClaude Opus 5.5 b3f19e20ad feat(web): show monitors as a group tree and limit roles to groups
The dashboard nests groups with an aggregated status, the monitor editor
offers the groups one may write to, a page manages groups, and members
can be assigned roles limited to a group or a status page. Settings move
to their own page to keep the navigation short.

Closes #57

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-22 09:19:55 +01:00

86 lines
2.9 KiB
Go

package api_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"strings"
"testing"
"time"
)
// page fetches path and returns the final status, path and body.
func (b *browser) page(path string) (int, string, string) {
b.t.Helper()
resp, err := b.hc.Get(b.base + path)
if err != nil {
b.t.Fatalf("GET %s failed: %v", path, err)
}
defer func() { _ = resp.Body.Close() }()
body, _ := io.ReadAll(resp.Body)
return resp.StatusCode, resp.Request.URL.Path, string(body)
}
func TestConsolePages(t *testing.T) {
e := newEnv(t)
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
go e.api.RunScheduler(ctx)
target := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadGateway)
}))
defer target.Close()
tok, region := e.platformChecker(t, uniq("eu-"))
e.startChecker(t, tok)
owner := e.browser(t)
owner.signup("o@"+uniq("p")+".test", uniq("pages-"))
st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{
"name": "Checkout API", "type": "http", "target": target.URL, "interval_seconds": 60,
"confirmations": 1, "regions": []string{region}, "config": map[string]any{"keyword": "ok"},
})
if st != http.StatusCreated {
t.Fatalf("create monitor = %d %v", st, body)
}
id := body["id"].(string)
waitFor(t, 10*time.Second, "incident opened", func() bool {
_, body := owner.do("GET", "/api/v1/org/incidents", nil)
return len(body["incidents"].([]any)) == 1
})
owner.do("POST", "/api/v1/org/channels", map[string]any{"name": "Hook", "type": "webhook", "url": "https://hooks.example.test/x"})
pages := map[string][]string{
"/": {"Checkout API", "<svg"},
"/monitors/" + id: {"Checkout API", "unexpected status 502"},
"/monitors/" + id + "/edit": {`value="Checkout API"`, `name="config.keyword"`, `value="ok"`},
"/monitors/new": {`name="regions"`},
"/incidents": {"Checkout API", "unexpected status 502"},
"/settings/channels": {"hooks.example.test"},
"/settings": {`href="/settings/members"`, `href="/groups"`},
}
for path, wants := range pages {
st, final, html := owner.page(path)
if st != http.StatusOK || final != path {
t.Errorf("GET %s = %d at %s", path, st, final)
continue
}
for _, w := range wants {
if !strings.Contains(html, w) {
t.Errorf("GET %s: page lacks %q", path, w)
}
}
}
viewer := e.joinVia(t, owner.invite("v@"+uniq("v")+".test", owner.roleID("viewer")))
for path, want := range map[string]string{"/monitors/new": "/", "/monitors/" + id + "/edit": "/", "/monitors/" + id: "/monitors/" + id} {
if _, final, _ := viewer.page(path); final != want {
t.Errorf("viewer GET %s ended at %s, want %s", path, final, want)
}
}
if _, _, html := viewer.page("/"); strings.Contains(html, "/monitors/new") {
t.Error("viewer dashboard offers creating monitors")
}
}