package cabana_test import ( "fmt" "net/http" "regexp" "strings" "testing" "git.golem15.com/golem15/summercms/modules/cabana" "git.golem15.com/golem15/summercms/modules/postcard" ) const mailBrandPath = "/settings/mail_brand" // headerSpan is the site name in the built-in header partial's cell. var headerSpan = regexp.MustCompile(`]*>\s*]*>([^<]*)`) // brandPreview renders the Branding preview of values and returns its HTML. func brandPreview(t *testing.T, env *mailEnv, token string, values map[string]any) string { t.Helper() got := env.expect(t, http.StatusOK, http.MethodPost, "/mail/preview", token, map[string]any{"mode": "branding", "id": 0, "values": values}) return fmt.Sprint(dataOf(got)["html"]) } // TestMailBrandSiteName covers the Branding site name: saved trimmed, // limited to 255 characters, shown by the Branding preview and by sends in // the built-in layouts; no header row without a name or a logo. func TestMailBrandSiteName(t *testing.T) { env := newMailScreensEnv(t, nil) _, root := env.admin(t, "mail-site-root", true, "") t.Run("saved_trimmed", func(t *testing.T) { saved := env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": " Acme Mail "}) if name := dataOf(saved)["data"].(map[string]any)["site_name"]; name != "Acme Mail" { t.Fatalf("response site_name = %#v", name) } var stored cabana.MailBrandSetting if err := env.gdb.First(&stored, 1).Error; err != nil || stored.SiteName != "Acme Mail" { t.Fatalf("brand row = %+v err=%v", stored, err) } got := env.expect(t, http.StatusOK, http.MethodGet, mailBrandPath, root, nil) if name := dataOf(got)["data"].(map[string]any)["site_name"]; name != "Acme Mail" { t.Errorf("read back site_name = %#v", name) } }) t.Run("max_length", func(t *testing.T) { env.expectFieldRefusal(t, http.StatusUnprocessableEntity, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": strings.Repeat("x", 256)}, "site_name", "255", false) var stored cabana.MailBrandSetting if err := env.gdb.First(&stored, 1).Error; err != nil || stored.SiteName != "Acme Mail" { t.Fatalf("refused save changed the row: %+v err=%v", stored, err) } env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": strings.Repeat("y", 255)}) env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": "Acme Mail"}) }) t.Run("preview", func(t *testing.T) { html := brandPreview(t, env, root, map[string]any{"site_name": "Preview Name"}) if m := headerSpan.FindStringSubmatch(html); m == nil || m[1] != "Preview Name" { t.Errorf("preview header does not show the site name:\n%s", html) } if !strings.Contains(html, "Preview Name

