package controller
import (
"net/http"
"os"
"path/filepath"
"testing"
)
// V2.0.1 ③: with the public page off, the root path and theme paths
// answer a plain 404 to anonymous visitors instead of the theme
// shell (whose "public page off" redirect chain dead-ends on
// /admin). A logged-in operator still reaches the theme, and with
// the page on the shell is public again.
func TestProbeFallbackPublicPageGate(t *testing.T) {
rig := newProbeTestRig(t)
themeDir := filepath.Join(rig.dbDir, "themes", "testtheme", "dist")
if err := os.MkdirAll(themeDir, 0o755); err != nil {
t.Fatal(err)
}
manifest := `{"name":"Test Theme","short":"testtheme","description":"gate test fixture","version":"0.0.1","author":"tests","url":""}`
if err := os.WriteFile(filepath.Join(rig.dbDir, "themes", "testtheme", "theme.json"), []byte(manifest), 0o644); err != nil {
t.Fatal(err)
}
indexHTML := "
theme"
if err := os.WriteFile(filepath.Join(themeDir, "index.html"), []byte(indexHTML), 0o644); err != nil {
t.Fatal(err)
}
rig.setSetting(t, "theme", "testtheme")
// Public page off (the default): anonymous root + SPA paths 404.
if rec := rig.do(http.MethodGet, "/", nil, false); rec.Code != http.StatusNotFound {
t.Fatalf("anon root with public page off: %d, want 404", rec.Code)
}
if rec := rig.do(http.MethodGet, "/some/spa/route", nil, false); rec.Code != http.StatusNotFound {
t.Fatalf("anon spa path with public page off: %d, want 404", rec.Code)
}
// API 404 discipline is unchanged.
if rec := rig.do(http.MethodGet, "/api/nope", nil, false); rec.Code != http.StatusNotFound {
t.Fatalf("api 404 with public page off: %d", rec.Code)
}
// Logged-in operator still reaches the theme shell.
if rec := rig.do(http.MethodGet, "/", nil, true); rec.Code != 200 || rec.Body.String() != indexHTML {
t.Fatalf("authed root with public page off: %d %q", rec.Code, rec.Body.String())
}
// Public page on: anonymous shell access is back.
rig.setSetting(t, "public_page", "on")
if rec := rig.do(http.MethodGet, "/", nil, false); rec.Code != 200 || rec.Body.String() != indexHTML {
t.Fatalf("anon root with public page on: %d %q", rec.Code, rec.Body.String())
}
}