package admin import ( "net/http" "net/http/httptest" "net/url" "strings" "testing" ) func formRequest(values url.Values) *http.Request { req := httptest.NewRequest("POST", "/admin/events", strings.NewReader(values.Encode())) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") return req } func baseEventForm() url.Values { v := url.Values{} v.Set("name", "CascadiaJS") v.Set("start_time", "2026-05-31T09:00") v.Set("end_time", "2026-05-31T17:00") return v } func TestParseEventForm_ParsesLinks(t *testing.T) { v := baseEventForm() v.Set("link_label_0", "Schedule") v.Set("link_url_0", "https://example.com/s") v.Set("link_label_1", "") // empty label, has URL -> kept (host fallback at render) v.Set("link_url_1", "https://example.com/map") v.Set("link_label_2", "Ghost") // label only, no URL -> dropped v.Set("link_url_2", "") in, _, errMsg := parseEventForm(formRequest(v)) if errMsg != "" { t.Fatalf("unexpected error: %s", errMsg) } if len(in.Links) != 2 { t.Fatalf("got %d links, want 2: %+v", len(in.Links), in.Links) } if in.Links[0].Label != "Schedule" || in.Links[0].URL != "https://example.com/s" { t.Errorf("link[0] = %+v", in.Links[0]) } if in.Links[1].URL != "https://example.com/map" { t.Errorf("link[1] = %+v", in.Links[1]) } } func TestParseEventForm_RejectsNonHTTPLink(t *testing.T) { v := baseEventForm() v.Set("link_url_0", "javascript:alert(1)") _, _, errMsg := parseEventForm(formRequest(v)) if errMsg == "" { t.Fatal("expected error for non-http link, got none") } } func TestParseEventForm_NoLinks(t *testing.T) { in, _, errMsg := parseEventForm(formRequest(baseEventForm())) if errMsg != "" { t.Fatalf("unexpected error: %s", errMsg) } if len(in.Links) != 0 { t.Errorf("expected no links, got %+v", in.Links) } }