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.
236 lines
9.3 KiB
Go
236 lines
9.3 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"os/exec"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"pageswiki/internal/calendar"
|
|
)
|
|
|
|
func TestCalendarChromiumSmoke(t *testing.T) {
|
|
if os.Getenv("PAGESWIKI_BROWSER_TEST") != "1" {
|
|
t.Skip("opt-in Chromium")
|
|
}
|
|
s, token, domain := configFixture(t)
|
|
if err := s.store.SetConfigInDomain(domain, "timezone", "Asia/Tokyo"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.registerCalendarRoutes()
|
|
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Calendar Smoke"}`, "")
|
|
if w.Code != 201 {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
var c calendar.Calendar
|
|
json.Unmarshal(w.Body.Bytes(), &c)
|
|
w = calendarRequest(s, token, "POST", calendarAPI+"calendars/"+c.ID+"/events", `{"summary":"Browser meeting","start":"2026-09-22T09:00:00Z","end":"2026-09-22T10:00:00Z"}`, "")
|
|
if w.Code != 201 {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
var event calendar.Event
|
|
json.Unmarshal(w.Body.Bytes(), &event)
|
|
// Exercise the Web viewer with a DAV-client filename, not base64url(UID).
|
|
native, err := s.calendarRepo.Event(context.Background(), domain, c.ID, c.Owner, event.UID)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.calendarRepo.Change(context.Background(), domain, c.ID, c.Owner, event.UID, native.ETag, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, _, err = s.calendarRepo.ChangeDAV(context.Background(), domain, c.ID, c.Owner, "browser-client-event.ics", "", &native, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
r.Host = "localhost"
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
}))
|
|
defer server.Close()
|
|
for _, route := range []string{"/v/plan/cal/2026/09", "/v/plan/cal/2026/09/22/week", "/v/plan/cal/2026/09/22/timeline", "/v/plan/event/" + event.UID} {
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
|
cmd := exec.CommandContext(ctx, "chromium", "--headless", "--no-sandbox", "--disable-gpu", "--disable-background-networking", "--no-proxy-server", "--enable-logging=stderr", "--log-level=0", "--user-data-dir="+t.TempDir(), "--dump-dom", "--virtual-time-budget=4000", server.URL+route+"?calendar="+c.ID)
|
|
out, e := cmd.CombinedOutput()
|
|
cancel()
|
|
if e != nil || strings.Contains(string(out), "Uncaught ") || !strings.Contains(string(out), "Browser meeting") {
|
|
t.Fatalf("%s: %v %s", route, e, out)
|
|
}
|
|
}
|
|
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
|
|
defer cancel()
|
|
cmd := exec.CommandContext(ctx, "node", "internal/server/testdata/calendar-browser-smoke.mjs", server.URL+"/v/plan/cal/2026/09?calendar="+c.ID, t.TempDir())
|
|
if out, e := cmd.CombinedOutput(); e != nil {
|
|
t.Fatalf("browser interactions: %v %s", e, out)
|
|
} else {
|
|
t.Log(string(out))
|
|
}
|
|
}
|
|
|
|
func calendarRequest(s *Server, token, method, path, body, etag string) *httptest.ResponseRecorder {
|
|
r := httptest.NewRequest(method, "http://localhost"+path, strings.NewReader(body))
|
|
if token != "" {
|
|
r.Header.Set("Authorization", "Bearer "+token)
|
|
}
|
|
r.Header.Set("Content-Type", "application/json")
|
|
if method == "REPORT" || method == "PROPFIND" {
|
|
r.Header.Set("Content-Type", "application/xml")
|
|
r.Header.Set("Depth", "1")
|
|
}
|
|
if method == "PUT" && !strings.Contains(path, "settings") {
|
|
r.Header.Set("Content-Type", "text/calendar")
|
|
}
|
|
if etag == "*" {
|
|
r.Header.Set("If-None-Match", "*")
|
|
} else if etag != "" {
|
|
r.Header.Set("If-Match", etag)
|
|
}
|
|
w := httptest.NewRecorder()
|
|
s.withAuthorization(s.mux).ServeHTTP(w, r)
|
|
return w
|
|
}
|
|
func TestCalendarEndToEndDAVAndPublicRevocation(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
s.registerCalendarRoutes()
|
|
if _, err := s.store.DB.Exec(`INSERT INTO locality_publication_policies(domain_uuid,node_kind,node_uuid,policy,actor_uuid,updated_at_ms) VALUES(?,'domain',?,'public','test',0)`, domain, domain); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Calendar"}`, "")
|
|
if w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
var c calendar.Calendar
|
|
if e := json.Unmarshal(w.Body.Bytes(), &c); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
path := calendarAPI + "calendars/" + c.ID + "/events"
|
|
w = calendarRequest(s, token, "POST", path, `{"summary":"Meeting","start":"2026-09-30T23:00:00Z","end":"2026-10-01T02:00:00Z"}`, "")
|
|
if w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
var event calendar.Event
|
|
json.Unmarshal(w.Body.Bytes(), &event)
|
|
href := w.Header().Get("Location")
|
|
report := `<c:calendar-query xmlns:c="urn:ietf:params:xml:ns:caldav" xmlns:d="DAV:"><d:prop><d:getetag/><c:calendar-data/></d:prop><c:filter><c:comp-filter name="VCALENDAR"><c:comp-filter name="VEVENT"><c:time-range start="20261001T000000Z" end="20261002T000000Z"/></c:comp-filter></c:comp-filter></c:filter></c:calendar-query>`
|
|
w = calendarRequest(s, token, "REPORT", calendarDAV+c.ID+"/", report, "")
|
|
if w.Code != 207 || !strings.Contains(w.Body.String(), "Meeting") {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
w = calendarRequest(s, "", "GET", href, "", "")
|
|
if w.Code != 404 {
|
|
t.Fatal("private leaked", w.Code)
|
|
}
|
|
w = calendarRequest(s, token, "GET", href, "", "")
|
|
ics := w.Body.String()
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Code, ics)
|
|
}
|
|
w = calendarRequest(s, token, "PUT", href, ics, "")
|
|
if w.Code != 428 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
w = calendarRequest(s, token, "PUT", href, ics, "*")
|
|
if w.Code != 412 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
c, _ = s.calendarRepo.Get(s.domainUUIDForRequest(httptest.NewRequest("GET", "http://localhost/", nil)), c.ID, c.Owner)
|
|
settings := calendarAPI + "calendars/" + c.ID + "/settings"
|
|
body, _ := json.Marshal(map[string]any{"name": c.Name, "public": true, "members": map[string]string{}, "version": c.Version})
|
|
w = calendarRequest(s, token, "PUT", settings, string(body), "")
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
json.Unmarshal(w.Body.Bytes(), &c)
|
|
w = calendarRequest(s, "", "REPORT", calendarDAV+c.ID+"/", report, "")
|
|
if w.Code != 207 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
w = calendarRequest(s, "", "DELETE", href, "", event.ETag)
|
|
if w.Code != 403 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
body, _ = json.Marshal(map[string]any{"name": c.Name, "public": false, "members": map[string]string{}, "version": c.Version})
|
|
w = calendarRequest(s, token, "PUT", settings, string(body), "")
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
w = calendarRequest(s, "", "GET", href, "", "")
|
|
if w.Code != 404 {
|
|
t.Fatal("revocation failed", w.Code)
|
|
}
|
|
// Calendar-level publication cannot override a newly restricted Domain.
|
|
body, _ = json.Marshal(map[string]any{"name": c.Name, "public": true, "members": map[string]string{}, "version": func() string { fresh, _ := s.calendarRepo.Get(domain, c.ID, c.Owner); return fresh.Version }()})
|
|
w = calendarRequest(s, token, "PUT", settings, string(body), "")
|
|
if w.Code != 200 {
|
|
t.Fatal(w.Body.String())
|
|
}
|
|
if _, e := s.store.DB.Exec(`UPDATE locality_publication_policies SET policy='authenticated_acl' WHERE domain_uuid=? AND node_kind='domain'`, domain); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
w = calendarRequest(s, "", "GET", href, "", "")
|
|
if w.Code != 404 {
|
|
t.Fatal("Domain ceiling bypass", w.Code)
|
|
}
|
|
// A disabled extension closes both protocols, even for its owner.
|
|
if e := s.setSystemExtensionEnabled(domain, "organizer.calendar", false); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
w = calendarRequest(s, token, "GET", href, "", "")
|
|
if w.Code != 404 {
|
|
t.Fatal(w.Code)
|
|
}
|
|
if e := s.setSystemExtensionEnabled(domain, "organizer.calendar", true); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
for _, p := range []string{"/v/plan", "/v/plan/cal/2026", "/v/plan/cal/2026/09", "/v/plan/cal/2026/09/22/week", "/v/plan/cal/2026/09/22/timeline"} {
|
|
w = calendarRequest(s, token, "GET", p, "", "")
|
|
if w.Code != 200 || !strings.Contains(w.Body.String(), "calendar-app") {
|
|
t.Fatal(p, w.Code, w.Body.String())
|
|
}
|
|
}
|
|
for _, p := range []string{"/v/plan/cal/2026/02/30", "/v/plan/cal/2026/13", "/v/plan/bogus"} {
|
|
w = calendarRequest(s, token, "GET", p, "", "")
|
|
if w.Code != 404 {
|
|
t.Fatal(p, w.Code)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestCalendarDeleteAPIUsesVersionAndCancelsQueuedSync(t *testing.T) {
|
|
s, token, domain := configFixture(t)
|
|
s.registerCalendarRoutes()
|
|
w := calendarRequest(s, token, "POST", calendarAPI+"calendars", `{"name":"Disposable","pretty_name":"disposable"}`, "")
|
|
if w.Code != 201 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
var c calendar.Calendar
|
|
if err := json.Unmarshal(w.Body.Bytes(), &c); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err := s.store.EnqueueEventWithOptions("", calendarSyncType, domain+":"+c.ID, `{}`, time.Now().Add(time.Hour), -10, 1); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
settings := calendarAPI + "calendars/" + c.ID + "/settings"
|
|
w = calendarRequest(s, token, "DELETE", settings, `{"version":"`+uuid.NewString()+`"}`, "")
|
|
if w.Code != 412 {
|
|
t.Fatal("stale delete", w.Code, w.Body.String())
|
|
}
|
|
w = calendarRequest(s, token, "DELETE", settings, `{"version":"`+c.Version+`"}`, "")
|
|
if w.Code != 204 || w.Body.Len() != 0 {
|
|
t.Fatal(w.Code, w.Body.String())
|
|
}
|
|
if _, err := s.calendarRepo.Get(domain, c.ID, c.Owner); !errors.Is(err, calendar.ErrMissing) {
|
|
t.Fatal(err)
|
|
}
|
|
var status string
|
|
if err := s.store.DB.QueryRow(`SELECT status FROM event_jobs WHERE event_type=? AND event_key=? ORDER BY created_at_ms DESC LIMIT 1`, calendarSyncType, domain+":"+c.ID).Scan(&status); err != nil || status != "cancelled" {
|
|
t.Fatal(status, err)
|
|
}
|
|
}
|