Settings -> CardDAV connects an external CardDAV endpoint (any RFC 6352
service; on Core the deployment's NEXTCLOUD_URL adds a Nextcloud-contacts
one-click prefill, composed visibly with the username so the URL shown is
the URL saved). Each server address book materializes as its own Collection
and every contact becomes a Bookmark carrying the card's DAV .vcf URL — FN
as title, email/phone in the description as display sugar set once.
internal/carddav mirrors the WebDAV/CalDAV siblings:
- client.go — discovery accepts a single book or a home (resourcetype
<card:addressbook/>), addressbook-query REPORT bounded at 50 books /
2000 cards, netguard-pinned, Basic auth in the header only, redirects
refused, folded-vCard parser;
- sync.go — RunLoop/RunOnce under a dedicated carddav-tick advisory lease;
full ledger discipline: idempotent re-runs, etag refreshes never touch
user-edited bookmark fields, vanished cards detach membership but keep
the bookmark, user-deleted bookmarks are tombstones that never
resurrect, a re-deleted Collection is re-created and re-linked, a
truncated listing never reconciles deletions, one unreadable book does
not abort the rest.
CardDAV dials pin to 443 only and require https — CardDAV ships
Basic-auth credentials, and the Contacts sync's review rulings (M-3/CR-01)
stand; the INTEGRATIONS_ALLOW_PRIVATE escape hatch keeps loopback fakes
working. This surface is deliberately separate from the single-config
Contacts sync (Contact rows powering contact-linking): same transport,
different product model, stated in API.md and the UI copy.
Three tables (CardDavConnection/CardDavAddressbook/CardDavItem) via
migration 022 + schema.sql. Handlers expose /api/carddav/connections CRUD
+ /test + /sync with the house credential (encrypted, masked, write-only),
version (409 on stale) and AllowConnector("carddav") plan gate on creation
AND every sync; cap 10; wired as the sixth background loop.
Settings -> CardDAV tab (web/components/carddav-settings.tsx): masked
write-only password, version on PATCH, per-row address-book list,
verbatim test/sync results, Core suggestion prefill, allowed flag drives
the plan notice.
Verification: full Go integration suite green incl. TestCardDavLifecycle
(materialize/URL+title checks, idempotency, etag refresh, detach-but-
survive, tombstone, version contract, 502+lastError, owner scoping) and
TestCardDavSuggestion; carddav unit tests on httptest fakes;
make db-converge passes; browser regressions all 19 PASS incl. the new
CardDAV walkthrough (and a deterministic Sonner-toast drain before the
sign-out assertion, which the added toasts had made racy); web lint/SSR-
lint/build clean. API.md contract section, AGENTS invariant, env docs and
the Core installer wording updated for the three-way DAV prefill.
143 lines
5.5 KiB
Go
143 lines
5.5 KiB
Go
package carddav
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func allowPrivate(t *testing.T) { t.Setenv("INTEGRATIONS_ALLOW_PRIVATE", "1") }
|
|
|
|
const (
|
|
srvUser = "erwin"
|
|
srvPass = "s3cret"
|
|
)
|
|
|
|
// fakeCards serves a home at /books with one "contacts" address book holding
|
|
// two vCards (one folded FN with a parameter-ized EMAIL), plus a plain
|
|
// collection child that must NOT be discovered as a book.
|
|
func fakeCards(t *testing.T) *httptest.Server {
|
|
t.Helper()
|
|
vcard1 := "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:uid-ana\r\nFN:Ana\r\n Escobar\r\nEMAIL;type=WORK, PREF:ana@work.example\r\nEMAIL:ana@home.example\r\nTEL:+1 555 0100\r\nEND:VCARD"
|
|
vcard2 := "BEGIN:VCARD\nVERSION:4.0\nUID:uid-bo\nFN:Bo Peep\nEND:VCARD"
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
u, p, ok := r.BasicAuth()
|
|
if !ok || u != srvUser || p != srvPass {
|
|
w.Header().Set("WWW-Authenticate", `Basic realm="x"`)
|
|
http.Error(w, "no", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
switch r.Method {
|
|
case "PROPFIND":
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
w.WriteHeader(http.StatusMultiStatus)
|
|
if r.URL.Path == "/books/" && r.Header.Get("Depth") == "0" {
|
|
io.WriteString(w, `<multistatus xmlns="DAV:"><response><href>/books/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response></multistatus>`)
|
|
return
|
|
}
|
|
if r.URL.Path == "/books/" {
|
|
io.WriteString(w, `<multistatus xmlns="DAV:" xmlns:card="urn:ietf:params:xml:ns:carddav">
|
|
<response><href>/books/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response>
|
|
<response><href>/books/contacts/</href><propstat><prop><resourcetype><card:addressbook/></resourcetype><displayname>Contacts</displayname></prop></propstat></response>
|
|
<response><href>/books/notes/</href><propstat><prop><resourcetype><collection/></resourcetype><displayname>Notes</displayname></prop></propstat></response>
|
|
</multistatus>`)
|
|
return
|
|
}
|
|
http.Error(w, "no", http.StatusNotFound)
|
|
case "REPORT":
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
w.WriteHeader(http.StatusMultiStatus)
|
|
io.WriteString(w, `<multistatus xmlns="DAV:" xmlns:card="urn:ietf:params:xml:ns:carddav">
|
|
<response><href>/books/contacts/</href><propstat><prop><getetag>"book"</getetag></prop></propstat></response>
|
|
<response><href>/books/contacts/ana.vcf</href><propstat><prop><getetag>"A1"</getetag><card:address-data>`+xmlEscape(vcard1)+`</card:address-data></prop></propstat></response>
|
|
<response><href>/books/contacts/bo%20peep.vcf</href><propstat><prop><getetag>"B1"</getetag><card:address-data>`+xmlEscape(vcard2)+`</card:address-data></prop></propstat></response>
|
|
</multistatus>`)
|
|
default:
|
|
http.Error(w, "no", http.StatusMethodNotAllowed)
|
|
}
|
|
}))
|
|
t.Cleanup(ts.Close)
|
|
return ts
|
|
}
|
|
|
|
func xmlEscape(s string) string {
|
|
return strings.NewReplacer("&", "&", "<", "<", ">", ">").Replace(s)
|
|
}
|
|
|
|
func TestDiscoverAddressBooks(t *testing.T) {
|
|
allowPrivate(t)
|
|
ts := fakeCards(t)
|
|
books, err := Discover(context.Background(), Conn{BaseURL: ts.URL + "/books", Username: srvUser, Password: srvPass})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if len(books) != 1 || books[0].DisplayName != "Contacts" {
|
|
t.Fatalf("want only the Contacts book, got %+v", books)
|
|
}
|
|
}
|
|
|
|
func TestCardsParsesFoldedVCard(t *testing.T) {
|
|
allowPrivate(t)
|
|
ts := fakeCards(t)
|
|
res, err := Cards(context.Background(), Conn{BaseURL: ts.URL, Username: srvUser, Password: srvPass}, ts.URL+"/books/contacts/")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if res.Truncated || len(res.Cards) != 2 {
|
|
t.Fatalf("cards: %+v trunc=%v", res.Cards, res.Truncated)
|
|
}
|
|
byUID := map[string]Card{}
|
|
for _, c := range res.Cards {
|
|
byUID[c.UID] = c
|
|
}
|
|
ana := byUID["uid-ana"]
|
|
// Unfolded FN and the FIRST EMAIL occurrence (simple parser by design).
|
|
if ana.Name != "Ana Escobar" {
|
|
t.Fatalf("folded FN not joined: %q", ana.Name)
|
|
}
|
|
if ana.Email != "ana@work.example" {
|
|
t.Fatalf("email wrong: %q", ana.Email)
|
|
}
|
|
if ana.Phone != "+1 555 0100" {
|
|
t.Fatalf("phone wrong: %q", ana.Phone)
|
|
}
|
|
if ana.ETag != "A1" {
|
|
t.Fatalf("etag wrong: %q", ana.ETag)
|
|
}
|
|
bo := byUID["uid-bo"]
|
|
if bo.Path != "bo peep.vcf" || !strings.HasSuffix(bo.URL, "/books/contacts/bo%20peep.vcf") {
|
|
t.Fatalf("path/URL escaping wrong: %+v", bo)
|
|
}
|
|
}
|
|
|
|
func TestDiscoverRejectsUserinfo(t *testing.T) {
|
|
if _, err := Discover(context.Background(), Conn{BaseURL: "https://u:p@host/books"}); err == nil || !strings.Contains(err.Error(), "never in the URL") {
|
|
t.Fatalf("want userinfo rejection, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestDiscoverNoBooksIsError(t *testing.T) {
|
|
allowPrivate(t)
|
|
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
w.Header().Set("Content-Type", "application/xml")
|
|
w.WriteHeader(http.StatusMultiStatus)
|
|
io.WriteString(w, `<multistatus xmlns="DAV:"><response><href>/x/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response></multistatus>`)
|
|
}))
|
|
defer ts.Close()
|
|
if _, err := Discover(context.Background(), Conn{BaseURL: ts.URL + "/x"}); err == nil || !strings.Contains(err.Error(), "no CardDAV address books") {
|
|
t.Fatalf("want no-books error, got %v", err)
|
|
}
|
|
}
|
|
|
|
func TestCardsUnauthIsVerbatim401(t *testing.T) {
|
|
allowPrivate(t)
|
|
ts := fakeCards(t)
|
|
_, err := Cards(context.Background(), Conn{BaseURL: ts.URL, Username: srvUser, Password: "nope"}, ts.URL+"/books/contacts/")
|
|
if err == nil || !strings.Contains(err.Error(), "rejected these credentials") {
|
|
t.Fatalf("want 401 class error, got %v", err)
|
|
}
|
|
}
|