package server
import (
"context"
"encoding/xml"
"net/http"
"net/http/httptest"
"os"
"os/exec"
"strings"
"testing"
"time"
"pageswiki/internal/store"
)
func TestPublicPeriodicalProjection(t *testing.T) {
s, _, source, actor := periodicalFixture(t)
s.mux.HandleFunc("/", s.handleHome)
ctx := context.Background()
check := func(err error) {
t.Helper()
if err != nil {
t.Fatal(err)
}
}
d, err := s.store.GetDomainByUUID(source)
check(err)
check(s.store.CreateDomain(d.CampusUUID, "blog-public", "Public Blog", "blog.example.test", "local", "", "", true))
var target string
var id int64
check(s.store.DB.QueryRow(`SELECT uuid,id FROM domains WHERE name='blog-public'`).Scan(&target, &id))
for _, domain := range []string{source, target} {
_, err = s.store.DB.Exec(`INSERT INTO periodical_settings(domain_uuid,kind,enabled) VALUES(?,'blog',1) ON CONFLICT(domain_uuid,kind) DO UPDATE SET enabled=1`, domain)
check(err)
}
check(s.store.SetConfigInDomain(target, "home_layout", "blog"))
check(s.store.SetPeriodicalProjection(ctx, id, actor, "blog", source, "/blog"))
quarter, err := s.store.CreateZoneAtPath(source, actor, "quartier", "", "blog", "PRIVATE-ZONE", "", "/blog")
check(err)
page, err := s.store.CreatePageAtPathInDomain(source, "/blog/2026/09/17")
check(err)
_, err = s.store.SaveRevisionContent(page, []byte(`
Approved heading
Article body
Local article
`), "", "und", "text/html", "")
check(err)
_, err = s.store.DB.Exec(`INSERT INTO periodical_entries(domain_uuid,kind,page_uuid,owner_uuid,local_date,title,category,timezone) VALUES(?,'blog',?,?,'2026-09-20','Public registered title & notes','PRIVATE-CATEGORY','UTC')`, source, page, actor)
check(err)
for _, path := range []string{"/blog/2026/09/16", "/blog/2026/09/18/keyword"} {
other, err := s.store.CreatePageAtPathInDomain(source, path)
check(err)
_, err = s.store.SaveRevisionContent(other, []byte("Other heading
"), "", "und", "text/html", "")
check(err)
}
for _, node := range []struct{ kind, id string }{{"domain", source}, {"quartier", quarter}} {
g, err := s.store.CityGovernance(ctx, source, actor, node.kind, node.id)
check(err)
policy := "public"
_, err = s.store.ChangeCityGovernance(ctx, source, actor, node.kind, node.id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &policy, ConfirmRelaxation: true}, false)
check(err)
}
p, err := s.store.LocalityPublication(ctx, source, actor, "quartier", quarter, nil)
check(err)
_, err = s.store.LocalityPublication(ctx, source, actor, "quartier", quarter, &store.PublicationChange{Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, PublicationVersion: p.PublicationVersion, Enabled: true, Confirm: true, ImpactToken: p.ImpactToken})
check(err)
private, err := s.store.CreatePageAtPathInDomain(source, "/blog/2026/09/19/PRIVATE-PATH")
check(err)
_, err = s.store.SaveRevision(private, "PRIVATE-BODY", "")
check(err)
call := func(path, credential string) *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", "http://blog.example.test"+path, nil)
if credential != "" {
r.Header.Set("Authorization", credential)
}
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
for _, mode := range []string{"legacy", "enforcing"} {
check(s.store.SetConfigInDomain(target, "authorization_mode", mode))
for _, path := range []string{"/", "/v/blog", "/v/blog/2026/09", "/v/blog/2026/09/17", "/2026/09/17"} {
w := call(path, "")
if w.Code != 200 || !strings.Contains(w.Body.String(), "Public registered title & notes") || !strings.Contains(w.Body.String(), "Public Blog") {
t.Fatalf("%s %s: %d %s", mode, path, w.Code, w.Body.String())
}
for _, secret := range []string{"PRIVATE-", "data-periodical-compose", "data-periodical-add", "name=\"own\"", actor} {
if strings.Contains(w.Body.String(), secret) {
t.Fatalf("%s leaked %s", path, secret)
}
}
if w.Header().Get("Cache-Control") != "private, no-store" {
t.Fatal("cache policy")
}
}
if w := call("/", "Bearer invalid"); w.Code == 200 {
t.Fatal("credential fallback")
}
body := call("/2026/09/17", "").Body.String()
for _, fragment := range []string{`href="/2026/09/16"`, `href="/2026/09/18/keyword"`, `rel="prev"`, `rel="next"`, "periodical-calendar", "Article body"} {
if !strings.Contains(body, fragment) {
t.Fatal("missing article navigation/body", fragment)
}
}
if strings.Contains(body, "UNSAFE_") {
t.Fatal("unsafe article HTML")
}
if w := call("/2026/09/19/PRIVATE-PATH", ""); w.Code != 404 {
t.Fatal("private article", w.Code)
}
if w := call("/2026/09/18/keyword", ""); w.Code != 200 {
t.Fatal("keyword article", w.Code)
}
w := call("/rss.xml", "")
var feed periodicalRSS
check(xml.Unmarshal(w.Body.Bytes(), &feed))
var decoded struct {
Items []struct {
Content string `xml:"http://purl.org/rss/1.0/modules/content/ encoded"`
} `xml:"channel>item"`
}
check(xml.Unmarshal(w.Body.Bytes(), &decoded))
if len(decoded.Items) != 3 {
t.Fatal("RSS content namespace", w.Body.String())
}
content := decoded.Items[1].Content
for _, fragment := range []string{"Article body
", `href="https://blog.example.test/2026/09/18/keyword"`, `/v/raw/path:/blog/image`} {
if !strings.Contains(content, fragment) {
t.Fatal("RSS body missing", fragment, content)
}
}
if strings.Contains(content, "UNSAFE_") || strings.Contains(w.Body.String(), "PRIVATE-") {
t.Fatal("unsafe RSS")
}
if w.Code != 200 || feed.Channel.Title != "Public Blog" || len(feed.Channel.Items) != 3 || feed.Channel.Items[1].Title != "Public registered title & notes" || feed.Channel.Items[1].Link != "https://blog.example.test/2026/09/17" || !strings.HasPrefix(w.Header().Get("Content-Type"), "application/rss+xml") {
t.Fatal("RSS", w.Code, w.Body.String())
}
}
if w := call("/v/blog/2026/09/20", ""); strings.Contains(w.Body.String(), "