Files

91 lines
2.4 KiB
Go

package api
import (
"strings"
"testing"
"home-vue-go/internal/config"
)
func TestURLValidation(t *testing.T) {
for _, value := range []string{"https://example.com", "http://localhost:8080"} {
if !validHTTPURL(value) {
t.Errorf("expected valid site URL: %s", value)
}
}
for _, value := range []string{"javascript:alert(1)", "//example.com", "example.com"} {
if validHTTPURL(value) {
t.Errorf("expected invalid site URL: %s", value)
}
}
for _, value := range []string{"mailto:a@example.com", "tel:+8613800000000", "https://example.com"} {
if !validContactURL(value) {
t.Errorf("expected valid contact URL: %s", value)
}
}
for _, value := range []string{"/uploads/qr.png", "https://example.com/qr.png"} {
if !validQRCode(value) {
t.Errorf("expected valid QR path: %s", value)
}
}
for _, value := range []string{"/uploads/../secret.png", "data:image/png;base64,abc"} {
if validQRCode(value) {
t.Errorf("expected invalid QR path: %s", value)
}
}
}
func TestIconValueValidation(t *testing.T) {
for _, value := range []string{
"fas fa-home",
"fab fa-github",
"iconify:mdi:home",
"iconify:material-symbols:add-home-outline",
} {
if !validIconValue(value) {
t.Errorf("expected valid icon value: %s", value)
}
}
for _, value := range []string{
"",
"iconify:MDI:home",
"iconify:mdi:",
"iconify:mdi:home/../../x",
"fas fa-home\"><script>",
strings.Repeat("a", maxIconValueLength+1),
} {
if validIconValue(value) {
t.Errorf("expected invalid icon value: %s", value)
}
}
}
func TestAboutLinkIconValidation(t *testing.T) {
settings := config.DefaultSiteSettings()
settings.AboutLinks[0].Icon = ""
if err := validateSiteSettings(settings); err != nil {
t.Fatalf("expected empty about link icon to use the frontend fallback: %v", err)
}
settings.AboutLinks[0].Icon = "iconify:tabler:brand-github"
if err := validateSiteSettings(settings); err != nil {
t.Fatalf("expected valid Iconify about link: %v", err)
}
settings.AboutLinks[0].Icon = "<script>"
if err := validateSiteSettings(settings); err == nil {
t.Fatal("expected invalid about link icon to be rejected")
}
}
func TestPercentageCountsSumsToOneHundred(t *testing.T) {
values := percentageCounts(map[string]int{"direct": 1, "search": 1, "other": 1}, 3)
total := 0
for _, value := range values {
total += value["value"].(int)
}
if total != 100 {
t.Fatalf("percentage total = %d, want 100", total)
}
}