test: cover the ICS writer and the calendar feed routes

This commit is contained in:
Jakub Zych
2026-10-02 17:38:48 +02:00
parent 90247a460d
commit b39f627fe3
6 changed files with 665 additions and 5 deletions

View File

@@ -4,6 +4,7 @@ import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/bouncer"
@@ -48,6 +49,10 @@ func handlers(h *api.Handlers) map[string]http.HandlerFunc {
"unmark": h.UnmarkRead,
"materials": h.Materials,
"material": h.Material,
"feed": h.CalendarFeed,
"feed-put": h.UpdateCalendarFeed,
"rotate": h.RotateCalendarFeed,
"feed-del": h.DeleteCalendarFeed,
}
}
@@ -142,3 +147,33 @@ func TestValidQueriesReachTheDatabase(t *testing.T) {
}
}
}
func TestCalendarICSRejectsMalformedTokens(t *testing.T) {
h := noDB()
good := strings.Repeat("ab", 32)
for _, token := range []string{"", "abc", good[:63], good + "0", strings.ToUpper(good), good[:62] + "zz", good + ".ical", "../" + good} {
code, body := serve(t, h.CalendarICS, http.MethodGet, "/", nil, map[string]string{"token": token})
if code != http.StatusNotFound {
t.Errorf("token %q = %d %v, want 404 without a database lookup", token, code, body)
}
}
// A well-formed token, with or without .ics, is looked up (503 here).
for _, token := range []string{good, good + ".ics"} {
if code, _ := serve(t, h.CalendarICS, http.MethodGet, "/", nil, map[string]string{"token": token}); code != http.StatusServiceUnavailable {
t.Errorf("token %q = %d, want 503 without a database", token, code)
}
}
}
func TestUpdateCalendarFeedRejectsAnInvalidBody(t *testing.T) {
h := noDB()
for _, body := range []string{"[1,2]", "{", `"x"`} {
req := httptest.NewRequest(http.MethodPut, "/", strings.NewReader(body))
req = req.WithContext(bouncer.WithUser(req.Context(), &bouncer.Principal{ID: 7}))
rec := httptest.NewRecorder()
h.UpdateCalendarFeed(rec, req)
if rec.Code != http.StatusBadRequest {
t.Errorf("body %s = %d %s, want 400", body, rec.Code, rec.Body.String())
}
}
}