From ab37619919dfe6e3e8fa061fff7f136b360cfd5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20M=C3=BCller?= <1005065+DeepDiver1975@users.noreply.github.com> Date: Thu, 22 Feb 2024 21:19:58 +0100 Subject: [PATCH] caldav: add support for more props on MKCOL and PROPFIND --- caldav/caldav.go | 2 + caldav/elements.go | 33 +++- caldav/server.go | 23 ++- caldav/server_test.go | 393 ++++++++++++++++++++++++++++++++++++++++-- 4 files changed, 426 insertions(+), 25 deletions(-) diff --git a/caldav/caldav.go b/caldav/caldav.go index 02705ef..21040c9 100644 --- a/caldav/caldav.go +++ b/caldav/caldav.go @@ -67,8 +67,10 @@ type Calendar struct { Path string Name string Description string + Color string MaxResourceSize int64 SupportedComponentSet []string + Timezone string } type CalendarCompRequest struct { diff --git a/caldav/elements.go b/caldav/elements.go index 5759f31..2dcc507 100644 --- a/caldav/elements.go +++ b/caldav/elements.go @@ -21,8 +21,16 @@ var ( calendarQueryName = xml.Name{namespace, "calendar-query"} calendarMultigetName = xml.Name{namespace, "calendar-multiget"} - calendarName = xml.Name{namespace, "calendar"} - calendarDataName = xml.Name{namespace, "calendar-data"} + calendarName = xml.Name{namespace, "calendar"} + calendarDataName = xml.Name{namespace, "calendar-data"} + calendarColorName = xml.Name{ + Space: "http://apple.com/ns/ical/", + Local: "calendar-color", + } + calendarTimezoneName = xml.Name{ + Space: namespace, + Local: "calendar-timezone", + } ) // https://tools.ietf.org/html/rfc4791#section-6.2.1 @@ -41,6 +49,16 @@ type calendarDescription struct { Description string `xml:",chardata"` } +type calendarColor struct { + XMLName xml.Name `xml:"http://apple.com/ns/ical/ calendar-color"` + Color string `xml:",chardata"` +} + +type calendarTimezone struct { + XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav calendar-timezone"` + Timezone string `xml:",chardata"` +} + // https://tools.ietf.org/html/rfc4791#section-5.2.4 type supportedCalendarData struct { XMLName xml.Name `xml:"urn:ietf:params:xml:ns:caldav supported-calendar-data"` @@ -230,8 +248,11 @@ func (r *reportReq) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error { } type mkcolReq struct { - XMLName xml.Name `xml:"DAV: mkcol"` - ResourceType internal.ResourceType `xml:"set>prop>resourcetype"` - DisplayName string `xml:"set>prop>displayname"` - // TODO this could theoretically contain all addressbook properties? + XMLName xml.Name `xml:"DAV: mkcol"` + ResourceType internal.ResourceType `xml:"set>prop>resourcetype"` + DisplayName string `xml:"set>prop>displayname"` + Description string `xml:"set>prop>calendar-description"` + CalendarColor string `xml:"set>prop>calendar-color"` + CalendarTimeZone string `xml:"set>prop>calendar-timezone"` + SupportedCalendarComponentSet supportedCalendarComponentSet `xml:"set>prop>supported-calendar-component-set"` } diff --git a/caldav/server.go b/caldav/server.go index d3e83b8..485aeb1 100644 --- a/caldav/server.go +++ b/caldav/server.go @@ -568,6 +568,20 @@ func (b *backend) propFindCalendar(ctx context.Context, propfind *internal.PropF return &calendarDescription{Description: cal.Description}, nil } } + if cal.Color != "" { + props[calendarColorName] = func(*internal.RawXMLValue) (interface{}, error) { + return &calendarColor{ + Color: cal.Color, + }, nil + } + } + if cal.Timezone != "" { + props[calendarTimezoneName] = func(*internal.RawXMLValue) (interface{}, error) { + return &calendarTimezone{ + Timezone: cal.Timezone, + }, nil + } + } if cal.MaxResourceSize > 0 { props[maxResourceSizeName] = func(*internal.RawXMLValue) (interface{}, error) { return &maxResourceSize{Size: cal.MaxResourceSize}, nil @@ -737,7 +751,14 @@ func (b *backend) Mkcol(r *http.Request) error { return internal.HTTPErrorf(http.StatusBadRequest, "carddav: unexpected resource type") } cal.Name = m.DisplayName - // TODO ... + cal.Description = m.Description + cal.Color = strings.TrimSpace(m.CalendarColor) + cal.Timezone = strings.TrimSpace(m.CalendarTimeZone) + + cal.SupportedComponentSet = make([]string, len(m.SupportedCalendarComponentSet.Comp)) + for i, v := range m.SupportedCalendarComponentSet.Comp { + cal.SupportedComponentSet[i] = v.Name + } } return b.Backend.CreateCalendar(r.Context(), &cal) diff --git a/caldav/server_test.go b/caldav/server_test.go index 594b87b..584dcf7 100644 --- a/caldav/server_test.go +++ b/caldav/server_test.go @@ -2,7 +2,9 @@ package caldav import ( "context" + "encoding/xml" "fmt" + "github.com/emersion/go-webdav/internal" "io" "io/ioutil" "net/http/httptest" @@ -22,9 +24,9 @@ var propFindSupportedCalendarComponentRequest = ` ` var testPropFindSupportedCalendarComponentCases = map[*Calendar][]string{ - &Calendar{Path: "/user/calendars/cal"}: []string{"VEVENT"}, - &Calendar{Path: "/user/calendars/cal", SupportedComponentSet: []string{"VTODO"}}: []string{"VTODO"}, - &Calendar{Path: "/user/calendars/cal", SupportedComponentSet: []string{"VEVENT", "VTODO"}}: []string{"VEVENT", "VTODO"}, + &Calendar{Path: "/user/calendars/cal"}: {"VEVENT"}, + &Calendar{Path: "/user/calendars/cal", SupportedComponentSet: []string{"VTODO"}}: {"VTODO"}, + &Calendar{Path: "/user/calendars/cal", SupportedComponentSet: []string{"VEVENT", "VTODO"}}: {"VEVENT", "VTODO"}, } func TestPropFindSupportedCalendarComponent(t *testing.T) { @@ -33,7 +35,7 @@ func TestPropFindSupportedCalendarComponent(t *testing.T) { req.Body = io.NopCloser(strings.NewReader(propFindSupportedCalendarComponentRequest)) req.Header.Set("Content-Type", "application/xml") w := httptest.NewRecorder() - handler := Handler{Backend: testBackend{calendars: []Calendar{*calendar}}} + handler := Handler{Backend: &testBackend{calendars: []Calendar{*calendar}}} handler.ServeHTTP(w, req) res := w.Result() @@ -52,6 +54,108 @@ func TestPropFindSupportedCalendarComponent(t *testing.T) { } } +var propFindCalendarRequest = ` + + + + + + + + +` + +func TestPropFindCalendar(t *testing.T) { + calendar := Calendar{ + Path: "/user/calendars/cal", + Name: "Test Calendar", + Description: "This is a test calendar", + Timezone: "BEGIN:VCALENDARfoo", + Color: "#DEADBEEF", + } + + req := httptest.NewRequest("PROPFIND", calendar.Path, nil) + req.Body = io.NopCloser(strings.NewReader(propFindCalendarRequest)) + req.Header.Set("Content-Type", "application/xml") + w := httptest.NewRecorder() + handler := Handler{Backend: &testBackend{calendars: []Calendar{calendar}}} + handler.ServeHTTP(w, req) + + resp := w.Result() + + var ms internal.MultiStatus + err := xml.NewDecoder(resp.Body).Decode(&ms) + if err != nil { + t.Fatalf("Unexpcted error in xml.NewDecoder: %s", err) + } + if len(ms.Responses) != 1 { + t.Fatalf("Found %d multi status responses, expected 1", len(ms.Responses)) + } + if len(ms.Responses[0].PropStats) != 1 { + t.Fatalf("Found %d prop stats, expected 1", len(ms.Responses[0].PropStats)) + } + if ms.Responses[0].PropStats[0].Status.Code != 200 { + t.Fatalf("Received %d prop stat status, expected 200", ms.Responses[0].PropStats[0].Status.Code) + } + if len(ms.Responses[0].PropStats[0].Prop.Raw) != 4 { + t.Fatalf("Found %d props, expected 4", len(ms.Responses[0].PropStats[0].Prop.Raw)) + } + + rawDisplayName := ms.Responses[0].PropStats[0].Prop.Get(internal.DisplayNameName) + rawCalendarDescription := ms.Responses[0].PropStats[0].Prop.Get(calendarDescriptionName) + rawTimezone := ms.Responses[0].PropStats[0].Prop.Get(calendarTimezoneName) + rawColor := ms.Responses[0].PropStats[0].Prop.Get(calendarColorName) + if rawDisplayName == nil { + t.Fatal("Got unexpected nil rawDisplayName") + } + if rawCalendarDescription == nil { + t.Fatal("Got unexpected nil rawCalendarDescription") + } + if rawTimezone == nil { + t.Fatal("Got unexpected nil rawTimezone") + } + if rawColor == nil { + t.Fatal("Got unexpected nil rawColor") + } + + v0 := internal.DisplayName{} + err = rawDisplayName.Decode(&v0) + if err != nil { + t.Fatalf("Unexpcted error in rawDisplayName.Decode: %s", err) + } + if calendar.Name != v0.Name { + t.Fatalf("Calendar name is '%s', expected '%s'", calendar.Name, v0.Name) + } + + v1 := calendarDescription{} + err = rawCalendarDescription.Decode(&v1) + if err != nil { + t.Fatalf("Unexpcted error in rawCalendarDescription.Decode: %s", err) + } + if calendar.Description != v1.Description { + t.Fatalf("Calendar description is '%s', expected '%s'", calendar.Description, v1.Description) + } + + v2 := calendarTimezone{} + err = rawTimezone.Decode(&v2) + if err != nil { + t.Fatalf("Unexpcted error in rawTimezone.Decode: %s", err) + } + if calendar.Timezone != v2.Timezone { + t.Fatalf("Calendar timezone is '%s', expected '%s'", calendar.Timezone, v2.Timezone) + } + + v3 := calendarColor{} + err = rawColor.Decode(&v3) + if err != nil { + t.Fatalf("Unexpcted error in rawColor.Decode: %s", err) + } + if calendar.Color != v3.Color { + t.Fatalf("Calendar color is '%s', expected '%s'", calendar.Color, v3.Color) + } +} + var propFindUserPrincipal = ` @@ -68,7 +172,7 @@ func TestPropFindRoot(t *testing.T) { req.Header.Set("Content-Type", "application/xml") w := httptest.NewRecorder() calendar := &Calendar{} - handler := Handler{Backend: testBackend{calendars: []Calendar{*calendar}}} + handler := Handler{Backend: &testBackend{calendars: []Calendar{*calendar}}} handler.ServeHTTP(w, req) res := w.Result() @@ -118,7 +222,7 @@ func TestMultiCalendarBackend(t *testing.T) { req := httptest.NewRequest("PROPFIND", "/user/calendars/", strings.NewReader(propFindUserPrincipal)) req.Header.Set("Content-Type", "application/xml") w := httptest.NewRecorder() - handler := Handler{Backend: testBackend{ + handler := Handler{Backend: &testBackend{ calendars: calendars, objectMap: map[string][]CalendarObject{ calendarB.Path: []CalendarObject{object}, @@ -177,41 +281,294 @@ func TestMultiCalendarBackend(t *testing.T) { } } +var mkcolRequestData = ` + + + + + + + + + Test calendar + A calendar for testing + #009688FF + + + + + + + + + + + +` + +func TestCreateCalendar(t *testing.T) { + tb := testBackend{ + calendars: nil, + objectMap: nil, + } + b := backend{ + Backend: &tb, + Prefix: "/dav", + } + req := httptest.NewRequest("MKCOL", "/dav/calendars/user0/test-calendar", strings.NewReader(mkcolRequestData)) + req.Header.Set("Content-Type", "application/xml") + + err := b.Mkcol(req) + if err != nil { + t.Fatalf("Unexpcted error in Mkcol: %s", err) + } + if len(tb.calendars) != 1 { + t.Fatalf("Found %d calendars, expected 1", len(tb.calendars)) + } + c := tb.calendars[0] + if c.Name != "Test calendar" { + t.Fatalf("Calendar name is '%s', expected 'Test calendar'", c.Name) + } + expectedPath := "/dav/calendars/user0/test-calendar" + if c.Path != expectedPath { + t.Fatalf("Calendar path is '%s', expected '%s'", c.Path, expectedPath) + } + expectedDescription := "A calendar for testing" + if c.Description != expectedDescription { + t.Fatalf("Calendar description is '%s', expected '%s'", c.Description, expectedDescription) + } + expectedColor := "#009688FF" + if c.Color != expectedColor { + t.Fatalf("Calendar color is '%s', expected '%s'", c.Color, expectedColor) + } + expectedTimezone := "BEGIN:VCALENDAR" + if !strings.Contains(c.Timezone, expectedTimezone) { + t.Fatalf("Calendar timezone is '%s', expected to contain '%s'", c.Timezone, expectedTimezone) + } + if len(c.SupportedComponentSet) != 3 { + t.Fatalf("Found %d SupportedComponentSet, expected 3", len(c.SupportedComponentSet)) + } + if c.SupportedComponentSet[0] != "VEVENT" { + t.Fatalf("Calendar 0.SupportedComponentSet is '%s', expected '%s'", c.SupportedComponentSet[0], "VEVENT") + } + if c.SupportedComponentSet[1] != "VTODO" { + t.Fatalf("Calendar 1.SupportedComponentSet is '%s', expected '%s'", c.SupportedComponentSet[1], "VTODO") + } + if c.SupportedComponentSet[2] != "VJOURNAL" { + t.Fatalf("Calendar 2.SupportedComponentSet is '%s', expected '%s'", c.SupportedComponentSet[2], "VJOURNAL") + } +} + +var mkcolRequestDataMinimalBody = ` + + + + + + + + + Test calendar + + +` + +func TestCreateCalendarMinimalBody(t *testing.T) { + tb := testBackend{ + calendars: nil, + objectMap: nil, + } + b := backend{ + Backend: &tb, + Prefix: "/dav", + } + req := httptest.NewRequest("MKCOL", "/dav/calendars/user0/test-calendar", strings.NewReader(mkcolRequestDataMinimalBody)) + req.Header.Set("Content-Type", "application/xml") + + err := b.Mkcol(req) + if err != nil { + t.Fatalf("Unexpcted error in Mkcol: %s", err) + } + if len(tb.calendars) != 1 { + t.Fatalf("Found %d calendars, expected 1", len(tb.calendars)) + } + c := tb.calendars[0] + if c.Name != "Test calendar" { + t.Fatalf("Calendar name is '%s', expected 'Test calendar'", c.Name) + } + expectedPath := "/dav/calendars/user0/test-calendar" + if c.Path != expectedPath { + t.Fatalf("Calendar path is '%s', expected '%s'", c.Path, expectedPath) + } + expectedDescription := "" + if c.Description != expectedDescription { + t.Fatalf("Calendar description is '%s', expected '%s'", c.Description, expectedDescription) + } + expectedColor := "" + if c.Color != expectedColor { + t.Fatalf("Calendar color is '%s', expected '%s'", c.Color, expectedColor) + } + expectedTimezone := "" + if c.Timezone != expectedTimezone { + t.Fatalf("Calendar timezone is '%s', expected '%s'", c.Timezone, expectedTimezone) + } + if len(c.SupportedComponentSet) != 0 { + t.Fatalf("Found %d SupportedComponentSet, expected 0", len(c.SupportedComponentSet)) + } +} + type testBackend struct { calendars []Calendar objectMap map[string][]CalendarObject } -func (t testBackend) CreateCalendar(ctx context.Context, calendar *Calendar) error { +func (t *testBackend) CreateCalendar(ctx context.Context, calendar *Calendar) error { + t.calendars = append(t.calendars, *calendar) return nil } -func (t testBackend) ListCalendars(ctx context.Context) ([]Calendar, error) { +func (t *testBackend) ListCalendars(ctx context.Context) ([]Calendar, error) { return t.calendars, nil } -func (t testBackend) GetCalendar(ctx context.Context, path string) (*Calendar, error) { +func (t *testBackend) GetCalendar(ctx context.Context, path string) (*Calendar, error) { for _, cal := range t.calendars { if cal.Path == path { return &cal, nil } } - return nil, fmt.Errorf("Calendar for path: %s not found", path) + return nil, fmt.Errorf("calendar for path: %s not found", path) } -func (t testBackend) CalendarHomeSetPath(ctx context.Context) (string, error) { +func (t *testBackend) CalendarHomeSetPath(ctx context.Context) (string, error) { return "/user/calendars/", nil } -func (t testBackend) CurrentUserPrincipal(ctx context.Context) (string, error) { +func (t *testBackend) CurrentUserPrincipal(ctx context.Context) (string, error) { return "/user/", nil } -func (t testBackend) DeleteCalendarObject(ctx context.Context, path string) error { +func (t *testBackend) DeleteCalendarObject(ctx context.Context, path string) error { return nil } -func (t testBackend) GetCalendarObject(ctx context.Context, path string, req *CalendarCompRequest) (*CalendarObject, error) { +func (t *testBackend) GetCalendarObject(ctx context.Context, path string, req *CalendarCompRequest) (*CalendarObject, error) { for _, objs := range t.objectMap { for _, obj := range objs { if obj.Path == path { @@ -219,17 +576,17 @@ func (t testBackend) GetCalendarObject(ctx context.Context, path string, req *Ca } } } - return nil, fmt.Errorf("Couldn't find calendar object at: %s", path) + return nil, fmt.Errorf("couldn't find calendar object at: %s", path) } -func (t testBackend) PutCalendarObject(ctx context.Context, path string, calendar *ical.Calendar, opts *PutCalendarObjectOptions) (*CalendarObject, error) { +func (t *testBackend) PutCalendarObject(ctx context.Context, path string, calendar *ical.Calendar, opts *PutCalendarObjectOptions) (*CalendarObject, error) { return nil, nil } -func (t testBackend) ListCalendarObjects(ctx context.Context, path string, req *CalendarCompRequest) ([]CalendarObject, error) { +func (t *testBackend) ListCalendarObjects(ctx context.Context, path string, req *CalendarCompRequest) ([]CalendarObject, error) { return t.objectMap[path], nil } -func (t testBackend) QueryCalendarObjects(ctx context.Context, path string, query *CalendarQuery) ([]CalendarObject, error) { +func (t *testBackend) QueryCalendarObjects(ctx context.Context, path string, query *CalendarQuery) ([]CalendarObject, error) { return nil, nil }