202 lines
7.3 KiB
Go
202 lines
7.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"testing"
|
|
)
|
|
|
|
func TestPathBackendMutations(t *testing.T) {
|
|
for _, b := range []storeSQLBackend{storeSQLite, storePostgreSQL} {
|
|
t.Run(fmt.Sprint(b), func(t *testing.T) {
|
|
f := newPublicationBackend(t, b)
|
|
s := f.s
|
|
s.DocumentRoot = t.TempDir()
|
|
if err := s.EnsureBrowserNavigationSchema(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := s.EnsureUserSpaceSchema(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
obj, err := s.CreatePageAtPathInDomain(f.domain, "/news_%/st/sp/a.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v, err := s.SaveRevisionContent(obj, []byte("text"), "original", "en", "text/plain", "a.txt")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.AddBrowserNavigationLink(f.domain, f.actor, "original", "/news_%/st/sp", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.AddBrowserNavigationLink(f.domain, f.actor, "destination", "/moved/st/sp", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
other, err := s.CreatePageAtPathInDomain(f.domain, "/newsAX/st/sp/not-selected")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
snap, err := s.SnapshotPathsInDomain(f.domain, "/news_%", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, err := s.RenamePathsInDomainChecked(f.domain, "/news_%", "/moved", true, snap); err != nil || n != 1 {
|
|
t.Fatal(n, err)
|
|
}
|
|
id, err := s.ResolvePathInDomain(f.domain, "/moved/st/sp/a.txt")
|
|
if err != nil || id != obj {
|
|
t.Fatal(id, err)
|
|
}
|
|
id, err = s.ResolvePathInDomain(f.domain, "/newsAX/st/sp/not-selected")
|
|
if err != nil || id != other {
|
|
t.Fatal("wildcard changed sibling", id, err)
|
|
}
|
|
var boundary string
|
|
if err = s.DB.QueryRow(`SELECT path_prefix FROM spots WHERE uuid='sp'`).Scan(&boundary); err != nil || boundary != "/moved/st/sp" {
|
|
t.Fatal(boundary, err)
|
|
}
|
|
links, err := s.ListBrowserNavigationLinks(f.domain, f.actor)
|
|
if err != nil || len(links) != 1 || links[0].Label != "destination" {
|
|
t.Fatal(links, err)
|
|
}
|
|
if _, err = s.RemovePathsInDomainChecked(f.domain, []*PathMutationSnapshot{snap}); !errors.Is(err, ErrPathSnapshotChanged) {
|
|
t.Fatal("stale delete", err)
|
|
}
|
|
if n, err := s.RenamePathsInDomain(f.domain, "/moved/st/sp/a.txt", "/moved/st/sp/b.txt", false); err != nil || n != 1 {
|
|
t.Fatal(n, err)
|
|
}
|
|
head, err := s.GetRevisionForExactLanguage(obj, "en")
|
|
if err != nil || head.UUID == v.UUID || head.Filename != "b.txt" || head.Body != "text" {
|
|
t.Fatal(head, err)
|
|
}
|
|
if err = s.AddPathLinkInDomain(f.domain, obj, "/alias"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.SetPrimaryPathLinkInDomain(f.domain, obj, "/alias"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err = s.SetPrimaryPathLinkInDomain(f.domain, obj, "/missing"); !errors.Is(err, ErrPathNotFound) {
|
|
t.Fatal(err)
|
|
}
|
|
pl, err := s.ListPathLinksInDomain(f.domain, obj)
|
|
if err != nil || len(pl) != 2 || pl[0].Path != "/alias" || !pl[0].IsPrimary {
|
|
t.Fatal(pl, err)
|
|
}
|
|
if err = s.DeletePathLinkInDomain(f.domain, obj, "/alias"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, err := s.CopyPathsInDomain(f.domain, "/moved", "/copy", true); err != nil || n != 1 {
|
|
t.Fatal(n, err)
|
|
}
|
|
copyID, err := s.ResolvePathInDomain(f.domain, "/copy/st/sp/b.txt")
|
|
if err != nil || copyID == obj {
|
|
t.Fatal(copyID, err)
|
|
}
|
|
cv, err := s.GetRevisionForExactLanguage(copyID, "en")
|
|
if err != nil || cv.Body != "text" || cv.UUID == head.UUID {
|
|
t.Fatal(cv, err)
|
|
}
|
|
snap, err = s.SnapshotPathsInDomain(f.domain, "/moved", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if n, err := s.RemovePathsInDomainChecked(f.domain, []*PathMutationSnapshot{snap, snap}); err != nil || n != 1 {
|
|
t.Fatal(n, err)
|
|
}
|
|
if _, err = s.GetRevision(v.UUID); err != nil {
|
|
t.Fatal("unlink destroyed history", err)
|
|
}
|
|
// A destination collision must leave the whole source subtree intact.
|
|
if _, err = s.CreatePageAtPathInDomain(f.domain, "/collision/st/sp/b.txt"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.RenamePathsInDomain(f.domain, "/copy", "/collision", true); !errors.Is(err, ErrPathConflict) {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.ResolvePathInDomain(f.domain, "/copy/st/sp/b.txt"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
// Outbox failure rolls back the Path changes and cloned filename revision.
|
|
f.exec(`ALTER TABLE activity_outbox RENAME COLUMN payload TO unavailable_payload`)
|
|
if _, err = s.RenamePathsInDomain(f.domain, "/copy/st/sp/b.txt", "/copy/st/sp/c.txt", false); err == nil {
|
|
t.Fatal("ignored outbox failure")
|
|
}
|
|
if _, err = s.ResolvePathInDomain(f.domain, "/copy/st/sp/b.txt"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
after, err := s.GetRevisionForExactLanguage(copyID, "en")
|
|
if err != nil || after.UUID != cv.UUID {
|
|
t.Fatal("partial filename revision", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestPostgreSQLPathProtectionAndReferences(t *testing.T) {
|
|
f := newPublicationBackend(t, storePostgreSQL)
|
|
s := f.s
|
|
s.DocumentRoot = t.TempDir()
|
|
for _, ensure := range []func() error{s.EnsureBrowserNavigationSchema, s.EnsureUserSpaceSchema} {
|
|
if err := ensure(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
obj, err := s.CreatePageAtPathInDomain(f.domain, "/files/a.html")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
v, err := s.SaveRevisionContent(obj, []byte("body"), "", "en", "text/html", "a.html")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
hash := revisionHash("embedded")
|
|
if err = s.persistDocument(hash, "embedded"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.exec(`INSERT INTO revision_document_refs(revision_uuid,document_hash) VALUES($1,$2)`, v.UUID, hash)
|
|
if _, err = s.RenamePathsInDomain(f.domain, "/files/a.html", "/files/b.html", false); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.CopyPathsInDomain(f.domain, "/files", "/copy", true); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var count int
|
|
if err = s.DB.QueryRow(`SELECT count(*) FROM revision_document_refs WHERE document_hash=$1`, hash).Scan(&count); err != nil || count != 4 {
|
|
t.Fatal("lost embedded references", count, err)
|
|
}
|
|
// Do not bypass the existing ACL namespace guard during a move/delete.
|
|
f.exec(`INSERT INTO locality_access_policies VALUES($1,'spot','protected','/files',1,$2,3)`, f.domain, f.actor)
|
|
if _, err = s.RenamePathsInDomain(f.domain, "/files", "/escape", true); err == nil {
|
|
t.Fatal("protected move allowed")
|
|
}
|
|
snap, err := s.SnapshotPathsInDomain(f.domain, "/files", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.RemovePathsInDomainChecked(f.domain, []*PathMutationSnapshot{snap}); err == nil {
|
|
t.Fatal("protected delete allowed")
|
|
}
|
|
if _, err = s.ResolvePathInDomain(f.domain, "/files/b.html"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
peer := &Store{DB: s.DB, sqlBackend: storePostgreSQL, DocumentRoot: s.DocumentRoot}
|
|
snap, err = s.SnapshotPathsInDomain(f.domain, "/copy", true)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = peer.CreatePageAtPathInDomain(f.domain, "/copy/new"); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if _, err = s.CopyPathsInDomainChecked(f.domain, "/copy", "/stale", true, snap); !errors.Is(err, ErrPathSnapshotChanged) {
|
|
t.Fatal("stale copy", err)
|
|
}
|
|
if _, err = s.SaveRevisionContentInDomain(context.Background(), f.domain, "", obj, []byte("next"), "", "en", "text/html", "b.html", nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var total, unique int
|
|
if err = s.DB.QueryRow(`SELECT count(*),count(DISTINCT sequence) FROM activity_outbox`).Scan(&total, &unique); err != nil || total != unique {
|
|
t.Fatal(total, unique, err)
|
|
}
|
|
}
|