Files
pages/internal/server/anonymous_browser_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

110 lines
3.7 KiB
Go

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(`<h1>Public browser sentinel</h1><script>window.authoredRan=true</script><p>{{children}}</p>`), "", "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("<p>Child</p>"), "", "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("<p>Navigation fixture</p>"), "", "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(), "</body>", browserFixtureScript(rec.Header(), script)+"</body>", 1)))
return
}
s.withAuthorization(s.mux).ServeHTTP(w, r)
}))
defer server.Close()
for _, width := range []string{"1440", "390"} {
t.Run(width, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 40*time.Second)
defer cancel()
screenshot := ""
if dir := os.Getenv("PAGESWIKI_PUBLIC_NAV_SCREENSHOT_DIR"); dir != "" {
if err := os.MkdirAll(dir, 0700); err != nil {
t.Fatal(err)
}
screenshot = filepath.Join(dir, "public-navigation-"+width+".png")
}
out, err := exec.CommandContext(ctx, "node", "internal/server/testdata/vim-browser-smoke.mjs", server.URL+"/v/city/q/public", t.TempDir(), screenshot, width).CombinedOutput()
if err != nil {
t.Fatalf("browser %v %s", err, out)
}
})
}
}