package server import ( "context" "encoding/json" "net/http" "net/http/httptest" "os" "os/exec" "strings" "testing" "time" "pageswiki/internal/store" ) func TestAutomaticPublication(t *testing.T) { s, token, domain := configFixture(t) ctx := context.Background() s.mux.HandleFunc("/", s.handleHome) s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI) actor, err := s.store.PrincipalForSession(domain, token) if err != nil { t.Fatal(err) } check := func(err error) { t.Helper() if err != nil { t.Fatal(err) } } q, err := s.store.CreateZoneAtPath(domain, actor.UUID, "quartier", "", "auto", "Auto", "", "/auto") check(err) street, err := s.store.CreateZoneAtPath(domain, actor.UUID, "street", q, "street", "Street", "", "/auto/street") check(err) spot, err := s.store.CreateSpotAtPath(domain, actor.UUID, street, "spot", "Spot", "", "/auto/street/spot") check(err) setAudience := func(kind, id, value string) { t.Helper() g, e := s.store.CityGovernance(ctx, domain, actor.UUID, kind, id) check(e) _, e = s.store.ChangeCityGovernance(ctx, domain, actor.UUID, kind, id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &value, ConfirmRelaxation: true}, false) check(e) } setAudience("domain", domain, "public") setAudience("quartier", q, "public") change := func(kind, id, mode string) { t.Helper() p, e := s.store.AutoPublication(ctx, domain, actor.UUID, kind, id, nil) check(e) _, e = s.store.AutoPublication(ctx, domain, actor.UUID, kind, id, &store.AutoPublicationChange{Mode: mode, Version: p.Version, Epoch: p.Epoch, TopologyVersion: p.TopologyVersion, Confirm: true, ImpactToken: p.Impact.ImpactToken}) check(e) } create := func(path, body string) string { t.Helper() id, e := s.store.CreatePageAtPathInDomain(domain, path) check(e) _, e = s.store.SaveRevisionContent(id, []byte(body), "", "und", "text/html", "") check(e) return id } get := func(path string) *httptest.ResponseRecorder { t.Helper() r := httptest.NewRequest("GET", "http://localhost"+path, nil) w := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(w, r) return w } page := create("/auto/street/first", "
Original public text
") if w := get("/auto/street/first"); w.Code == 200 { t.Fatal("default published") } preview, err := s.store.AutoPublication(ctx, domain, actor.UUID, "street", street, nil) check(err) _, err = s.store.AutoPublication(ctx, domain, actor.UUID, "street", street, &store.AutoPublicationChange{Mode: "automatic", Version: preview.Version, Epoch: preview.Epoch, TopologyVersion: preview.TopologyVersion, ImpactToken: preview.Impact.ImpactToken}) if err == nil { t.Fatal("missing confirmation") } create("/auto/street/stale-preview", "Changed impact
") _, err = s.store.AutoPublication(ctx, domain, actor.UUID, "street", street, &store.AutoPublicationChange{Mode: "automatic", Version: preview.Version, Epoch: preview.Epoch, TopologyVersion: preview.TopologyVersion, Confirm: true, ImpactToken: preview.Impact.ImpactToken}) if err == nil { t.Fatal("stale impact accepted") } change("street", street, "automatic") if err := s.store.SetConfigInDomain(domain, "authorization_mode", "legacy"); err == nil { t.Fatal("automatic scope allowed disabling authorization") } if w := get("/auto/street/first"); w.Code != 200 || !strings.Contains(w.Body.String(), "Original public text") { t.Fatal("existing", w.Code, w.Body.String()) } create("/auto/street/second", "Future public text
") _, err = s.store.SaveRevisionContent(page, []byte("Updated public text
"), "", "und", "text/html", "") check(err) for _, path := range []string{"/auto/street/first", "/auto/street/second"} { if w := get(path); w.Code != 200 { t.Fatal("auto head", path, w.Code, w.Body.String()) } } if w := get("/auto/street/first"); !strings.Contains(w.Body.String(), "Updated public text") { t.Fatal("stale head") } create("/auto/streets/not-in-scope", "PRIVATE-BOUNDARY") if w := get("/auto/streets/not-in-scope"); w.Code == 200 { t.Fatal("scope escaped") } create("/auto/street/spot/child", "Spot body
") create("/auto/street/spot", "Spot portal
") if w := get("/auto/street/spot"); w.Code != 200 { t.Fatal("scope root excluded", w.Code) } if w := get("/auto/street/spot/child"); w.Code != 200 { t.Fatal("inherited auto", w.Code) } change("spot", spot, "required") if w := get("/auto/street/spot"); w.Code == 200 { t.Fatal("scope root escaped manual floor") } if w := get("/auto/street/spot/child"); w.Code == 200 { t.Fatal("manual child did not narrow") } change("spot", spot, "inherit") if w := get("/auto/street/spot/child"); w.Code != 200 { t.Fatal("inherit did not restore") } change("quartier", q, "required") if w := get("/auto/street/first"); w.Code == 200 { t.Fatal("superior floor ignored") } p, err := s.store.AutoPublication(ctx, domain, actor.UUID, "spot", spot, nil) check(err) if p.CanAutomatic { t.Fatal("subordinate bypass offered") } change("quartier", q, "inherit") setAudience("spot", spot, "authenticated_acl") if w := get("/auto/street/spot/child"); w.Code == 200 { t.Fatal("private audience leaked") } setAudience("spot", spot, "inherit") acl, err := s.store.LocalityAccess(ctx, domain, actor.UUID, "spot", spot, nil) check(err) acl.Mode = "restricted" acl.Entries = []store.LocalityAccessEntry{{Kind: "user", Name: actor.Username, Permission: "read_write"}} acl, err = s.store.LocalityAccess(ctx, domain, actor.UUID, "spot", spot, &acl) check(err) if w := get("/auto/street/spot/child"); w.Code == 200 { t.Fatal("restricted ACL leaked") } acl.Mode = "inherit" acl.Entries = nil _, err = s.store.LocalityAccess(ctx, domain, actor.UUID, "spot", spot, &acl) check(err) aliasPage := create("/auto/street/aliased", "PRIVATE-ALIAS") _, err = s.store.DB.Exec(`INSERT INTO paths(domain_uuid,path,object_uuid,created_at,is_primary) VALUES(?,'/private-alias',?,0,0)`, domain, aliasPage) check(err) if w := get("/auto/street/aliased"); w.Code == 200 { t.Fatal("aliased object published") } // The existing revoke action must stop inherited automatic publication too. publish, err := s.store.LocalityPublication(ctx, domain, actor.UUID, "spot", spot, nil) check(err) _, err = s.store.LocalityPublication(ctx, domain, actor.UUID, "spot", spot, &store.PublicationChange{Epoch: publish.Epoch, TopologyVersion: publish.TopologyVersion, PublicationVersion: publish.PublicationVersion, Enabled: false}) check(err) if w := get("/auto/street/spot/child"); w.Code == 200 { t.Fatal("revoke left automatic publication active") } endpoint := "/_/api/v1/city/zones/" + street + "/governance/auto-publication" if w := get(endpoint); w.Code == 200 { t.Fatal("anonymous policy API") } r := httptest.NewRequest("GET", "http://localhost"+endpoint, nil) r.Header.Set("Authorization", "Bearer "+token) w := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(w, r) var state store.AutoPublication check(json.Unmarshal(w.Body.Bytes(), &state)) if w.Code != 200 || state.Mode != "automatic" { t.Fatal("API", w.Code, w.Body.String()) } empty, err := s.store.CreateZoneAtPath(domain, actor.UUID, "street", q, "empty", "Empty", "", "/auto/empty") check(err) change("street", empty, "automatic") create("/auto/empty/new", "First article in empty scope
") if w := get("/auto/empty/new"); w.Code != 200 { t.Fatal("empty scope cannot auto publish", w.Code) } if os.Getenv("PAGESWIKI_BROWSER_TEST") == "1" { s.mux.HandleFunc("/v/city/", s.handleCityMap) script, e := os.ReadFile("internal/server/testdata/automatic-publication.js") check(e) server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Host = "localhost" r.Header.Set("Authorization", "Bearer "+token) if r.Header.Get("Origin") != "" { r.Header.Set("Origin", "http://localhost") } if r.URL.Path == "/v/city/s/auto/empty" { rec := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(rec, r) for k, v := range rec.Header() { w.Header()[k] = v } w.WriteHeader(rec.Code) _, _ = w.Write([]byte(strings.Replace(rec.Body.String(), "