Files
uptimesebi/internal/api/console_test.go
T
Sebastian UnterschützandClaude Opus 5.5 fe5c4fb24c
ci / build (pull_request) Successful in 5m13s
ci / lint (pull_request) Successful in 5m45s
ci / image (pull_request) Successful in 6m20s
ci / test (pull_request) Successful in 11m46s
feat: sign up and sign in with platform identity providers
Platform admins register identity providers for the whole installation:
any OpenID Connect provider, checked by discovery, and GitHub or GitHub
Enterprise through OAuth, which reads the primary email GitHub verified.
Sign-in pages offer them; the sign-up page offers those open to sign-up,
also where email sign-up is closed for lack of a mail server.

Accounts are reached only through identities stored by subject. A new
identity signs up only with an email the provider confirmed, within the
per-address limit and sign-up mode, and never takes over an existing
account, including through an alias of its address: its owner links
the provider in the security settings. The account's TOTP still applies
after the provider. Unlinking refuses to remove the last way to sign in,
and removing a provider refuses while accounts depend on it.

The test browser takes the CSRF token from pages, as the console does.

Closes #114

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-23 01:26:14 +01:00

93 lines
3.1 KiB
Go

package api_test
import (
"context"
"io"
"net/http"
"net/http/httptest"
"regexp"
"strings"
"testing"
"time"
)
var csrfMeta = regexp.MustCompile(`<meta name="csrf-token" content="([^"]+)"`)
// page fetches path and returns the final status, path and body. Like the
// console's script, it takes the CSRF token from the page.
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)
if m := csrfMeta.FindSubmatch(body); m != nil {
b.csrf = string(m[1])
}
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")
}
}