package postcard
import (
"context"
"errors"
"path/filepath"
"reflect"
"regexp"
"strings"
"testing"
"git.golem15.com/golem15/summercms/modules/compass"
"git.golem15.com/golem15/summercms/modules/lagoon/attach"
"github.com/go-gormigrate/gormigrate/v2"
)
// brandCfg writes config files and opens them: files maps a config file
// name such as mail.yaml to its content.
func brandCfg(t *testing.T, files map[string]string) *compass.Config {
t.Helper()
dir := t.TempDir()
for name, body := range files {
writeFile(t, filepath.Join(dir, name), body)
}
return loadMailCfg(t, dir, nil)
}
// styleBlock returns the content of the first style element of html.
func styleBlock(t *testing.T, html string) string {
t.Helper()
_, rest, ok := strings.Cut(html, "")
return css
}
// styleAttr matches a style attribute, which inlining adds to elements.
var styleAttr = regexp.MustCompile(` style="[^"]*"`)
// unstyled returns html without its style attributes, for assertions on
// the markup of inlined mail.
func unstyled(html string) string { return styleAttr.ReplaceAllString(html, "") }
// noInline is an inliner that leaves the HTML as it is, for tests of what
// fills the CSS slots rather than of inlining (TestMailPartialsSmoke/inline).
func noInline(html string) (string, error) { return html, nil }
// TestMailBrandSmoke covers Branding: the compiled brand CSS and its colour
// checks, the precedence over mail.brandCss once the settings row exists,
// the logo URL built at send time from configuration, and the editor's
// preview of unsaved colours.
func TestMailBrandSmoke(t *testing.T) {
ctx := context.Background()
t.Run("absolute_url", func(t *testing.T) {
const key = "abc/def/123/abcdef123456.png"
cases := []struct {
name string
files map[string]string
want string
}{
{"public_url", map[string]string{"storage.yaml": "uploads:\n public_url: https://cdn.example.com/u\n", "app.yaml": "url: https://acme.test\n"}, "https://cdn.example.com/u/" + key},
{"public_url trailing slash", map[string]string{"storage.yaml": "uploads:\n public_url: https://cdn.example.com/u/\n"}, "https://cdn.example.com/u/" + key},
{"app.url and the default prefix", map[string]string{"app.yaml": "url: https://acme.test\n"}, "https://acme.test/storage/uploads/" + key},
{"app.url with a path and a prefix", map[string]string{"app.yaml": "url: https://acme.test/shop/\n", "storage.yaml": "uploads:\n public_path_prefix: /files/\n"}, "https://acme.test/shop/files/" + key},
}
for _, tc := range cases {
got, err := attach.AbsolutePublicURL(brandCfg(t, tc.files), "/"+key)
if err != nil || got != tc.want {
t.Errorf("%s: %q, %v; want %q", tc.name, got, err, tc.want)
}
}
for name, files := range map[string]map[string]string{
"neither": {"mail.yaml": "driver: memory\n"},
"relative app.url": {"app.yaml": "url: /shop\n"},
"app.url no scheme": {"app.yaml": "url: acme.test\n"},
"app.url ftp": {"app.yaml": "url: ftp://acme.test\n"},
} {
if got, err := attach.AbsolutePublicURL(brandCfg(t, files), key); !errors.Is(err, attach.ErrNoAbsoluteURL) || got != "" {
t.Errorf("%s: %q, %v; want ErrNoAbsoluteURL", name, got, err)
}
}
if _, err := attach.AbsolutePublicURL(nil, key); !errors.Is(err, attach.ErrNoAbsoluteURL) {
t.Errorf("nil config: %v", err)
}
if _, err := attach.AbsolutePublicURL(brandCfg(t, map[string]string{"storage.yaml": "uploads:\n public_url: cdn.example.com\n", "app.yaml": "url: https://acme.test\n"}), key); err == nil || errors.Is(err, attach.ErrNoAbsoluteURL) {
t.Errorf("a malformed public_url = %v, want its own error", err)
}
})
t.Run("defaults", func(t *testing.T) {
want := Brand{
BodyBg: "#f5f8fa", ContentBg: "#ffffff", HeaderColor: "#bbbfc3", FooterColor: "#aeaeae",
HeadingColor: "#2f3133", TextColor: "#74787e", LinkColor: "#2da7c7", ButtonTextColor: "#ffffff",
ButtonPrimaryBg: "#d66829", ButtonPositiveBg: "#52a838", ButtonNegativeBg: "#e01346",
PanelBg: "#edeff2", TableBorderColor: "#edeff2",
}
if got := DefaultBrand(); got != want {
t.Errorf("DefaultBrand() = %+v, want %+v", got, want)
}
})
t.Run("compile", func(t *testing.T) {
// Thirteen distinct colours, so each one can be found in the output.
b := Brand{
BodyBg: "#010101", ContentBg: "#020202", HeaderColor: "#030303", FooterColor: "#040404",
HeadingColor: "#050505", TextColor: "#060606", LinkColor: "#070707", ButtonTextColor: "#080808",
ButtonPrimaryBg: "#090909", ButtonPositiveBg: "#0a0a0a", ButtonNegativeBg: "#0b0b0b",
PanelBg: "#0c0c0c", TableBorderColor: "#0d0d0d",
}
css, err := CompileBrandCSS(b)
if err != nil {
t.Fatalf("CompileBrandCSS: %v", err)
}
v := reflect.ValueOf(b)
for i := range v.NumField() {
if colour := v.Field(i).String(); colour != "" && !strings.Contains(css, colour) {
t.Errorf("compiled CSS lacks %s (%s)", v.Type().Field(i).Name, colour)
}
}
for _, want := range []string{
"body {", "background-color: #010101", "a {\n\tcolor: #070707", "h1 {\n\tcolor: #050505",
".button-primary {\n\tbackground-color: #090909", ".button-positive {\n\tbackground-color: #0a0a0a",
".button-negative {\n\tbackground-color: #0b0b0b", ".panel-content {\n\tbackground-color: #0c0c0c",
".table th {\n\tborder-bottom: 1px solid #0d0d0d", ".subcopy {", ".footer p {\n\tcolor: #040404",
".header a, .header span {\n\tcolor: #030303", ".body {\n\tbackground-color: #020202",
} {
if !strings.Contains(css, want) {
t.Errorf("compiled CSS lacks %q", want)
}
}
if err := validateHTML(""); err != nil {
t.Errorf("the compiled CSS fails the mail safety checks: %v", err)
}
if strings.Contains(css, "<") || strings.Contains(css, "{{") {
t.Errorf("compiled CSS holds markup or an unexecuted action:\n%s", css)
}
def, err := CompileBrandCSS(DefaultBrand())
if err != nil {
t.Fatal(err)
}
for _, colour := range []string{"#f5f8fa", "#ffffff", "#bbbfc3", "#aeaeae", "#2f3133", "#74787e", "#2da7c7", "#d66829", "#52a838", "#e01346", "#edeff2"} {
if !strings.Contains(def, colour) {
t.Errorf("default CSS lacks %s", colour)
}
}
// An empty colour is that field's default; the zero Brand is the default brand.
if zero, err := CompileBrandCSS(Brand{}); err != nil || zero != def {
t.Errorf("CompileBrandCSS(Brand{}) differs from the default brand: %v", err)
}
// Upper-case hex is accepted and emitted lower-case.
upper, err := CompileBrandCSS(Brand{ButtonPrimaryBg: "#AB12CD"})
if err != nil || !strings.Contains(upper, "#ab12cd") || strings.Contains(upper, "#AB12CD") {
t.Errorf("upper-case hex: %v", err)
}
// The logo URL is not a colour and does not reach the CSS.
if with, err := CompileBrandCSS(Brand{LogoURL: "https://cdn.example.com/logo.png"}); err != nil || with != def {
t.Errorf("LogoURL changed the CSS: %v", err)
}
bad := map[string]string{
"short hex": "#ABC",
"name": "red",
"style end": "#fff