package server import ( "encoding/json" "net/http" "net/http/httptest" "net/url" "strings" "testing" "pageswiki/internal/store" ) func saveWallpaperFixture(t *testing.T, s *Server, domain, path, contentType string) { t.Helper() object, err := s.store.CreatePageAtPathInDomain(domain, path) if err != nil { t.Fatal(err) } if _, err := s.store.SaveRevisionContent(object, []byte("fixture"), "test", "und", contentType, strings.TrimPrefix(path, "/wallpapers/")); err != nil { t.Fatal(err) } } func TestAuthPageUsesConfiguredWallpaperAndGlass(t *testing.T) { s, _, domain := configFixture(t) s.mux.HandleFunc("/_/auth/", s.handleAuth) saveWallpaperFixture(t, s, domain, "/wallpapers/login.jpg", "image/jpeg") for key, value := range map[string]string{ "home_wallpaper_path": "/wallpapers/login.jpg", "home_wallpaper_opacity": "41", "home_glass_transparency": "47", } { if err := s.store.SetConfigInDomain(domain, key, value); err != nil { t.Fatal(err) } } if err := s.store.SetAuthProviderConfig(domain, store.AuthProviderConfig{Kind: "oidc", Enabled: true, Issuer: "https://id.example.test", ClientID: "pages"}); err != nil { t.Fatal(err) } request := httptest.NewRequest(http.MethodGet, "http://localhost/_/auth/login", nil) response := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(response, request) body := response.Body.String() for _, expected := range []string{ `class="auth-page"`, `--home-glass-opacity:53%`, `class="home-wallpaper" src="/_/auth/wallpaper"`, `style="opacity:0.41"`, `/static/css/auth.css?v=1`, `class="auth-provider-button"`, `OpenID Connectでログイン`, } { if response.Code != http.StatusOK || !strings.Contains(body, expected) { t.Fatalf("auth page missing %q: status=%d body=%s", expected, response.Code, body) } } wallpaper := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(wallpaper, httptest.NewRequest(http.MethodGet, "http://localhost/_/auth/wallpaper?path=/private/ignored", nil)) if wallpaper.Code != http.StatusOK || wallpaper.Body.String() != "fixture" || wallpaper.Header().Get("Content-Type") != "image/jpeg" || wallpaper.Header().Get("X-Content-Type-Options") != "nosniff" { t.Fatalf("login wallpaper: status=%d type=%q body=%q", wallpaper.Code, wallpaper.Header().Get("Content-Type"), wallpaper.Body.String()) } method := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(method, httptest.NewRequest(http.MethodPost, "http://localhost/_/auth/wallpaper", nil)) if method.Code != http.StatusMethodNotAllowed || method.Header().Get("Allow") != "GET, HEAD" { t.Fatalf("login wallpaper accepted mutation: %d", method.Code) } } func TestHomeWallpaperFolderPickerAndTheme(t *testing.T) { s, token, domain := configFixture(t) s.mux.HandleFunc("/_/api/v1/path-picker", s.handlePathPickerAPI) saveWallpaperFixture(t, s, domain, "/wallpapers/a.jpg", "image/jpeg") saveWallpaperFixture(t, s, domain, "/wallpapers/b.png", "image/png") saveWallpaperFixture(t, s, domain, "/wallpapers/readme.txt", "text/plain") form := url.Values{ "section": {"home"}, "home_layout": {"search"}, "home_wallpaper_path": {"/wallpapers"}, "home_wallpaper_opacity": {"41"}, "home_glass_transparency": {"47"}, "locale": {"ja"}, "timezone": {"Asia/Tokyo"}, "theme": {"fate-t-dark"}, "zombie_grace_hours": {"24"}, "virtual_folder_pages": {"1"}, } response := configRequest(s, token, http.MethodPost, "/-/sys/config", form) if response.Code != http.StatusFound { t.Fatalf("save: %d %s", response.Code, response.Body.String()) } if got := s.getTheme(domain); got != "fate-t-dark" { t.Fatalf("theme=%q", got) } path, opacity := s.homeWallpaperConfig(domain) if path != "/wallpapers" || opacity != 41 { t.Fatalf("wallpaper=%q opacity=%d", path, opacity) } if got := s.homeGlassTransparency(domain); got != 47 { t.Fatalf("glass transparency=%d", got) } r := httptest.NewRequest(http.MethodGet, "http://localhost/", nil) r.Header.Set("Authorization", "Bearer "+token) view := s.homeWallpaperForRequest(r) if view.Opacity != "0.41" || !strings.HasPrefix(view.URL, "/v/raw/rev:") || len(view.Key) != 64 { t.Fatalf("view=%#v", view) } homeResponse := httptest.NewRecorder() s.handleHome(homeResponse, r) homeBody := homeResponse.Body.String() if homeResponse.Code != http.StatusOK || !strings.Contains(homeBody, `class="home-wallpaper"`) || !strings.Contains(homeBody, `style="opacity:0.41"`) || !strings.Contains(homeBody, `--home-glass-opacity:53%`) || !strings.Contains(homeBody, `/static/css/theme/fate-t-dark.css`) { t.Fatalf("home wallpaper render: %d %s", homeResponse.Code, homeBody) } settings := configRequest(s, token, http.MethodGet, "/-/sys/config?panel=home", nil) if settings.Code != http.StatusOK || !strings.Contains(settings.Body.String(), `data-picker-mode="image-or-folder"`) || !strings.Contains(settings.Body.String(), `value="fate-t-dark"`) { t.Fatalf("wallpaper settings UI: %d", settings.Code) } picker := configRequest(s, token, http.MethodGet, "/_/api/v1/path-picker?path=%2Fwallpapers&mode=image-or-folder", nil) var listing struct { Path string `json:"path"` Entries []pathPickerEntry `json:"entries"` } if picker.Code != http.StatusOK || json.Unmarshal(picker.Body.Bytes(), &listing) != nil { t.Fatalf("picker: %d %s", picker.Code, picker.Body.String()) } if listing.Path != "/wallpapers" || len(listing.Entries) != 2 { t.Fatalf("listing=%#v", listing) } direct := configRequest(s, token, http.MethodGet, "/_/api/v1/path-picker?path=%2Fwallpapers%2Fa.jpg&mode=image-or-folder", nil) var selected struct { Path string `json:"path"` Selected string `json:"selected"` } if direct.Code != http.StatusOK || json.Unmarshal(direct.Body.Bytes(), &selected) != nil || selected.Path != "/wallpapers" || selected.Selected != "/wallpapers/a.jpg" { t.Fatalf("direct picker: %d %s", direct.Code, direct.Body.String()) } } func TestHomeWallpaperValidation(t *testing.T) { for _, value := range []string{"relative", "/wallpapers/../private", "/bad\\path", "/bad\npath"} { if _, ok := normalizePickerPath(value); ok { t.Fatalf("accepted %q", value) } } }