Skip to content

Commit 89545b3

Browse files
committed
test: add unit test for CardDAV mkcol
1 parent c2ad716 commit 89545b3

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

carddav/carddav_test.go

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package carddav
33
import (
44
"context"
55
"fmt"
6+
"github.com/stretchr/testify/assert"
67
"net/http"
78
"net/http/httptest"
89
"strings"
@@ -12,7 +13,9 @@ import (
1213
"github.com/emersion/go-webdav"
1314
)
1415

15-
type testBackend struct{}
16+
type testBackend struct {
17+
addressBooks []AddressBook
18+
}
1619

1720
type contextKey string
1821

@@ -68,8 +71,9 @@ func (b *testBackend) GetAddressBook(ctx context.Context, path string) (*Address
6871
return nil, webdav.NewHTTPError(404, fmt.Errorf("Not found"))
6972
}
7073

71-
func (*testBackend) CreateAddressBook(ctx context.Context, ab *AddressBook) error {
72-
panic("TODO: implement")
74+
func (b *testBackend) CreateAddressBook(ctx context.Context, ab *AddressBook) error {
75+
b.addressBooks = append(b.addressBooks, *ab)
76+
return nil
7377
}
7478

7579
func (*testBackend) DeleteAddressBook(ctx context.Context, path string) error {
@@ -190,3 +194,40 @@ func TestAddressBookDiscovery(t *testing.T) {
190194
})
191195
}
192196
}
197+
198+
var mkcolRequestBody = `
199+
<?xml version="1.0" encoding="utf-8" ?>
200+
<D:mkcol xmlns:D="DAV:"
201+
xmlns:C="urn:ietf:params:xml:ns:carddav">
202+
<D:set>
203+
<D:prop>
204+
<D:resourcetype>
205+
<D:collection/>
206+
<C:addressbook/>
207+
</D:resourcetype>
208+
<D:displayname>Lisa's Contacts</D:displayname>
209+
<C:addressbook-description xml:lang="en"
210+
>My primary address book.</C:addressbook-description>
211+
</D:prop>
212+
</D:set>
213+
</D:mkcol>`
214+
215+
func TestCreateAddressbookMinimalBody(t *testing.T) {
216+
tb := testBackend{
217+
addressBooks: nil,
218+
}
219+
b := backend{
220+
Backend: &tb,
221+
Prefix: "/dav",
222+
}
223+
req := httptest.NewRequest("MKCOL", "/dav/addressbooks/user0/test-addressbook", strings.NewReader(mkcolRequestBody))
224+
req.Header.Set("Content-Type", "application/xml")
225+
226+
err := b.Mkcol(req)
227+
assert.NoError(t, err)
228+
assert.Len(t, tb.addressBooks, 1)
229+
c := tb.addressBooks[0]
230+
assert.Equal(t, "Lisa's Contacts", c.Name)
231+
assert.Equal(t, "/dav/addressbooks/user0/test-addressbook", c.Path)
232+
assert.Equal(t, "My primary address book.", c.Description)
233+
}

0 commit comments

Comments
 (0)