feat: per-organization limits set by platform admins
Admins set default limits for monitors (all types), status pages and regions per monitor, whether an organization's own checker regions count towards the region limit, and how many organizations an account may own, and override them per organization or account. Defaults are not copied: changing one applies at once to everyone without an override. 0 means unlimited; self-hosted defaults are unlimited, the cloud allows three organizations per account. Creations count under a per-organization advisory lock, so concurrent requests cannot overshoot. A lowered limit keeps everything that exists: monitors already beyond the region limit stay editable but may not grow. Organizations see their usage in their settings and GET /org/limits; the admin panel lists organizations with owner, usage and limits next to their suspend and delete actions. Empty numeric form fields marked data-nullable are sent as null. Refs #111 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
This commit is contained in:
1 parent
90e74d091a
commit
e4ce619fbe
26 files changed
+2386
-82
No files matched your search
@@ -0,0 +1,157 @@
|
||||
package api_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"github.com/jackc/pgx/v5"
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
)
|
||||
|
||||
func TestLimits(t *testing.T) {
|
||||
e := newEnv(t)
|
||||
ctx := context.Background()
|
||||
reset := func() {
|
||||
_ = e.asSystem(t, func(tx pgx.Tx) error {
|
||||
_, err := tx.Exec(ctx, `DELETE FROM platform_settings WHERE key = 'limits'`)
|
||||
return err
|
||||
})
|
||||
}
|
||||
reset()
|
||||
t.Cleanup(reset)
|
||||
admin := e.browser(t)
|
||||
admin.signup("admin@"+uniq("lim")+".test", uniq("lim-admin-"))
|
||||
if err := tenant.New(e.dbPool).Tx(ctx, tenant.Global(), func(q *store.Queries) error {
|
||||
return q.MakeAdmin(ctx, admin.userIDSelf())
|
||||
}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
setDefaults := func(l map[string]any) {
|
||||
t.Helper()
|
||||
if st, body := admin.do("PUT", "/api/v1/admin/limits", l); st != http.StatusNoContent {
|
||||
t.Fatalf("set defaults = %d %v", st, body)
|
||||
}
|
||||
}
|
||||
setDefaults(map[string]any{"monitors": 2, "status_pages": 1, "regions_per_monitor": 1, "own_regions_count": false, "orgs_per_account": 3})
|
||||
|
||||
_, p1 := e.platformChecker(t, uniq("eu-"))
|
||||
_, p2 := e.platformChecker(t, uniq("us-"))
|
||||
owner := e.browser(t)
|
||||
orgSlug := uniq("lim-org-")
|
||||
owner.signup("o@"+uniq("lim")+".test", orgSlug)
|
||||
_, own := owner.do("POST", "/api/v1/org/regions", map[string]any{"key": uniq("office-"), "name": "Office"})
|
||||
o1 := own["id"].(string)
|
||||
monitor := func(regions ...string) (int, map[string]any) {
|
||||
return owner.do("POST", "/api/v1/org/monitors", map[string]any{
|
||||
"name": "m", "type": "tcp", "target": "example.com:443", "interval_seconds": 60, "regions": regions,
|
||||
})
|
||||
}
|
||||
|
||||
// Monitors, passive ones included.
|
||||
if st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "hb", "type": "heartbeat", "interval_seconds": 3600}); st != http.StatusCreated {
|
||||
t.Fatalf("heartbeat = %d %v", st, body)
|
||||
}
|
||||
if st, body := monitor(p1); st != http.StatusCreated {
|
||||
t.Fatalf("second monitor = %d %v", st, body)
|
||||
}
|
||||
st, body := monitor(p1)
|
||||
expect(t, "third monitor", st, body, http.StatusUnprocessableEntity, "monitor_limit")
|
||||
if !strings.Contains(body["message"].(string), "2") {
|
||||
t.Errorf("message lacks the limit: %v", body["message"])
|
||||
}
|
||||
_, usage := owner.do("GET", "/api/v1/org/limits", nil)
|
||||
if raw := mustJSON(usage); !strings.Contains(raw, `"monitors":2`) || !strings.Contains(raw, `"usage":{"monitors":2,"status_pages":0}`) {
|
||||
t.Errorf("org limits = %s", raw)
|
||||
}
|
||||
|
||||
// An override lifts the monitor limit; own regions do not count.
|
||||
_, orgs := admin.do("GET", "/api/v1/admin/orgs?q="+orgSlug, nil)
|
||||
org := orgs["orgs"].([]any)[0].(map[string]any)
|
||||
orgID := org["id"].(string)
|
||||
setOrg := func(l map[string]any) {
|
||||
t.Helper()
|
||||
if st, body := admin.do("PUT", "/api/v1/admin/orgs/"+orgID+"/limits", l); st != http.StatusNoContent {
|
||||
t.Fatalf("set org limits = %d %v", st, body)
|
||||
}
|
||||
}
|
||||
setOrg(map[string]any{"monitors": 10, "status_pages": nil, "regions_per_monitor": nil, "own_regions_count": nil})
|
||||
st, body = monitor(p1, p2)
|
||||
expect(t, "two platform regions", st, body, http.StatusUnprocessableEntity, "platform_region_limit")
|
||||
st, mixed := monitor(p1, o1)
|
||||
expect(t, "platform and own region", st, mixed, http.StatusCreated, "")
|
||||
|
||||
// Once own regions count, the existing monitor keeps both but may not
|
||||
// grow, and new ones are refused.
|
||||
setOrg(map[string]any{"monitors": 10, "own_regions_count": "true"})
|
||||
st, body = monitor(p1, o1)
|
||||
expect(t, "own region counted", st, body, http.StatusUnprocessableEntity, "region_limit")
|
||||
st, body = owner.do("PUT", "/api/v1/org/monitors/"+mixed["id"].(string), map[string]any{
|
||||
"name": "renamed", "type": "tcp", "target": "example.com:443", "interval_seconds": 60, "regions": []string{p1, o1},
|
||||
})
|
||||
expect(t, "edit a monitor beyond a lowered limit", st, body, http.StatusOK, "")
|
||||
st, body = owner.do("PUT", "/api/v1/org/monitors/"+mixed["id"].(string), map[string]any{
|
||||
"name": "renamed", "type": "tcp", "target": "example.com:443", "interval_seconds": 60, "regions": []string{p1, p2, o1},
|
||||
})
|
||||
expect(t, "grow a monitor beyond the limit", st, body, http.StatusUnprocessableEntity, "region_limit")
|
||||
|
||||
// Status pages; concurrent creations cannot overshoot.
|
||||
var wg sync.WaitGroup
|
||||
var mu sync.Mutex
|
||||
created := 0
|
||||
for range 5 {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
defer wg.Done()
|
||||
st, _ := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": uniq("p-"), "title": "x"})
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if st == http.StatusCreated {
|
||||
created++
|
||||
}
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
if created != 1 {
|
||||
t.Errorf("%d status pages created under a limit of 1", created)
|
||||
}
|
||||
|
||||
// Defaults change for every organization without an override, at once.
|
||||
other := e.browser(t)
|
||||
other.signup("x@"+uniq("lim")+".test", uniq("lim-x-"))
|
||||
setDefaults(map[string]any{"monitors": 1})
|
||||
if st, _ := other.do("POST", "/api/v1/org/monitors", map[string]any{"name": "hb", "type": "heartbeat", "interval_seconds": 3600}); st != http.StatusCreated {
|
||||
t.Fatalf("first monitor = %d", st)
|
||||
}
|
||||
st, body = other.do("POST", "/api/v1/org/monitors", map[string]any{"name": "hb2", "type": "heartbeat", "interval_seconds": 3600})
|
||||
expect(t, "default lowered", st, body, http.StatusUnprocessableEntity, "monitor_limit")
|
||||
setDefaults(map[string]any{"monitors": 0})
|
||||
st, body = other.do("POST", "/api/v1/org/monitors", map[string]any{"name": "hb2", "type": "heartbeat", "interval_seconds": 3600})
|
||||
expect(t, "default unlimited", st, body, http.StatusCreated, "")
|
||||
|
||||
// Account limits and the admin list.
|
||||
ownerID := org["owner"].(map[string]any)["id"].(string)
|
||||
if st, body := admin.do("PUT", "/api/v1/admin/users/"+ownerID+"/limits", map[string]any{"orgs": 5}); st != http.StatusNoContent {
|
||||
t.Fatalf("set user limit = %d %v", st, body)
|
||||
}
|
||||
_, orgs = admin.do("GET", "/api/v1/admin/orgs?q="+orgSlug, nil)
|
||||
raw := mustJSON(orgs)
|
||||
for _, want := range []string{`"org_limit":5`, `"orgs":1`, `"monitors":10`, `"own_regions_count":true`, `"usage":{"monitors":3,"status_pages":1}`} {
|
||||
if !strings.Contains(raw, want) {
|
||||
t.Errorf("admin org list lacks %s: %s", want, raw)
|
||||
}
|
||||
}
|
||||
if st, _ := owner.do("GET", "/api/v1/admin/orgs", nil); st != http.StatusForbidden {
|
||||
t.Errorf("non-admin org list = %d", st)
|
||||
}
|
||||
if _, _, html := admin.page("/admin?oq=" + orgSlug); !strings.Contains(html, `id="orgs"`) || !strings.Contains(html, orgSlug) {
|
||||
t.Error("the admin page lacks the organization list")
|
||||
}
|
||||
if _, _, html := owner.page("/settings/org"); !strings.Contains(html, "3 / 10") {
|
||||
t.Error("the organization settings lack the usage")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user