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.
251 lines
10 KiB
Go
251 lines
10 KiB
Go
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 = `<?xml version="1.0" encoding="UTF-8"?>
|
|
<mkcalendar xmlns="urn:ietf:params:xml:ns:caldav"><set xmlns="DAV:"><prop>
|
|
<displayname>テスト</displayname>
|
|
<supported-calendar-component-set xmlns="urn:ietf:params:xml:ns:caldav"><comp name="VTODO"/></supported-calendar-component-set>
|
|
</prop></set></mkcalendar>`
|
|
|
|
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 := `<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>` + resource + `</d:href><d:href>` + taskDAVCollection + `client.ics</d:href></c:calendar-multiget>`
|
|
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, "</prop>", "<unknown/></prop>", 1), 403)
|
|
request(write, "MKCALENDAR", col, strings.Replace(taskMKCalendarFixture, "<displayname>テスト</displayname>", "<displayname>テスト</displayname><displayname>duplicate</displayname>", 1), 400)
|
|
request(write, "MKCALENDAR", col, "<invalid", 400)
|
|
request(write, "MKCALENDAR", calendarDAV+"not-a-uuid/", taskMKCalendarFixture, 403)
|
|
request(write, "MKCALENDAR", col+"child/", taskMKCalendarFixture, 403)
|
|
var count int
|
|
if err := s.store.DB.QueryRow(`SELECT count(*) FROM organizer_dav_collections`).Scan(&count); err != nil || count != 0 {
|
|
t.Fatal(count, err)
|
|
}
|
|
request(write, "MKCALENDAR", col, taskMKCalendarFixture, 201)
|
|
other, err := s.store.CreatePasswordAccount(domain, "other-list-owner", "a sufficiently long password", "Other", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
otherAuth := issue(other.UUID, "all", true)
|
|
request(otherAuth, "PROPFIND", col, "", 404)
|
|
request(otherAuth, "PUT", col+"secret.ics", taskICALFixture, 404)
|
|
request(otherAuth, "MKCALENDAR", col, taskMKCalendarFixture, 405)
|
|
w := davTaskRequest(s, otherAuth, "PROPFIND", calendarDAV, "", nil)
|
|
if w.Code != 207 || strings.Contains(w.Body.String(), col) {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
// Refuse collisions with an existing VEVENT calendar; read no event bodies.
|
|
repo, err := s.calendars()
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
c, err := repo.Create(ctx, domain, actor, "Collision fixture")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request(write, "MKCALENDAR", calendarDAV+c.ID+"/", taskMKCalendarFixture, 405)
|
|
// An arbitrary JSON update cannot convert a project into a DAV route.
|
|
p := newTaskRecord("project", "ordinary")
|
|
p.DAVCollection = uuid.NewString()
|
|
p, err = s.store.SaveTask(ctx, domain, actor, p, 0)
|
|
if err != nil || p.DAVCollection != "" {
|
|
t.Fatal(p, err)
|
|
}
|
|
// Concurrent retry creates exactly one project/revision for the URI.
|
|
concurrent := calendarDAV + uuid.NewString() + "/"
|
|
codes := make(chan int, 2)
|
|
var wg sync.WaitGroup
|
|
for i := 0; i < 2; i++ {
|
|
wg.Add(1)
|
|
go func() {
|
|
defer wg.Done()
|
|
codes <- davTaskRequest(s, "Bearer "+token, "MKCALENDAR", concurrent, taskMKCalendarFixture, map[string]string{"Content-Type": "text/xml"}).Code
|
|
}()
|
|
}
|
|
wg.Wait()
|
|
close(codes)
|
|
results := map[int]int{}
|
|
for code := range codes {
|
|
results[code]++
|
|
}
|
|
if results[201] != 1 || results[405] != 1 {
|
|
t.Fatal(results)
|
|
}
|
|
if err = s.store.SetConfigInDomain(domain, "system_extension_states", `{"organizer.tasks":false}`); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
request(write, "MKCALENDAR", calendarDAV+uuid.NewString()+"/", taskMKCalendarFixture, 404)
|
|
request(write, "PROPFIND", col, "", 404)
|
|
}
|