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{}) }) }