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.
100 lines
3.5 KiB
Go
100 lines
3.5 KiB
Go
package server
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"pageswiki/internal/db"
|
|
"pageswiki/internal/server/routes"
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func TestHomeLayoutsAndScopedTreeRoutes(t *testing.T) {
|
|
t.Chdir("../..")
|
|
database, err := db.Open(t.TempDir() + "/navigation.db")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
t.Cleanup(func() { _ = database.Close() })
|
|
if err := db.Migrate(database); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
st := store.New(database)
|
|
if err := st.EnsureConfigTable(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
domain, err := st.GetDefaultDomain()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s := &Server{store: st}
|
|
s.mustLoadTemplates()
|
|
for _, path := range []string{"/top", "/docs/child", "/unrelated"} {
|
|
object, err := st.CreatePageAtPathInDomain(domain.UUID, path)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := st.SaveRevisionContent(object, []byte("<p>Home content</p>"), "test", "und", "text/html", "page.html"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
if err := st.SetConfigInDomain(domain.UUID, "home_path", "/top"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := st.SetConfigInDomain(domain.UUID, "home_bookmarks", `[{"label":"Documentation","href":"/docs"}]`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
mux := http.NewServeMux()
|
|
routes.RegisterUIRoutes(mux, routes.UIHandlers{Home: s.handleHome, Tree: s.handleTree, Browser: s.handleBrowser})
|
|
for _, layout := range []string{"classic", "search", "directory"} {
|
|
if err := st.SetConfigInDomain(domain.UUID, "home_layout", layout); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
mux.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "http://localhost/", nil))
|
|
body := rr.Body.String()
|
|
if rr.Code != 200 || strings.Contains(body, "can't evaluate") || !strings.Contains(body, `name="scope" value="/"`) {
|
|
t.Fatalf("%s: %d %s", layout, rr.Code, body)
|
|
}
|
|
if layout == "classic" && !strings.Contains(body, `class="topbar"`) {
|
|
t.Fatal("classic topbar missing")
|
|
}
|
|
if layout != "classic" && strings.Contains(body, `class="topbar"`) {
|
|
t.Fatal("search home has topbar")
|
|
}
|
|
if layout == "directory" && !strings.Contains(body, "Documentation") {
|
|
t.Fatal("bookmarks missing")
|
|
}
|
|
}
|
|
rr := httptest.NewRecorder()
|
|
mux.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "http://localhost/v/tree/docs", nil))
|
|
body := rr.Body.String()
|
|
if rr.Code != 200 || !strings.Contains(body, `data-path="/docs/child"`) || strings.Contains(body, `data-path="/unrelated"`) || strings.Contains(body, `data-path="/docs"`) {
|
|
t.Fatalf("scoped tree: %d %s", rr.Code, body)
|
|
}
|
|
policy := policyForRequest(httptest.NewRequest(http.MethodGet, "http://localhost/v/tree/docs", nil))
|
|
if policy.Resolver != resolveHandler {
|
|
t.Fatalf("scoped tree authorization: %#v", policy)
|
|
}
|
|
// Directly visiting /top remains a folder-scoped Viewer, not a home layout.
|
|
rr = httptest.NewRecorder()
|
|
mux.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "http://localhost/top", nil))
|
|
if rr.Code != 200 || !strings.Contains(rr.Body.String(), `name="scope" value="/top"`) {
|
|
t.Fatalf("semantic viewer: %d %s", rr.Code, rr.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestHomeBookmarksRejectUnsafeLinks(t *testing.T) {
|
|
for _, raw := range []string{"bad | javascript:alert(1)", "bad | //evil.test/path", "bad | /\\evil.test", "missing separator"} {
|
|
if _, err := parseHomeBookmarks(raw); err == nil {
|
|
t.Fatalf("accepted %q", raw)
|
|
}
|
|
}
|
|
items, err := parseHomeBookmarks("Docs | /docs\nExternal | https://example.com/\n")
|
|
if err != nil || len(items) != 2 {
|
|
t.Fatalf("bookmarks=%#v err=%v", items, err)
|
|
}
|
|
}
|