") { t.Errorf("preview footer does not show the site name:\n%s", html) } // No logo and no name (mail.from has no display name): no header row. html = brandPreview(t, env, root, map[string]any{"site_name": ""}) if strings.Contains(html, `class="header"`) { t.Errorf("preview without a logo or a name has a header row:\n%s", html) } }) t.Run("send", func(t *testing.T) { // The welcome template moved to the built-in default layout shows the // saved name in its header and footer. tmplID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome) defaultID := env.mailRowID(t, postcard.LayoutsTable, "postcard::mail.layouts.default") env.expect(t, http.StatusOK, http.MethodPut, fmt.Sprintf("%s/%d", mailTemplatesPath, tmplID), root, templateBody("Witaj, {{ .name }}", "Cześć **{{ .name }}**.", defaultID)) got := sendGrace(t, env, blogWelcome) if m := headerSpan.FindStringSubmatch(got.HTML); m == nil || m[1] != "Acme Mail" { t.Errorf("sent header does not show the saved site name:\n%s", got.HTML) } if !strings.Contains(got.Text, "Acme Mail") { t.Errorf("sent text part lacks the footer's site name:\n%s", got.Text) } env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": ""}) if got = sendGrace(t, env, blogWelcome); strings.Contains(got.HTML, `class="header"`) { t.Errorf("a send without a site name, a logo or a mail.from name has a header row:\n%s", got.HTML) } }) t.Run("unsafe_name_current_behaviour", func(t *testing.T) { // Open item (35-06 threat flag): the site name is not checked for a // URL scheme at save time. It is stored as typed; the rendered // header then fails the safety check, so the preview refuses it. env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": "javascript: Acme"}) rec := env.call(t, http.MethodPost, "/mail/preview", root, map[string]any{"mode": "branding", "id": 0, "values": map[string]any{"site_name": "javascript: Acme"}}) if rec.Code != http.StatusUnprocessableEntity { t.Errorf("preview of an unsafe site name: status %d, want 422 (%s)", rec.Code, rec.Body.String()) } env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"site_name": ""}) }) } // inlineLayoutFiles is the blog fixture whose blog layout carries a style // block and a button, so whether its CSS was inlined shows on the button. func inlineLayoutFiles() map[string]string { return map[string]string{ "views/mail/layouts/blog.htm": `name = "Blog" == {{ .Content }} ==
{{ .Content }}{{ partial "button" "url" "https://example.com" "body" "Go" }}
`, } } var buttonTagRe = regexp.MustCompile(`]*button-primary[^>]*>`) // sentButtonInlined reports whether the last send's button carries the // brand colour in a style attribute; it fails the test without a button. func sentButtonInlined(t *testing.T, msg postcard.RenderedMessage, colour string) bool { t.Helper() tag := buttonTagRe.FindString(msg.HTML) if tag == "" { t.Fatalf("no button in %s", msg.HTML) } _, style, ok := strings.Cut(tag, ` style="`) return ok && strings.Contains(style, colour) } // TestMailLayoutInlineOptOut covers the layout form's inlining switch: on a // file layout it keeps the CSS in the style block of the next send, reset // restores the file's value, and a custom layout created with the switch // off inlines. func TestMailLayoutInlineOptOut(t *testing.T) { files := mailBlogFiles() for name, data := range inlineLayoutFiles() { files[name].Data = []byte(data) } env := newMailScreensEnvWith(t, mailEnvOptions{files: files}) _, root := env.admin(t, "mail-inline-root", true, "") const colour = "#123456" env.expect(t, http.StatusOK, http.MethodPut, mailBrandPath, root, map[string]any{"button_primary_bg": colour}) blogID := env.mailRowID(t, postcard.LayoutsTable, blogLayout) blogPath := fmt.Sprintf("%s/%d", mailLayoutsPath, blogID) blogFile, _ := mailEditor(t, env).Catalog().Layout(blogLayout) stored := func() cabana.MailLayout { t.Helper() var l cabana.MailLayout if err := env.gdb.First(&l, blogID).Error; err != nil { t.Fatal(err) } return l } // The file layout inlines (its header has no disable_auto_inline_css). if blogFile.DisableAutoInlineCSS || stored().DisableAutoInlineCSS { t.Fatal("the fixture layout opts out") } if !sentButtonInlined(t, sendGrace(t, env, blogWelcomeEn), colour) { t.Fatal("the file layout did not inline") } if form := dataOf(env.expect(t, http.StatusOK, http.MethodGet, blogPath, root, nil)); form["disable_auto_inline_css"] != false { t.Errorf("form value = %#v, want false", form["disable_auto_inline_css"]) } t.Run("switch_on", func(t *testing.T) { env.expect(t, http.StatusOK, http.MethodPut, blogPath, root, map[string]any{ "name": "Blog", "content_html": blogFile.HTML, "content_text": blogFile.Text, "content_css": "", "disable_auto_inline_css": true, }) if l := stored(); !l.DisableAutoInlineCSS || !l.IsCustom { t.Fatalf("saved layout = %+v", l) } msg := sendGrace(t, env, blogWelcomeEn) if sentButtonInlined(t, msg, colour) { t.Errorf("the switch did not keep the CSS in the style block:\n%s", msg.HTML) } if !strings.Contains(msg.HTML, colour) { t.Errorf("the style block lost the brand CSS:\n%s", msg.HTML) } if form := dataOf(env.expect(t, http.StatusOK, http.MethodGet, blogPath, root, nil)); form["disable_auto_inline_css"] != true { t.Errorf("form value = %#v, want true", form["disable_auto_inline_css"]) } }) t.Run("reset_restores_the_file_value", func(t *testing.T) { env.expect(t, http.StatusOK, http.MethodPost, blogPath+"/actions/reset", root, map[string]any{}) if l := stored(); l.DisableAutoInlineCSS || l.IsCustom { t.Fatalf("reset layout = %+v", l) } if !sentButtonInlined(t, sendGrace(t, env, blogWelcomeEn), colour) { t.Error("the reset layout does not inline") } }) t.Run("custom_layout_switch_off", func(t *testing.T) { created := env.expect(t, http.StatusCreated, http.MethodPost, mailLayoutsPath, root, map[string]any{ "name": "Promo", "code": "promo-inline", "content_html": blogFile.HTML, "content_text": blogFile.Text, "content_css": ".promo { margin: 0; }", "disable_auto_inline_css": false, }) customID := uint(dataOf(created)["id"].(float64)) var custom cabana.MailLayout if err := env.gdb.First(&custom, customID).Error; err != nil || custom.DisableAutoInlineCSS { t.Fatalf("custom layout = %+v err=%v", custom, err) } enID := env.mailRowID(t, postcard.TemplatesTable, blogWelcomeEn) env.expect(t, http.StatusOK, http.MethodPut, fmt.Sprintf("%s/%d", mailTemplatesPath, enID), root, templateBody("Welcome, {{ .name }}", "Hi **{{ .name }}**.", customID)) if msg := sendGrace(t, env, blogWelcomeEn); !sentButtonInlined(t, msg, colour) { t.Errorf("a custom layout with the switch off did not inline:\n%s", msg.HTML) } }) }