Files
pages/internal/server/gc_integrity_test.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

202 lines
8.0 KiB
Go

package server
import (
"encoding/json"
"math"
"net/http"
"net/http/httptest"
"net/url"
"strconv"
"strings"
"testing"
"pageswiki/internal/store"
)
func seedServerZombie(t *testing.T, s *Server, id, domain string) {
t.Helper()
if _, err := s.store.DB.Exec(`INSERT INTO objects(uuid,kind,created_at) VALUES(?,'page',1)`, id); err != nil {
t.Fatal(err)
}
if domain != "" {
if _, err := s.store.DB.Exec(`INSERT INTO object_origins(object_uuid,domain_uuid,created_at) VALUES(?,?,1)`, id, domain); err != nil {
t.Fatal(err)
}
}
}
func requireServerZombie(t *testing.T, s *Server, id string, want bool) {
t.Helper()
var exists bool
if err := s.store.DB.QueryRow(`SELECT EXISTS(SELECT 1 FROM objects WHERE uuid=?)`, id).Scan(&exists); err != nil || exists != want {
t.Fatalf("Object %s exists=%v want=%v err=%v", id, exists, want, err)
}
}
func TestZombieHTTPAndConfigAreCurrentDomainOnly(t *testing.T) {
for _, operation := range []string{"api", "config"} {
t.Run(operation, func(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/gc/zombies/list", s.handleZombieListAPI)
s.mux.HandleFunc("/_/api/v1/gc/zombies/purge", s.handleZombiePurgeAPI)
seedServerZombie(t, s, "gc-own", domain)
seedServerZombie(t, s, "gc-foreign", "other-domain")
seedServerZombie(t, s, "gc-unknown", "")
listURL := "/_/api/v1/gc/zombies/list?domain_uuid=other-domain&grace_seconds=0"
if operation == "config" {
listURL = "/-/sys/config?panel=objects"
}
w := configRequest(s, token, http.MethodGet, listURL, nil)
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), "gc-own") || strings.Contains(w.Body.String(), "gc-foreign") || strings.Contains(w.Body.String(), "gc-unknown") {
t.Fatalf("%s scoped listing: %d %s", operation, w.Code, w.Body.String())
}
if operation == "api" {
w = configRequest(s, token, http.MethodPost, "/_/api/v1/gc/zombies/purge?domain_uuid=other-domain&grace_seconds=0", nil)
if w.Code != http.StatusOK || !strings.Contains(w.Body.String(), `"deleted":1`) {
t.Fatalf("purge API: %d %s", w.Code, w.Body.String())
}
} else {
w = configRequest(s, token, http.MethodPost, "/-/sys/config?panel=objects", url.Values{"section": {"zombie-purge"}, "domain_uuid": {"other-domain"}})
if w.Code != http.StatusFound {
t.Fatalf("config purge: %d %s", w.Code, w.Body.String())
}
}
requireServerZombie(t, s, "gc-own", false)
requireServerZombie(t, s, "gc-foreign", true)
requireServerZombie(t, s, "gc-unknown", true)
})
}
}
func TestZombieHTTPRejectsUnboundedOptionsAndNonAdmin(t *testing.T) {
s, token, domain := configFixture(t)
s.mux.HandleFunc("/_/api/v1/gc/zombies/list", s.handleZombieListAPI)
s.mux.HandleFunc("/_/api/v1/gc/zombies/purge", s.handleZombiePurgeAPI)
seedServerZombie(t, s, "gc-kept", domain)
for _, query := range []string{"limit=0", "limit=-1", "limit=1001", "limit=all", "grace_seconds=-1", "grace_seconds=invalid", "grace_seconds=9223372036854775808"} {
for _, endpoint := range []struct{ method, path string }{{http.MethodGet, "list"}, {http.MethodPost, "purge"}} {
w := configRequest(s, token, endpoint.method, "/_/api/v1/gc/zombies/"+endpoint.path+"?"+query, nil)
if w.Code != http.StatusBadRequest {
t.Fatalf("invalid %s %s accepted: %d %s", endpoint.path, query, w.Code, w.Body.String())
}
}
}
member, err := s.store.CreatePasswordAccount(domain, "gc-reader", "a long reader password", "Reader", "")
if err != nil {
t.Fatal(err)
}
memberToken, err := s.store.CreateAuthSession(domain, member.UUID)
if err != nil {
t.Fatal(err)
}
for _, endpoint := range []struct{ method, path string }{{http.MethodGet, "list"}, {http.MethodPost, "purge"}} {
w := configRequest(s, memberToken, endpoint.method, "/_/api/v1/gc/zombies/"+endpoint.path, nil)
if w.Code != http.StatusForbidden {
t.Fatalf("non-admin %s: %d %s", endpoint.path, w.Code, w.Body.String())
}
}
requireServerZombie(t, s, "gc-kept", true)
}
func TestManualEventsCannotInvokeGlobalZombieGC(t *testing.T) {
s, token, domain := configFixture(t)
seedServerZombie(t, s, "gc-own", domain)
seedServerZombie(t, s, "gc-foreign", "foreign")
seedServerZombie(t, s, "gc-unknown", "")
post := func(path string, payload any) *httptest.ResponseRecorder {
raw, err := json.Marshal(payload)
if err != nil {
t.Fatal(err)
}
r := httptest.NewRequest(http.MethodPost, "http://localhost"+path, strings.NewReader(string(raw)))
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
for _, eventType := range []string{"gc.zombies.purge", "zombie.purge", "gc.purge"} {
definition := store.EventDefinition{Name: "Attempt global GC", EventType: eventType, EventKey: eventType, Payload: `{"grace_seconds":0,"domain_uuid":"foreign"}`, ScheduleKind: "manual", Enabled: true}
w := post("/_/api/v1/event", definition)
if w.Code != http.StatusCreated || json.Unmarshal(w.Body.Bytes(), &definition) != nil {
t.Fatalf("definition: %d %s", w.Code, w.Body.String())
}
w = post("/_/api/v1/event/run", map[string]string{"uuid": definition.UUID})
if w.Code != http.StatusAccepted {
t.Fatalf("manual trigger: %d %s", w.Code, w.Body.String())
}
job, err := s.store.ClaimDueEvent()
if err != nil {
t.Fatal(err)
}
runErr := s.runEvent(job)
if runErr == nil || !strings.Contains(runErr.Error(), "unknown event type") {
t.Fatalf("manual GC type became executable: %v", runErr)
}
if err := s.store.FinishEvent(job.UUID, runErr); err != nil {
t.Fatal(err)
}
}
for _, id := range []string{"gc-own", "gc-foreign", "gc-unknown"} {
requireServerZombie(t, s, id, true)
}
}
func TestZombieMaintenanceRetainsGraceAcrossDomainChangesAndRestart(t *testing.T) {
s, _, domain := configFixture(t)
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "")
if err := s.store.SetConfigInDomain(domain, "zombie_grace_hours", "72"); err != nil {
t.Fatal(err)
}
if grace, err := s.zombieMaintenanceGrace(); err != nil || grace != 72*3600 {
t.Fatalf("legacy grace shortened: %d %v", grace, err)
}
if err := s.store.SetConfigInDomain(domain, "zombie_grace_hours", "0"); err != nil {
t.Fatal(err)
}
if grace, err := (&Server{store: s.store}).zombieMaintenanceGrace(); err != nil || grace != 72*3600 {
t.Fatalf("Domain changed instance retention: %d %v", grace, err)
}
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "168")
if grace, err := s.zombieMaintenanceGrace(); err != nil || grace != 168*3600 {
t.Fatalf("operator extension: %d %v", grace, err)
}
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "24")
if grace, err := (&Server{store: s.store}).zombieMaintenanceGrace(); err != nil || grace != 168*3600 {
t.Fatalf("restart shortened retention: %d %v", grace, err)
}
for _, value := range []string{"invalid", "-1", strconv.FormatInt(math.MaxInt64, 10)} {
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", value)
if _, err := s.zombieMaintenanceGrace(); err == nil {
t.Fatalf("invalid operator grace %s accepted", value)
}
}
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "")
if err := s.store.SetConfigInDomain(domain, "zombie_grace_hours", strconv.FormatInt(math.MaxInt64, 10)); err != nil {
t.Fatal(err)
}
if grace := s.getZombieGraceSeconds(domain); grace != math.MaxInt64 {
t.Fatalf("hours overflowed: %d", grace)
}
if grace, err := s.zombieMaintenanceGrace(); err != nil || grace != math.MaxInt64 {
t.Fatalf("legacy long grace overflow: %d %v", grace, err)
}
if _, err := s.store.DB.Exec(`DROP TABLE config`); err != nil {
t.Fatal(err)
}
if _, err := s.zombieMaintenanceGrace(); err == nil {
t.Fatal("config failure silently shortened retention")
}
}
func TestZombieMaintenanceDefaultIsStill24Hours(t *testing.T) {
s, _, domain := configFixture(t)
t.Setenv("PAGESWIKI_ZOMBIE_GC_GRACE_HOURS", "")
if err := s.store.SetConfigInDomain(domain, "zombie_grace_hours", "0"); err != nil {
t.Fatal(err)
}
if grace, err := s.zombieMaintenanceGrace(); err != nil || grace != store.ZombieGCDefaultGraceSeconds {
t.Fatalf("default retention changed: %d %v", grace, err)
}
}