- Pin override precedence, fallback, partials, samples, draft validation and branding - Postgres TestStore/TestSync for index sync, drift hashes and reset - Pact row-state and mail capability method-set pins; attach AbsolutePublicURL cases Co-authored-by: Cursor <cursoragent@cursor.com>
114 lines
4.1 KiB
Go
114 lines
4.1 KiB
Go
package postcard
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
)
|
|
|
|
func TestPartial(t *testing.T) {
|
|
t.Parallel()
|
|
|
|
t.Run("boot_foreign_namespace", func(t *testing.T) {
|
|
p := partialsPlugin{id: "acme.blog", fsys: fstest.MapFS{}, partials: []string{"other.plugin::mail.partials.x"}}
|
|
app := backpack.New(nil)
|
|
if err := Activate(app, []partialsPlugin{p}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := BootPlugin(app, p); err == nil {
|
|
t.Fatal("want foreign-namespace error")
|
|
}
|
|
})
|
|
t.Run("boot_missing_file", func(t *testing.T) {
|
|
p := partialsPlugin{id: "acme.blog", fsys: fstest.MapFS{}, partials: []string{"acme.blog::mail.partials.missing"}}
|
|
app := backpack.New(nil)
|
|
if err := Activate(app, []partialsPlugin{p}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := BootPlugin(app, p); err == nil {
|
|
t.Fatal("want missing-file error")
|
|
}
|
|
})
|
|
t.Run("boot_duplicate", func(t *testing.T) {
|
|
fsys := fstest.MapFS{"views/mail/partials/note.htm": {Data: []byte(noteHTM)}}
|
|
p := partialsPlugin{id: "acme.blog", fsys: fsys, partials: []string{"acme.blog::mail.partials.note", "acme.blog::mail.partials.note"}}
|
|
app := backpack.New(nil)
|
|
if err := Activate(app, []partialsPlugin{p}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := BootPlugin(app, p); err == nil {
|
|
t.Fatal("want duplicate error")
|
|
}
|
|
})
|
|
t.Run("boot_name_outside_mail_partials", func(t *testing.T) {
|
|
p := partialsPlugin{id: "acme.blog", fsys: fstest.MapFS{}, partials: []string{"acme.blog::views.note"}}
|
|
app := backpack.New(nil)
|
|
if err := Activate(app, []partialsPlugin{p}); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := BootPlugin(app, p); err == nil {
|
|
t.Fatal("want name-outside-mail.partials error")
|
|
}
|
|
})
|
|
|
|
t.Run("bare_and_full_name", func(t *testing.T) {
|
|
m, drv := partialsMailer(t, Options{}, nil)
|
|
got := sendTemplate(t, m, drv, "acme.blog::mail.welcome", map[string]any{"Name": "Ada", "Link": "https://example.com"})
|
|
if !strings.Contains(got.HTML, "https://example.com") || !strings.Contains(got.HTML, "Go") {
|
|
t.Fatalf("bare button partial missing: %q", got.HTML)
|
|
}
|
|
got = sendTemplate(t, m, drv, "acme.blog::mail.note", map[string]any{"Text": "hello"})
|
|
if !strings.Contains(got.HTML, `<div class="note">hello</div>`) {
|
|
t.Fatalf("full plugin partial missing: %q", got.HTML)
|
|
}
|
|
})
|
|
|
|
t.Run("odd_kv_and_unknown", func(t *testing.T) {
|
|
cat := NewCatalog()
|
|
p := blogPlugin()
|
|
if err := cat.RegisterPartials(p.id, p.fsys, p.partials); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := cat.Register(p.id, p.fsys, p.templates, p.layouts); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
run := newPartialRun(cat, nil)
|
|
if _, err := run.htmlCall("button", "url"); err == nil {
|
|
t.Fatal("want odd kv error")
|
|
}
|
|
if _, err := run.htmlCall("no.such.partial"); err == nil {
|
|
t.Fatal("want unknown partial error")
|
|
}
|
|
})
|
|
|
|
t.Run("forged_token", func(t *testing.T) {
|
|
m, drv := partialsMailer(t, Options{}, nil)
|
|
// A variable that looks like a partial token must not be swapped for real HTML.
|
|
got := sendTemplate(t, m, drv, "acme.blog::mail.welcome", map[string]any{
|
|
"Name": "MAILPARTIAL" + strings.Repeat("a", 32) + "X0X",
|
|
"Link": "https://example.com",
|
|
})
|
|
if strings.Contains(got.HTML, `<a `) && strings.Count(got.HTML, "https://example.com") > 1 {
|
|
// The real button still renders; the forged name must remain literal in the body.
|
|
}
|
|
if !strings.Contains(got.HTML, "MAILPARTIAL") {
|
|
t.Fatalf("forged token was consumed: %q", got.HTML)
|
|
}
|
|
})
|
|
|
|
t.Run("layout_and_text_and_nonce", func(t *testing.T) {
|
|
m, drv := partialsMailer(t, Options{}, nil)
|
|
a := sendTemplate(t, m, drv, "acme.blog::mail.note", map[string]any{"Text": "one"})
|
|
b := sendTemplate(t, m, drv, "acme.blog::mail.note", map[string]any{"Text": "two"})
|
|
if !strings.Contains(a.HTML, "footer") || !strings.Contains(a.Text, "NOTE:") {
|
|
t.Fatalf("layout partial / text part missing: html=%q text=%q", a.HTML, a.Text)
|
|
}
|
|
// Nonces differ between renders so a forged token from one message cannot be replayed.
|
|
if a.HTML == b.HTML {
|
|
t.Fatal("two renders produced identical HTML; nonces may be sticky")
|
|
}
|
|
})
|
|
}
|