package server import ( "context" "encoding/json" "net/http" "net/http/httptest" "net/url" "os" "os/exec" "path/filepath" "strings" "testing" "time" ) func TestEditorAndPublicApplicationAssets(t *testing.T) { s, token, domain := configFixture(t) s.mux.HandleFunc("/e/", s.handleSemanticEditor) for _, path := range []string{"/", "/docs"} { object, err := s.store.CreatePageAtPathInDomain(domain, path) if err != nil { t.Fatal(err) } if _, err := s.store.SaveRevisionContent(object, []byte("
editor-regression
"), "test", "en", "text/html", ""); err != nil { t.Fatal(err) } } for _, route := range []string{"/e/", "/e/-", "/e/-/", "/e/docs", "/e/-/docs"} { w := configRequest(s, token, "GET", route, nil) if w.Code != 200 || !strings.Contains(w.Body.String(), "editor-regression") || !strings.Contains(w.Body.String(), `/static/vendor/codemirror/editor.bundle.js`) { t.Fatalf("editor %s: %d %s", route, w.Code, w.Body.String()) } _, target := authorizationTargetForRequest(httptest.NewRequest("GET", route, nil), domain) if target != semanticEditorPath(route) { t.Fatalf("auth target mismatch %s", route) } } if _, err := s.store.ResolvePathInDomain(domain, "/e/docs"); err == nil { t.Fatal("editor created wrong path") } for _, prefix := range []string{"/static/", "/o/path:/system/static/"} { for _, file := range []string{"css/main.css", "vendor/codemirror/editor.css", "vendor/codemirror/editor.bundle.js"} { w := configRequest(s, "", "GET", prefix+file, nil) if w.Code != 200 || w.Header().Get("Location") != "" { t.Fatalf("asset %s%s: %d", prefix, file, w.Code) } } } w := configRequest(s, "", "GET", "/o/uuid:private", nil) if w.Code == 200 { t.Fatal("private object bypassed authorization") } } func TestBookmarkSectionsAndHomeManagement(t *testing.T) { s, token, domain := configFixture(t) s.mux.HandleFunc("/", s.handleHome) s.mux.HandleFunc("/v/bookmarks", s.handleBookmarkChildren) form := url.Values{"section": {"bookmarks"}, "return_home": {"true"}, "bookmark_label": {"Reference", "Example", "Documents"}, "bookmark_kind": {"section", "url", "section"}, "bookmark_target": {"", "https://example.com/", "/docs"}, "bookmark_children": {"false", "false", "true"}} w := configRequest(s, token, "POST", "/-/sys/config", form) if w.Code != 303 || w.Header().Get("Location") != "/" { t.Fatalf("save %d %s", w.Code, w.Body.String()) } for _, p := range []string{"/docs/100% #日本語", "/docs/nested/page", "/other"} { if _, err := s.store.CreatePageAtPathInDomain(domain, p); err != nil { t.Fatal(err) } } w = configRequest(s, token, "GET", "/v/bookmarks?section=2", nil) var result struct { Links []NavItem Next string } if w.Code != 200 || json.Unmarshal(w.Body.Bytes(), &result) != nil || len(result.Links) != 2 || result.Links[0].Href != "/docs/100%25%20%23%E6%97%A5%E6%9C%AC%E8%AA%9E" { t.Fatalf("children %d %s", w.Code, w.Body.String()) } for _, layout := range []string{"classic", "search", "directory"} { if err := s.store.SetConfigInDomain(domain, "home_layout", layout); err != nil { t.Fatal(err) } w = configRequest(s, token, "GET", "/", nil) if w.Code != 200 || !strings.Contains(w.Body.String(), `id="bookmark-form"`) { t.Fatalf("home manager %s: %d %s", layout, w.Code, w.Body.String()) } if layout != "search" && !strings.Contains(w.Body.String(), `data-bookmark-source="/v/bookmarks?section=2"`) { t.Fatal("section missing") } } member, err := s.store.CreatePasswordAccount(domain, "member", "a long member password", "Member", "") if err != nil { t.Fatal(err) } memberToken, err := s.store.CreateAuthSession(domain, member.UUID) if err != nil { t.Fatal(err) } if _, err := s.store.DB.Exec(`DELETE FROM auth_role_bindings WHERE membership_uuid IN (SELECT uuid FROM domain_memberships WHERE principal_uuid=?)`, member.UUID); err != nil { t.Fatal(err) } if w = configRequest(s, memberToken, "GET", "/v/bookmarks?section=2", nil); w.Code != 403 { t.Fatalf("child authorization: %d", w.Code) } if w = configRequest(s, memberToken, "POST", "/-/sys/config", form); w.Code != 403 { t.Fatalf("manage authorization: %d", w.Code) } form["bookmark_target"][2] = "" if w = configRequest(s, token, "POST", "/-/sys/config", form); w.Code != 400 { t.Fatal("accepted missing parent") } if len(s.homeBookmarks(domain)) != 3 { t.Fatal("invalid save lost old config") } } func TestEditorHomeChromiumSmoke(t *testing.T) { if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" { t.Skip("opt-in Chromium") } s, token, domain := configFixture(t) s.mux.HandleFunc("/", s.handleHome) s.mux.HandleFunc("/e/", s.handleSemanticEditor) s.mux.HandleFunc("/v/bookmarks", s.handleBookmarkChildren) if err := s.store.SetConfigInDomain(domain, "home_layout", "directory"); err != nil { t.Fatal(err) } if err := s.store.SetConfigInDomain(domain, "home_bookmarks", `[{"kind":"section","label":"Docs","href":"/docs","children":true}]`); err != nil { t.Fatal(err) } object, err := s.store.CreatePageAtPathInDomain(domain, "/docs/test") if err != nil { t.Fatal(err) } if _, err = s.store.SaveRevisionContent(object, []byte("editor-regression
"), "test", "en", "text/html", ""); err != nil { t.Fatal(err) } server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { r.Host = "localhost" r.Header.Set("Authorization", "Bearer "+token) s.withAuthorization(s.mux).ServeHTTP(w, r) })) defer server.Close() for _, route := range []string{"/", "/e/docs/test"} { ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second) cmd := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--enable-logging=stderr", "--log-level=0", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=2500", server.URL+route) if dir := os.Getenv("PAGESWIKI_BROWSER_ARTIFACTS"); dir != "" { if err := os.MkdirAll(dir, 0755); err != nil { t.Fatal(err) } name := "home" if route != "/" { name = "editor" } cmd.Args = append(cmd.Args, "--window-size=1440,1000", "--screenshot="+filepath.Join(dir, name+".png")) } out, err := cmd.CombinedOutput() cancel() body := string(out) if err != nil || strings.Contains(body, "Uncaught ") { t.Fatalf("browser %s: %v %s", route, err, body) } if route == "/" && !strings.Contains(body, `test`) { t.Fatalf("async children missing: %s", body) } if route != "/" && !strings.Contains(body, `class="cm-editor`) { t.Fatalf("CodeMirror did not mount: %s", body) } } }