From 36d2981ffe5050fc4b44f878c22a6ef301984ee9 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] fix: add support for more props on CalDAV MKCOL
---
caldav/caldav.go | 2 +
caldav/elements.go | 11 ++-
caldav/server.go | 9 +-
caldav/server_test.go | 213 +++++++++++++++++++++++++++++++++++++++++-
go.mod | 1 +
go.sum | 16 ++++
6 files changed, 243 insertions(+), 9 deletions(-)
diff --git a/caldav/caldav.go b/caldav/caldav.go
index 02705ef..e03a0dd 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..476211a 100644
--- a/caldav/elements.go
+++ b/caldav/elements.go
@@ -230,8 +230,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"`
+ CalemdarTimeZone 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 24977b8..c3e8859 100644
--- a/caldav/server.go
+++ b/caldav/server.go
@@ -724,7 +724,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.CalemdarTimeZone)
+
+ 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 3d1e63c..5b84581 100644
--- a/caldav/server_test.go
+++ b/caldav/server_test.go
@@ -3,6 +3,7 @@ package caldav
import (
"context"
"fmt"
+ "github.com/stretchr/testify/assert"
"io"
"io/ioutil"
"net/http/httptest"
@@ -33,7 +34,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()
@@ -68,7 +69,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 +119,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,12 +178,216 @@ 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)
+ assert.NoError(t, err)
+ assert.Len(t, tb.calendars, 1)
+ c := tb.calendars[0]
+ assert.Equal(t, "Test calendar", c.Name)
+ assert.Equal(t, "/dav/calendars/user0/test-calendar", c.Path)
+ assert.Equal(t, "A calendar for testing", c.Description)
+ assert.Equal(t, "#009688FF", c.Color)
+ assert.Equal(t, []string{"VEVENT", "VTODO", "VJOURNAL"}, c.SupportedComponentSet)
+ assert.Contains(t, c.TimeZone, "BEGIN:VCALENDAR")
+}
+
+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)
+ assert.NoError(t, err)
+ assert.Len(t, tb.calendars, 1)
+ c := tb.calendars[0]
+ assert.Equal(t, "Test calendar", c.Name)
+ assert.Equal(t, "/dav/calendars/user0/test-calendar", c.Path)
+ assert.Equal(t, "", c.Description)
+ assert.Equal(t, "", c.Color)
+ assert.Equal(t, []string{}, c.SupportedComponentSet)
+ assert.Equal(t, "", c.TimeZone)
+}
+
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
}
diff --git a/go.mod b/go.mod
index 7c5445a..78d380d 100644
--- a/go.mod
+++ b/go.mod
@@ -5,5 +5,6 @@ go 1.13
require (
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f
github.com/emersion/go-vcard v0.0.0-20230815062825-8fda7d206ec9
+ github.com/stretchr/testify v1.8.4 // indirect
github.com/teambition/rrule-go v1.8.2 // indirect
)
diff --git a/go.sum b/go.sum
index 472fda6..777af55 100644
--- a/go.sum
+++ b/go.sum
@@ -1,7 +1,23 @@
+github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
+github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
+github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f h1:feGUUxxvOtWVOhTko8Cbmp33a+tU0IMZxMEmnkoAISQ=
github.com/emersion/go-ical v0.0.0-20220601085725-0864dccc089f/go.mod h1:2MKFUgfNMULRxqZkadG1Vh44we3y5gJAtTBlVsx1BKQ=
github.com/emersion/go-vcard v0.0.0-20230815062825-8fda7d206ec9 h1:ATgqloALX6cHCranzkLb8/zjivwQ9DWWDCQRnxTPfaA=
github.com/emersion/go-vcard v0.0.0-20230815062825-8fda7d206ec9/go.mod h1:HMJKR5wlh/ziNp+sHEDV2ltblO4JD2+IdDOWtGcQBTM=
+github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
+github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
+github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
+github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
+github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
+github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
+github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
+github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
+github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/teambition/rrule-go v1.7.2/go.mod h1:mBJ1Ht5uboJ6jexKdNUJg2NcwP8uUMNvStWXlJD3MvU=
github.com/teambition/rrule-go v1.8.2 h1:lIjpjvWTj9fFUZCmuoVDrKVOtdiyzbzc93qTmRVe/J8=
github.com/teambition/rrule-go v1.8.2/go.mod h1:Ieq5AbrKGciP1V//Wq8ktsTXwSwJHDD5mD/wLBGl3p4=
+gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
+gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
+gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
+gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=