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.
231 lines
8.5 KiB
Go
231 lines
8.5 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"fmt"
|
|
"strings"
|
|
"sync"
|
|
"testing"
|
|
|
|
"pageswiki/internal/store"
|
|
)
|
|
|
|
func TestTaskDAVNativeProjectsAndUpgrade(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
auth := "Bearer " + token
|
|
save := func(v store.TaskRecord) store.TaskRecord {
|
|
t.Helper()
|
|
out, err := s.store.SaveTask(ctx, domain, actor, v, v.Version)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return out
|
|
}
|
|
project := save(newTaskRecord("project", "Native project"))
|
|
col := calendarDAV + project.TaskDAVCollectionPath() + "/"
|
|
child := newTaskRecord("task", "Native project task")
|
|
child.ProjectUUID = project.UUID
|
|
child = save(child)
|
|
loose := save(newTaskRecord("task", "Unassigned task"))
|
|
href := col + child.UUID + ".ics"
|
|
get := davTaskRequest(s, auth, "GET", href, "", nil)
|
|
if get.Code != 200 {
|
|
t.Fatal(get.Code, get.Body)
|
|
}
|
|
for _, home := range []string{calendarDAV, taskDAVRoot} {
|
|
w := davTaskRequest(s, auth, "PROPFIND", home, "", nil)
|
|
if w.Code != 207 || !strings.Contains(w.Body.String(), col) || !strings.Contains(w.Body.String(), "Native project") {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
}
|
|
w := davTaskRequest(s, auth, "REPORT", taskDAVCollection, commonDAVQuery, nil)
|
|
if w.Code != 207 || strings.Contains(w.Body.String(), child.UUID) || !strings.Contains(w.Body.String(), loose.UUID) {
|
|
t.Fatal("default must mean no project", w.Code, w.Body)
|
|
}
|
|
// Simulate a pre-upgrade DB where a native project's task lived in default.
|
|
if _, err := s.store.DB.Exec(`DELETE FROM organizer_dav_collections WHERE project_uuid=?`, project.UUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.store.DB.Exec(`UPDATE organizer_dav_resources SET name=?,uid=? WHERE item_uuid=?`, child.UUID+".ics", child.UUID, child.UUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
if err := s.store.EnsureTasksSchema(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
after, err := s.store.GetTask(ctx, domain, actor, child.UUID)
|
|
if err != nil || after.Version != child.Version || after.ProjectUUID != project.UUID {
|
|
t.Fatal(after, err)
|
|
}
|
|
if w := davTaskRequest(s, auth, "GET", href, "", nil); w.Code != 200 || w.Header().Get("ETag") != get.Header().Get("ETag") {
|
|
t.Fatal("upgrade changed task representation", w.Code, w.Body)
|
|
}
|
|
oldHref := taskDAVCollection + child.UUID + ".ics"
|
|
if w := davTaskRequest(s, auth, "GET", oldHref, "", nil); w.Code != 404 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
if w := davTaskRequest(s, auth, "PUT", oldHref, get.Body.String(), nil); w.Code != 409 || !strings.Contains(w.Body.String(), "task_collection_relocated") {
|
|
t.Fatal("stale client recreated relocated task", w.Code, w.Body)
|
|
}
|
|
// Native collections accept direct-client creates, with matching Web membership.
|
|
body := strings.Replace(taskICALFixture, "cli-task@example.test", "native-project-client@example.test", 1)
|
|
if w := davTaskRequest(s, auth, "PUT", col+"client.ics", body, nil); w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
created, err := s.store.TaskDAVByName(ctx, domain, actor, "client.ics", project.UUID)
|
|
if err != nil || created.ProjectUUID != project.UUID {
|
|
t.Fatal(created, err)
|
|
}
|
|
project.Title = "Renamed native project"
|
|
project = save(project)
|
|
if project.TaskDAVCollectionPath()+"/" != strings.TrimPrefix(col, calendarDAV) {
|
|
t.Fatal("rename changed URI")
|
|
}
|
|
// The same account/domain scoping applies to newly exposed native projects.
|
|
other, err := s.store.CreatePasswordAccount(domain, "native-other", "long fixture password", "Other", "")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
credential, password, err := s.store.CreateDAVCredential(ctx, domain, other.UUID, "other", true, 1, "tasks", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
otherAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(credential.ID+":"+password))
|
|
if w := davTaskRequest(s, otherAuth, "GET", href, "", nil); w.Code != 404 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
if w := davTaskRequest(s, otherAuth, "PROPFIND", calendarDAV, "", nil); w.Code != 207 || strings.Contains(w.Body.String(), col) {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
project.Archived = true
|
|
save(project)
|
|
if w := davTaskRequest(s, auth, "GET", href, "", nil); w.Code != 404 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
w = davTaskRequest(s, auth, "REPORT", taskDAVCollection, commonDAVQuery, nil)
|
|
if w.Code != 207 || strings.Contains(w.Body.String(), child.UUID) || strings.Contains(w.Body.String(), "client.ics") {
|
|
t.Fatal("archived project spilled into default", w.Code, w.Body)
|
|
}
|
|
}
|
|
|
|
func TestTaskDAVUnconditionalWritesRetainHistory(t *testing.T) {
|
|
s, _, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
// No token-specific compatibility setting; ordinary task credentials work.
|
|
c, pass, err := s.store.CreateDAVCredential(ctx, domain, actor, "direct CalDAV", true, 1, "tasks", false)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
auth := "Basic " + base64.StdEncoding.EncodeToString([]byte(c.ID+":"+pass))
|
|
p := taskDAVCollection + "direct.ics"
|
|
if w := davTaskRequest(s, auth, "PUT", p, taskICALFixture, nil); w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
item, err := s.store.TaskDAVByName(ctx, domain, actor, "direct.ics")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item.Bucket, item.PlannedOn, item.Estimate = "backlog", "2026-10-02", 45
|
|
item, err = s.store.SaveTask(ctx, domain, actor, item, item.Version)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var wg sync.WaitGroup
|
|
codes := make(chan int, 4)
|
|
for i := 0; i < 4; i++ {
|
|
body := strings.Replace(taskICALFixture, "SUMMARY:CLI task", fmt.Sprintf("SUMMARY:Direct update %d", i), 1)
|
|
wg.Add(1)
|
|
go func() { defer wg.Done(); codes <- davTaskRequest(s, auth, "PUT", p, body, nil).Code }()
|
|
}
|
|
wg.Wait()
|
|
close(codes)
|
|
for code := range codes {
|
|
if code != 204 {
|
|
t.Fatal("unconditional concurrent write", code)
|
|
}
|
|
}
|
|
after, err := s.store.GetTask(ctx, domain, actor, item.UUID)
|
|
if err != nil || after.Version != 6 || after.Estimate != 45 || after.PlannedOn != item.PlannedOn || after.Bucket != "backlog" {
|
|
t.Fatal(after, err)
|
|
}
|
|
history, err := s.store.TaskHistory(ctx, domain, actor, item.UUID)
|
|
if err != nil || len(history) != 6 {
|
|
t.Fatal(history, err)
|
|
}
|
|
if w := davTaskRequest(s, auth, "PUT", p, taskICALFixture, map[string]string{"If-Match": `"obsolete"`}); w.Code != 412 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
if w := davTaskRequest(s, auth, "DELETE", p, "", nil); w.Code != 204 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
if w := davTaskRequest(s, auth, "PUT", p, taskICALFixture, nil); w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
revived, err := s.store.TaskDAVByName(ctx, domain, actor, "direct.ics")
|
|
if err != nil || revived.UUID != item.UUID || revived.Version != 8 || revived.Archived {
|
|
t.Fatal(revived, err)
|
|
}
|
|
}
|
|
|
|
func TestTaskDAVUnconditionalConcurrentCreate(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
auth := "Bearer " + token
|
|
p := taskDAVCollection + "concurrent.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, nil).Code }()
|
|
}
|
|
wg.Wait()
|
|
close(codes)
|
|
counts := map[int]int{}
|
|
for code := range codes {
|
|
counts[code]++
|
|
}
|
|
if counts[201] != 1 || counts[204] != 1 {
|
|
t.Fatal(counts)
|
|
}
|
|
items, err := s.store.TaskDAVList(context.Background(), domain, actor)
|
|
if err != nil || len(items) != 1 || items[0].Version != 2 {
|
|
t.Fatal(items, err)
|
|
}
|
|
}
|
|
|
|
func TestTaskDAVIndexRebuildRollsBack(t *testing.T) {
|
|
s, token, domain, actor := taskFixture(t)
|
|
ctx := context.Background()
|
|
first, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("project", "First"), 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, err := s.store.SaveTask(ctx, domain, actor, newTaskRecord("project", "Second"), 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
item := newTaskRecord("task", "Keep routing")
|
|
item.ProjectUUID = first.UUID
|
|
item, err = s.store.SaveTask(ctx, domain, actor, item, 0)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Corrupt only this isolated fixture's index source to force a uniqueness
|
|
// failure after both DELETEs. Existing routing must survive the rollback.
|
|
if _, err = s.store.DB.Exec(`UPDATE organizer_items SET data=json_set(data,'$.dav_collection',?) WHERE uuid=?`, first.TaskDAVCollectionPath(), second.UUID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.store.EnsureTasksSchema(); err == nil {
|
|
t.Fatal("expected duplicate route rejection")
|
|
}
|
|
var count int
|
|
if err = s.store.DB.QueryRow(`SELECT count(*) FROM organizer_dav_collections`).Scan(&count); err != nil || count != 2 {
|
|
t.Fatal(count, err)
|
|
}
|
|
if w := davTaskRequest(s, "Bearer "+token, "GET", calendarDAV+first.TaskDAVCollectionPath()+"/"+item.UUID+".ics", "", nil); w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body)
|
|
}
|
|
}
|