package nextcloud
import (
"strings"
"testing"
)
func TestBuildSyncCollectionRequest(t *testing.T) {
initial := buildSyncCollectionRequest("")
if strings.Contains(initial, "sync-token") {
t.Fatal("initial sync must omit sync-token element")
}
if !strings.Contains(initial, "sync-collection") {
t.Fatal("expected sync-collection request")
}
incremental := buildSyncCollectionRequest(`http://nc/sync/1`)
if !strings.Contains(incremental, "http://nc/sync/1") {
t.Fatalf("unexpected incremental body: %s", incremental)
}
}
func TestParseContactSyncResponse(t *testing.T) {
body := strings.NewReader(`
http://nc/sync/2
/remote.php/dav/addressbooks/users/alice/contacts/ada.vcf
"etag-1"
BEGIN:VCARD
UID:ada@ulti
FN:Ada Lovelace
EMAIL:ada@example.com
END:VCARD
HTTP/1.1 200 OK
/remote.php/dav/addressbooks/users/alice/contacts/old.vcf
HTTP/1.1 404 Not Found
`)
result, err := parseContactSyncResponse(body)
if err != nil {
t.Fatal(err)
}
if result.SyncToken != "http://nc/sync/2" {
t.Fatalf("sync token: got %q", result.SyncToken)
}
if len(result.Contacts) != 1 {
t.Fatalf("contacts: got %d", len(result.Contacts))
}
c := result.Contacts[0]
if c.FullName != "Ada Lovelace" || c.Path != "/remote.php/dav/addressbooks/users/alice/contacts/ada.vcf" {
t.Fatalf("unexpected contact: %+v", c)
}
if c.ETag != "\"etag-1\"" {
t.Fatalf("etag: got %q", c.ETag)
}
if len(result.Deleted) != 1 || result.Deleted[0] != "/remote.php/dav/addressbooks/users/alice/contacts/old.vcf" {
t.Fatalf("deleted: %+v", result.Deleted)
}
}
func TestParseContactListSetsPathAndETag(t *testing.T) {
body := strings.NewReader(`
/remote.php/dav/addressbooks/users/alice/contacts/
/remote.php/dav/addressbooks/users/alice/contacts/bob.vcf
"e2"
BEGIN:VCARD
FN:Bob
END:VCARD
`)
contacts, err := parseContactList(body)
if err != nil {
t.Fatal(err)
}
if len(contacts) != 1 {
t.Fatalf("contacts: got %d", len(contacts))
}
if contacts[0].Path != "/remote.php/dav/addressbooks/users/alice/contacts/bob.vcf" {
t.Fatalf("path: %q", contacts[0].Path)
}
if contacts[0].ETag != "\"e2\"" {
t.Fatalf("etag: %q", contacts[0].ETag)
}
}