package server import ( "context" "net/http" "net/http/httptest" "os" "os/exec" "strings" "testing" "time" ) func TestThemePreferenceDoesNotChangeDomain(t *testing.T) { s, token, domain, _ := editorFixture(t) for _, tc := range []struct{ value, css string }{ {"light", "/static/css/theme/light.css"}, {"index-white", "/static/css/theme/index-white.css"}, {"https://evil.test/theme.css", "/static/css/theme/dark.css"}, {"unknown", "/static/css/theme/dark.css"}, } { r := httptest.NewRequest("GET", "http://localhost/e/-/editor-fixture", nil) r.Header.Set("Authorization", "Bearer "+token) r.AddCookie(&http.Cookie{Name: "pageswiki_theme", Value: tc.value}) w := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(w, r) if w.Code != 200 || !strings.Contains(w.Body.String(), `href="`+tc.css+`"`) { t.Fatalf("%s: %d %s", tc.value, w.Code, w.Body.String()) } if s.getTheme(domain) != "dark" { t.Fatal("browser preference overwrote Domain theme") } if !strings.Contains(strings.Join(w.Header().Values("Vary"), ","), "Cookie") { t.Fatal("missing cache variation") } } } func TestEditorThemeChromium(t *testing.T) { if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" { t.Skip("opt-in Chromium") } s, token, domain, _ := editorFixture(t) if err := s.store.SetConfigInDomain(domain, "editor_keymap", "vim"); err != nil { t.Fatal(err) } script, err := os.ReadFile("internal/server/testdata/editor-theme.js") if 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) if r.URL.Path == "/fixture" { w.Header().Set("Content-Type", "text/html; charset=utf-8") _, _ = w.Write([]byte("
")) return } s.withAuthorization(s.mux).ServeHTTP(w, r) })) defer server.Close() ctx, cancel := context.WithTimeout(context.Background(), 55*time.Second) defer cancel() args := []string{"internal/server/testdata/vim-browser-smoke.mjs", server.URL + "/fixture", t.TempDir()} if shot := os.Getenv("EDITOR_THEME_SCREENSHOT"); shot != "" { args = append(args, shot) } if out, err := exec.CommandContext(ctx, "node", args...).CombinedOutput(); err != nil { t.Fatalf("theme: %v\n%s", err, out) } }