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
158 lines
6.8 KiB
Go
158 lines
6.8 KiB
Go
package api_test
|
|
|
|
import (
|
|
"context"
|
|
"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"
|
|
)
|
|
|
|
func TestLegalDocuments(t *testing.T) {
|
|
e := newEnv(t)
|
|
admin := e.browser(t)
|
|
admin.signup("a@"+uniq("lg")+".test", uniq("lg-"))
|
|
err := tenant.New(e.dbPool).Tx(context.Background(), tenant.Global(), func(q *store.Queries) error {
|
|
return q.MakeAdmin(context.Background(), admin.userIDSelf())
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { clearPlatformLegal(admin) })
|
|
|
|
imprint := "# Impressum\n\nBetreiber GmbH <script>alert(1)</script>"
|
|
st, body := admin.do("PUT", "/api/v1/admin/legal/imprint", map[string]any{"markdown": imprint})
|
|
expect(t, "admin sets imprint", st, body, http.StatusNoContent, "")
|
|
st, body = admin.do("PUT", "/api/v1/admin/legal/privacy", map[string]any{"url": "javascript:alert(1)"})
|
|
expect(t, "javascript link", st, body, http.StatusUnprocessableEntity, "invalid_legal_url")
|
|
st, body = admin.do("PUT", "/api/v1/admin/legal/privacy", map[string]any{"url": "https://operator.example/privacy"})
|
|
expect(t, "admin sets privacy link", st, body, http.StatusNoContent, "")
|
|
admin.acceptConsent()
|
|
st, body = admin.do("PUT", "/api/v1/admin/legal/cookies", map[string]any{"markdown": "x"})
|
|
expect(t, "unknown kind", st, body, http.StatusNotFound, "not_found")
|
|
|
|
// The console footer links them, also before signing in.
|
|
_, _, login := e.browser(t).page("/login")
|
|
if !strings.Contains(login, `href="/legal/imprint"`) || !strings.Contains(login, `href="https://operator.example/privacy"`) {
|
|
t.Error("login page footer lacks the legal links")
|
|
}
|
|
st, _, html := e.browser(t).page("/legal/imprint")
|
|
if st != http.StatusOK || !strings.Contains(html, "<h1>Impressum</h1>") || strings.Contains(html, "<script>alert") {
|
|
t.Errorf("GET /legal/imprint = %d, rendered %q", st, html)
|
|
}
|
|
if !strings.Contains(html, `href="/login"`) {
|
|
t.Error("the imprint offers visitors no way to sign in")
|
|
}
|
|
// Signed in, the imprint keeps the console navigation.
|
|
if _, _, html := admin.page("/legal/imprint"); !strings.Contains(html, `href="/monitors`) && !strings.Contains(html, `href="/incidents"`) {
|
|
t.Error("the imprint lacks the console navigation for signed-in users")
|
|
}
|
|
|
|
// Status pages fall back to the platform's documents.
|
|
owner := e.browser(t)
|
|
owner.signup("o@"+uniq("lg")+".test", uniq("lg-"))
|
|
slug := uniq("lgp-")
|
|
_, page := owner.do("POST", "/api/v1/org/statuspages", map[string]any{"slug": slug, "title": "Acme"})
|
|
owner.do("PUT", "/api/v1/org/statuspages/"+page["id"].(string), map[string]any{"slug": slug, "title": "Acme", "published": true})
|
|
client := e.statuspageClient(t, nil)
|
|
legalOf := func() map[string]agentapi.LegalDoc {
|
|
snap, err := client.Page(context.Background(), "", slug)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
out := map[string]agentapi.LegalDoc{}
|
|
for _, d := range snap.Legal {
|
|
out[d.Kind] = d
|
|
}
|
|
return out
|
|
}
|
|
if got := legalOf(); got["imprint"].Markdown != imprint || got["privacy"].URL != "https://operator.example/privacy" {
|
|
t.Errorf("snapshot legal = %+v, want the platform's", got)
|
|
}
|
|
|
|
// An organization's own documents win.
|
|
st, body = owner.do("PUT", "/api/v1/org/legal/imprint", map[string]any{"markdown": "# Acme GmbH"})
|
|
expect(t, "org sets imprint", st, body, http.StatusNoContent, "")
|
|
if got := legalOf(); got["imprint"].Markdown != "# Acme GmbH" || got["privacy"].URL != "https://operator.example/privacy" {
|
|
t.Errorf("snapshot legal = %+v, want own imprint and platform privacy", got)
|
|
}
|
|
_, list := owner.do("GET", "/api/v1/org/legal", nil)
|
|
if !strings.Contains(mustJSON(list), "Acme GmbH") || strings.Contains(mustJSON(list), "operator.example") {
|
|
t.Errorf("org legal list = %v, want only its own documents", list)
|
|
}
|
|
st, _ = owner.do("PUT", "/api/v1/org/legal/imprint", map[string]any{})
|
|
if got := legalOf(); st != http.StatusNoContent || got["imprint"].Markdown != imprint {
|
|
t.Errorf("after removing the own imprint snapshot = %+v", got)
|
|
}
|
|
|
|
viewer := e.joinVia(t, owner.invite("v@"+uniq("lg")+".test", owner.roleID("viewer")))
|
|
if st, _ := viewer.do("PUT", "/api/v1/org/legal/imprint", map[string]any{"markdown": "x"}); st != http.StatusForbidden {
|
|
t.Errorf("viewer sets imprint = %d, want 403", st)
|
|
}
|
|
if st, _ := owner.do("PUT", "/api/v1/admin/legal/imprint", map[string]any{"markdown": "x"}); st != http.StatusForbidden {
|
|
t.Errorf("non-admin sets platform imprint = %d, want 403", st)
|
|
}
|
|
for path, want := range map[string]string{"/admin/legal": "Platform legal notice", "/settings/legal": "Legal notice & privacy"} {
|
|
b := admin
|
|
if path == "/settings/legal" {
|
|
b = owner
|
|
}
|
|
if st, _, html := b.page(path); st != http.StatusOK || !strings.Contains(html, want) {
|
|
t.Errorf("GET %s = %d, lacks %q", path, st, want)
|
|
}
|
|
}
|
|
}
|
|
|
|
// A change on one replica shows on another with the next request.
|
|
func TestLegalChangeReachesOtherReplicas(t *testing.T) {
|
|
a := newEnv(t)
|
|
b := newEnv(t)
|
|
admin := a.browser(t)
|
|
admin.signup("a@"+uniq("lr")+".test", uniq("lr-"))
|
|
err := tenant.New(a.dbPool).Tx(context.Background(), tenant.Global(), func(q *store.Queries) error {
|
|
return q.MakeAdmin(context.Background(), admin.userIDSelf())
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { clearPlatformLegal(admin) })
|
|
admin.do("PUT", "/api/v1/admin/legal/imprint", map[string]any{})
|
|
|
|
visitor := b.browser(t)
|
|
if _, _, html := visitor.page("/login"); strings.Contains(html, `href="/legal/imprint"`) {
|
|
t.Fatal("imprint linked before it exists")
|
|
}
|
|
admin.do("PUT", "/api/v1/admin/legal/imprint", map[string]any{"markdown": "# Impressum"})
|
|
if _, _, html := visitor.page("/login"); !strings.Contains(html, `href="/legal/imprint"`) {
|
|
t.Error("the other replica still serves the old footer")
|
|
}
|
|
}
|
|
|
|
func TestMarkdownPreview(t *testing.T) {
|
|
e := newEnv(t)
|
|
if st, _ := e.browser(t).do("POST", "/api/v1/markdown/preview", map[string]any{"markdown": "x"}); st != http.StatusUnauthorized {
|
|
t.Errorf("anonymous preview = %d, want 401", st)
|
|
}
|
|
b := e.browser(t)
|
|
b.signup("p@"+uniq("md")+".test", uniq("md-"))
|
|
st, body := b.do("POST", "/api/v1/markdown/preview", map[string]any{"markdown": "**hi** <script>x</script> "})
|
|
html, _ := body["html"].(string)
|
|
if st != http.StatusOK || !strings.Contains(html, "<strong>hi</strong>") || strings.Contains(html, "<script") || strings.Contains(html, "<img") {
|
|
t.Errorf("preview = %d %q", st, html)
|
|
}
|
|
}
|
|
|
|
// clearPlatformLegal removes the platform's documents again, so other tests
|
|
// sign up without consent.
|
|
func clearPlatformLegal(admin *browser) {
|
|
admin.acceptConsent()
|
|
for _, kind := range []string{"imprint", "privacy", "terms"} {
|
|
if st, body := admin.do("PUT", "/api/v1/admin/legal/"+kind, map[string]any{}); st != http.StatusNoContent {
|
|
admin.t.Errorf("clearing platform %s = %d %v", kind, st, body)
|
|
}
|
|
}
|
|
}
|