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:
1 parent
288e6e94ac
commit
c1175d389f
699 files changed
+110197
-4117
No files matched your search
@@ -0,0 +1,170 @@
|
||||
package server
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"encoding/xml"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"pageswiki/internal/store"
|
||||
)
|
||||
|
||||
func taskDefaultNamePatch(name string) string {
|
||||
return `<d:propertyupdate xmlns:d="DAV:"><d:set><d:prop><d:displayname>` + xmlText(name) + `</d:displayname></d:prop></d:set></d:propertyupdate>`
|
||||
}
|
||||
|
||||
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 := "個人 & <Inbox>"
|
||||
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"), "</d:prop>", `<x:color xmlns:x="urn:unsupported">red</x:color></d:prop>`, 1), 207, "424 Failed Dependency"},
|
||||
{strings.Replace(taskDefaultNamePatch("Changed"), "</d:prop>", `<unqualified>red</unqualified></d:prop>`, 1), 207, "403 Forbidden"},
|
||||
{taskDefaultNamePatch(""), 207, "409 Conflict"},
|
||||
{taskDefaultNamePatch(strings.Repeat("x", 301)), 207, "409 Conflict"},
|
||||
{taskDefaultNamePatch("new\nline"), 207, "409 Conflict"},
|
||||
{`<d:propertyupdate xmlns:d="DAV:"><d:remove><d:prop><d:displayname/></d:prop></d:remove></d:propertyupdate>`, 207, "403 Forbidden"},
|
||||
{`<d:propertyupdate xmlns:d="DAV:"><d:set><d:prop><d:displayname/></d:prop></d:set><d:set><d:prop><d:displayname>Valid later</d:displayname></d:prop></d:set></d:propertyupdate>`, 207, "409 Conflict"},
|
||||
{`<d:propertyupdate xmlns:d="DAV:"><d:set><d:prop><d:displayname>Changed</d:displayname></d:prop></d:set><d:invalid/></d:propertyupdate>`, 400, ""},
|
||||
{`<!DOCTYPE d:propertyupdate><d:propertyupdate xmlns:d="DAV:"/>`, 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 := `<d:propertyupdate xmlns:d="DAV:"><d:set><d:prop><d:displayname>First</d:displayname></d:prop></d:set><d:set><d:prop><d:displayname>Last</d:displayname></d:prop></d:set></d:propertyupdate>`
|
||||
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")
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user