Files
pages/internal/server/task_dav_discovery_test.go
Anya Tokugawa c1175d389f 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.
2026-09-27 14:44:43 +09:00

90 lines
2.8 KiB
Go

package server
import (
"context"
"encoding/xml"
"net/http/httptest"
"strings"
"sync"
"testing"
)
func TestTaskDAVSlashlessDiscovery(t *testing.T) {
s, token, _, _ := taskFixture(t)
for _, p := range []string{taskDAVRoot, strings.TrimSuffix(taskDAVRoot, "/"), taskDAVCollection, strings.TrimSuffix(taskDAVCollection, "/")} {
r := httptest.NewRequest("PROPFIND", "http://localhost"+p, strings.NewReader(`<d:propfind xmlns:d="DAV:"><d:prop><d:resourcetype/></d:prop></d:propfind>`))
r.Header.Set("Authorization", "Bearer "+token)
r.Header.Set("Depth", "0")
w := httptest.NewRecorder()
s.handler().ServeHTTP(w, r)
if w.Code != 207 || w.Header().Get("Location") != "" {
t.Fatal(p, w.Code, w.Header(), w.Body)
}
}
}
func TestTaskDAVFullCalendarDataSelection(t *testing.T) {
full := `<calendar-data xmlns="urn:ietf:params:xml:ns:caldav"><comp name="VCALENDAR"><allprop/><allcomp/></comp></calendar-data>`
for _, test := range []struct {
data string
want bool
}{
{full, true},
{strings.Replace(full, `<allprop/>`, `<allcomp/>`, 1), false},
{strings.Replace(full, `<allprop/>`, `<prop name="SUMMARY"/>`, 1), false},
{strings.Replace(full, `VCALENDAR`, `VTODO`, 1), false},
{strings.Replace(full, `<allcomp/>`, `<allcomp/><expand start="20260901T000000Z" end="20261001T000000Z"/>`, 1), false},
{strings.Replace(full, `<allcomp/>`, `<allcomp extra="ignored"/>`, 1), false},
} {
var n davNode
if e := xml.Unmarshal([]byte(test.data), &n); e != nil {
t.Fatal(e)
}
if got := taskDAVFullProperty(n); got != test.want {
t.Fatal(test.data, got)
}
}
}
func TestTaskDAVConditionalCreateCannotOverwrite(t *testing.T) {
s, token, domain, actor := taskFixture(t)
auth := "Bearer " + token
p := taskDAVCollection + "new.ics"
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, taskICALFixture, map[string]string{"If-None-Match": "*"}).Code
}()
}
wg.Wait()
close(codes)
success := 0
for code := range codes {
if code == 201 {
success++
} else if code != 403 && code != 412 {
t.Fatal("unsafe concurrent create", code)
}
}
if success != 1 {
t.Fatal(success)
}
item, e := s.store.TaskDAVByName(context.Background(), domain, actor, "new.ics")
if e != nil || item.Version != 1 {
t.Fatal(item.Version, e)
}
get := davTaskRequest(s, auth, "GET", p, "", nil)
if w := davTaskRequest(s, auth, "PUT", p, taskICALFixture, map[string]string{"If-None-Match": "*"}); w.Code != 412 {
t.Fatal("conditional overwrite", w.Code)
}
if w := davTaskRequest(s, auth, "DELETE", p, "", map[string]string{"If-Match": get.Header().Get("ETag")}); w.Code != 204 {
t.Fatal(w.Code)
}
if w := davTaskRequest(s, auth, "PUT", p, taskICALFixture, nil); w.Code != 201 {
t.Fatal("unconditional revive", w.Code)
}
}