test(cabana): grouped mail templates, site name and the inlining switch
- TestMailLocaleGroups: one list row per template, customized locales, grouped badges and status filter, localeVariants order, labels, states and current flag, the default version on a fallback row, save then send, reset, drift through the base file, the file version against the base file, out-of-group ids, the permission split and locales enabled at run time - TestMailBrandSiteName: saved trimmed, 255-character limit, preview and send header, no header row without a name or a logo, current handling of a name holding a URL scheme - TestMailLayoutInlineOptOut: the switch on a file layout, reset to the file value, a custom layout created with the switch off - check-phase35.sh requires the three names (DOCKER_TESTS)
This commit is contained in:
215
modules/cabana/mail_brand_layout_postgres_test.go
Normal file
215
modules/cabana/mail_brand_layout_postgres_test.go
Normal file
@@ -0,0 +1,215 @@
|
||||
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(`<td class="header"[^>]*>\s*<span[^>]*>([^<]*)</span>`)
|
||||
|
||||
// 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</p>") {
|
||||
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 }}
|
||||
==
|
||||
<html><head><style>{{ .brandCss }}{{ .css }}</style></head><body><div class="blog-mail">{{ .Content }}{{ partial "button" "url" "https://example.com" "body" "Go" }}</div></body></html>
|
||||
`,
|
||||
}
|
||||
}
|
||||
|
||||
var buttonTagRe = regexp.MustCompile(`<a [^>]*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)
|
||||
}
|
||||
})
|
||||
}
|
||||
313
modules/cabana/mail_locale_postgres_test.go
Normal file
313
modules/cabana/mail_locale_postgres_test.go
Normal file
@@ -0,0 +1,313 @@
|
||||
package cabana_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
|
||||
"git.golem15.com/golem15/summercms/modules/cabana"
|
||||
"git.golem15.com/golem15/summercms/modules/postcard"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
// mutableLocaleWriter is a TranslationWriter whose enabled locales a test
|
||||
// changes while the application runs, as a translate plugin does when a
|
||||
// locale is enabled.
|
||||
type mutableLocaleWriter struct {
|
||||
mu sync.Mutex
|
||||
def string
|
||||
enabled []string
|
||||
}
|
||||
|
||||
func (w *mutableLocaleWriter) DefaultLocale(context.Context, *gorm.DB) (string, error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
return w.def, nil
|
||||
}
|
||||
|
||||
func (w *mutableLocaleWriter) EnabledLocales(context.Context, *gorm.DB) ([]string, error) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
return append([]string(nil), w.enabled...), nil
|
||||
}
|
||||
|
||||
func (w *mutableLocaleWriter) enable(code string) {
|
||||
w.mu.Lock()
|
||||
defer w.mu.Unlock()
|
||||
w.enabled = append(w.enabled, code)
|
||||
}
|
||||
|
||||
func (*mutableLocaleWriter) WriteTranslated(context.Context, *gorm.DB, any, string, string, string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (*mutableLocaleWriter) TranslatedExact(context.Context, *gorm.DB, any, string, string) (string, bool, error) {
|
||||
return "", false, nil
|
||||
}
|
||||
|
||||
// getInLanguage is a GET with Accept-Language lang instead of en.
|
||||
func getInLanguage(t *testing.T, env *mailEnv, rel, token, lang string) map[string]any {
|
||||
t.Helper()
|
||||
req, err := env.request(http.MethodGet, rel, token, nil)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
req.Header.Set("Accept-Language", lang)
|
||||
rec := httptest.NewRecorder()
|
||||
env.h.ServeHTTP(rec, req)
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Fatalf("GET %s (%s) status=%d body=%s", rel, lang, rec.Code, rec.Body.String())
|
||||
}
|
||||
var out map[string]any
|
||||
if err := json.Unmarshal(rec.Body.Bytes(), &out); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// sendGrace sends name to one reader with name=Grace and returns the message.
|
||||
func sendGrace(t *testing.T, env *mailEnv, name string) postcard.RenderedMessage {
|
||||
t.Helper()
|
||||
if err := env.mailer.Send(context.Background(), postcard.Message{Template: name, To: []string{"reader@example.test"}, Vars: map[string]any{"name": "Grace"}}); err != nil {
|
||||
t.Fatalf("Send %s: %v", name, err)
|
||||
}
|
||||
msgs := env.driver.messages()
|
||||
return msgs[len(msgs)-1]
|
||||
}
|
||||
|
||||
// TestMailLocaleGroups covers G-35-1 on the admin side: one list row per
|
||||
// template, the customized locales and grouped states, the locale switcher's
|
||||
// variants, a locale without a file (its default version, save then send,
|
||||
// reset, drift through the base file, its file version and info), ids
|
||||
// outside the group, the permission split and locales enabled at run time.
|
||||
func TestMailLocaleGroups(t *testing.T) {
|
||||
writer := &mutableLocaleWriter{def: "pl", enabled: []string{"pl", "en", "de"}}
|
||||
env := newMailScreensEnvWith(t, mailEnvOptions{writer: writer})
|
||||
_, root := env.admin(t, "mail-groups-root", true, "")
|
||||
_, templatesTok := env.admin(t, "mail-groups-tmpl", false, `{"system.manage_mail_templates":1}`)
|
||||
_, layoutsTok := env.admin(t, "mail-groups-lay", false, `{"system.manage_mail_layouts":1}`)
|
||||
|
||||
layoutID := env.mailRowID(t, postcard.LayoutsTable, blogLayout)
|
||||
baseID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome)
|
||||
enID := env.mailRowID(t, postcard.TemplatesTable, blogWelcomeEn)
|
||||
deID := env.mailRowID(t, postcard.TemplatesTable, blogWelcome+"-de")
|
||||
basePath := fmt.Sprintf("%s/%d", mailTemplatesPath, baseID)
|
||||
dePath := fmt.Sprintf("%s/%d", mailTemplatesPath, deID)
|
||||
file, _ := mailEditor(t, env).Catalog().Template(blogWelcome)
|
||||
|
||||
// The default locale gets no row: the base file is its version.
|
||||
var plRows int64
|
||||
env.gdb.Raw(`SELECT COUNT(*) FROM `+postcard.TemplatesTable+` WHERE code = ?`, blogWelcome+"-pl").Scan(&plRows)
|
||||
if plRows != 0 {
|
||||
t.Fatal("the default locale pl has a row of its own")
|
||||
}
|
||||
|
||||
t.Run("one_row_per_template", func(t *testing.T) {
|
||||
row := welcomeListRow(t, env, root, "")
|
||||
if row == nil || row["customized_locales"] != "" || row["_states"] != "[]" {
|
||||
t.Fatalf("fresh welcome row = %v", row)
|
||||
}
|
||||
if welcomeListRow(t, env, root, "?filter[status]=not_customized") == nil || welcomeListRow(t, env, root, "?filter[status]=customized") != nil {
|
||||
t.Fatal("status filter on a fresh group")
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("variants", func(t *testing.T) {
|
||||
show := env.expect(t, http.StatusOK, http.MethodGet, basePath, root, nil)
|
||||
variants := localeVariants(t, show)
|
||||
if got := variantSummary(variants); got != "Default[]* de[] en[]" {
|
||||
t.Fatalf("welcome variants = %s", got)
|
||||
}
|
||||
wantIDs := []uint{baseID, deID, enID}
|
||||
wantLocales := []string{"", "de", "en"}
|
||||
for i, v := range variants {
|
||||
if uint(v["id"].(float64)) != wantIDs[i] || v["locale"] != wantLocales[i] {
|
||||
t.Errorf("variant %d = %v, want id %d locale %q", i, v, wantIDs[i], wantLocales[i])
|
||||
}
|
||||
}
|
||||
// Labels and states are translated in the request language.
|
||||
pl := localeVariants(t, getInLanguage(t, env, basePath, root, "pl"))
|
||||
if pl[0]["label"] != "Domyślny" || pl[1]["label"] != "de" {
|
||||
t.Errorf("pl labels = %s", variantSummary(pl))
|
||||
}
|
||||
// Every version of the group lists the same variants, its own current.
|
||||
if got := variantSummary(localeVariants(t, env.expect(t, http.StatusOK, http.MethodGet, dePath, root, nil))); got != "Default[] de[]* en[]" {
|
||||
t.Errorf("de variants = %s", got)
|
||||
}
|
||||
if got := variantSummary(localeVariants(t, env.expect(t, http.StatusOK, http.MethodGet, fmt.Sprintf("%s/%d", mailTemplatesPath, enID), root, nil))); got != "Default[] de[] en[]*" {
|
||||
t.Errorf("en variants = %s", got)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("fallback_shows_default_version", func(t *testing.T) {
|
||||
de := dataOf(env.expect(t, http.StatusOK, http.MethodGet, dePath, root, nil))
|
||||
if de["subject"] != file.Subject || de["body"] != file.Body || fmt.Sprint(de["layout"]) != fmt.Sprint(layoutID) {
|
||||
t.Fatalf("not customized de = %v, want the base file", de)
|
||||
}
|
||||
info := partialText(t, env, mailTemplatesPath, "info", root, deID)
|
||||
if !strings.Contains(info, "Locale de has no file of its own") || !strings.Contains(info, "views/mail/welcome.htm") {
|
||||
t.Errorf("de info = %s", info)
|
||||
}
|
||||
version := partialText(t, env, mailTemplatesPath, "file_version", root, deID)
|
||||
if !strings.Contains(version, "dzięki za rejestrację") || strings.Contains(version, "summer-diff__add") {
|
||||
t.Errorf("not customized de file version = %s, want the base file without differences", version)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("save_then_send", func(t *testing.T) {
|
||||
env.expect(t, http.StatusOK, http.MethodPut, dePath, root, templateBody("Willkommen, {{ .name }}", "Hallo **{{ .name }}**.", layoutID))
|
||||
row := welcomeListRow(t, env, root, "")
|
||||
if row["customized_locales"] != "de" || row["_states"] != "[customized]" {
|
||||
t.Fatalf("welcome row after the de save = %v", row)
|
||||
}
|
||||
if welcomeListRow(t, env, root, "?filter[status]=customized") == nil || welcomeListRow(t, env, root, "?filter[status]=not_customized") != nil {
|
||||
t.Error("status filter does not follow the de customization")
|
||||
}
|
||||
if got := sendGrace(t, env, blogWelcome+"-de"); got.Subject != "Willkommen, Grace" || !strings.Contains(got.HTML, "Hallo <strong>Grace</strong>") {
|
||||
t.Errorf("welcome-de sent %q / %q", got.Subject, got.HTML)
|
||||
}
|
||||
if got := sendGrace(t, env, blogWelcome); got.Subject != "Witaj, Grace" {
|
||||
t.Errorf("welcome sent %q, want the base file", got.Subject)
|
||||
}
|
||||
if got := variantSummary(localeVariants(t, env.expect(t, http.StatusOK, http.MethodGet, basePath, root, nil))); got != "Default[]* de[Customized] en[]" {
|
||||
t.Errorf("variants after the de save = %s", got)
|
||||
}
|
||||
version := partialText(t, env, mailTemplatesPath, "file_version", root, deID)
|
||||
if !strings.Contains(version, "summer-diff__add") || !strings.Contains(version, "Willkommen") || !strings.Contains(version, "Witaj") {
|
||||
t.Errorf("customized de file version does not compare with the base file: %s", version)
|
||||
}
|
||||
})
|
||||
|
||||
// The base file changes in a second process on the same database.
|
||||
files := mailBlogFiles()
|
||||
files["views/mail/welcome.htm"] = &fstest.MapFile{Data: []byte(`subject = "Witaj, {{ .name }}"
|
||||
description = "Sent after registration"
|
||||
layout = "blog"
|
||||
sample.name = "Ada"
|
||||
==
|
||||
Cześć **{{ .name }}**, dzięki za rejestrację na blogu.
|
||||
`)}
|
||||
next := newMailScreensEnvWith(t, mailEnvOptions{files: files, keep: true, writer: writer})
|
||||
|
||||
t.Run("drift_through_the_base_file", func(t *testing.T) {
|
||||
row := welcomeListRow(t, next, root, "")
|
||||
if row["_states"] != "[customized outdated]" {
|
||||
t.Fatalf("welcome row after the base file changed = %v", row)
|
||||
}
|
||||
if welcomeListRow(t, next, root, "?filter[status]=outdated") == nil {
|
||||
t.Error("outdated filter misses welcome")
|
||||
}
|
||||
if got := variantSummary(localeVariants(t, next.expect(t, http.StatusOK, http.MethodGet, dePath, root, nil))); got != "Default[] de[Customized File changed]* en[]" {
|
||||
t.Errorf("de variants after the base file changed = %s", got)
|
||||
}
|
||||
var stored cabana.MailTemplate
|
||||
if err := next.gdb.First(&stored, deID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
newFile, _ := mailEditor(t, next).Catalog().Template(blogWelcome)
|
||||
if stored.FileHash == nil || *stored.FileHash != newFile.Hash || stored.BaseHash == nil || *stored.BaseHash != file.Hash {
|
||||
t.Errorf("de hashes = %v / %v, want file %s and base %s", stored.FileHash, stored.BaseHash, newFile.Hash, file.Hash)
|
||||
}
|
||||
if version := partialText(t, next, mailTemplatesPath, "file_version", root, deID); !strings.Contains(version, "na blogu") {
|
||||
t.Errorf("de file version is not the changed base file: %s", version)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("reset", func(t *testing.T) {
|
||||
next.expect(t, http.StatusOK, http.MethodPut, basePath, root, templateBody("Witamy, {{ .name }}", "Base.", layoutID))
|
||||
if row := welcomeListRow(t, next, root, ""); row["customized_locales"] != "Default, de" {
|
||||
t.Fatalf("welcome row after the base save = %v", row)
|
||||
}
|
||||
next.expect(t, http.StatusOK, http.MethodPost, dePath+"/actions/reset", root, map[string]any{})
|
||||
var stored cabana.MailTemplate
|
||||
if err := next.gdb.First(&stored, deID).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if stored.IsCustom || stored.BaseHash != nil {
|
||||
t.Fatalf("reset de row = %+v", stored)
|
||||
}
|
||||
// Not customized again: de shows and sends the base customization.
|
||||
de := dataOf(next.expect(t, http.StatusOK, http.MethodGet, dePath, root, nil))
|
||||
if de["subject"] != "Witamy, {{ .name }}" || de["body"] != "Base." {
|
||||
t.Errorf("de after reset = %v, want the default version", de)
|
||||
}
|
||||
if got := sendGrace(t, next, blogWelcome+"-de"); got.Subject != "Witamy, Grace" {
|
||||
t.Errorf("welcome-de after reset sent %q", got.Subject)
|
||||
}
|
||||
if row := welcomeListRow(t, next, root, ""); row["customized_locales"] != "Default" {
|
||||
t.Errorf("welcome row after the de reset = %v", row)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("ids_outside_the_group", func(t *testing.T) {
|
||||
var orphan uint
|
||||
if err := next.gdb.Raw(`INSERT INTO ` + postcard.TemplatesTable + ` (code, source, base_code) VALUES ('acme.gone::mail.x-de', 'acme.gone', 'acme.gone::mail.x') RETURNING id`).Scan(&orphan).Error; err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, method := range []string{http.MethodGet, http.MethodPut} {
|
||||
var body any
|
||||
if method == http.MethodPut {
|
||||
body = templateBody("x", "y", layoutID)
|
||||
}
|
||||
if rec := next.call(t, method, fmt.Sprintf("%s/%d", mailTemplatesPath, orphan), root, body); rec.Code != http.StatusNotFound {
|
||||
t.Errorf("%s orphan status = %d", method, rec.Code)
|
||||
}
|
||||
}
|
||||
if rec := next.call(t, http.MethodGet, fmt.Sprintf("%s/%d", mailTemplatesPath, 99999999), root, nil); rec.Code != http.StatusNotFound {
|
||||
t.Errorf("unknown id status = %d", rec.Code)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("permissions_on_fallback_rows", func(t *testing.T) {
|
||||
for _, tc := range []struct {
|
||||
name, token, method string
|
||||
want int
|
||||
}{
|
||||
{"layouts-only show", layoutsTok, http.MethodGet, http.StatusForbidden},
|
||||
{"layouts-only save", layoutsTok, http.MethodPut, http.StatusForbidden},
|
||||
{"layouts-only reset", layoutsTok, http.MethodPost, http.StatusForbidden},
|
||||
{"templates-only show", templatesTok, http.MethodGet, http.StatusOK},
|
||||
{"templates-only save", templatesTok, http.MethodPut, http.StatusOK},
|
||||
} {
|
||||
path, body := dePath, any(nil)
|
||||
switch tc.method {
|
||||
case http.MethodPut:
|
||||
body = templateBody("Guten Tag", "Hallo.", layoutID)
|
||||
case http.MethodPost:
|
||||
path, body = dePath+"/actions/reset", map[string]any{}
|
||||
}
|
||||
if rec := next.call(t, tc.method, path, tc.token, body); rec.Code != tc.want {
|
||||
t.Errorf("%s: status %d, want %d (%s)", tc.name, rec.Code, tc.want, rec.Body.String())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("locale_enabled_at_run_time", func(t *testing.T) {
|
||||
// fr is enabled while the process runs: the switcher lists it on
|
||||
// the next show, without a restart.
|
||||
writer.enable("fr")
|
||||
variants := localeVariants(t, next.expect(t, http.StatusOK, http.MethodGet, basePath, root, nil))
|
||||
if got := variantSummary(variants); got != "Default[Customized]* de[Customized] en[] fr[]" {
|
||||
t.Fatalf("variants after fr was enabled = %s", got)
|
||||
}
|
||||
frID := next.mailRowID(t, postcard.TemplatesTable, blogWelcome+"-fr")
|
||||
if fr := dataOf(next.expect(t, http.StatusOK, http.MethodGet, fmt.Sprintf("%s/%d", mailTemplatesPath, frID), root, nil)); fr["subject"] != "Witamy, {{ .name }}" {
|
||||
t.Errorf("fr = %v, want the base customization", fr)
|
||||
}
|
||||
// it is enabled and only the list is read: the changed locale set
|
||||
// re-syncs the index, and the list keeps one row per template.
|
||||
writer.enable("it")
|
||||
row := welcomeListRow(t, next, root, "")
|
||||
if row == nil || row["customized_locales"] != "Default, de" {
|
||||
t.Fatalf("welcome row after it was enabled = %v", row)
|
||||
}
|
||||
next.mailRowID(t, postcard.TemplatesTable, blogWelcome+"-it")
|
||||
next.mailRowID(t, postcard.TemplatesTable, blogWelcomeEn) // untouched file-backed row
|
||||
})
|
||||
}
|
||||
@@ -119,6 +119,9 @@ DOCKER_TESTS=(
|
||||
./modules/cabana:TestMailBrand
|
||||
./modules/cabana:TestMailTestSend
|
||||
./modules/postcard:TestEnsureLocaleRows
|
||||
./modules/cabana:TestMailLocaleGroups
|
||||
./modules/cabana:TestMailBrandSiteName
|
||||
./modules/cabana:TestMailLayoutInlineOptOut
|
||||
)
|
||||
|
||||
# admin stage: vitest files that must pass.
|
||||
|
||||
Reference in New Issue
Block a user