The footer cache expired after 30 seconds, so other replicas showed old links for that long. A version counter in Redis, bumped after each change, now tells every replica to reload with its next request. Refs #67 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
129 lines
5.5 KiB
Go
129 lines
5.5 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() {
|
|
for _, kind := range []string{"imprint", "privacy"} {
|
|
admin.do("PUT", "/api/v1/admin/legal/"+kind, map[string]any{})
|
|
}
|
|
})
|
|
|
|
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, "")
|
|
st, body = admin.do("PUT", "/api/v1/admin/legal/terms", 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)
|
|
}
|
|
|
|
// 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() { admin.do("PUT", "/api/v1/admin/legal/imprint", map[string]any{}) })
|
|
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")
|
|
}
|
|
}
|