2026-11-28 11:11:28 +01:00
|
|
|
package layout
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"net/url"
|
|
|
|
|
"strings"
|
|
|
|
|
"testing"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestStyleAttributeAndMenu(t *testing.T) {
|
|
|
|
|
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("NewRenderer: %v", err)
|
|
|
|
|
}
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
if err := r.Page(&buf, "dashboard", PageHeader{}); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if !strings.Contains(buf.String(), `<html lang="en" data-style="modern">`) {
|
|
|
|
|
t.Errorf("unset Style does not render the modern default\n---\n%s", buf.String())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
buf.Reset()
|
|
|
|
|
data := PageHeader{Lang: "de", Style: StyleClassic, AppearancePath: "/appearance"}
|
|
|
|
|
if err := r.Page(&buf, "dashboard", data); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
out := buf.String()
|
|
|
|
|
for _, want := range []string{
|
|
|
|
|
`<html lang="de" data-style="classic">`,
|
|
|
|
|
`<h6>Stil</h6>`,
|
|
|
|
|
`name="style" value="modern" aria-pressed="false">Modern</button>`,
|
|
|
|
|
`name="style" value="classic" aria-pressed="true">Klassisch</button>`,
|
|
|
|
|
} {
|
|
|
|
|
if !strings.Contains(out, want) {
|
|
|
|
|
t.Errorf("rendered output missing %q\n---\n%s", want, out)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestStyleCookie(t *testing.T) {
|
|
|
|
|
h := AppearanceHandler("/ui")
|
|
|
|
|
form := url.Values{"style": {StyleModern}, "back": {"/x"}}
|
|
|
|
|
req := httptest.NewRequest(http.MethodPost, "/ui/appearance", strings.NewReader(form.Encode()))
|
|
|
|
|
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
rec := httptest.NewRecorder()
|
|
|
|
|
h.ServeHTTP(rec, req)
|
|
|
|
|
cookies := rec.Result().Cookies()
|
|
|
|
|
if rec.Code != http.StatusSeeOther || len(cookies) != 1 || cookies[0].Name != StyleCookie || cookies[0].Value != StyleModern {
|
|
|
|
|
t.Fatalf("got %d, cookies %+v", rec.Code, cookies)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bad := httptest.NewRequest(http.MethodPost, "/ui/appearance", strings.NewReader("style=fancy"))
|
|
|
|
|
bad.Header.Set("Content-Type", "application/x-www-form-urlencoded")
|
|
|
|
|
rec = httptest.NewRecorder()
|
|
|
|
|
h.ServeHTTP(rec, bad)
|
|
|
|
|
if rec.Code != http.StatusBadRequest {
|
|
|
|
|
t.Errorf("unknown style: got %d, want 400", rec.Code)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
get := httptest.NewRequest(http.MethodGet, "/ui/x", nil)
|
|
|
|
|
get.AddCookie(cookies[0])
|
|
|
|
|
hdr := PageHeader{BasePath: "/ui"}
|
|
|
|
|
hdr.ApplyAppearance(get)
|
|
|
|
|
if hdr.Style != StyleModern {
|
|
|
|
|
t.Errorf("Style = %q, want %q from the cookie", hdr.Style, StyleModern)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestSidebarGroupsAndIcons(t *testing.T) {
|
|
|
|
|
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("NewRenderer: %v", err)
|
|
|
|
|
}
|
|
|
|
|
data := PageHeader{
|
|
|
|
|
BasePath: "/ui",
|
|
|
|
|
ActiveNav: "b",
|
|
|
|
|
NavItems: []NavItem{
|
|
|
|
|
{ID: "o", Label: "Overview", Href: "/"},
|
|
|
|
|
{ID: "a", Label: "Account", Href: "/a", Group: "Account", Icon: "user"},
|
|
|
|
|
{ID: "b", Label: "Security", Href: "/b", Group: "Account", Icon: "shield"},
|
|
|
|
|
{ID: "c", Label: "Hosts", Href: "/c", Group: "Infra", Icon: "no-such-icon"},
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
if err := r.Page(&buf, "dashboard", data); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
out := buf.String()
|
|
|
|
|
if n := strings.Count(out, `<div class="yw-nav-group">Account</div>`); n != 1 {
|
|
|
|
|
t.Errorf("group heading Account rendered %d times, want 1\n---\n%s", n, out)
|
|
|
|
|
}
|
|
|
|
|
for _, want := range []string{
|
|
|
|
|
`<div class="yw-nav-group">Infra</div>`,
|
|
|
|
|
`<a class="nav-link active" href="/ui/b" aria-current="page"><span class="yw-nav-icon" aria-hidden="true"><svg`,
|
|
|
|
|
`<a class="nav-link" href="/ui/c">Hosts</a>`,
|
|
|
|
|
`<a class="nav-link" href="/ui/">Overview</a>`,
|
|
|
|
|
} {
|
|
|
|
|
if !strings.Contains(out, want) {
|
|
|
|
|
t.Errorf("rendered output missing %q\n---\n%s", want, out)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2026-11-28 12:23:20 +01:00
|
|
|
|
|
|
|
|
// A page without NavItems (a login page) renders no sidebar container:
|
|
|
|
|
// the modern style draws it as a card, which would show up empty.
|
|
|
|
|
func TestSidebarWithoutItems(t *testing.T) {
|
|
|
|
|
r, err := NewRenderer(testPagesFS(), "pages", []string{"dashboard"}, ".html.tmpl", testFuncs())
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatalf("NewRenderer: %v", err)
|
|
|
|
|
}
|
|
|
|
|
var buf bytes.Buffer
|
|
|
|
|
if err := r.Page(&buf, "dashboard", PageHeader{Title: "Log in"}); err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
|
|
|
|
if strings.Contains(buf.String(), `class="nav flex-column`) {
|
|
|
|
|
t.Errorf("empty sidebar container rendered\n---\n%s", buf.String())
|
|
|
|
|
}
|
|
|
|
|
}
|