Files
summercms/modules/postcard/brand_test.go
Jakub Zych d320733510 feat(postcard): inline mail CSS at render with a per-layout opt-out
- github.com/vanng822/go-premailer v1.38.0 (default options) inlines the
  rendered HTML after the safety checks; the result is checked again,
  HTML without a style element is left as it is, an inliner error sends
  the style block only and warns (key inline:<layout>)
- opt-out: layout header key disable_auto_inline_css, the layouts column
  of the same name (migration 202611130003), LayoutFile/LayoutOverride/
  Draft.DisableAutoInlineCSS; SyncIndex mirrors and ResetOverride restores
  the file's value
- cabana layout form switch on the CSS tab, mapped into the layout draft
  and the save validation; en/pl copy
- tests: TestMailPartialsSmoke/inline, TestMailEditorSmoke/inline_flag,
  the layouts switch in TestMailScreensSmoke; CSS-slot tests read the
  style block through a no-op inliner
- 35-06 Task 2 (G-35-8)
2026-11-13 21:12:01 +01:00

119 lines
3.1 KiB
Go

package postcard
import (
"context"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/backpack"
"git.golem15.com/golem15/summercms/modules/compass"
)
func TestBrand(t *testing.T) {
t.Parallel()
t.Run("compile_defaults", func(t *testing.T) {
css, err := CompileBrandCSS(DefaultBrand())
if err != nil {
t.Fatal(err)
}
if !strings.Contains(css, "#") {
t.Fatalf("css = %q", css)
}
})
t.Run("refuse_bad_colour", func(t *testing.T) {
_, err := CompileBrandCSS(Brand{ButtonPrimaryBg: "red"})
if err == nil {
t.Fatal("want colour error")
}
if !strings.Contains(err.Error(), "button_primary_bg") {
t.Fatalf("error = %v, want field name", err)
}
})
t.Run("empty_uses_default", func(t *testing.T) {
css, err := CompileBrandCSS(Brand{})
if err != nil {
t.Fatal(err)
}
def, err := CompileBrandCSS(DefaultBrand())
if err != nil {
t.Fatal(err)
}
if css != def {
t.Fatalf("empty brand css differs from defaults")
}
})
t.Run("lower_casing", func(t *testing.T) {
css, err := CompileBrandCSS(Brand{ButtonPrimaryBg: "#AABBCC"})
if err != nil {
t.Fatal(err)
}
if !strings.Contains(css, "#aabbcc") {
t.Fatalf("css = %q, want lower-cased colour", css)
}
})
t.Run("precedence_over_config", func(t *testing.T) {
plugin := acmeBlog{}
cat := NewCatalog()
if err := cat.Register(plugin.ID(), plugin.MailTemplatesFS(), plugin.MailTemplates(), plugin.MailLayouts()); err != nil {
t.Fatal(err)
}
brand := DefaultBrand()
brand.ButtonPrimaryBg = "#112233"
ov := Overrides{Brand: &brand}
drv := NewMemoryDriver()
m := NewMailer(cat, drv, Options{
BrandCSS: ".from-config{color:red}",
CSS: ".mail-css{margin:0}",
Overrides: fakeOverrides{ov: ov},
})
// Which CSS fills the slots, not how it is inlined.
m.(*mailer).inline = noInline
sendFixture(t, m, acmeNotice, "")
got := lastMessage(t, drv)
if !strings.Contains(got.HTML, "#112233") {
t.Fatalf("brand row CSS missing: %q", got.HTML)
}
if strings.Contains(got.HTML, ".from-config") {
t.Fatalf("mail.brandCss still applied after brand row: %q", got.HTML)
}
if !strings.Contains(got.HTML, ".mail-css") {
t.Fatalf("mail.css missing: %q", got.HTML)
}
})
t.Run("no_row_uses_brandCss", func(t *testing.T) {
plugin := acmeBlog{}
cat := NewCatalog()
if err := cat.Register(plugin.ID(), plugin.MailTemplatesFS(), plugin.MailTemplates(), plugin.MailLayouts()); err != nil {
t.Fatal(err)
}
drv := NewMemoryDriver()
m := NewMailer(cat, drv, Options{BrandCSS: ".from-config{color:red}", CSS: ".mail-css{margin:0}"})
m.(*mailer).inline = noInline
sendFixture(t, m, acmeNotice, "")
got := lastMessage(t, drv)
if !strings.Contains(got.HTML, ".from-config") || !strings.Contains(got.HTML, ".mail-css") {
t.Fatalf("config brand/css missing: %q", got.HTML)
}
})
t.Run("logo_url", func(t *testing.T) {
b := DefaultBrand()
b.LogoURL = "https://cdn.example.test/logo.png"
if b.LogoURL == "" {
t.Fatal("logo url empty")
}
b.LogoURL = ""
if b.LogoURL != "" {
t.Fatal("empty logo must stay empty")
}
_ = context.Background()
_ = backpack.New(&compass.Config{})
})
}