package server import ( "context" "database/sql" "encoding/json" "errors" "net/http/httptest" "strings" "testing" "pageswiki/internal/store" ) func periodicalFixture(t *testing.T) (*Server, string, string, string) { t.Helper() s, token, domain := configFixture(t) s.mux.HandleFunc("/v/diary", s.handlePeriodical) s.mux.HandleFunc("/v/diary/", s.handlePeriodical) s.mux.HandleFunc("/v/blog", s.handlePeriodical) s.mux.HandleFunc("/v/blog/", s.handlePeriodical) s.mux.HandleFunc("/_/api/v1/extension/", s.handleSystemExtensions) p, err := s.store.PrincipalForSession(domain, token) if err != nil { t.Fatal(err) } return s, token, domain, p.UUID } func periodicalRequest(s *Server, token, method, target, body string) *httptest.ResponseRecorder { r := httptest.NewRequest(method, "http://localhost"+target, strings.NewReader(body)) if token != "" { r.Header.Set("Authorization", "Bearer "+token) } r.Header.Set("Content-Type", "application/json") r.Header.Set("Accept", "application/json") w := httptest.NewRecorder() s.withAuthorization(s.mux).ServeHTTP(w, r) return w } func TestPeriodicalPrefixPlannerAndHome(t *testing.T) { s, token, domain, actor := periodicalFixture(t) ctx := context.Background() s.mux.HandleFunc("/", s.handleHome) base := "/_/api/v1/extension/content.diary/" for _, test := range []struct { method, target, body string status int }{ {"GET", base + "settings", "", 200}, {"GET", base + "plan?date=2026-09-17", "", 409}, {"POST", base + "settings", `{"enabled":true,"prefix":"/journal"}`, 200}, {"POST", base + "settings", `{"enabled":false,"prefix":"/../invalid"}`, 400}, {"GET", base + "plan?date=2026-09-17&keyword=walk", "", 200}, {"GET", base + "plan?date=2026-09-17&keyword=..", "", 400}, {"POST", base + "plan", `{}`, 403}, } { w := periodicalRequest(s, token, test.method, test.target, test.body) if w.Code != test.status { t.Fatalf("%s %s: %d %s", test.method, test.target, w.Code, w.Body.String()) } } prefix, err := s.store.PeriodicalPrefix(ctx, domain, "diary") if err != nil || prefix != "/journal" { t.Fatal(prefix, err) } w := periodicalRequest(s, token, "GET", base+"plan?date=2026-09-17&keyword=walk", "") if !strings.Contains(w.Body.String(), `"path":"/journal/2026/09/17/walk"`) { t.Fatal(w.Body.String()) } if _, err = s.store.ResolvePathInDomain(domain, "/journal/2026/09/17/walk"); !errors.Is(err, sql.ErrNoRows) { t.Fatal("planning created a Page") } for _, p := range []string{"/journal/2026/09/17", "/journal/2026/09/17/walk"} { u, err := s.store.CreatePageAtPathInDomain(domain, p) if err != nil { t.Fatal(err) } if _, err = s.store.SaveRevision(u, "

Body

", ""); err != nil { t.Fatal(err) } entry := store.PeriodicalEntry{Path: p, Title: p, Date: "2026-09-17", Timezone: "UTC"} if err = s.store.ChangePeriodical(ctx, domain, actor, "diary", nil, &entry); err != nil { t.Fatal(err) } } if w = periodicalRequest(s, token, "GET", base+"plan?date=2026-09-17", ""); w.Code != 409 { t.Fatal(w.Code) } if err = s.store.SetConfigInDomain(domain, "home_layout", "diary"); err != nil { t.Fatal(err) } w = periodicalRequest(s, token, "GET", "/", "") if w.Code != 200 || !strings.Contains(w.Body.String(), `href="/v/diary/`) { t.Fatal(w.Code, w.Body.String()) } if w = periodicalRequest(s, "", "GET", base+"plan?date=2026-09-17", ""); w.Code != 401 { t.Fatal(w.Code) } if w = periodicalRequest(s, "", "GET", "/", ""); w.Code == 200 { t.Fatal("private root leaked") } if w = periodicalRequest(s, token, "POST", base+"settings", `{"enabled":true,"prefix":"/"}`); w.Code != 200 { t.Fatal(w.Code) } if _, err = s.store.ResolvePathInDomain(domain, "/journal/2026/09/17/walk"); err != nil { t.Fatal("prefix changed old Path", err) } if w = periodicalRequest(s, token, "GET", base+"plan?date=2026-09-17&keyword=new", ""); w.Code != 200 || !strings.Contains(w.Body.String(), `"path":"/2026/09/17/new"`) { t.Fatal(w.Code, w.Body.String()) } } func TestPeriodicalLifecycleAndRename(t *testing.T) { s, token, domain, actor := periodicalFixture(t) ctx := context.Background() for _, kind := range []string{"diary", "blog"} { if enabled, err := s.store.PeriodicalEnabled(ctx, domain, kind); err != nil || enabled { t.Fatal("new plugin must be disabled", err) } w := periodicalRequest(s, token, "POST", "/_/api/v1/extension/content."+kind+"/settings", `{"enabled":true}`) if w.Code != 200 { t.Fatal(w.Code, w.Body.String()) } } page, err := s.store.CreatePageAtPathInDomain(domain, "/notes/first") if err != nil { t.Fatal(err) } if _, err = s.store.SaveRevision(page, "

Canonical unchanged

", ""); err != nil { t.Fatal(err) } entry := store.PeriodicalEntry{Path: "/notes/first", Title: "Diary title", Date: "2026-09-16", Category: "Test", Timezone: "Asia/Tokyo", Owner: "forged"} if err = s.store.ChangePeriodical(ctx, domain, actor, "diary", nil, &entry); err != nil { t.Fatal(err) } list, err := s.store.ListPeriodicalEntries(ctx, domain, "diary") if err != nil || len(list) != 1 || list[0].Owner != actor { t.Fatal("server owner", list, err) } entry = list[0] if err = s.store.RenamePathInDomain(domain, "/notes/first", "/notes/renamed"); err != nil { t.Fatal(err) } w := periodicalRequest(s, token, "GET", "/v/diary/2026/09", "") if w.Code != 200 || !strings.Contains(w.Body.String(), `href="/notes/renamed"`) || strings.Contains(w.Body.String(), "Diary title") || !strings.Contains(w.Body.String(), "has-entries") || !strings.Contains(w.Header().Get("Content-Security-Policy"), "nonce-") { t.Fatalf("calendar %d %s", w.Code, w.Body.String()) } entry.Path = "/notes/renamed" entry.Title = "Updated" entry.Timezone = "UTC" if err = s.store.ChangePeriodical(ctx, domain, actor, "diary", nil, &entry); err != nil { t.Fatal(err) } if rows, err := s.store.ListPeriodicalEntries(ctx, domain, "diary"); err != nil || rows[0].Timezone != "Asia/Tokyo" { t.Fatal("editing changed original timezone", err) } if err = s.store.ChangePeriodical(ctx, domain, actor, "diary", nil, &entry); !errors.Is(err, store.ErrPeriodicalConflict) { t.Fatal("stale update", err) } entry.Version = 2 entry.Remove = true if err = s.store.ChangePeriodical(ctx, domain, actor, "diary", nil, &entry); err != nil { t.Fatal(err) } if object, err := s.store.ResolvePathInDomain(domain, entry.Path); err != nil || object != page { t.Fatal("unregister deleted Page", err) } entry.Remove = false entry.Version = 0 if err = s.store.ChangePeriodical(ctx, domain, actor, "diary", nil, &entry); err != nil { t.Fatal(err) } list, _ = s.store.ListPeriodicalEntries(ctx, domain, "diary") if list[0].Version != 4 { t.Fatal("ABA generation reset", list) } entry.Version = 1 if err = s.store.ChangePeriodical(ctx, domain, actor, "diary", nil, &entry); !errors.Is(err, store.ErrPeriodicalConflict) { t.Fatal("pre-delete request replay", err) } off := false if err = s.store.ChangePeriodical(ctx, domain, actor, "diary", &off, nil); err != nil { t.Fatal(err) } w = periodicalRequest(s, token, "GET", "/_/api/v1/extension/content.diary/entries", "") if w.Code != 200 || strings.Contains(w.Body.String(), "Updated") { t.Fatal("disabled index exposed", w.Body.String()) } if list, _ = s.store.ListPeriodicalEntries(ctx, domain, "diary"); len(list) != 1 { t.Fatal("disable deleted metadata") } } func TestPeriodicalPermissionsAndConstraints(t *testing.T) { s, token, domain, admin := periodicalFixture(t) ctx := context.Background() on := true if err := s.store.ChangePeriodical(ctx, domain, admin, "diary", &on, nil); err != nil { t.Fatal(err) } user, err := s.store.CreatePasswordAccount(domain, "writer", "a very long password", "Writer", "") if err != nil { t.Fatal(err) } userToken, err := s.store.CreateAuthSession(domain, user.UUID) if err != nil { t.Fatal(err) } if _, err = s.store.DB.Exec(`DELETE FROM auth_role_bindings WHERE membership_uuid IN (SELECT uuid FROM domain_memberships WHERE principal_uuid=?)`, user.UUID); err != nil { t.Fatal(err) } group, err := s.store.CreateGroup(domain, admin, "journalers", "Journalers", "", "members") if err != nil { t.Fatal(err) } if err = s.store.AddGroupMemberByUsername(domain, group.UUID, "writer", "member"); err != nil { t.Fatal(err) } if err = s.store.SetGroupPermission(group.UUID, "/journal", "read_write"); err != nil { t.Fatal(err) } for _, p := range []string{"/journal/one", "/journal/two", "/private/three"} { o, e := s.store.CreatePageAtPathInDomain(domain, p) if e != nil { t.Fatal(e) } if _, e = s.store.SaveRevision(o, "Body", ""); e != nil { t.Fatal(e) } } entry := store.PeriodicalEntry{Path: "/journal/one", Title: "VISIBLE-ENTRY", Date: "2024-02-29", Timezone: "UTC", Category: "VISIBLE-CATEGORY"} if err = s.store.ChangePeriodical(ctx, domain, user.UUID, "diary", nil, &entry); err != nil { t.Fatal(err) } entry.Path = "/journal/two" if err = s.store.ChangePeriodical(ctx, domain, user.UUID, "diary", nil, &entry); err != nil { t.Fatal("multiple diary articles per day", err) } entry.Path = "/journal/one" entry.PageUUID, _ = s.store.ResolvePathInDomain(domain, entry.Path) entry.Version = 1 entry.Date = "2023-02-29" if err = s.store.ChangePeriodical(ctx, domain, user.UUID, "diary", nil, &entry); !errors.Is(err, store.ErrPeriodicalInvalid) { t.Fatal("invalid date", err) } entry.Date = "2024-03-01" entry.PageUUID = "" entry.Version = 0 entry.Path = "/private/three" if err = s.store.ChangePeriodical(ctx, domain, user.UUID, "diary", nil, &entry); !errors.Is(err, store.ErrAccessDenied) { t.Fatal("write boundary", err) } entry.Title = "PRIVATE-ENTRY" entry.Category = "PRIVATE-CATEGORY" if err = s.store.ChangePeriodical(ctx, domain, admin, "diary", nil, &entry); err != nil { t.Fatal(err) } w := periodicalRequest(s, userToken, "GET", "/v/diary/2024/02", "") if w.Code != 200 || !strings.Contains(w.Body.String(), "VISIBLE-ENTRY") || strings.Contains(w.Body.String(), "PRIVATE-") { t.Fatal("permission-filtered widgets", w.Code, w.Body.String()) } if err = s.store.ChangePeriodical(ctx, domain, user.UUID, "diary", &on, nil); !errors.Is(err, store.ErrAccessDenied) { t.Fatal("nonadmin enabled", err) } if err = s.store.ChangePeriodical(ctx, "other-domain", user.UUID, "diary", &on, nil); !errors.Is(err, store.ErrAccessDenied) { t.Fatal("domain crossing", err) } if _, err = s.store.CreatePageAtPathInDomain(domain, "/u/writer/diary/day"); err != nil { t.Fatal(err) } own := store.PeriodicalEntry{Path: "/u/writer/diary/day", Title: "Personal diary", Date: "2024-03-02", Timezone: "UTC"} if err = s.store.ChangePeriodical(ctx, domain, user.UUID, "diary", nil, &own); err != nil { t.Fatal("own Wiki authority", err) } if _, err = s.store.DB.Exec(`DELETE FROM group_members WHERE group_uuid=?`, group.UUID); err != nil { t.Fatal(err) } w = periodicalRequest(s, userToken, "GET", "/v/diary/2024/02", "") if w.Code != 200 || strings.Contains(w.Body.String(), "VISIBLE-") || strings.Contains(w.Body.String(), "PRIVATE-") { t.Fatal("membership revocation", w.Code, w.Body.String()) } for _, target := range []string{"/v/diary", "/v/blog", "/_/api/v1/extension/content.diary/entries"} { w = periodicalRequest(s, "", "GET", target, "") if w.Code == 200 { t.Fatal("anonymous metadata exposed", target) } } for _, body := range []string{`{"enabled":true,"extra":1}`, `{"enabled":true} {}`, `{}`} { w = periodicalRequest(s, token, "POST", "/_/api/v1/extension/content.diary/settings", body) if w.Code != 400 { t.Fatal("strict JSON", w.Code) } } w = periodicalRequest(s, token, "DELETE", "/_/api/v1/extension/content.diary/entries", "") if w.Code < 400 { t.Fatal("undefined method") } } func TestPeriodicalAuditRollbackAndAPI(t *testing.T) { s, token, domain, actor := periodicalFixture(t) ctx := context.Background() on := true if err := s.store.ChangePeriodical(ctx, domain, actor, "blog", &on, nil); err != nil { t.Fatal(err) } page, err := s.store.CreatePageAtPathInDomain(domain, "/post") if err != nil { t.Fatal(err) } if _, err = s.store.DB.Exec(`CREATE TRIGGER fail_periodical_event BEFORE INSERT ON periodical_events BEGIN SELECT RAISE(ABORT,'fixture'); END`); err != nil { t.Fatal(err) } body := `{"path":"/post","title":"Blog article","date":"2026-09-16","category":"News","version":0}` w := periodicalRequest(s, token, "POST", "/_/api/v1/extension/content.blog/entries", body) if w.Code != 500 { t.Fatal(w.Code) } list, err := s.store.ListPeriodicalEntries(ctx, domain, "blog") if err != nil || len(list) != 0 { t.Fatal("journal failure did not roll back", list, err) } if _, err = s.store.DB.Exec(`DROP TRIGGER fail_periodical_event`); err != nil { t.Fatal(err) } w = periodicalRequest(s, token, "POST", "/_/api/v1/extension/content.blog/entries", body) if w.Code != 200 { t.Fatal(w.Code, w.Body.String()) } w = periodicalRequest(s, token, "GET", "/_/api/v1/extension/content.blog/entries", "") var result struct{ Entries []store.PeriodicalEntry } if err = json.Unmarshal(w.Body.Bytes(), &result); err != nil || len(result.Entries) != 1 || result.Entries[0].PageUUID != page { t.Fatal("API entry", w.Body.String(), err) } if w = periodicalRequest(s, token, "GET", "/v/blog?category=News", ""); w.Code != 200 || !strings.Contains(w.Body.String(), "Blog article") { t.Fatal("blog view", w.Code, w.Body.String()) } }