Unified Nextcloud connect: one credential connects files, calendars and contacts
POST /api/nextcloud/connect composes the three per-user DAV roots server-side, verifies the credential against each protocol in parallel (netguard-pinned, sharing the siblings' /test budgets) BEFORE storing anything, and creates one ordinary sibling connection per protocol that passes. Orchestration, not a fourth connector: every sibling plan gate, validation (incl. CardDAV https/443), cap and projection applies; partial success is a 200 with per-connector created|exists|gated|error results; repeat calls are idempotent by composed URL. GET /api/nextcloud/status serves the new Settings -> Nextcloud tab (origin hint + allowed flags + masked connections); the UI fires the existing /sync surfaces right after connect so collections materialize immediately. Contacts sync untouched; no schema change. Tests: unified Nextcloud fake (sibling fakes re-rooted to Nextcloud paths on one origin) covering happy path, idempotency, wrong-password-stores- nothing, owner scoping, partial plan gate + restore; browser regression for the full connect/sync/idempotent/Manage flow.
This commit is contained in:
@@ -15,10 +15,12 @@ import (
|
||||
)
|
||||
|
||||
// fakeCaldav is a mutable RFC 4791 server for integration tests: a home at
|
||||
// /home with a "work" calendar whose events a test can add/remove/change to
|
||||
// root (default /home; the unified Nextcloud fake re-roots it) with "work"
|
||||
// and "personal" calendars whose events a test can add/remove/change to
|
||||
// exercise the full reconcile. Auth erwin/s3cret.
|
||||
type fakeCaldav struct {
|
||||
mu sync.Mutex
|
||||
root string
|
||||
events map[string]cdEvent // href -> event
|
||||
srv *httptest.Server
|
||||
extra map[string]cdEvent // for the personal calendar
|
||||
@@ -31,7 +33,7 @@ type cdEvent struct {
|
||||
|
||||
func newFakeCaldav(t *testing.T) *fakeCaldav {
|
||||
t.Helper()
|
||||
f := &fakeCaldav{events: map[string]cdEvent{}, extra: map[string]cdEvent{}}
|
||||
f := &fakeCaldav{root: "/home", events: map[string]cdEvent{}, extra: map[string]cdEvent{}}
|
||||
f.srv = httptest.NewServer(http.HandlerFunc(f.serve))
|
||||
t.Cleanup(f.srv.Close)
|
||||
return f
|
||||
@@ -67,16 +69,16 @@ func (f *fakeCaldav) serve(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
w.WriteHeader(http.StatusMultiStatus)
|
||||
if r.Header.Get("Depth") == "0" {
|
||||
io.WriteString(w, `<multistatus xmlns="DAV:"><response><href>/home/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response></multistatus>`)
|
||||
fmt.Fprintf(w, `<multistatus xmlns="DAV:"><response><href>%s/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response></multistatus>`, f.root)
|
||||
return
|
||||
}
|
||||
io.WriteString(w, `<multistatus xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
|
||||
<response><href>/home/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response>
|
||||
<response><href>/home/work/</href><propstat><prop xmlns:C="urn:ietf:params:xml:ns:caldav"><resourcetype><C:calendar/></resourcetype><displayname>Work</displayname></prop></propstat></response>
|
||||
<response><href>/home/personal/</href><propstat><prop xmlns:C="urn:ietf:params:xml:ns:caldav"><resourcetype><C:calendar/></resourcetype><displayname>Personal</displayname></prop></propstat></response>
|
||||
</multistatus>`)
|
||||
fmt.Fprintf(w, `<multistatus xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">
|
||||
<response><href>%s/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response>
|
||||
<response><href>%s/work/</href><propstat><prop xmlns:C="urn:ietf:params:xml:ns:caldav"><resourcetype><C:calendar/></resourcetype><displayname>Work</displayname></prop></propstat></response>
|
||||
<response><href>%s/personal/</href><propstat><prop xmlns:C="urn:ietf:params:xml:ns:caldav"><resourcetype><C:calendar/></resourcetype><displayname>Personal</displayname></prop></propstat></response>
|
||||
</multistatus>`, f.root, f.root, f.root)
|
||||
case "REPORT":
|
||||
key := strings.Trim(strings.TrimPrefix(r.URL.Path, "/home/"), "/")
|
||||
key := strings.Trim(strings.TrimPrefix(r.URL.Path, f.root+"/"), "/")
|
||||
collection := f.events
|
||||
if key == "personal" {
|
||||
collection = f.extra
|
||||
@@ -85,12 +87,12 @@ func (f *fakeCaldav) serve(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusMultiStatus)
|
||||
var b strings.Builder
|
||||
b.WriteString(`<multistatus xmlns="DAV:" xmlns:C="urn:ietf:params:xml:ns:caldav">`)
|
||||
fmt.Fprintf(&b, `<response><href>/home/%s/</href><propstat><prop><getetag>"cal"</getetag></prop></propstat></response>`, key)
|
||||
fmt.Fprintf(&b, `<response><href>%s/%s/</href><propstat><prop><getetag>"cal"</getetag></prop></propstat></response>`, f.root, key)
|
||||
for href, ev := range collection {
|
||||
ics := "BEGIN:VCALENDAR\r\nBEGIN:VEVENT\r\nUID:" + ev.uid + "\r\nSUMMARY:" + ev.summary +
|
||||
"\r\nDTSTART:" + ev.start.UTC().Format("20060102T150405") + "Z\r\nEND:VEVENT\r\nEND:VCALENDAR"
|
||||
fmt.Fprintf(&b, `<response><href>/home/%s/%s</href><propstat><prop xmlns:C="urn:ietf:params:xml:ns:caldav"><getetag>"%s"</getetag><C:calendar-data>%s</C:calendar-data></prop></propstat></response>`,
|
||||
key, strings.ReplaceAll(href, " ", "%20"), ev.etag, xmlEsc(ics))
|
||||
fmt.Fprintf(&b, `<response><href>%s/%s/%s</href><propstat><prop xmlns:C="urn:ietf:params:xml:ns:caldav"><getetag>"%s"</getetag><C:calendar-data>%s</C:calendar-data></prop></propstat></response>`,
|
||||
f.root, key, strings.ReplaceAll(href, " ", "%20"), ev.etag, xmlEsc(ics))
|
||||
}
|
||||
b.WriteString(`</multistatus>`)
|
||||
io.WriteString(w, b.String())
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -10,16 +11,18 @@ import (
|
||||
)
|
||||
|
||||
// fakeCarddav is a mutable CardDAV server for integration tests: a home at
|
||||
// /books holding one "Contacts" address book; cards map path -> (etag, vcard).
|
||||
// root (default /books; the unified Nextcloud fake re-roots it) holding one
|
||||
// "Contacts" address book; cards map path -> (etag, vcard).
|
||||
type fakeCarddav struct {
|
||||
mu sync.Mutex
|
||||
root string
|
||||
cards map[string][2]string // path -> {etag, fn}
|
||||
srv *httptest.Server
|
||||
}
|
||||
|
||||
func newFakeCarddav(t *testing.T) *fakeCarddav {
|
||||
t.Helper()
|
||||
f := &fakeCarddav{cards: map[string][2]string{
|
||||
f := &fakeCarddav{root: "/books", cards: map[string][2]string{
|
||||
"ana.vcf": {"A1", "Ana Escobar"},
|
||||
"bo.vcf": {"B1", "Bo Peep"},
|
||||
}}
|
||||
@@ -51,22 +54,22 @@ func (f *fakeCarddav) serve(w http.ResponseWriter, r *http.Request) {
|
||||
defer f.mu.Unlock()
|
||||
switch r.Method {
|
||||
case "PROPFIND":
|
||||
if r.URL.Path != "/books/" {
|
||||
if r.URL.Path != f.root+"/" {
|
||||
http.Error(w, "no", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
w.Header().Set("Content-Type", "application/xml")
|
||||
w.WriteHeader(http.StatusMultiStatus)
|
||||
if r.Header.Get("Depth") == "0" {
|
||||
io.WriteString(w, `<multistatus xmlns="DAV:"><response><href>/books/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response></multistatus>`)
|
||||
fmt.Fprintf(w, `<multistatus xmlns="DAV:"><response><href>%s/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response></multistatus>`, f.root)
|
||||
return
|
||||
}
|
||||
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>
|
||||
</multistatus>`)
|
||||
fmt.Fprintf(w, `<multistatus xmlns="DAV:" xmlns:card="urn:ietf:params:xml:ns:carddav">
|
||||
<response><href>%s/</href><propstat><prop><resourcetype><collection/></resourcetype></prop></propstat></response>
|
||||
<response><href>%s/contacts/</href><propstat><prop><resourcetype><card:addressbook/></resourcetype><displayname>Contacts</displayname></prop></propstat></response>
|
||||
</multistatus>`, f.root, f.root)
|
||||
case "REPORT":
|
||||
if r.URL.Path != "/books/contacts/" {
|
||||
if r.URL.Path != f.root+"/contacts/" {
|
||||
http.Error(w, "no", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
@@ -74,12 +77,12 @@ func (f *fakeCarddav) serve(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusMultiStatus)
|
||||
var b strings.Builder
|
||||
b.WriteString(`<multistatus xmlns="DAV:" xmlns:card="urn:ietf:params:xml:ns:carddav">`)
|
||||
b.WriteString(`<response><href>/books/contacts/</href><propstat><prop><getetag>"book"</getetag></prop></propstat></response>`)
|
||||
fmt.Fprintf(&b, `<response><href>%s/contacts/</href><propstat><prop><getetag>"book"</getetag></prop></propstat></response>`, f.root)
|
||||
for path, v := range f.cards {
|
||||
vcard := "BEGIN:VCARD\r\nVERSION:3.0\r\nUID:uid-" + path + "\r\nFN:" + v[1] +
|
||||
"\r\nEMAIL:" + strings.ToLower(strings.SplitN(v[1], " ", 2)[0]) + "@example.invalid\r\nEND:VCARD"
|
||||
b.WriteString(`<response><href>/books/contacts/` + path + `</href><propstat><prop><getetag>"` + v[0] +
|
||||
`"</getetag><address-data>` + cdavEsc(vcard) + `</address-data></prop></propstat></response>`)
|
||||
fmt.Fprintf(&b, `<response><href>%s/contacts/%s</href><propstat><prop><getetag>"%s"</getetag><address-data>%s</address-data></prop></propstat></response>`,
|
||||
f.root, path, v[0], cdavEsc(vcard))
|
||||
}
|
||||
b.WriteString(`</multistatus>`)
|
||||
io.WriteString(w, b.String())
|
||||
|
||||
@@ -0,0 +1,215 @@
|
||||
package http
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"gitea.federated.computer/Federated_Computer/brain/internal/db"
|
||||
"gitea.federated.computer/Federated_Computer/brain/internal/entitlements"
|
||||
)
|
||||
|
||||
// fakeNextcloud serves all three DAV protocols on ONE origin with the
|
||||
// suite-standard Nextcloud path shape, by re-rooting the three sibling
|
||||
// fakes. Auth erwin/s3cret everywhere (one account, one app password).
|
||||
type fakeNextcloud struct {
|
||||
files *fakeShare
|
||||
cals *fakeCaldav
|
||||
books *fakeCarddav
|
||||
srv *httptest.Server
|
||||
}
|
||||
|
||||
func newFakeNextcloud(t *testing.T) *fakeNextcloud {
|
||||
t.Helper()
|
||||
f := &fakeNextcloud{
|
||||
files: &fakeShare{root: "/remote.php/dav/files/erwin", files: map[string]string{
|
||||
"a.txt": "A1", "sub/d.txt": "D1",
|
||||
}},
|
||||
cals: &fakeCaldav{root: "/remote.php/dav/calendars/erwin",
|
||||
events: map[string]cdEvent{}, extra: map[string]cdEvent{}},
|
||||
books: &fakeCarddav{root: "/remote.php/dav/addressbooks/users/erwin",
|
||||
cards: map[string][2]string{"ana.vcf": {"A1", "Ana Escobar"}}},
|
||||
}
|
||||
f.srv = httptest.NewServer(http.HandlerFunc(f.serve))
|
||||
t.Cleanup(f.srv.Close)
|
||||
return f
|
||||
}
|
||||
|
||||
func (f *fakeNextcloud) serve(w http.ResponseWriter, r *http.Request) {
|
||||
switch {
|
||||
case strings.HasPrefix(r.URL.Path, f.files.root):
|
||||
f.files.serve(w, r)
|
||||
case strings.HasPrefix(r.URL.Path, f.cals.root):
|
||||
f.cals.serve(w, r)
|
||||
case strings.HasPrefix(r.URL.Path, f.books.root):
|
||||
f.books.serve(w, r)
|
||||
default:
|
||||
http.Error(w, "no", http.StatusNotFound)
|
||||
}
|
||||
}
|
||||
|
||||
func nextcloudBody(origin string) map[string]any {
|
||||
return map[string]any{"url": origin, "username": "erwin", "password": "s3cret"}
|
||||
}
|
||||
|
||||
// One credential entry connects files, calendars and contacts; the stored
|
||||
// rows are ordinary sibling connections (masked, syncable through the
|
||||
// existing /sync surfaces); a repeat call is idempotent; a wrong password
|
||||
// stores nothing.
|
||||
func TestNextcloudConnect(t *testing.T) {
|
||||
srv, _ := testServerDB(t)
|
||||
nc := newFakeNextcloud(t)
|
||||
nc.cals.add("work", "standup.ics", "S1", "uid-standup", "Daily standup", time.Now().UTC().Add(24*time.Hour))
|
||||
c, _ := accountClient(t, srv.URL, "nc-connect@superbrain.test")
|
||||
|
||||
// Status before: every connector allowed (selfhosted default), nothing
|
||||
// connected, no deployment origin hint.
|
||||
st := c.request("GET", "/api/nextcloud/status", nil, 200)
|
||||
if st["origin"] != "" {
|
||||
t.Fatalf("no NEXTCLOUD_URL -> no origin hint, got %v", st["origin"])
|
||||
}
|
||||
for _, key := range []string{"files", "calendars", "contacts"} {
|
||||
part := st[key].(map[string]any)
|
||||
if part["allowed"] != true || len(part["connections"].([]any)) != 0 {
|
||||
t.Fatalf("%s status before: %v", key, part)
|
||||
}
|
||||
}
|
||||
|
||||
// Malformed input is a 400 for the whole request.
|
||||
c.request("POST", "/api/nextcloud/connect", map[string]any{"url": "ftp://x", "username": "erwin", "password": "s3cret"}, 400)
|
||||
c.request("POST", "/api/nextcloud/connect", map[string]any{"url": nc.srv.URL, "username": "", "password": "s3cret"}, 400)
|
||||
c.request("POST", "/api/nextcloud/connect", map[string]any{"url": nc.srv.URL, "username": "erwin"}, 400)
|
||||
c.request("POST", "/api/nextcloud/connect", map[string]any{"url": nc.srv.URL, "username": "erwin", "password": "••••••••"}, 400)
|
||||
|
||||
// A pasted DAV endpoint normalizes back to the origin.
|
||||
res := c.request("POST", "/api/nextcloud/connect",
|
||||
map[string]any{"url": nc.srv.URL + "/remote.php/dav/files/erwin/Documents", "username": "erwin", "password": "s3cret"}, 200)
|
||||
results := res["results"].(map[string]any)
|
||||
ids := map[string]string{}
|
||||
for _, key := range []string{"files", "calendars", "contacts"} {
|
||||
r := results[key].(map[string]any)
|
||||
if r["status"] != "created" {
|
||||
t.Fatalf("%s: want created, got %v", key, r)
|
||||
}
|
||||
ids[key] = r["id"].(string)
|
||||
}
|
||||
if !strings.HasSuffix(results["files"].(map[string]any)["url"].(string), "/remote.php/dav/files/erwin/") {
|
||||
t.Fatalf("files root wrong: %v", results["files"])
|
||||
}
|
||||
|
||||
// The rows are ordinary sibling connections: masked secrets, and the
|
||||
// existing /sync surfaces materialize the collections.
|
||||
list := c.request("GET", "/api/webdav/connections", nil, 200)
|
||||
conn := list["connections"].([]any)[0].(map[string]any)
|
||||
if conn["secret"] != "••••••••" || conn["name"] != "Nextcloud files" {
|
||||
t.Fatalf("webdav row wrong: %v", conn)
|
||||
}
|
||||
if r := c.request("POST", "/api/webdav/connections/"+ids["files"]+"/sync", map[string]any{}, 200); r["files"] != float64(2) || r["created"] != float64(2) {
|
||||
t.Fatalf("files sync: %v", r)
|
||||
}
|
||||
if r := c.request("POST", "/api/caldav/connections/"+ids["calendars"]+"/sync", map[string]any{}, 200); r["calendars"] != float64(2) || r["events"] != float64(1) || r["created"] != float64(1) {
|
||||
t.Fatalf("calendars sync: %v", r)
|
||||
}
|
||||
if r := c.request("POST", "/api/carddav/connections/"+ids["contacts"]+"/sync", map[string]any{}, 200); r["books"] != float64(1) || r["cards"] != float64(1) || r["created"] != float64(1) {
|
||||
t.Fatalf("contacts sync: %v", r)
|
||||
}
|
||||
names := map[string]bool{}
|
||||
for _, x := range c.getJSON("/api/collections", 200) {
|
||||
names[x["name"].(string)] = true
|
||||
}
|
||||
for _, want := range []string{"Nextcloud files", "Work", "Personal", "Contacts"} {
|
||||
if !names[want] {
|
||||
t.Fatalf("collection %q missing, got %v", want, names)
|
||||
}
|
||||
}
|
||||
|
||||
// Idempotent: the same origin+username reports exists, never duplicates.
|
||||
again := c.request("POST", "/api/nextcloud/connect", nextcloudBody(nc.srv.URL), 200)["results"].(map[string]any)
|
||||
for _, key := range []string{"files", "calendars", "contacts"} {
|
||||
r := again[key].(map[string]any)
|
||||
if r["status"] != "exists" || r["id"] != ids[key] {
|
||||
t.Fatalf("%s repeat connect: %v", key, r)
|
||||
}
|
||||
}
|
||||
if n := len(c.request("GET", "/api/webdav/connections", nil, 200)["connections"].([]any)); n != 1 {
|
||||
t.Fatalf("repeat connect duplicated rows: %d", n)
|
||||
}
|
||||
|
||||
// Another user's status is empty (owner-scoped) and a wrong password
|
||||
// stores nothing for them.
|
||||
bob, _ := accountClient(t, srv.URL, "nc-bob@superbrain.test")
|
||||
bst := bob.request("GET", "/api/nextcloud/status", nil, 200)
|
||||
if n := len(bst["files"].(map[string]any)["connections"].([]any)); n != 0 {
|
||||
t.Fatalf("bob sees alice's connections: %d", n)
|
||||
}
|
||||
bad := bob.request("POST", "/api/nextcloud/connect",
|
||||
map[string]any{"url": nc.srv.URL, "username": "erwin", "password": "wrong"}, 200)["results"].(map[string]any)
|
||||
for _, key := range []string{"files", "calendars", "contacts"} {
|
||||
r := bad[key].(map[string]any)
|
||||
if r["status"] != "error" || r["error"] == "" {
|
||||
t.Fatalf("%s wrong password: %v", key, r)
|
||||
}
|
||||
}
|
||||
bst = bob.request("GET", "/api/nextcloud/status", nil, 200)
|
||||
for _, key := range []string{"files", "calendars", "contacts"} {
|
||||
if n := len(bst[key].(map[string]any)["connections"].([]any)); n != 0 {
|
||||
t.Fatalf("wrong password stored %s rows: %d", key, n)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// The per-connector plan gates apply independently: a carddav-only plan
|
||||
// connects contacts and reports files/calendars as gated (verbatim plan
|
||||
// message); restoring the connectors opens them without a restart.
|
||||
func TestNextcloudConnectPlanGate(t *testing.T) {
|
||||
srv, d := testServerDB(t)
|
||||
nc := newFakeNextcloud(t)
|
||||
c, uid := accountClient(t, srv.URL, "nc-gate@superbrain.test")
|
||||
var tid string
|
||||
if err := d.QueryRowContext(context.Background(), `SELECT "tenantId" FROM "User" WHERE id=$1`, uid).Scan(&tid); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
repo := db.NewTenantRepo(d)
|
||||
if err := repo.SetEntitlement(context.Background(), tid, entitlements.KeyConnectors, "url,file,carddav"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
res := c.request("POST", "/api/nextcloud/connect", nextcloudBody(nc.srv.URL), 200)["results"].(map[string]any)
|
||||
for _, key := range []string{"files", "calendars"} {
|
||||
r := res[key].(map[string]any)
|
||||
if r["status"] != "gated" || r["error"] != "Not available on your plan" {
|
||||
t.Fatalf("%s must be gated: %v", key, r)
|
||||
}
|
||||
}
|
||||
if res["contacts"].(map[string]any)["status"] != "created" {
|
||||
t.Fatalf("contacts must connect on a carddav-only plan: %v", res["contacts"])
|
||||
}
|
||||
st := c.request("GET", "/api/nextcloud/status", nil, 200)
|
||||
if st["files"].(map[string]any)["allowed"] != false || st["contacts"].(map[string]any)["allowed"] != true {
|
||||
t.Fatalf("status allowed flags: %v", st)
|
||||
}
|
||||
|
||||
if err := repo.SetEntitlement(context.Background(), tid, entitlements.KeyConnectors, "url,file,carddav,webdav,caldav"); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
res = c.request("POST", "/api/nextcloud/connect", nextcloudBody(nc.srv.URL), 200)["results"].(map[string]any)
|
||||
if res["files"].(map[string]any)["status"] != "created" || res["calendars"].(map[string]any)["status"] != "created" {
|
||||
t.Fatalf("post-restore connect: %v", res)
|
||||
}
|
||||
if res["contacts"].(map[string]any)["status"] != "exists" {
|
||||
t.Fatalf("contacts must stay idempotent: %v", res["contacts"])
|
||||
}
|
||||
}
|
||||
|
||||
// NEXTCLOUD_URL is deployment DATA: the status origin hint mirrors it.
|
||||
func TestNextcloudStatusOrigin(t *testing.T) {
|
||||
t.Setenv("NEXTCLOUD_URL", "https://cloud.example/")
|
||||
srv := testServer(t)
|
||||
c, _ := accountClient(t, srv.URL, "nc-origin@superbrain.test")
|
||||
if o := c.request("GET", "/api/nextcloud/status", nil, 200)["origin"]; o != "https://cloud.example" {
|
||||
t.Fatalf("origin hint: %v", o)
|
||||
}
|
||||
}
|
||||
@@ -215,6 +215,15 @@ func NewServer(cfg *config.Config, database *db.DB) *http.Server {
|
||||
mux.HandleFunc("POST /api/carddav/connections/{id}/test", authed(handlers.CardDavTestLimiter(handlers.HandleCardDavConnectionsTest)))
|
||||
mux.HandleFunc("POST /api/carddav/connections/{id}/sync", authed(handlers.CardDavSyncLimiter(handlers.HandleCardDavSync)))
|
||||
|
||||
// 4.7j Unified Nextcloud connect (Settings → Nextcloud): one credential
|
||||
// entry verifies against all three DAV roots and stores one connection
|
||||
// per protocol that passes — files/calendars/contacts. Orchestration
|
||||
// only: each sibling's plan gate, validation, caps and budgets apply;
|
||||
// partial success is a 200 with per-connector results. Materialization
|
||||
// stays on the siblings' /sync surfaces.
|
||||
mux.HandleFunc("GET /api/nextcloud/status", authed(handlers.HandleNextcloudStatus))
|
||||
mux.HandleFunc("POST /api/nextcloud/connect", authed(handlers.NextcloudConnectLimiter(handlers.HandleNextcloudConnect)))
|
||||
|
||||
// 4.8 Alerts, contacts, email, dates, users
|
||||
mux.HandleFunc("GET /api/alerts/check", authed(handlers.HandleAlertsCheck))
|
||||
mux.HandleFunc("DELETE /api/alerts/{bookmarkId}", authed(handlers.HandleAlertsDismiss))
|
||||
|
||||
@@ -16,17 +16,19 @@ import (
|
||||
)
|
||||
|
||||
// fakeShare is a mutable minimal RFC 4918 server (Basic-auth erwin/s3cret)
|
||||
// rooted at /dav. files maps share-relative paths to etags; the listing is
|
||||
// rebuilt per PROPFIND so tests can add/change/remove files between syncs.
|
||||
// rooted at root (default /dav; the unified Nextcloud fake re-roots it).
|
||||
// files maps share-relative paths to etags; the listing is rebuilt per
|
||||
// PROPFIND so tests can add/change/remove files between syncs.
|
||||
type fakeShare struct {
|
||||
mu sync.Mutex
|
||||
root string
|
||||
files map[string]string
|
||||
srv *httptest.Server
|
||||
}
|
||||
|
||||
func newFakeShare(t *testing.T) *fakeShare {
|
||||
t.Helper()
|
||||
f := &fakeShare{files: map[string]string{
|
||||
f := &fakeShare{root: "/dav", files: map[string]string{
|
||||
"a.txt": "A1", "b c.md": "B1", "sub/d.txt": "D1",
|
||||
}}
|
||||
f.srv = httptest.NewServer(http.HandlerFunc(f.serve))
|
||||
@@ -57,7 +59,7 @@ func (f *fakeShare) serve(w http.ResponseWriter, r *http.Request) {
|
||||
http.Error(w, "no", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
dir := strings.Trim(strings.TrimPrefix(r.URL.Path, "/dav"), "/")
|
||||
dir := strings.Trim(strings.TrimPrefix(r.URL.Path, f.root), "/")
|
||||
f.mu.Lock()
|
||||
defer f.mu.Unlock()
|
||||
if dir != "" {
|
||||
@@ -88,7 +90,7 @@ func (f *fakeShare) serve(w http.ResponseWriter, r *http.Request) {
|
||||
segs[i] = url.PathEscape(segs[i])
|
||||
}
|
||||
}
|
||||
out := "/dav/" + strings.Join(segs, "/")
|
||||
out := f.root + "/" + strings.Join(segs, "/")
|
||||
if isDir {
|
||||
out += "/"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user