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.
300 lines
12 KiB
Go
300 lines
12 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"encoding/xml"
|
|
"errors"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
"time"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func davTaskRequest(s *Server, auth, method, path, body string, headers map[string]string) *httptest.ResponseRecorder {
|
|
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 headers {
|
|
r.Header.Set(k, v)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
|
|
const taskICALFixture = "BEGIN:VCALENDAR\r\nVERSION:2.0\r\nPRODID:-//CLI test//EN\r\nBEGIN:VTODO\r\nUID:cli-task@example.test\r\nDTSTAMP:20260925T000000Z\r\nSUMMARY:CLI task\r\nDESCRIPTION:Private note\\nSecond line\r\nSTATUS:NEEDS-ACTION\r\nPRIORITY:3\r\nDUE;VALUE=DATE:20261001\r\nCATEGORIES:Test,CLI\r\nX-CLIENT-TEST:retained\r\nEND:VTODO\r\nEND:VCALENDAR\r\n"
|
|
|
|
func TestTaskDAVRoundTrip(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
auth := "Bearer " + token
|
|
ctx := context.Background()
|
|
request := func(method, path, body string, h map[string]string, want int) *httptest.ResponseRecorder {
|
|
t.Helper()
|
|
w := davTaskRequest(s, auth, method, path, body, h)
|
|
if w.Code != want {
|
|
t.Fatalf("%s %s: %d want %d %s", method, path, w.Code, want, w.Body)
|
|
}
|
|
return w
|
|
}
|
|
home := request("PROPFIND", taskDAVRoot, `<d:propfind xmlns:d="DAV:"><d:prop><d:resourcetype/><d:displayname/></d:prop></d:propfind>`, nil, 207)
|
|
if !strings.Contains(home.Body.String(), taskDAVCollection) {
|
|
t.Fatal(home.Body)
|
|
}
|
|
p := taskDAVCollection + "client-name.ics"
|
|
request("PUT", p, taskICALFixture, map[string]string{"If-None-Match": "*"}, 201)
|
|
item, err := s.store.TaskDAVByName(ctx, domain, actor, "client-name.ics")
|
|
if err != nil || item.DueOn != "2026-10-01" || item.Notes != "Private note\nSecond line" || item.Priority != 2 {
|
|
t.Fatal(item, err)
|
|
}
|
|
get := request("GET", p, "", nil, 200)
|
|
tag := get.Header().Get("ETag")
|
|
if tag == "" || !strings.Contains(get.Body.String(), "X-CLIENT-TEST:retained") {
|
|
t.Fatal(get.Body)
|
|
}
|
|
if _, err = parseTaskICAL(get.Body.String()); err != nil {
|
|
t.Fatal(err, get.Body)
|
|
}
|
|
request("GET", p, "", map[string]string{"If-None-Match": tag}, 304)
|
|
request("PUT", p, taskICALFixture, nil, 204)
|
|
get = request("GET", p, "", nil, 200)
|
|
tag = get.Header().Get("ETag")
|
|
request("PUT", p, taskICALFixture, map[string]string{"If-Match": `"stale"`}, 412)
|
|
request("PUT", p, taskICALFixture, map[string]string{"If-None-Match": "*"}, 412)
|
|
request("PUT", taskDAVCollection+"other.ics", taskICALFixture, map[string]string{"If-None-Match": "*"}, 403)
|
|
completed := strings.Replace(get.Body.String(), "STATUS:NEEDS-ACTION", "STATUS:COMPLETED", 1)
|
|
request("PUT", p, completed, map[string]string{"If-Match": tag}, 204)
|
|
item, err = s.store.GetTask(ctx, domain, actor, item.UUID)
|
|
if err != nil || item.Status != "done" || item.CompletedMS == 0 {
|
|
t.Fatal(item, err)
|
|
}
|
|
get = request("GET", p, "", nil, 200)
|
|
if get.Header().Get("ETag") == tag {
|
|
t.Fatal("unchanged etag")
|
|
}
|
|
tag = get.Header().Get("ETag")
|
|
// Web edits preserve UID/name and metadata even if not sent by the UI.
|
|
item.Title = "Web title 日本語"
|
|
item.CalDAV = nil
|
|
if _, err = s.store.SaveTask(ctx, domain, actor, item, item.Version); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
get = request("GET", p, "", nil, 200)
|
|
if !strings.Contains(get.Body.String(), "SUMMARY:Web title 日本語") || !strings.Contains(get.Body.String(), "CATEGORIES:Test,CLI") || get.Header().Get("ETag") == tag {
|
|
t.Fatal(get.Body)
|
|
}
|
|
query := `<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"><d:prop><d:getetag/><c:calendar-data/></d:prop><c:filter><c:comp-filter name="VCALENDAR"><c:comp-filter name="VTODO"/></c:comp-filter></c:filter></c:calendar-query>`
|
|
report := request("REPORT", taskDAVCollection, query, nil, 207)
|
|
if !strings.Contains(report.Body.String(), "STATUS:COMPLETED") {
|
|
t.Fatal("completed tasks missing", report.Body)
|
|
}
|
|
var n davNode
|
|
if err = xml.Unmarshal(report.Body.Bytes(), &n); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
badFilter := strings.Replace(query, `<c:comp-filter name="VTODO"/>`, `<c:comp-filter name="VTODO"><c:time-range start="20260901T000000Z"/></c:comp-filter>`, 1)
|
|
request("REPORT", taskDAVCollection, badFilter, nil, 403)
|
|
multi := `<c:calendar-multiget xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"><d:prop><d:getetag/><c:calendar-data/></d:prop><d:href>` + p + `</d:href><d:href>` + taskDAVCollection + `missing.ics</d:href><d:href>https://evil.test` + p + `</d:href></c:calendar-multiget>`
|
|
report = request("REPORT", taskDAVCollection, multi, nil, 207)
|
|
if strings.Count(report.Body.String(), "404 Not Found") != 2 {
|
|
t.Fatal(report.Body)
|
|
}
|
|
request("DELETE", p, "", map[string]string{"If-Match": get.Header().Get("ETag")}, 204)
|
|
request("GET", p, "", nil, 404)
|
|
report = request("REPORT", taskDAVCollection, query, nil, 207)
|
|
if strings.Contains(report.Body.String(), p) {
|
|
t.Fatal("archived task in collection")
|
|
}
|
|
item, err = s.store.GetTask(ctx, domain, actor, item.UUID)
|
|
if err != nil || !item.Archived {
|
|
t.Fatal("delete must archive", err)
|
|
}
|
|
item.Archived = false
|
|
if _, err = s.store.SaveTask(ctx, domain, actor, item, item.Version); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request("GET", p, "", nil, 200)
|
|
}
|
|
|
|
func TestTaskDAVCredentialsIsolation(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
w := taskRequest(s, token, "POST", "dav-credentials", map[string]any{"label": "CLI", "write": true, "days": 30}, "")
|
|
if w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
var c struct {
|
|
Username, Password string
|
|
Credential store.TaskDAVCredential
|
|
}
|
|
if err := json.Unmarshal(w.Body.Bytes(), &c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(c.Username+":"+c.Password))
|
|
req := func(method, path, body string, headers map[string]string, want int) {
|
|
t.Helper()
|
|
w := davTaskRequest(s, auth, method, path, body, headers)
|
|
if w.Code != want {
|
|
t.Fatal(method, path, w.Code, w.Body)
|
|
}
|
|
}
|
|
req("PROPFIND", taskDAVRoot, "", nil, 207)
|
|
req("PUT", taskDAVCollection+"one.ics", taskICALFixture, map[string]string{"If-None-Match": "*"}, 201)
|
|
req("GET", tasksAPI+"items", "", nil, 401)
|
|
req("POST", tasksAPI+"dav-credentials", `{"label":"escalation","write":true}`, nil, 401)
|
|
list := taskRequest(s, token, "GET", "dav-credentials", nil, "")
|
|
if list.Code != 200 || strings.Contains(list.Body.String(), c.Password) || strings.Contains(list.Body.String(), "secret_hash") {
|
|
t.Fatal(list.Body)
|
|
}
|
|
var hash string
|
|
s.store.DB.QueryRow(`SELECT secret_hash FROM organizer_dav_credentials WHERE id=?`, c.Username).Scan(&hash)
|
|
if hash == "" || hash == c.Password {
|
|
t.Fatal("credential not hashed")
|
|
}
|
|
credential, pass, err := s.store.CreateTaskDAVCredential(ctx, domain, actor, "Reader", false, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
auth = "Basic " + base64.StdEncoding.EncodeToString([]byte(credential.ID+":"+pass))
|
|
req("GET", taskDAVCollection+"one.ics", "", nil, 200)
|
|
req("PUT", taskDAVCollection+"two.ics", taskICALFixture, map[string]string{"If-None-Match": "*"}, 403)
|
|
if err = s.store.RevokeTaskDAVCredential(ctx, domain, actor, credential.ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
req("GET", taskDAVCollection+"one.ics", "", nil, 401)
|
|
auth = "Basic " + base64.StdEncoding.EncodeToString([]byte(c.Username+":"+c.Password))
|
|
s.store.DB.Exec(`UPDATE principals SET state='suspended' WHERE uuid=?`, actor)
|
|
req("GET", taskDAVCollection+"one.ics", "", nil, 401)
|
|
s.store.DB.Exec(`UPDATE principals SET state='active' WHERE uuid=?`, actor)
|
|
s.store.DB.Exec(`UPDATE organizer_dav_credentials SET expires_ms=? WHERE id=?`, time.Now().Add(-time.Hour).UnixMilli(), c.Username)
|
|
req("GET", taskDAVCollection+"one.ics", "", nil, 401)
|
|
// Revocation rechecked inside mutation transaction, not just at HTTP entry.
|
|
item, err := s.store.TaskDAVByName(ctx, domain, actor, "one.ics")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item.Title = "must not save"
|
|
if _, err = s.store.SaveTaskDAV(ctx, domain, actor, item, item.Version, nil, c.Username); err != store.ErrAccessDenied {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
|
|
func TestTaskICALValidationAndFolding(t *testing.T) {
|
|
for _, change := range []string{"RRULE:FREQ=DAILY", "DUE:20260925T100000Z", "ATTENDEE:mailto:anyone@example.test", "DURATION:PT1H", "BEGIN:VALARM\r\nACTION:DISPLAY\r\nEND:VALARM", "SUMMARY:duplicate"} {
|
|
input := strings.Replace(taskICALFixture, "END:VTODO", change+"\r\nEND:VTODO", 1)
|
|
if _, err := parseTaskICAL(input); err == nil {
|
|
t.Fatal("accepted", change)
|
|
}
|
|
}
|
|
item := newTaskRecord("task", strings.Repeat("日", 90))
|
|
item.CreatedMS = 1
|
|
item.UpdatedMS = 2
|
|
item.Version = 1
|
|
data, _, err := taskICAL(item)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, line := range strings.Split(data, "\r\n") {
|
|
if len(line) > 75 {
|
|
t.Fatal("unfolded long line", len(line))
|
|
}
|
|
}
|
|
if _, err = parseTaskICAL(data); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// All-day UI start=due cannot be encoded as invalid RFC VTODO dates.
|
|
item.StartOn = "2026-09-25"
|
|
item.DueOn = "2026-09-25"
|
|
data, _, err = taskICAL(item)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cal, err := parseTaskICAL(data)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
round := taskFromICAL(cal, item)
|
|
if round.StartOn != item.StartOn {
|
|
t.Fatal("same-day start lost")
|
|
}
|
|
}
|
|
|
|
func TestTaskDAVPrivateScopeAndConcurrency(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
own, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Secret task"), 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
other, err := s.store.CreatePasswordAccount(domain, "davother", "a sufficiently long password", "Other", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
credential, password, err := s.store.CreateTaskDAVCredential(ctx, domain, other.UUID, "Other CLI", true, 1)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(credential.ID+":"+password))
|
|
p := taskDAVCollection + own.UUID + ".ics"
|
|
if w := davTaskRequest(s, auth, "GET", p, "", nil); w.Code != 404 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
if w := davTaskRequest(s, auth, "PROPFIND", taskDAVCollection, "", nil); w.Code != 207 || strings.Contains(w.Body.String(), "Secret task") {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
if _, _, err = s.store.AuthenticateTaskDAV(ctx, "wrong-domain", credential.ID, password); !errors.Is(err, store.ErrAccessDenied) {
|
|
t.Fatal("cross-domain credential", err)
|
|
}
|
|
auth = "Bearer " + token
|
|
g := davTaskRequest(s, auth, "GET", p, "", nil)
|
|
if g.Code != 200 {
|
|
t.Fatal(g.Code)
|
|
}
|
|
body := strings.Replace(g.Body.String(), "SUMMARY:Secret task", "SUMMARY:Changed", 1)
|
|
var wg sync.WaitGroup
|
|
codes := make(chan int, 2)
|
|
for i := 0; i < 2; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
codes <- davTaskRequest(s, auth, "PUT", p, body, map[string]string{"If-Match": g.Header().Get("ETag")}).Code
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
close(codes)
|
|
counts := map[int]int{}
|
|
for code := range codes {
|
|
counts[code]++
|
|
}
|
|
if counts[204] != 1 || counts[412] != 1 {
|
|
t.Fatal("CAS", counts)
|
|
}
|
|
// Extension kill switch applies to discovery, reads and writes alike.
|
|
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if w := davTaskRequest(s, auth, "PROPFIND", taskDAVRoot, "", nil); w.Code != 404 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
}
|
|
|
|
func TestTaskDAVCollectionNeverTruncates(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
// Synthetic DB-only rows exercise the listing guard without expensive CAS writes.
|
|
_, err := s.store.DB.Exec(`WITH RECURSIVE n(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM n WHERE x<10001)
|
|
INSERT INTO organizer_items(domain_uuid,owner_uuid,uuid,kind,project_uuid,title,status,bucket,due_on,planned_on,review_on,archived,version,created_at_ms,updated_at_ms,data,document_hash)
|
|
SELECT ?,?,'fixture-'||x,'task','','Fixture','todo','inbox','','','',0,1,x,x,'{}','' FROM n`, domain, actor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w := davTaskRequest(s, "Bearer "+token, "PROPFIND", taskDAVCollection, "", nil)
|
|
if w.Code != 507 || strings.Contains(w.Body.String(), "multistatus") {
|
|
t.Fatal("partial listing would imply deletions", w.Code, w.Body)
|
|
}
|
|
}
|