package server
import (
"context"
"encoding/base64"
"net/http/httptest"
"strings"
"sync"
"testing"
"github.com/google/uuid"
)
// The exact request shape reported by OneTask, with synthetic identifiers only.
const taskMKCalendarFixture = `
テスト
`
func TestTaskDAVNamedCollection(t *testing.T) {
s, _, domain, actor := taskFixture(t)
ctx := context.Background()
credential, password, err := s.store.CreateDAVCredential(ctx, domain, actor, "OneTask fixture", true, 1, "all", true)
if err != nil {
t.Fatal(err)
}
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(credential.ID+":"+password))
req := func(method, path, body string, headers 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", "application/xml; charset=utf-8")
if method == "PUT" {
r.Header.Set("Content-Type", "text/calendar")
}
for k, v := range headers {
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
}
id := uuid.NewString()
col := calendarDAV + id + "/"
w := req("MKCALENDAR", col, taskMKCalendarFixture, nil, 201)
if w.Header().Get("Location") != col || !strings.Contains(w.Header().Get("Cache-Control"), "no-cache") {
t.Fatal(w.Header())
}
req("MKCALENDAR", col, taskMKCalendarFixture, nil, 405)
project, err := s.store.TaskDAVCollection(ctx, domain, actor, id)
if err != nil || project.Title != "テスト" || project.Kind != "project" || project.DAVCollection != id {
t.Fatal(project, err)
}
for _, home := range []string{calendarDAV, taskDAVRoot} {
w = req("PROPFIND", home, "", nil, 207)
if !strings.Contains(w.Body.String(), col) || !strings.Contains(w.Body.String(), "テスト") {
t.Fatal(w.Body)
}
}
for _, path := range []string{col, strings.TrimSuffix(col, "/")} {
req("OPTIONS", path, "", nil, 204)
w = req("PROPFIND", path, "", nil, 207)
if !strings.Contains(w.Body.String(), `name="VTODO"`) {
t.Fatal(w.Body)
}
}
resource := col + "client.ics"
req("PUT", resource, taskICALFixture, nil, 201)
item, err := s.store.TaskDAVByName(ctx, domain, actor, "client.ics", project.UUID)
if err != nil || item.ProjectUUID != project.UUID {
t.Fatal(item, err)
}
// The same filename AND UID in another collection is a different resource.
defaultBody := strings.Replace(taskICALFixture, "SUMMARY:CLI task", "SUMMARY:Default-only task", 1)
req("PUT", taskDAVCollection+"client.ics", defaultBody, nil, 201)
w = req("REPORT", col, commonDAVQuery, nil, 207)
if !strings.Contains(w.Body.String(), resource) || strings.Contains(w.Body.String(), "Default-only task") {
t.Fatal(w.Body)
}
w = req("REPORT", taskDAVCollection, commonDAVQuery, nil, 207)
if strings.Contains(w.Body.String(), resource) || strings.Contains(w.Body.String(), "SUMMARY:CLI task") {
t.Fatal(w.Body)
}
multi := `` + resource + `` + taskDAVCollection + `client.ics`
w = req("REPORT", col, multi, nil, 207)
if strings.Count(w.Body.String(), "404 Not Found") != 1 || strings.Contains(w.Body.String(), "Default-only task") {
t.Fatal(w.Body)
}
get := req("GET", resource, "", nil, 200)
req("PUT", resource, taskICALFixture, nil, 204)
get = req("GET", resource, "", nil, 200)
req("PUT", resource, taskICALFixture, map[string]string{"If-Match": `"stale"`}, 412)
completed := strings.Replace(get.Body.String(), "STATUS:NEEDS-ACTION", "STATUS:COMPLETED", 1)
req("PUT", resource, completed, map[string]string{"If-Match": get.Header().Get("ETag")}, 204)
item, err = s.store.GetTask(ctx, domain, actor, item.UUID)
if err != nil || item.Status != "done" {
t.Fatal(item, err)
}
// Web edits preserve both canonical collection identity and ICS metadata.
project.Title, project.DAVCollection = "Web renamed list", ""
project, err = s.store.SaveTask(ctx, domain, actor, project, project.Version)
if err != nil || project.DAVCollection != id {
t.Fatal(project, err)
}
item.Title = "Web edited task"
item, err = s.store.SaveTask(ctx, domain, actor, item, item.Version)
if err != nil {
t.Fatal(err)
}
get = req("GET", resource, "", nil, 200)
if !strings.Contains(get.Body.String(), "Web edited task") {
t.Fatal(get.Body)
}
// Index rebuild from canonical project/task heads preserves collection hrefs.
if _, err = s.store.DB.Exec(`DELETE FROM organizer_dav_resources; DELETE FROM organizer_dav_collections`); err != nil {
t.Fatal(err)
}
if err = s.store.EnsureTasksSchema(); err != nil {
t.Fatal(err)
}
req("GET", resource, "", nil, 200)
req("DELETE", resource, "", map[string]string{"If-Match": get.Header().Get("ETag")}, 204)
req("GET", resource, "", nil, 404)
// A native Web task follows its project between default and named lists.
native, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Move between lists"), 0)
if err != nil {
t.Fatal(err)
}
defaultHref := taskDAVCollection + native.UUID + ".ics"
namedHref := col + native.UUID + ".ics"
req("GET", defaultHref, "", nil, 200)
native.ProjectUUID = project.UUID
native, err = s.store.SaveTask(ctx, domain, actor, native, native.Version)
if err != nil {
t.Fatal(err)
}
req("GET", defaultHref, "", nil, 404)
req("GET", namedHref, "", nil, 200)
native.ProjectUUID = ""
if _, err = s.store.SaveTask(ctx, domain, actor, native, native.Version); err != nil {
t.Fatal(err)
}
req("GET", defaultHref, "", nil, 200)
req("GET", namedHref, "", nil, 404)
// Collection DELETE isn't secretly implemented as deleting all its tasks.
req("DELETE", col, "", nil, 405)
// Named task collections keep working with the VEVENT extension disabled.
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.calendar":false}`); err != nil {
t.Fatal(err)
}
req("PROPFIND", col, "", nil, 207)
req("MKCALENDAR", calendarDAV+uuid.NewString()+"/", taskMKCalendarFixture, nil, 201)
}
func TestTaskDAVCollectionAuthorizationAndAtomicity(t *testing.T) {
s, token, domain, actor := taskFixture(t)
ctx := context.Background()
col := calendarDAV + uuid.NewString() + "/"
request := func(auth, method, path, body string, want int) {
t.Helper()
w := davTaskRequest(s, auth, method, path, body, map[string]string{"Content-Type": "application/xml"})
if w.Code != want {
t.Fatalf("%s: %d want %d %s", method, w.Code, want, w.Body)
}
}
issue := func(owner, scope string, write bool) string {
t.Helper()
c, p, e := s.store.CreateDAVCredential(ctx, domain, owner, "fixture", write, 1, scope, true)
if e != nil {
t.Fatal(e)
}
return "Basic " + base64.StdEncoding.EncodeToString([]byte(c.ID+":"+p))
}
write := issue(actor, "tasks", true)
read := issue(actor, "tasks", false)
calendarOnly := issue(actor, "calendar", true)
request("", "MKCALENDAR", col, taskMKCalendarFixture, 401)
request(read, "MKCALENDAR", col, taskMKCalendarFixture, 403)
request(calendarOnly, "MKCALENDAR", col, taskMKCalendarFixture, 401)
request(write, "MKCALENDAR", col, strings.Replace(taskMKCalendarFixture, "VTODO", "VEVENT", 1), 403)
request(write, "MKCALENDAR", col, strings.Replace(taskMKCalendarFixture, "", "", 1), 403)
request(write, "MKCALENDAR", col, strings.Replace(taskMKCalendarFixture, "テスト", "テストduplicate", 1), 400)
request(write, "MKCALENDAR", col, "