feat(limits): limit the monitors shown on one status page
Platform admins set a default for how many monitors a status page may show, and an override per organization, like the other limits. Saving a page with more gives 422 page_monitor_limit; a page beyond a lowered limit keeps its monitors but cannot grow. The page editor names the limit and the organization's usage lists it. 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
4cde0f7e56
commit
ca852c8299
17 files changed
+611
-416
No files matched your search
@@ -155,3 +155,68 @@ func TestLimits(t *testing.T) {
|
||||
t.Error("the organization settings lack the usage")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonitorsPerStatusPageLimit(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("mpp")+".test", uniq("mpp-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)
|
||||
}
|
||||
if st, body := admin.do("PUT", "/api/v1/admin/limits", map[string]any{"monitors_per_page": 2}); st != http.StatusNoContent {
|
||||
t.Fatalf("set defaults = %d %v", st, body)
|
||||
}
|
||||
|
||||
owner := e.browser(t)
|
||||
owner.signup("o@"+uniq("mpp")+".test", uniq("mpp-"))
|
||||
var ids []map[string]any
|
||||
for range 3 {
|
||||
_, m := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "hb", "type": "heartbeat", "interval_seconds": 3600})
|
||||
ids = append(ids, map[string]any{"monitor_id": m["id"]})
|
||||
}
|
||||
_, page := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": uniq("mpp-"), "title": "Status"})
|
||||
set := func(n int) (int, map[string]any) {
|
||||
return owner.do("PUT", "/api/v1/org/statuspages/"+page["id"].(string)+"/monitors", map[string]any{"monitors": ids[:n]})
|
||||
}
|
||||
if st, body := set(2); st != http.StatusNoContent {
|
||||
t.Fatalf("two monitors = %d %v", st, body)
|
||||
}
|
||||
st, body := set(3)
|
||||
expect(t, "three monitors", st, body, http.StatusUnprocessableEntity, "page_monitor_limit")
|
||||
if _, _, html := owner.page("/statuspages/" + page["id"].(string)); !strings.Contains(html, "at most 2 monitors") {
|
||||
t.Error("the editor does not name the limit")
|
||||
}
|
||||
|
||||
// An override for the organization raises it; lowered again, the page
|
||||
// keeps its monitors but cannot grow.
|
||||
org := owner.orgID()
|
||||
if st, body := admin.do("PUT", "/api/v1/admin/orgs/"+org+"/limits", map[string]any{"monitors_per_page": 3}); st != http.StatusNoContent {
|
||||
t.Fatalf("override = %d %v", st, body)
|
||||
}
|
||||
if st, body := set(3); st != http.StatusNoContent {
|
||||
t.Fatalf("three monitors with the override = %d %v", st, body)
|
||||
}
|
||||
admin.do("PUT", "/api/v1/admin/orgs/"+org+"/limits", map[string]any{"monitors_per_page": 1})
|
||||
if st, body := set(3); st != http.StatusNoContent {
|
||||
t.Errorf("saving a page beyond a lowered limit = %d %v", st, body)
|
||||
}
|
||||
if st, body := set(2); st != http.StatusNoContent {
|
||||
t.Errorf("shrinking a page beyond a lowered limit = %d %v", st, body)
|
||||
}
|
||||
st, body = set(3)
|
||||
expect(t, "growing again beyond the lowered limit", st, body, http.StatusUnprocessableEntity, "page_monitor_limit")
|
||||
if _, _, html := admin.page("/admin"); !strings.Contains(html, "Monitors per status page") {
|
||||
t.Error("the admin page does not offer the limit")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user