108 lines
4.8 KiB
Go
108 lines
4.8 KiB
Go
package main
|
|
|
|
import (
|
|
"bytes"
|
|
"html/template"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestPublicChartTemplateParses(t *testing.T) {
|
|
tmpl, err := template.New("landwelle").Funcs(template.FuncMap{
|
|
"year": func() int { return 2026 }, "seoHead": seoHead, "playlistURL": playlistPageURL,
|
|
"publicNavigation": func() template.HTML { return navigationHTML(defaultNavigationItems()) },
|
|
"seq": func(a, b int) []int {
|
|
out := make([]int, 0, b-a+1)
|
|
for i := a; i <= b; i++ {
|
|
out = append(out, i)
|
|
}
|
|
return out
|
|
},
|
|
}).ParseFS(webFS, "web/partials.html", "web/charts-listener.html")
|
|
if err != nil {
|
|
t.Fatalf("public charts template does not parse: %v", err)
|
|
}
|
|
entry := ChartEntry{ID: 1, Position: 1, Artist: "Artist", Title: "Titel", FrontendVoting: true}
|
|
var out bytes.Buffer
|
|
if err := tmpl.ExecuteTemplate(&out, "charts-listener.html", ChartsPageData{PageData: pageData(), Config: chartPageConfig{ChartKey: "listener", RoutePath: "/charts/zuhoerer", VotingEnabled: true}, TopThree: []ChartEntry{entry}}); err != nil {
|
|
t.Fatalf("regular chart template execute: %v", err)
|
|
}
|
|
entry.FrontendVoting, entry.FrontendAnnual, entry.FrontendAnnualYear, entry.HasInfo = false, true, 2025, true
|
|
out.Reset()
|
|
if err := tmpl.ExecuteTemplate(&out, "charts-listener.html", ChartsPageData{PageData: pageData(), Config: chartPageConfig{ChartKey: "annual", RoutePath: "/charts/jahrescharts", Annual: true}, TopThree: []ChartEntry{entry}, AnnualYear: 2025, AnnualYears: []ChartYear{{Year: 2025, Label: "2025", Count: 1}}}); err != nil {
|
|
t.Fatalf("annual chart template execute: %v", err)
|
|
}
|
|
}
|
|
|
|
func TestPublicChartTemplateSupportsVotingPreviewAndAnnualInfo(t *testing.T) {
|
|
raw, err := os.ReadFile(filepath.Join("web", "charts-listener.html"))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
text := string(raw)
|
|
for _, want := range []string{"charts-v8.css", "charts-v7.js", "charts-podium", "charts-new-grid", "data-song-info", "data-preview-modal"} {
|
|
if !strings.Contains(text, want) {
|
|
t.Fatalf("public chart template missing %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestAdminChartTemplateRendersAllZonesAndAnnualRows(t *testing.T) {
|
|
tmpl, err := parseAdminTemplate("web/admin/management.html")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
entry := ChartEntry{ID: 11, ChartKey: "listener", Position: 4, Artist: "Landwelle Artist", Title: "Landwelle Titel", FixedVotePoints: 42, VotePoints: 47, VoteCount: 1}
|
|
data := adminManagementData{
|
|
Username: "admin", CSRFToken: "csrf", Module: "charts", DatabaseReady: true,
|
|
ChartKey: "listener", ChartTitle: "Zuhörer Charts", Charts: []ChartEntry{entry},
|
|
MainChartRows: makeAdminChartRows([]ChartEntry{entry}, "csrf", "listener"),
|
|
ChartMainEnd: 32, ChartTotalEnd: 45, ChartMainTitle: "Platz 4 bis 32", ChartPreviewTitle: "Neuvorstellungen 33 bis 45", ChartCanGenerate: true,
|
|
}
|
|
var out bytes.Buffer
|
|
if err := tmpl.ExecuteTemplate(&out, "management-admin.html", data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
html := out.String()
|
|
for _, want := range []string{"Landwelle Artist", "data-chart-zone", "data-chart-generate", "draggable=\"true\"", "admin-charts-v7.js", ">47<", "1 Online-Stimme"} {
|
|
if !strings.Contains(html, want) {
|
|
t.Fatalf("rendered chart admin missing %q", want)
|
|
}
|
|
}
|
|
|
|
annual := adminManagementData{
|
|
Username: "admin", CSRFToken: "csrf", Module: "charts", DatabaseReady: true,
|
|
ChartKey: "annual", ChartTitle: "Jahrescharts", SelectedYear: 2025,
|
|
ChartYears: []ChartYear{{Year: 2025, Label: "Jahrescharts 2025", Count: 1}},
|
|
ChartYearEntries: []ChartYearEntry{{ID: 12, ChartYear: 2025, Position: 1, Artist: "Jahres Artist", Title: "Jahres Titel"}},
|
|
AnnualChartRows: makeAdminAnnualChartRows([]ChartYearEntry{{ID: 12, ChartYear: 2025, Position: 1, Artist: "Jahres Artist", Title: "Jahres Titel"}}, "csrf", 2025),
|
|
}
|
|
out.Reset()
|
|
if err := tmpl.ExecuteTemplate(&out, "management-admin.html", annual); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(out.String(), "Titel-Infos KI") || !strings.Contains(out.String(), "Jahres Artist") {
|
|
t.Fatal("annual admin chart controls or rows missing")
|
|
}
|
|
}
|
|
|
|
func TestChartSecurityHeadersAllowYouTubeOnlyOnChartsAndOnTour(t *testing.T) {
|
|
next := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) })
|
|
for _, path := range []string{"/charts/zuhoerer", "/charts/docx", "/charts/jahrescharts", "/ontour"} {
|
|
rec := httptest.NewRecorder()
|
|
securityHeaders(next).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, path, nil))
|
|
if !strings.Contains(rec.Header().Get("Content-Security-Policy"), "youtube-nocookie.com") {
|
|
t.Fatalf("youtube frame source missing for %s", path)
|
|
}
|
|
}
|
|
rec := httptest.NewRecorder()
|
|
securityHeaders(next).ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/", nil))
|
|
if strings.Contains(rec.Header().Get("Content-Security-Policy"), "youtube") {
|
|
t.Fatal("youtube frame source leaked to unrelated public page")
|
|
}
|
|
}
|