Files
pages/internal/server/public_navigation_test.go
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

273 lines
11 KiB
Go

package server
import (
"context"
"encoding/json"
"net/http/httptest"
"strings"
"testing"
"pageswiki/internal/store"
)
func TestPublicNavigationVisibilityAndCacheRevocation(t *testing.T) {
s, token, domain := configFixture(t)
ctx := context.Background()
admin, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
s.mux.HandleFunc("/", s.handleHome)
q, err := s.store.CreateZoneAtPath(domain, admin.UUID, "quartier", "", "public", "PRIVATE-QUARTIER-TITLE", "PRIVATE-DESCRIPTION", "/public")
if err != nil {
t.Fatal(err)
}
st, err := s.store.CreateZoneAtPath(domain, admin.UUID, "street", q, "books", "PRIVATE-STREET-TITLE", "", "/public/books")
if err != nil {
t.Fatal(err)
}
spot, err := s.store.CreateSpotAtPath(domain, admin.UUID, st, "shelf", "PRIVATE-SPOT-TITLE", "PRIVATE-SUMMARY", "/public/books/shelf")
if err != nil {
t.Fatal(err)
}
create := func(p, body string) string {
t.Helper()
o, e := s.store.CreatePageAtPathInDomain(domain, p)
if e != nil {
t.Fatal(e)
}
if _, e = s.store.SaveRevisionContent(o, []byte(body), "", "und", "text/html", ""); e != nil {
t.Fatal(e)
}
return o
}
create("/public/books", "PRIVATE-PARENT-BODY")
page := create("/public/books/shelf/page", "<h1>Public navigation</h1><p>{{children:/public}}</p><p>{{spot-pages}}</p>")
create("/public/books/shelf/second", "<p>Second</p>")
for _, node := range []struct{ kind, id string }{{"domain", domain}, {"quartier", q}} {
g, e := s.store.CityGovernance(ctx, domain, admin.UUID, node.kind, node.id)
if e != nil {
t.Fatal(e)
}
value := "public"
if _, e = s.store.ChangeCityGovernance(ctx, domain, admin.UUID, node.kind, node.id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &value, ConfirmRelaxation: true}, false); e != nil {
t.Fatal(e)
}
}
approve := func() {
t.Helper()
p, e := s.store.LocalityPublication(ctx, domain, admin.UUID, "spot", spot, nil)
if e != nil {
t.Fatal(e)
}
if _, e = s.store.LocalityPublication(ctx, domain, admin.UUID, "spot", spot, &store.PublicationChange{Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, PublicationVersion: p.PublicationVersion, Enabled: true, Confirm: true, ImpactToken: p.ImpactToken}); e != nil {
t.Fatal(e)
}
}
approve()
create("/public/books/shelf/PRIVATE-NEW", "PRIVATE-NEW-BODY")
call := func(target string) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest("GET", "http://localhost"+target, nil)
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
for _, target := range []string{"/", "/public", "/public/books/shelf/page", "/v/tree", "/v/tree/public/books", "/v/browser", "/v/browser/public/books/shelf", "/v/children?path=/public/books/shelf&limit=2", "/_/api/v1/city/map"} {
w := call(target)
if w.Code != 200 {
t.Fatalf("%s = %d %s", target, w.Code, w.Body.String())
}
for _, secret := range []string{"PRIVATE-", " download", "data-children-next"} {
if strings.Contains(w.Body.String(), secret) {
t.Fatalf("%s leaked %s: %s", target, secret, w.Body.String())
}
}
if !strings.Contains(w.Header().Get("Cache-Control"), "no-store") {
t.Fatalf("cache headers %s", target)
}
}
if w := call("/public/books"); w.Code != 200 || strings.Contains(w.Body.String(), "PRIVATE-PARENT-BODY") || !strings.Contains(w.Body.String(), "data-public-children-ready") {
t.Fatal("virtual parent must expose only the directory, not the private body")
}
for _, target := range []string{"/v/raw/path:/public/books", "/v/browser/public/books/shelf/PRIVATE-NEW", "/v/tree/public/books/shelf/PRIVATE-NEW"} {
if w := call(target); w.Code == 200 {
t.Fatalf("private content or branch granted %s", target)
}
}
for _, target := range []string{"/", "/v/browser", "/public/books"} {
r := httptest.NewRequest("GET", "http://localhost"+target, nil)
r.Header.Set("Authorization", "Bearer invalid")
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
if w.Code == 200 {
t.Fatalf("invalid credential downgraded: %s", target)
}
}
if w := call("/v/children?path=/public"); !strings.Contains(w.Body.String(), `href="/v/tree/public/books"`) {
t.Fatalf("virtual link %s", w.Body.String())
}
for _, target := range []string{"/v/children?path=/public/books/shelf&exclude=/public/books/shelf/second&limit=1", "/v/browser/public/books/shelf?exclude=/public/books/shelf/second&limit=1"} {
w := call(target)
if w.Code != 200 || !strings.Contains(w.Body.String(), `href="/public/books/shelf/page"`) || strings.Contains(w.Body.String(), `href="/public/books/shelf/second"`) || strings.Contains(w.Body.String(), "data-children-next=") {
t.Fatalf("exclude before pagination: %s = %s", target, w.Body.String())
}
}
if w := call("/v/browser/public/books/shelf?limit=1&offset=1"); w.Code != 200 || strings.Contains(w.Body.String(), `href="/public/books/shelf/page"`) || !strings.Contains(w.Body.String(), `href="/public/books/shelf/second"`) || !strings.Contains(w.Body.String(), "offset=0") {
t.Fatalf("server-rendered pagination %d %s", w.Code, w.Body.String())
}
for _, method := range []string{"POST", "PUT", "DELETE"} {
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, httptest.NewRequest(method, "http://localhost/v/browser/public", nil))
if w.Code < 400 {
t.Fatalf("anonymous browser mutation %s = %d", method, w.Code)
}
}
var city store.CityMap
if err = json.Unmarshal(call("/_/api/v1/city/map").Body.Bytes(), &city); err != nil {
t.Fatal(err)
}
if len(city.Quartiers) != 1 || len(city.Quartiers[0].Streets) != 1 || len(city.Quartiers[0].Streets[0].Spots) != 1 || city.Quartiers[0].Streets[0].Spots[0].PageCount != 2 {
t.Fatalf("visible City counts %+v", city)
}
// The already-warm data cache must not survive a direct SQL approval removal.
if _, err = s.store.DB.Exec(`DELETE FROM locality_publication_entries WHERE domain_uuid=? AND object_uuid=?`, domain, page); err != nil {
t.Fatal(err)
}
for _, target := range []string{"/v/tree", "/v/children?path=/public/books/shelf", "/_/api/v1/city/map"} {
w := call(target)
if w.Code != 200 || strings.Contains(w.Body.String(), "/public/books/shelf/page") {
t.Fatalf("stale approval %s: %d %s", target, w.Code, w.Body.String())
}
}
// Re-approval, warm cache, then edit: current-revision fencing wins over cache.
approve()
call("/v/tree")
if _, err = s.store.SaveRevisionContent(page, []byte("UNAPPROVED EDIT"), "", "und", "text/html", ""); err != nil {
t.Fatal(err)
}
if w := call("/v/tree"); strings.Contains(w.Body.String(), `href="/public/books/shelf/page"`) {
t.Fatal("edited Path retained in cached tree")
}
p, e := s.store.LocalityPublication(ctx, domain, admin.UUID, "spot", spot, nil)
if e != nil {
t.Fatal(e)
}
if _, e = s.store.LocalityPublication(ctx, domain, admin.UUID, "spot", spot, &store.PublicationChange{Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, PublicationVersion: p.PublicationVersion, Enabled: false}); e != nil {
t.Fatal(e)
}
if w := call("/_/api/v1/city/map"); w.Code != 200 || strings.Contains(w.Body.String(), spot) {
t.Fatal("revoked City cached")
}
if w := call("/v/children?path=/public"); strings.Contains(w.Body.String(), "books") {
t.Fatal("revoked children cached")
}
if w := call("/v/browser"); w.Code != 200 || strings.Contains(w.Body.String(), "/public/books") {
t.Fatal("empty public root must remain usable without stale names")
}
}
func TestPublicTreeComponentOrderingAndFolders(t *testing.T) {
p := &publicNavigationSnapshot{pages: map[string]store.Revision{"/a": {}, "/a/x": {}, "/a-b/z": {}, "/a/x/c": {}}, branches: map[string]bool{"/": true, "/a": true, "/a/x": true, "/a/x/c": true, "/a-b": true, "/a-b/z": true}}
nodes := publicTreeNodes(p, "/")
want := []string{"/a", "/a/x", "/a/x/c", "/a-b", "/a-b/z"}
for i, node := range nodes {
if node.Path != want[i] {
t.Fatalf("noncontiguous subtree: %+v", nodes)
}
}
if nodes[0].IsDir || !nodes[0].HasChildren || nodes[0].FolderHref != "/v/browser/a" || !nodes[3].IsDir || !nodes[3].HasChildren {
t.Fatal("page plus folder semantics lost")
}
}
func TestNavigationCacheDoesNotCacheGroupPermissions(t *testing.T) {
s, token, domain := configFixture(t)
ctx := context.Background()
admin, err := s.store.PrincipalForSession(domain, token)
if err != nil {
t.Fatal(err)
}
s.mux.HandleFunc("/v/tree", s.handleTree)
s.mux.HandleFunc("/v/tree/", s.handleTree)
s.mux.HandleFunc("/v/children", s.handleChildrenProjection)
userDoc, e := s.store.CreatePageAtPathInDomain(domain, "/u/admin/wiki")
if e != nil {
t.Fatal(e)
}
if _, e = s.store.SaveRevision(userDoc, "User Wiki", ""); e != nil {
t.Fatal(e)
}
q, err := s.store.CreateZoneAtPath(domain, admin.UUID, "quartier", "", "team", "Team", "", "/team")
if err != nil {
t.Fatal(err)
}
obj, err := s.store.CreatePageAtPathInDomain(domain, "/team/nested/visible")
if err != nil {
t.Fatal(err)
}
if _, err = s.store.SaveRevision(obj, "<p>hello</p>", ""); err != nil {
t.Fatal(err)
}
user, err := s.store.CreatePasswordAccount(domain, "reader", "long enough test password", "Reader", "")
if err != nil {
t.Fatal(err)
}
userToken, err := s.store.CreateAuthSession(domain, user.UUID)
if err != nil {
t.Fatal(err)
}
g, err := s.store.CreateGroup(domain, admin.UUID, "readers", "Readers", "", "members")
if err != nil {
t.Fatal(err)
}
if err = s.store.AddGroupMemberByUsername(domain, g.UUID, "reader", "member"); err != nil {
t.Fatal(err)
}
a, err := s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, nil)
if err != nil {
t.Fatal(err)
}
a.Mode = "restricted"
a.Entries = []store.LocalityAccessEntry{{Kind: "group", Name: "readers", Permission: "read"}}
if _, err = s.store.LocalityAccess(ctx, domain, admin.UUID, "quartier", q, &a); err != nil {
t.Fatal(err)
}
call := func(target string) *httptest.ResponseRecorder {
r := httptest.NewRequest("GET", "http://localhost"+target, nil)
r.Header.Set("Authorization", "Bearer "+userToken)
w := httptest.NewRecorder()
s.withAuthorization(s.mux).ServeHTTP(w, r)
return w
}
for _, target := range []string{"/v/tree", "/v/children?path=/team/nested"} {
w := call(target)
if w.Code != 200 || !strings.Contains(w.Body.String(), "/team/nested/visible") {
t.Fatalf("visible %s %d %s", target, w.Code, w.Body.String())
}
}
if w := call("/v/children?path=/u/admin"); w.Code != 200 || !strings.Contains(w.Body.String(), "/u/admin/wiki") {
t.Fatal("explicit user Wiki children lost")
}
if w := call("/v/tree"); strings.Contains(w.Body.String(), "/u/admin/wiki") {
t.Fatal("user Wiki leaked into global tree")
}
before, err := s.store.NavigationGeneration(ctx)
if err != nil {
t.Fatal(err)
}
if _, err = s.store.DB.Exec(`DELETE FROM group_members WHERE group_uuid=?`, g.UUID); err != nil {
t.Fatal(err)
}
after, err := s.store.NavigationGeneration(ctx)
if err != nil || after != before {
t.Fatal("test must hit unchanged data cache")
}
for _, target := range []string{"/v/tree", "/v/children?path=/team/nested"} {
w := call(target)
if w.Code != 200 || strings.Contains(w.Body.String(), "/team/nested/visible") {
t.Fatalf("revoked %s %d %s", target, w.Code, w.Body.String())
}
}
}