ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 11m21s
ci / test (pull_request) Successful in 13m31s
ci / build (pull_request) Successful in 18m20s
ci / image (pull_request) Successful in 5m47s
GET /api/v1/public/regions lists the platform regions with their location and whether a checker is online, without authentication and to any origin, for the website's front page. Each instance answers from memory for 30 seconds; organizations' own regions are never listed. On touch screens one finger scrolls the page and two move the map, the pins are larger, and a tap no longer leaves rows highlighted. The gesture hints are translated, and the regions table drops the key and owner columns on phones so the location fits. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
330 lines
13 KiB
Go
330 lines
13 KiB
Go
package api_test
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/buildinfo"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
)
|
|
|
|
func (b *browser) createRegion(key string) string {
|
|
b.t.Helper()
|
|
st, body := b.do("POST", "/api/v1/org/regions", map[string]any{"key": key, "name": "Office " + key})
|
|
if st != http.StatusCreated {
|
|
b.t.Fatalf("create region %s = %d %v", key, st, body)
|
|
}
|
|
return body["id"].(string)
|
|
}
|
|
|
|
func (b *browser) regionIDs() []string {
|
|
b.t.Helper()
|
|
_, body := b.do("GET", "/api/v1/org/regions", nil)
|
|
var ids []string
|
|
for _, r := range body["regions"].([]any) {
|
|
ids = append(ids, r.(map[string]any)["id"].(string))
|
|
}
|
|
return ids
|
|
}
|
|
|
|
func TestOrgRegionsAndAgents(t *testing.T) {
|
|
e := newEnv(t)
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
t.Cleanup(cancel)
|
|
go e.api.RunScheduler(ctx)
|
|
|
|
_, platformRegion := e.platformChecker(t, uniq("shared-"))
|
|
|
|
a := e.browser(t)
|
|
a.signup("a@"+uniq("a")+".test", uniq("org-a-"))
|
|
b := e.browser(t)
|
|
b.signup("b@"+uniq("b")+".test", uniq("org-b-"))
|
|
|
|
aOffice := a.createRegion("office")
|
|
bOffice := b.createRegion("office") // same key in another org is fine
|
|
|
|
t.Run("own regions first, then platform", func(t *testing.T) {
|
|
ids := a.regionIDs()
|
|
if len(ids) < 2 || ids[0] != aOffice {
|
|
t.Errorf("org A regions = %v, want %s first", ids, aOffice)
|
|
}
|
|
for _, id := range ids {
|
|
if id == bOffice {
|
|
t.Errorf("org A sees org B's region")
|
|
}
|
|
}
|
|
found := false
|
|
for _, id := range ids {
|
|
found = found || id == platformRegion
|
|
}
|
|
if !found {
|
|
t.Errorf("org A regions = %v, want platform region %s", ids, platformRegion)
|
|
}
|
|
})
|
|
|
|
t.Run("foreign regions are unusable", func(t *testing.T) {
|
|
st, body := b.do("POST", "/api/v1/org/monitors", map[string]any{
|
|
"name": "x", "type": "tcp", "target": "example.com:443", "interval_seconds": 60, "regions": []string{aOffice},
|
|
})
|
|
expect(t, "monitor in foreign region", st, body, http.StatusUnprocessableEntity, "invalid_region")
|
|
st, body = a.do("POST", "/api/v1/org/agents", map[string]any{"kind": "checker", "name": "x", "region_id": bOffice})
|
|
expect(t, "agent in foreign region", st, body, http.StatusUnprocessableEntity, "invalid_region")
|
|
st, body = a.do("POST", "/api/v1/org/agents", map[string]any{"kind": "checker", "name": "x", "region_id": platformRegion})
|
|
expect(t, "org agent in platform region", st, body, http.StatusUnprocessableEntity, "invalid_region")
|
|
st, body = a.do("DELETE", "/api/v1/org/regions/"+platformRegion, nil)
|
|
expect(t, "org deletes platform region", st, body, http.StatusNotFound, "not_found")
|
|
})
|
|
|
|
t.Run("org checker runs only its org's monitors", func(t *testing.T) {
|
|
st, body := a.do("POST", "/api/v1/org/agents", map[string]any{"kind": "checker", "name": "office-1", "region_id": aOffice})
|
|
if st != http.StatusCreated {
|
|
t.Fatalf("create org agent = %d %v", st, body)
|
|
}
|
|
checker, _ := e.startChecker(t, body["token"].(string))
|
|
|
|
target := httptest.NewServer(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {}))
|
|
defer target.Close()
|
|
st, body = a.do("POST", "/api/v1/org/monitors", map[string]any{
|
|
"name": "intranet", "type": "http", "target": target.URL, "interval_seconds": 60, "regions": []string{aOffice},
|
|
})
|
|
if st != http.StatusCreated {
|
|
t.Fatalf("create monitor = %d %v", st, body)
|
|
}
|
|
waitFor(t, 10*time.Second, "org checker reports", func() bool {
|
|
var n int
|
|
_ = e.dbPool.QueryRow(context.Background(), `SELECT count(*) FROM check_results WHERE checker_id = $1 AND region = $2`, checker, aOffice).Scan(&n)
|
|
return n > 0
|
|
})
|
|
st, body = a.do("DELETE", "/api/v1/org/regions/"+aOffice, nil)
|
|
expect(t, "delete region in use", st, body, http.StatusConflict, "region_in_use")
|
|
_, agents := a.do("GET", "/api/v1/org/agents", nil)
|
|
if n := len(agents["agents"].([]any)); n != 1 {
|
|
t.Errorf("org A agents = %d, want 1", n)
|
|
}
|
|
_, agents = b.do("GET", "/api/v1/org/agents", nil)
|
|
if n := len(agents["agents"].([]any)); n != 0 {
|
|
t.Errorf("org B agents = %d, want 0", n)
|
|
}
|
|
})
|
|
|
|
t.Run("platform regions are admin only", func(t *testing.T) {
|
|
st, body := a.do("POST", "/api/v1/admin/regions", map[string]any{"key": "global"})
|
|
expect(t, "non-admin creates platform region", st, body, http.StatusForbidden, "forbidden")
|
|
|
|
err := tenant.New(e.dbPool).Tx(context.Background(), tenant.Global(), func(q *store.Queries) error {
|
|
return q.MakeAdmin(context.Background(), a.userIDSelf())
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
key := uniq("glob-")
|
|
st, body = a.do("POST", "/api/v1/admin/regions", map[string]any{"key": key, "name": "Global"})
|
|
if st != http.StatusCreated || body["own"] != false {
|
|
t.Fatalf("admin creates platform region = %d %v", st, body)
|
|
}
|
|
global := body["id"].(string)
|
|
st, body = a.do("POST", "/api/v1/admin/agents", map[string]any{"kind": "checker", "name": "g1", "region_id": global})
|
|
expect(t, "admin creates platform agent", st, body, http.StatusCreated, "")
|
|
found := false
|
|
for _, id := range b.regionIDs() {
|
|
found = found || id == global
|
|
}
|
|
if !found {
|
|
t.Errorf("org B does not see the new platform region")
|
|
}
|
|
for _, path := range []string{"/admin", "/settings/regions"} {
|
|
if got := a.visit(path); got.Path != path {
|
|
t.Errorf("admin GET %s ended at %s", path, got.Path)
|
|
}
|
|
}
|
|
if got := b.visit("/admin"); got.Path != "/" {
|
|
t.Errorf("non-admin GET /admin ended at %s, want /", got.Path)
|
|
}
|
|
})
|
|
}
|
|
|
|
func (b *browser) userIDSelf() uuid.UUID {
|
|
b.t.Helper()
|
|
_, me := b.do("GET", "/api/v1/me", nil)
|
|
return uuid.MustParse(me["id"].(string))
|
|
}
|
|
|
|
func TestAgentInstallConsole(t *testing.T) {
|
|
e := newEnvWith(t, func(c *config.API) { c.PublicURL = "https://uptime.example.com" })
|
|
owner := e.browser(t)
|
|
owner.signup("o@"+uniq("inst")+".test", uniq("inst-"))
|
|
st, body := owner.do("POST", "/api/v1/org/agents", map[string]any{"kind": "statuspage", "name": "pages"})
|
|
if st != http.StatusCreated {
|
|
t.Fatalf("create agent = %d %v", st, body)
|
|
}
|
|
tok := body["token"].(string)
|
|
|
|
old := buildinfo.Version
|
|
buildinfo.Version = "v1.4.2"
|
|
t.Cleanup(func() { buildinfo.Version = old })
|
|
_, _, html := owner.page("/settings/regions")
|
|
for _, want := range []string{
|
|
"curl -fsSL https://uptime.example.com/install.sh | sudo sh -s -- {kind} --token {token}{manual}",
|
|
`data-channel="latest"`, "Update automatically", "}-statuspage:${UPTIME_AGENT_TAG",
|
|
"The platform runs v1.4.2.",
|
|
} {
|
|
if !strings.Contains(html, want) {
|
|
t.Errorf("the page lacks %q", want)
|
|
}
|
|
}
|
|
if strings.Contains(html, ">· outdated<") {
|
|
t.Error("an agent that never reported is marked outdated")
|
|
}
|
|
|
|
client := agentapi.NewClient(e.srv.URL, tok, uuid.New(), "v1.4.1").WithHTTPClient(e.srv.Client())
|
|
if _, err := client.Hello(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, html := owner.page("/settings/regions"); !strings.Contains(html, "v1.4.1") || !strings.Contains(html, "outdated") {
|
|
t.Error("an agent behind the platform is not marked outdated")
|
|
}
|
|
current := agentapi.NewClient(e.srv.URL, tok, uuid.New(), "v1.4.2").WithHTTPClient(e.srv.Client())
|
|
if _, err := current.Hello(context.Background()); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, html := owner.page("/settings/regions"); strings.Contains(html, "· outdated") {
|
|
t.Error("an agent on the platform's release is marked outdated")
|
|
}
|
|
}
|
|
|
|
func TestRegionLocations(t *testing.T) {
|
|
e := newEnv(t)
|
|
_, platformRegion := e.platformChecker(t, uniq("loc-"))
|
|
|
|
a := e.browser(t)
|
|
a.signup("a@"+uniq("a")+".test", uniq("org-a-"))
|
|
b := e.browser(t)
|
|
b.signup("b@"+uniq("b")+".test", uniq("org-b-"))
|
|
|
|
location := func(br *browser, id string) (any, any) {
|
|
t.Helper()
|
|
_, body := br.do("GET", "/api/v1/org/regions", nil)
|
|
for _, r := range body["regions"].([]any) {
|
|
if r := r.(map[string]any); r["id"] == id {
|
|
return r["latitude"], r["longitude"]
|
|
}
|
|
}
|
|
t.Fatalf("region %s not listed", id)
|
|
return nil, nil
|
|
}
|
|
|
|
st, body := a.do("POST", "/api/v1/org/regions", map[string]any{"key": "berlin", "latitude": 52.52, "longitude": 13.405})
|
|
if st != http.StatusCreated || body["latitude"] != 52.52 || body["longitude"] != 13.405 {
|
|
t.Fatalf("create region with location = %d %v", st, body)
|
|
}
|
|
berlin := body["id"].(string)
|
|
plain := a.createRegion("plain")
|
|
if lat, lon := location(a, plain); lat != nil || lon != nil {
|
|
t.Errorf("region without location lists %v, %v", lat, lon)
|
|
}
|
|
|
|
for _, tc := range []struct {
|
|
what string
|
|
body map[string]any
|
|
}{
|
|
{"latitude only", map[string]any{"latitude": 10.0}},
|
|
{"latitude out of range", map[string]any{"latitude": 91.0, "longitude": 0.0}},
|
|
{"longitude out of range", map[string]any{"latitude": 0.0, "longitude": -181.0}},
|
|
} {
|
|
st, body := a.do("PATCH", "/api/v1/org/regions/"+plain, tc.body)
|
|
expect(t, tc.what, st, body, http.StatusUnprocessableEntity, "invalid_location")
|
|
}
|
|
st, body = a.do("POST", "/api/v1/org/regions", map[string]any{"key": "half", "longitude": 3.0})
|
|
expect(t, "create with half a location", st, body, http.StatusUnprocessableEntity, "invalid_location")
|
|
|
|
st, body = a.do("PATCH", "/api/v1/org/regions/"+plain, map[string]any{"latitude": -33.8688, "longitude": 151.2093})
|
|
expect(t, "set location", st, body, http.StatusNoContent, "")
|
|
if lat, lon := location(a, plain); lat != -33.8688 || lon != 151.2093 {
|
|
t.Errorf("location after PATCH = %v, %v", lat, lon)
|
|
}
|
|
st, body = a.do("PATCH", "/api/v1/org/regions/"+berlin, map[string]any{"latitude": nil, "longitude": nil})
|
|
expect(t, "remove location", st, body, http.StatusNoContent, "")
|
|
if lat, lon := location(a, berlin); lat != nil || lon != nil {
|
|
t.Errorf("location after removal = %v, %v", lat, lon)
|
|
}
|
|
|
|
st, body = b.do("PATCH", "/api/v1/org/regions/"+plain, map[string]any{"latitude": 1.0, "longitude": 1.0})
|
|
expect(t, "other org sets location", st, body, http.StatusNotFound, "not_found")
|
|
st, body = a.do("PATCH", "/api/v1/org/regions/"+platformRegion, map[string]any{"latitude": 1.0, "longitude": 1.0})
|
|
expect(t, "org sets platform location", st, body, http.StatusNotFound, "not_found")
|
|
st, body = a.do("PATCH", "/api/v1/admin/regions/"+platformRegion, map[string]any{"latitude": 1.0, "longitude": 1.0})
|
|
expect(t, "non-admin sets platform location", st, body, http.StatusForbidden, "forbidden")
|
|
|
|
err := tenant.New(e.dbPool).Tx(context.Background(), tenant.Global(), func(q *store.Queries) error {
|
|
return q.MakeAdmin(context.Background(), a.userIDSelf())
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st, body = a.do("PATCH", "/api/v1/admin/regions/"+plain, map[string]any{"latitude": 1.0, "longitude": 1.0})
|
|
expect(t, "admin sets org region location", st, body, http.StatusNotFound, "not_found")
|
|
st, body = a.do("PATCH", "/api/v1/admin/regions/"+platformRegion, map[string]any{"latitude": 50.1109, "longitude": 8.6821})
|
|
expect(t, "admin sets platform location", st, body, http.StatusNoContent, "")
|
|
if lat, lon := location(b, platformRegion); lat != 50.1109 || lon != 8.6821 {
|
|
t.Errorf("platform location seen by org B = %v, %v", lat, lon)
|
|
}
|
|
|
|
t.Run("public list", func(t *testing.T) {
|
|
req, _ := http.NewRequest(http.MethodGet, a.base+"/api/v1/public/regions", nil)
|
|
req.Header.Set("Origin", "https://www.example.org")
|
|
// The browser's transport trusts the test server; no cookie jar, so no session.
|
|
resp, err := (&http.Client{Transport: a.hc.Transport}).Do(req)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
defer func() { _ = resp.Body.Close() }()
|
|
var body struct {
|
|
Regions []map[string]any `json:"regions"`
|
|
}
|
|
if err := json.NewDecoder(resp.Body).Decode(&body); err != nil || resp.StatusCode != http.StatusOK {
|
|
t.Fatalf("GET public regions = %d %v", resp.StatusCode, err)
|
|
}
|
|
if got := resp.Header.Get("Access-Control-Allow-Origin"); got != "*" {
|
|
t.Errorf("CORS header = %q, want *", got)
|
|
}
|
|
var found map[string]any
|
|
for _, r := range body.Regions {
|
|
if r["name"] == "Office plain" || r["key"] == "berlin" {
|
|
t.Errorf("public list shows an org region: %v", r)
|
|
}
|
|
if _, leaked := r["id"]; leaked {
|
|
t.Errorf("public list exposes region ids: %v", r)
|
|
}
|
|
if r["latitude"] == 50.1109 {
|
|
found = r
|
|
}
|
|
}
|
|
if _, isBool := found["online"].(bool); found == nil || found["longitude"] != 8.6821 || !isBool {
|
|
t.Errorf("platform region in public list = %v, want its location and online state", found)
|
|
}
|
|
})
|
|
|
|
_, _, html := a.page("/monitors/new")
|
|
if !strings.Contains(html, `data-region-map="select"`) || !strings.Contains(html, `data-lat="50.1109"`) || !strings.Contains(html, `data-lat="-33.8688"`) {
|
|
t.Errorf("monitor form lacks the region map or markers")
|
|
}
|
|
_, _, html = b.page("/monitors/new")
|
|
if !strings.Contains(html, `data-region-map="select"`) || strings.Contains(html, `data-lat="-33.8688"`) {
|
|
t.Errorf("org B monitor form: want the platform marker only")
|
|
}
|
|
_, _, html = a.page("/settings/regions")
|
|
if !strings.Contains(html, `data-region-map="edit"`) || !strings.Contains(html, `data-location-toggle="location-`+plain+`"`) {
|
|
t.Errorf("regions page lacks the map or the location form")
|
|
}
|
|
}
|