package server import ( "context" "encoding/base64" "encoding/xml" "strings" "testing" "pageswiki/internal/store" ) func taskDefaultNamePatch(name string) string { return `` + xmlText(name) + `` } func TestTaskDAVDefaultListRename(t *testing.T) { s, token, domain, actor := taskFixture(t) ctx := context.Background() c, pass, err := s.store.CreateDAVCredential(ctx, domain, actor, "rename", true, 1, "tasks", false) if err != nil { t.Fatal(err) } auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(c.ID+":"+pass)) item, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("task", "Keep this task"), 0) if err != nil { t.Fatal(err) } href := taskDAVCollection + item.UUID + ".ics" before := davTaskRequest(s, auth, "GET", href, "", nil) title := "個人 & " w := davTaskRequest(s, auth, "PROPPATCH", taskDAVCollection, taskDefaultNamePatch(title), map[string]string{"Content-Type": "application/xml; charset=utf-8"}) if w.Code != 207 || !strings.Contains(w.Body.String(), "200 OK") || !strings.Contains(w.Body.String(), taskDAVCollection) || !strings.Contains(w.Header().Get("Cache-Control"), "no-store") { t.Fatal(w.Code, w.Header(), w.Body) } for _, home := range []string{taskDAVCollection, strings.TrimSuffix(taskDAVCollection, "/"), taskDAVRoot, calendarDAV} { w = davTaskRequest(s, "Bearer "+token, "PROPFIND", home, commonDAVProps, nil) if w.Code != 207 || !strings.Contains(w.Body.String(), xmlText(title)) { t.Fatal(home, w.Code, w.Body) } } after := davTaskRequest(s, auth, "GET", href, "", nil) if after.Code != 200 || after.Body.String() != before.Body.String() || after.Header().Get("ETag") != before.Header().Get("ETag") { t.Fatal("rename changed task", after.Code, after.Body) } var count int if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_items WHERE kind='project'`).Scan(&count); err != nil || count != 0 { t.Fatal("default rename created a project", count, err) } if err = s.store.EnsureTasksSchema(); err != nil { t.Fatal(err) } if got, err := s.store.TaskDAVDefaultTitle(ctx, domain, actor); err != nil || got != title { t.Fatal("title lost on schema rebuild", got, err) } w = davTaskRequest(s, auth, "OPTIONS", taskDAVCollection, "", nil) if w.Code != 204 || !strings.Contains(w.Header().Get("Allow"), "PROPPATCH") { t.Fatal(w.Code, w.Header()) } // Do not accidentally add property writes to homes, tasks or named projects. for _, path := range []string{taskDAVRoot, calendarDAV, href} { w = davTaskRequest(s, auth, "PROPPATCH", path, taskDefaultNamePatch("Unexpected"), map[string]string{"Content-Type": "text/xml"}) if w.Code != 405 { t.Fatal(path, w.Code, w.Body) } } } func TestTaskDAVDefaultListRenameAtomicity(t *testing.T) { s, token, domain, actor := taskFixture(t) auth := "Bearer " + token ctx := context.Background() if err := s.store.SetTaskDAVDefaultTitle(ctx, domain, actor, "Original", ""); err != nil { t.Fatal(err) } for _, test := range []struct { body string code int status string }{ {strings.Replace(taskDefaultNamePatch("Changed"), "", `red`, 1), 207, "424 Failed Dependency"}, {strings.Replace(taskDefaultNamePatch("Changed"), "", `red`, 1), 207, "403 Forbidden"}, {taskDefaultNamePatch(""), 207, "409 Conflict"}, {taskDefaultNamePatch(strings.Repeat("x", 301)), 207, "409 Conflict"}, {taskDefaultNamePatch("new\nline"), 207, "409 Conflict"}, {``, 207, "403 Forbidden"}, {`Valid later`, 207, "409 Conflict"}, {`Changed`, 400, ""}, {``, 400, ""}, } { w := davTaskRequest(s, auth, "PROPPATCH", taskDAVCollection, test.body, map[string]string{"Content-Type": "application/xml"}) if w.Code != test.code || !strings.Contains(w.Body.String(), test.status) { t.Fatal(w.Code, w.Body) } if w.Code == 207 { var response davNode if err := xml.Unmarshal(w.Body.Bytes(), &response); err != nil { t.Fatal("invalid multistatus XML", err) } } if got, err := s.store.TaskDAVDefaultTitle(ctx, domain, actor); err != nil || got != "Original" { t.Fatal("partial update", got, err) } } // Supported instructions execute in document order, as one commit. body := `FirstLast` w := davTaskRequest(s, auth, "PROPPATCH", taskDAVCollection, body, map[string]string{"Content-Type": "text/xml"}) if w.Code != 207 || !strings.Contains(w.Body.String(), "200 OK") { t.Fatal(w.Code, w.Body) } if got, err := s.store.TaskDAVDefaultTitle(ctx, domain, actor); err != nil || got != "Last" { t.Fatal(got, err) } } func TestTaskDAVDefaultListRenameAuthority(t *testing.T) { s, _, domain, actor := taskFixture(t) ctx := context.Background() issue := func(owner, scope string, write bool) (store.TaskDAVCredential, string) { t.Helper() c, p, e := s.store.CreateDAVCredential(ctx, domain, owner, "fixture", write, 1, scope, false) if e != nil { t.Fatal(e) } return c, "Basic " + base64.StdEncoding.EncodeToString([]byte(c.ID+":"+p)) } patch := func(auth string, want int) { t.Helper() w := davTaskRequest(s, auth, "PROPPATCH", taskDAVCollection, taskDefaultNamePatch("Private title"), map[string]string{"Content-Type": "application/xml"}) if w.Code != want { t.Fatal(w.Code, w.Body) } } _, read := issue(actor, "tasks", false) _, cal := issue(actor, "calendar", true) c, write := issue(actor, "tasks", true) patch("", 401) patch(read, 403) patch(cal, 401) patch(write, 207) other, e := s.store.CreatePasswordAccount(domain, "default-list-other", "long fixture password", "Other", "") if e != nil { t.Fatal(e) } _, otherAuth := issue(other.UUID, "tasks", true) if got, e := s.store.TaskDAVDefaultTitle(ctx, domain, other.UUID); e != nil || got != store.DefaultTaskDAVTitle { t.Fatal(got, e) } w := davTaskRequest(s, otherAuth, "PROPFIND", calendarDAV, commonDAVProps, nil) if w.Code != 207 || strings.Contains(w.Body.String(), "Private title") { t.Fatal(w.Code, w.Body) } if err := s.store.SetTaskDAVDefaultTitle(ctx, "wrong-domain", actor, "No", c.ID); err == nil { t.Fatal("cross-domain write accepted") } if err := s.store.RevokeTaskDAVCredential(ctx, domain, actor, c.ID); err != nil { t.Fatal(err) } patch(write, 401) if err := s.store.SetTaskDAVDefaultTitle(ctx, domain, actor, "No", c.ID); err == nil { t.Fatal("revoked credential accepted by writer") } if err := s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil { t.Fatal(err) } patch(otherAuth, 404) if err := s.store.SetTaskDAVDefaultTitle(ctx, domain, actor, "No", ""); err == nil { t.Fatal("disabled extension accepted by writer") } }