diff --git a/carddav/carddav_test.go b/carddav/carddav_test.go index 337ac83..eda3532 100644 --- a/carddav/carddav_test.go +++ b/carddav/carddav_test.go @@ -3,6 +3,7 @@ package carddav import ( "context" "fmt" + "github.com/stretchr/testify/assert" "net/http" "net/http/httptest" "strings" @@ -12,7 +13,9 @@ import ( "github.com/emersion/go-webdav" ) -type testBackend struct{} +type testBackend struct { + addressBooks []AddressBook +} type contextKey string @@ -68,8 +71,9 @@ func (b *testBackend) GetAddressBook(ctx context.Context, path string) (*Address return nil, webdav.NewHTTPError(404, fmt.Errorf("Not found")) } -func (*testBackend) CreateAddressBook(ctx context.Context, ab *AddressBook) error { - panic("TODO: implement") +func (b *testBackend) CreateAddressBook(ctx context.Context, ab *AddressBook) error { + b.addressBooks = append(b.addressBooks, *ab) + return nil } func (*testBackend) DeleteAddressBook(ctx context.Context, path string) error { @@ -190,3 +194,40 @@ func TestAddressBookDiscovery(t *testing.T) { }) } } + +var mkcolRequestBody = ` + + + + + + + + + Lisa's Contacts + My primary address book. + + + ` + +func TestCreateAddressbookMinimalBody(t *testing.T) { + tb := testBackend{ + addressBooks: nil, + } + b := backend{ + Backend: &tb, + Prefix: "/dav", + } + req := httptest.NewRequest("MKCOL", "/dav/addressbooks/user0/test-addressbook", strings.NewReader(mkcolRequestBody)) + req.Header.Set("Content-Type", "application/xml") + + err := b.Mkcol(req) + assert.NoError(t, err) + assert.Len(t, tb.addressBooks, 1) + c := tb.addressBooks[0] + assert.Equal(t, "Lisa's Contacts", c.Name) + assert.Equal(t, "/dav/addressbooks/user0/test-addressbook", c.Path) + assert.Equal(t, "My primary address book.", c.Description) +}