Implement Knowledge City platform, organizer and DAV integration

Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
This commit is contained in:
Anya Tokugawa committed 2026-09-27 14:44:43 +09:00
1 parent 288e6e94ac
commit c1175d389f
699 files changed
+110197 -4117

No files matched your search

+153
View File
@@ -0,0 +1,153 @@
package server
import (
"context"
"encoding/base64"
"net/http/httptest"
"strings"
"testing"
"time"
"pageswiki/internal/calendar"
)
const commonDAVProps = `<d:propfind xmlns:d="DAV:" xmlns:c="urn:ietf:params:xml:ns:caldav"><d:prop><d:current-user-principal/><c:calendar-home-set/><d:resourcetype/><d:getetag/><d:displayname/><d:current-user-privilege-set/></d:prop></d:propfind>`
const commonDAVQuery = `<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"><d:prop><d:getetag/><c:calendar-data><c:comp name="VCALENDAR"><c:allprop/><c:allcomp/></c:comp></c:calendar-data></d:prop><c:filter><c:comp-filter name="VCALENDAR"/></c:filter></c:calendar-query>`
func TestCommonDAVScopesAndCalendarWrites(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
repo, err := s.calendars()
if err != nil {
t.Fatal(err)
}
c, err := repo.Create(ctx, domain, actor, "Common fixture")
if err != nil {
t.Fatal(err)
}
col := calendarDAV + c.ID + "/"
item, err := calendar.Build("", calendar.Event{UID: "common@example.test", Summary: "Synthetic event", Start: time.Date(2026, 10, 1, 9, 0, 0, 0, time.UTC), End: time.Date(2026, 10, 1, 10, 0, 0, 0, time.UTC)})
if err != nil {
t.Fatal(err)
}
makeAuth := func(scope string, write bool) (string, string) {
t.Helper()
c, p, e := s.store.CreateDAVCredential(ctx, domain, actor, scope, write, 1, scope)
if e != nil {
t.Fatal(e)
}
return "Basic " + base64.StdEncoding.EncodeToString([]byte(c.ID+":"+p)), c.ID
}
taskAuth, legacyID := makeAuth("tasks", true)
// Simulate a credential from before scope storage existed.
if _, err = s.store.DB.Exec(`DELETE FROM organizer_dav_scopes WHERE credential_id=?`, legacyID); err != nil {
t.Fatal(err)
}
calAuth, _ := makeAuth("calendar", true)
allAuth, allID := makeAuth("all", true)
readAuth, _ := makeAuth("all", false)
req := func(auth, method, path, body string, h map[string]string, want int) *httptest.ResponseRecorder {
t.Helper()
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(body))
r.Header.Set("Authorization", auth)
r.Header.Set("Depth", "1")
r.Header.Set("Content-Type", "text/calendar")
for k, v := range h {
r.Header.Set(k, v)
}
w := httptest.NewRecorder()
s.handler().ServeHTTP(w, r)
if w.Code != want {
t.Fatalf("%s %s: %d want %d %s", method, path, w.Code, want, w.Body)
}
return w
}
for _, p := range []string{calendarDAV, strings.TrimSuffix(calendarDAV, "/")} {
for _, test := range []struct {
auth string
tasks, cal bool
}{{taskAuth, true, false}, {calAuth, false, true}, {allAuth, true, true}} {
w := req(test.auth, "PROPFIND", p, commonDAVProps, nil, 207)
if strings.Contains(w.Body.String(), taskDAVCollection) != test.tasks || strings.Contains(w.Body.String(), col) != test.cal || w.Header().Get("Location") != "" {
t.Fatal(w.Body)
}
if !strings.Contains(w.Body.String(), "current-user-principal") {
t.Fatal(w.Body)
}
}
}
req(taskAuth, "PROPFIND", col, commonDAVProps, nil, 401)
req(calAuth, "PROPFIND", taskDAVRoot, commonDAVProps, nil, 401)
req(allAuth, "GET", davCredentialsAPI, "", nil, 401)
req(allAuth, "POST", davCredentialsAPI, `{"label":"escalate","scope":"all"}`, nil, 401)
unauth := req(allAuth, "GET", calendarAPI+"calendars", "", nil, 200)
if !strings.Contains(unauth.Body.String(), `"actor":""`) || strings.Contains(unauth.Body.String(), c.ID) {
t.Fatal("DAV credential escaped to JSON API", unauth.Body)
}
path := col + "arbitrary-client-name.ics"
req(readAuth, "PUT", path, item.Data, nil, 403)
req(calAuth, "PUT", path, item.Data, nil, 201)
w := req(allAuth, "GET", path, "", nil, 200)
tag := w.Header().Get("ETag")
req(allAuth, "GET", path, "", map[string]string{"If-None-Match": tag}, 304)
req(allAuth, "PUT", path, item.Data, nil, 428)
req(allAuth, "PUT", col+"duplicate.ics", item.Data, nil, 412)
req(allAuth, "PUT", path, item.Data, map[string]string{"If-Match": `"stale"`}, 412)
updated, err := calendar.Build(item.Data, calendar.Event{UID: item.UID, Summary: "Edited through CLI", Start: item.Start, End: item.End})
if err != nil {
t.Fatal(err)
}
req(allAuth, "PUT", path, updated.Data, map[string]string{"If-Match": tag}, 204)
for _, method := range []string{"PROPFIND", "REPORT"} {
body := commonDAVProps
if method == "REPORT" {
body = commonDAVQuery
}
w = req(allAuth, method, col, body, nil, 207)
if !strings.Contains(w.Body.String(), path) {
t.Fatal("lost client filename", w.Body)
}
}
// Reject a partial filter instead of silently returning the entire collection.
bad := strings.Replace(commonDAVQuery, `name="VCALENDAR"`, `name="VCALENDAR" test="anyof"`, 1)
req(allAuth, "REPORT", col, bad, nil, 403)
// Canonical URI used by the existing Web UI still resolves after CLI creation.
req("Bearer "+token, "GET", calendarHref(c.ID, item.UID), "", nil, 200)
projected := req("Bearer "+token, "GET", calendarAPI+"calendars/"+c.ID+"/events?resource=arbitrary-client-name.ics", "", nil, 200)
if !strings.Contains(projected.Body.String(), "Edited through CLI") {
t.Fatal("Web viewer projection failed", projected.Body)
}
// ACL revocation and membership checks are independent of the secret's scope.
if _, err = s.store.DB.Exec(`UPDATE principals SET state='suspended' WHERE uuid=?`, actor); err != nil {
t.Fatal(err)
}
req(allAuth, "GET", path, "", nil, 401)
s.store.DB.Exec(`UPDATE principals SET state='active' WHERE uuid=?`, actor)
if err = s.store.RevokeTaskDAVCredential(ctx, domain, actor, allID); err != nil {
t.Fatal(err)
}
req(allAuth, "GET", path, "", nil, 401)
// Permission recheck inside the calendar's locked write path.
_, _, err = repo.ChangeDAV(ctx, domain, c.ID, actor, "arbitrary-client-name.ics", updated.ETag, &updated, func() error { return s.store.CheckDAVCredential(ctx, domain, actor, allID, true, "calendar") })
if err != calendar.ErrForbidden {
t.Fatal("revoked credential wrote", err)
}
req(calAuth, "DELETE", path, "", map[string]string{"If-Match": updated.ETag}, 204)
req(calAuth, "GET", path, "", nil, 404)
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.calendar":false}`); err != nil {
t.Fatal(err)
}
w = req(taskAuth, "PROPFIND", calendarDAV, commonDAVProps, nil, 207)
if strings.Contains(w.Body.String(), col) || !strings.Contains(w.Body.String(), taskDAVCollection) {
t.Fatal("disabled calendar exposed", w.Body)
}
req(calAuth, "PROPFIND", col, commonDAVProps, nil, 404)
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil {
t.Fatal(err)
}
w = req(calAuth, "PROPFIND", calendarDAV, commonDAVProps, nil, 207)
if strings.Contains(w.Body.String(), taskDAVCollection) || !strings.Contains(w.Body.String(), col) {
t.Fatal("disabled tasks exposed", w.Body)
}
req(taskAuth, "PROPFIND", taskDAVRoot, commonDAVProps, nil, 404)
}