245 lines
8.5 KiB
Go
245 lines
8.5 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func periodicalBackendFixture(t *testing.T, b storeSQLBackend) (*Store, string, string) {
|
|
t.Helper()
|
|
f := newPublicationBackend(t, b)
|
|
f.s.DocumentRoot = t.TempDir()
|
|
for i := 0; i < 2; i++ {
|
|
if e := f.s.EnsurePeriodicalSchema(); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
on := true
|
|
if e := f.s.ChangePeriodical(context.Background(), f.domain, f.actor, "blog", &on, nil, "/blog"); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
return f.s, f.domain, f.actor
|
|
}
|
|
func syntheticPost() PeriodicalPost {
|
|
return PeriodicalPost{Title: "日本語の記事", Date: "2026-11-15", Keyword: "日記", Body: "<h1>日本語の記事</h1><p>synthetic</p>", RequestID: uuid.NewString(), Timezone: "Asia/Tokyo"}
|
|
}
|
|
|
|
func TestPeriodicalsBackend(t *testing.T) {
|
|
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
|
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
|
s, d, a := periodicalBackendFixture(t, b)
|
|
ctx := context.Background()
|
|
p := syntheticPost()
|
|
if on, e := s.PeriodicalEnabled(ctx, d, "blog"); e != nil || !on {
|
|
t.Fatal(on, e)
|
|
}
|
|
if prefix, e := s.PeriodicalPrefix(ctx, d, "blog"); e != nil || prefix != "/blog" {
|
|
t.Fatal(prefix, e)
|
|
}
|
|
if e := s.CreatePeriodical(ctx, d, a, "blog", &p); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if p.Path != "/blog/2026/11/15/日記" || p.PageUUID == "" {
|
|
t.Fatal(p)
|
|
}
|
|
replay := p
|
|
if e := s.CreatePeriodical(ctx, d, a, "blog", &replay); e != nil || replay.PageUUID != p.PageUUID {
|
|
t.Fatal(replay, e)
|
|
}
|
|
changed := p
|
|
changed.Body += "<p>different</p>"
|
|
if e := s.CreatePeriodical(ctx, d, a, "blog", &changed); !errors.Is(e, ErrPeriodicalConflict) {
|
|
t.Fatal(e)
|
|
}
|
|
if r, e := s.GetLatestRevision(p.PageUUID); e != nil || r.Body != p.Body {
|
|
t.Fatal(r, e)
|
|
}
|
|
entries, e := s.ListPeriodicalEntries(ctx, d, "blog")
|
|
if e != nil || len(entries) != 1 {
|
|
t.Fatal(entries, e)
|
|
}
|
|
entry := entries[0]
|
|
stale := entry
|
|
entry.Title = "Changed"
|
|
if e = s.ChangePeriodical(ctx, d, a, "blog", nil, &entry); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e = s.ChangePeriodical(ctx, d, a, "blog", nil, &stale); !errors.Is(e, ErrPeriodicalConflict) {
|
|
t.Fatal(e)
|
|
}
|
|
entries, e = s.ListPeriodicalEntries(ctx, d, "blog")
|
|
if e != nil || entries[0].Version != 2 {
|
|
t.Fatal(entries, e)
|
|
}
|
|
entry = entries[0]
|
|
entry.Remove = true
|
|
if e = s.ChangePeriodical(ctx, d, a, "blog", nil, &entry); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if id, e := s.ResolvePathInDomain(d, p.Path); e != nil || id != p.PageUUID {
|
|
t.Fatal("unregister removed page", id, e)
|
|
}
|
|
entry.Remove = false
|
|
entry.Version = 0
|
|
if e = s.ChangePeriodical(ctx, d, a, "blog", nil, &entry); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
entries, e = s.ListPeriodicalEntries(ctx, d, "blog")
|
|
if e != nil || entries[0].Version != 4 {
|
|
t.Fatal(entries, e)
|
|
}
|
|
s.ReadOnly = true
|
|
off := false
|
|
if e = s.ChangePeriodical(ctx, d, a, "blog", &off, nil); !errors.Is(e, ErrReadOnly) {
|
|
t.Fatal(e)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPostgreSQLPeriodicalAtomicity(t *testing.T) {
|
|
s, d, a := periodicalBackendFixture(t, storePostgreSQL)
|
|
ctx := context.Background()
|
|
p := syntheticPost()
|
|
if _, e := s.DB.Exec(`ALTER TABLE periodical_events RENAME COLUMN operation TO unavailable_operation`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
if e := s.CreatePeriodical(ctx, d, a, "blog", &p); e == nil {
|
|
t.Fatal("fault ignored")
|
|
}
|
|
var n int
|
|
if e := s.DB.QueryRow(`SELECT count(*) FROM periodical_posts`).Scan(&n); e != nil || n != 0 {
|
|
t.Fatal(n, e)
|
|
}
|
|
if e := s.DB.QueryRow(`SELECT count(*) FROM paths WHERE domain_uuid=$1 AND path=$2`, d, p.Path).Scan(&n); e != nil || n != 0 {
|
|
t.Fatal(n, e)
|
|
}
|
|
if _, e := s.DB.Exec(`ALTER TABLE periodical_events RENAME COLUMN unavailable_operation TO operation`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
peer := &Store{DB: s.DB, sqlBackend: storePostgreSQL, DocumentRoot: s.DocumentRoot}
|
|
ch := make(chan error, 2)
|
|
for _, w := range []*Store{s, peer} {
|
|
go func(w *Store) { local := p; ch <- w.CreatePeriodical(ctx, d, a, "blog", &local) }(w)
|
|
}
|
|
for i := 0; i < 2; i++ {
|
|
if e := <-ch; e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
if e := s.DB.QueryRow(`SELECT count(*) FROM periodical_posts`).Scan(&n); e != nil || n != 1 {
|
|
t.Fatal(n, e)
|
|
}
|
|
if _, e := s.DB.Exec(`UPDATE campuses SET enabled=0`); e != nil {
|
|
t.Fatal(e)
|
|
}
|
|
other := syntheticPost()
|
|
other.Keyword = "denied"
|
|
if e := s.CreatePeriodical(ctx, d, a, "blog", &other); !errors.Is(e, ErrAccessDenied) {
|
|
t.Fatal(e)
|
|
}
|
|
}
|
|
|
|
func TestPeriodicalsBackendAssetsAndProjection(t *testing.T) {
|
|
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
|
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
|
s, domain, actor := periodicalBackendFixture(t, b)
|
|
ctx := context.Background()
|
|
if err := s.EnsureAssetsSchema(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
asset, _, err := s.PutAsset(domain, "asset:image:cover", []byte("synthetic cover"), "image/png", "cover.png", "", actor)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
p := syntheticPost()
|
|
p.Body = `<h1>Article</h1><img src="asset:image:cover">`
|
|
if err = s.CreatePeriodical(ctx, domain, actor, "blog", &p); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
rev, err := s.GetLatestRevision(p.PageUUID)
|
|
if err != nil || !strings.Contains(rev.Body, `src="/_/obj/sha256:`+asset.HeadDocumentHash+`"`) {
|
|
t.Fatal(rev, err)
|
|
}
|
|
if _, _, err = s.PutAsset(domain, asset.Address, []byte("updated cover"), asset.ContentType, asset.Filename, asset.HeadDocumentHash, actor); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
after, err := s.GetLatestRevision(p.PageUUID)
|
|
if err != nil || after.Body != rev.Body {
|
|
t.Fatal("asset HEAD changed historical article", after, err)
|
|
}
|
|
titles, err := s.PublicPeriodicalTitles(ctx, domain, "blog", []PublishedNavigationPage{
|
|
{Path: p.Path, Revision: *rev},
|
|
{Path: "/public-alias", Revision: *rev},
|
|
})
|
|
if err != nil || len(titles) != 2 || titles[p.Path] != p.Title || titles["/public-alias"] != p.Title {
|
|
t.Fatal(titles, err)
|
|
}
|
|
if titles, err = s.PublicPeriodicalTitles(ctx, uuid.NewString(), "blog", []PublishedNavigationPage{{Path: p.Path, Revision: *rev}}); err != nil || len(titles) != 0 {
|
|
t.Fatal("foreign domain title exposed", titles, err)
|
|
}
|
|
if _, err = s.PublicPeriodicalTitles(ctx, domain, "blog", make([]PublishedNavigationPage, MaxNavigationEntries+1)); !errors.Is(err, ErrNavigationBudget) {
|
|
t.Fatal("unbounded title query", err)
|
|
}
|
|
deps, err := s.ListAssetDependencies(domain, asset.Address)
|
|
if err != nil || len(deps) != 1 || deps[0].Path != p.Path {
|
|
t.Fatal(deps, err)
|
|
}
|
|
bad := syntheticPost()
|
|
bad.Keyword = "missing"
|
|
bad.Body = `<img src="asset:image:missing">`
|
|
if err = s.CreatePeriodical(ctx, domain, actor, "blog", &bad); !errors.Is(err, ErrEmbeddedAssetUnavailable) {
|
|
t.Fatal(err)
|
|
}
|
|
var count int
|
|
if err = s.DB.QueryRow(`SELECT count(*) FROM periodical_posts`).Scan(&count); err != nil || count != 1 {
|
|
t.Fatal(count, err)
|
|
}
|
|
d, err := s.GetDomainByUUID(domain)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
target := seedNamespaceDomain(t, s, d.CampusUUID, "projection", "projection.example.test")
|
|
if err = s.SetPeriodicalProjection(ctx, target.ID, actor, "blog", domain, "/blog"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
projection, err := s.PeriodicalProjection(ctx, target.UUID, "blog")
|
|
if err != nil || projection == nil || projection.Source != domain || projection.Prefix != "/blog" {
|
|
t.Fatal(projection, err)
|
|
}
|
|
all, err := s.ListPeriodicalProjections()
|
|
if err != nil || len(all) != 1 || all[target.UUID+"|blog"].Source != domain {
|
|
t.Fatal(all, err)
|
|
}
|
|
if err = s.SetPeriodicalProjection(ctx, d.ID, actor, "blog", target.UUID, "/blog"); !errors.Is(err, ErrPeriodicalInvalid) {
|
|
t.Fatal("cycle accepted", err)
|
|
}
|
|
if _, err = s.DB.Exec(`UPDATE domains SET enabled=0 WHERE uuid=`+s.sqlParam(1), domain); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.PeriodicalProjection(ctx, target.UUID, "blog"); !errors.Is(err, ErrAccessDenied) {
|
|
t.Fatal("disabled source exposed", err)
|
|
}
|
|
if _, err = s.DB.Exec(`UPDATE domains SET enabled=1 WHERE uuid=`+s.sqlParam(1), domain); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
s.ReadOnly = true
|
|
if err = s.SetPeriodicalProjection(ctx, target.ID, actor, "blog", "", ""); !errors.Is(err, ErrReadOnly) {
|
|
t.Fatal(err)
|
|
}
|
|
s.ReadOnly = false
|
|
if err = s.SetPeriodicalProjection(ctx, target.ID, actor, "blog", "", ""); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if projection, err = s.PeriodicalProjection(ctx, target.UUID, "blog"); err != nil || projection != nil {
|
|
t.Fatal(projection, err)
|
|
}
|
|
})
|
|
}
|
|
}
|