package server import ( "context" "net/http" "net/http/httptest" "os" "os/exec" "path/filepath" "strings" "testing" "time" "pageswiki/internal/store" ) func TestAnonymousPublicationChromium(t *testing.T) { if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" { t.Skip("opt-in Chromium") } s, token, domain := configFixture(t) s.mux.HandleFunc("/_/api/v1/city/", s.handleCityAPI) s.mux.HandleFunc("/v/city/", s.handleCityMap) s.mux.HandleFunc("/", s.handleHome) admin, e := s.store.PrincipalForSession(domain, token) if e != nil { t.Fatal(e) } q, e := s.store.CreateZoneAtPath(domain, admin.UUID, "quartier", "", "public", "Public", "", "/public") if e != nil { t.Fatal(e) } o, e := s.store.CreatePageAtPathInDomain(domain, "/public/page") if e != nil { t.Fatal(e) } if _, e = s.store.SaveRevisionContent(o, []byte(`
{{children}}
`), "", "und", "text/html", ""); e != nil { t.Fatal(e) } child, e := s.store.CreatePageAtPathInDomain(domain, "/public/page/child") if e != nil { t.Fatal(e) } if _, e = s.store.SaveRevisionContent(child, []byte("Child
"), "", "und", "text/html", ""); e != nil { t.Fatal(e) } for _, path := range []string{"/public/page/child/grandchild", "/public/page-other"} { object, err := s.store.CreatePageAtPathInDomain(domain, path) if err != nil { t.Fatal(err) } if _, err = s.store.SaveRevisionContent(object, []byte("Navigation fixture
"), "", "und", "text/html", ""); err != nil { t.Fatal(err) } } policy := "public" for _, node := range []struct{ kind, id string }{{"domain", domain}, {"quartier", q}} { g, e := s.store.CityGovernance(context.Background(), domain, admin.UUID, node.kind, node.id) if e != nil { t.Fatal(e) } if _, e = s.store.ChangeCityGovernance(context.Background(), domain, admin.UUID, node.kind, node.id, store.CityGovernanceChange{Epoch: g.Epoch, TopologyVersion: g.TopologyVersion, Policy: &policy, ConfirmRelaxation: true}, false); e != nil { t.Fatal(e) } } script, e := os.ReadFile("internal/server/testdata/anonymous-publication.js") if e != nil { t.Fatal(e) } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Host = "localhost" if r.Header.Get("Origin") != "" { r.Header.Set("Origin", "http://localhost") } // Fixture authentication only on settings, never on public content. if strings.HasPrefix(r.URL.Path, "/_/api/v1/city/") && r.URL.Path != "/_/api/v1/city/map" || strings.HasPrefix(r.URL.Path, "/v/city/") { r.Header.Set("Authorization", "Bearer "+token) } if r.Method == "GET" && r.URL.Path == "/v/city/q/public" { 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(), "