ci / lint (push) Skipped
ci / test (push) Skipped
ci / build (push) Skipped
ci / image (push) Skipped
ci / lint (pull_request) Successful in 9m21s
ci / test (pull_request) Successful in 13m53s
ci / build (pull_request) Successful in 11m22s
ci / image (pull_request) Successful in 4m36s
Terms of service join the legal notice and privacy policy, for the platform only. Every account accepts the platform's privacy policy and terms: on sign-up (also through an invitation) by ticking a box for the versions shown, and after a sign-in through a provider right away. When an admin saves a material change with "everyone has to accept again", the document gets a new version and each account is asked at its next request; corrections ask nobody. Acceptances are kept per version with their time, as proof, and are part of the data export. Until an account accepted, and while it is suspended, it can read the documents, export its data, delete itself and sign out, nothing else: pages lead to /consent or /account/suspended, other API calls answer 403 consent_required or account_suspended. Suspended accounts therefore sign in again, by password or provider; their API tokens stay blocked. Deleting an account deletes the organizations it owns, monitors and all, also when they have other members (each organization has one owner); it no longer refuses. The account settings name those organizations first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
194 lines
8.2 KiB
Go
194 lines
8.2 KiB
Go
package api_test
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"net/http"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/agentapi"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
|
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
|
)
|
|
|
|
const testPassword = "correct horse battery"
|
|
|
|
func TestModeration(t *testing.T) {
|
|
e := newEnv(t)
|
|
ctx := context.Background()
|
|
admin := e.browser(t)
|
|
admin.signup("admin@"+uniq("mod")+".test", uniq("mod-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)
|
|
}
|
|
|
|
ownerEmail := "owner@" + uniq("mod") + ".test"
|
|
owner := e.browser(t)
|
|
owner.signup(ownerEmail, uniq("mod-org-"))
|
|
slug := uniq("acme-")
|
|
_, page := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Acme Status", "published": true})
|
|
_, draft := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": uniq("draft-"), "title": "Draft", "published": false})
|
|
_, hb := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Backup", "type": "heartbeat", "interval_seconds": 3600})
|
|
_, region := e.platformChecker(t, uniq("eu-"))
|
|
_, web := owner.do("POST", "/api/v1/org/monitors", map[string]any{"name": "Web", "type": "tcp", "target": "example.com:443", "interval_seconds": 60, "regions": []string{region}})
|
|
scheduled := func() bool {
|
|
var rows []store.ListEnabledMonitorsForScheduleRow
|
|
_ = tenant.New(e.dbPool).Tx(ctx, tenant.System(), func(q *store.Queries) error {
|
|
var err error
|
|
rows, err = q.ListEnabledMonitorsForSchedule(ctx)
|
|
return err
|
|
})
|
|
for _, r := range rows {
|
|
if r.ID.String() == web["id"] {
|
|
return true
|
|
}
|
|
}
|
|
return false
|
|
}
|
|
_, tok := owner.do("POST", "/api/v1/org/tokens", map[string]any{"name": "ci", "permissions": []string{"monitors.read"}})
|
|
api := &browser{t: t, base: owner.base, hc: e.srv.Client(), bearer: tok["token"].(string)}
|
|
sp := e.statuspageClient(t, nil)
|
|
served := func() bool {
|
|
_, err := sp.Page(ctx, "", slug)
|
|
if err != nil && !errors.Is(err, agentapi.ErrNotFound) {
|
|
t.Fatal(err)
|
|
}
|
|
return err == nil
|
|
}
|
|
// Pings of paused monitors are answered, so cron jobs do not fail, but
|
|
// not recorded.
|
|
recorded := func() bool {
|
|
resp, err := e.srv.Client().Post(e.srv.URL+"/hb/"+hb["target"].(string), "text/plain", nil)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
b, _ := io.ReadAll(resp.Body)
|
|
_ = resp.Body.Close()
|
|
return resp.StatusCode == http.StatusOK && !strings.Contains(string(b), "paused")
|
|
}
|
|
|
|
// Only admins see the list; it holds published pages with their
|
|
// organization and owner, and search narrows it.
|
|
if st, _ := owner.do("GET", "/api/v1/admin/status-pages", nil); st != http.StatusForbidden {
|
|
t.Errorf("non-admin listing = %d", st)
|
|
}
|
|
_, list := admin.do("GET", "/api/v1/admin/status-pages?q="+slug, nil)
|
|
raw := mustJSON(list)
|
|
if !strings.Contains(raw, slug) || !strings.Contains(raw, ownerEmail) || strings.Contains(raw, draft["slug"].(string)) {
|
|
t.Fatalf("list = %s", raw)
|
|
}
|
|
pg := list["status_pages"].([]any)[0].(map[string]any)
|
|
orgID := pg["org"].(map[string]any)["id"].(string)
|
|
userID := pg["owner"].(map[string]any)["id"].(string)
|
|
if _, other := admin.do("GET", "/api/v1/admin/status-pages?q=no-such-page-"+slug, nil); len(other["status_pages"].([]any)) != 0 {
|
|
t.Error("search does not narrow the list")
|
|
}
|
|
if !served() {
|
|
t.Fatal("the page is not served before suspension")
|
|
}
|
|
|
|
// A suspended page goes offline and its organization learns why.
|
|
suspend := func(path string, on bool, reason string) {
|
|
t.Helper()
|
|
if st, body := admin.do("PUT", "/api/v1/admin/"+path+"/suspension", map[string]any{"suspended": on, "reason": reason}); st != http.StatusNoContent {
|
|
t.Fatalf("suspend %s = %d %v", path, st, body)
|
|
}
|
|
}
|
|
suspend("status-pages/"+page["id"].(string), true, "phishing")
|
|
if served() {
|
|
t.Error("a suspended page is served")
|
|
}
|
|
_, pages := owner.do("GET", "/api/v1/org/statuspages", nil)
|
|
if raw := mustJSON(pages); !strings.Contains(raw, `"suspended":true`) || !strings.Contains(raw, `"suspended_reason":"phishing"`) {
|
|
t.Errorf("the organization does not see the suspension: %s", raw)
|
|
}
|
|
if _, _, html := owner.page("/statuspages"); !strings.Contains(html, "suspended by the operator") {
|
|
t.Error("the page list does not mark the suspension")
|
|
}
|
|
suspend("status-pages/"+page["id"].(string), false, "")
|
|
if !served() {
|
|
t.Error("a reinstated page is not served")
|
|
}
|
|
|
|
// A suspended organization is locked out, checks and pages stop.
|
|
suspend("orgs/"+orgID, true, "unpaid")
|
|
st, body := owner.do("GET", "/api/v1/org/monitors", nil)
|
|
expect(t, "member of a suspended org", st, body, http.StatusForbidden, "org_suspended")
|
|
st, body = api.do("GET", "/api/v1/org/monitors", nil)
|
|
expect(t, "token of a suspended org", st, body, http.StatusForbidden, "org_suspended")
|
|
if served() {
|
|
t.Error("a suspended organization's page is served")
|
|
}
|
|
if recorded() {
|
|
t.Error("a heartbeat of a suspended org is recorded")
|
|
}
|
|
if scheduled() {
|
|
t.Error("monitors of a suspended org are scheduled")
|
|
}
|
|
if _, _, html := owner.page("/"); !strings.Contains(html, "The operator suspended this organization") || !strings.Contains(html, "unpaid") {
|
|
t.Error("the console does not explain the suspension")
|
|
}
|
|
suspend("orgs/"+orgID, false, "")
|
|
if st, _ := owner.do("GET", "/api/v1/org/monitors", nil); st != http.StatusOK || !scheduled() {
|
|
t.Errorf("member of a reinstated org = %d, scheduled = %v", st, scheduled())
|
|
}
|
|
if !recorded() {
|
|
t.Error("a heartbeat after reinstatement is not recorded")
|
|
}
|
|
|
|
// A suspended account is signed out, its tokens stop working, and
|
|
// signing in again only lets it export or delete its data.
|
|
suspend("users/"+userID, true, "")
|
|
if st, _ := owner.do("GET", "/api/v1/me", nil); st != http.StatusUnauthorized {
|
|
t.Errorf("session of a suspended account = %d", st)
|
|
}
|
|
if st, _ := api.do("GET", "/api/v1/org/monitors", nil); st != http.StatusUnauthorized {
|
|
t.Errorf("token of a suspended account = %d", st)
|
|
}
|
|
fresh := e.browser(t)
|
|
if st, body := fresh.do("POST", "/api/v1/auth/login", map[string]any{"email": ownerEmail, "password": testPassword}); st != http.StatusOK {
|
|
t.Errorf("sign-in of a suspended account = %d %v", st, body)
|
|
}
|
|
st, body = fresh.do("GET", "/api/v1/org/monitors", nil)
|
|
expect(t, "API of a suspended account", st, body, http.StatusForbidden, "account_suspended")
|
|
if st, _ := e.browser(t).do("POST", "/api/v1/auth/login", map[string]any{"email": ownerEmail, "password": "wrong password"}); st != http.StatusUnauthorized {
|
|
t.Error("a wrong password reveals the suspension")
|
|
}
|
|
suspend("users/"+userID, false, "")
|
|
if st, _ := fresh.do("POST", "/api/v1/auth/login", map[string]any{"email": ownerEmail, "password": testPassword}); st != http.StatusOK {
|
|
t.Errorf("sign-in after reinstatement = %d", st)
|
|
}
|
|
|
|
// Admins are out of reach, also for themselves.
|
|
st, body = admin.do("PUT", "/api/v1/admin/users/"+admin.userIDSelf().String()+"/suspension", map[string]any{"suspended": true})
|
|
expect(t, "suspend an admin", st, body, http.StatusConflict, "target_is_admin")
|
|
st, body = admin.do("DELETE", "/api/v1/admin/users/"+admin.userIDSelf().String(), nil)
|
|
expect(t, "delete an admin", st, body, http.StatusConflict, "target_is_admin")
|
|
|
|
// Deleting: page, organization, account.
|
|
if st, _ := admin.do("DELETE", "/api/v1/admin/status-pages/"+page["id"].(string), nil); st != http.StatusNoContent || served() {
|
|
t.Errorf("delete page = %d", st)
|
|
}
|
|
if st, _ := admin.do("DELETE", "/api/v1/admin/orgs/"+orgID, nil); st != http.StatusNoContent {
|
|
t.Errorf("delete org = %d", st)
|
|
}
|
|
if st, body := admin.do("DELETE", "/api/v1/admin/users/"+userID, nil); st != http.StatusNoContent {
|
|
t.Errorf("delete account = %d %v", st, body)
|
|
}
|
|
if st, _ := e.browser(t).do("POST", "/api/v1/auth/login", map[string]any{"email": ownerEmail, "password": testPassword}); st != http.StatusUnauthorized {
|
|
t.Error("the deleted account can still sign in")
|
|
}
|
|
if st, _ := admin.do("DELETE", "/api/v1/admin/orgs/"+orgID, nil); st != http.StatusNotFound {
|
|
t.Error("deleting a deleted org is not 404")
|
|
}
|
|
|
|
// The admin page shows the list.
|
|
if _, _, html := admin.page("/admin/status-pages"); !strings.Contains(html, `id="moderation"`) {
|
|
t.Error("the admin page lacks the status page list")
|
|
}
|
|
}
|