test(quick-261115-cha): cover cross-plugin mail layouts through overrides, the editor and the index
- TestCrossPluginLayoutOverride: a template override naming the lender's layout, a customized lender layout, Editor Validate and Preview - TestCrossPluginLayoutSync (Docker): SyncIndex points the borrower rows' layout_id at the lender's layout row and keeps it on a second run
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package postcard
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
"testing/fstest"
|
||||
@@ -168,3 +169,141 @@ func TestCrossPluginLayout(t *testing.T) {
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestCrossPluginLayoutOverride covers the database and admin paths with a
|
||||
// borrowed layout: a template override naming the lender's layout, a
|
||||
// customized lender layout, and the editor's validate and preview.
|
||||
func TestCrossPluginLayoutOverride(t *testing.T) {
|
||||
t.Parallel()
|
||||
vars := map[string]any{"code": "123456"}
|
||||
|
||||
t.Run("override_layout_full_code", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cat := borrowerCatalog(t)
|
||||
if got := templateLayout(t, cat, borrowerPlain); got != defaultLayoutName {
|
||||
t.Fatalf("plain Layout = %q, want the default layout", got)
|
||||
}
|
||||
drv := NewMemoryDriver()
|
||||
ov := Overrides{Template: &TemplateOverride{Subject: "Mine", Body: "My **body**.", Layout: lenderCardLayout}}
|
||||
m := NewMailer(cat, drv, Options{Overrides: fakeOverrides{ov: ov}})
|
||||
msg := sendTemplate(t, m, drv, borrowerPlain, vars)
|
||||
if !strings.Contains(msg.HTML, lenderCardHTML) {
|
||||
t.Errorf("HTML has no lender card:\n%s", msg.HTML)
|
||||
}
|
||||
if !strings.Contains(msg.Text, lenderCardFooter) {
|
||||
t.Errorf("Text has no lender footer:\n%s", msg.Text)
|
||||
}
|
||||
if msg.Subject != "Mine" {
|
||||
t.Errorf("Subject = %q, want the override's", msg.Subject)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("customized_owner_layout", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cat := borrowerCatalog(t)
|
||||
drv := NewMemoryDriver()
|
||||
ov := Overrides{Layouts: map[string]LayoutOverride{
|
||||
lenderCardLayout: {HTML: `<section id="customized-card">{{ .Content }}</section>`, Text: "{{ .Content }}"},
|
||||
}}
|
||||
m := NewMailer(cat, drv, Options{Overrides: fakeOverrides{ov: ov}})
|
||||
msg := sendTemplate(t, m, drv, borrowerCode, vars)
|
||||
if !strings.Contains(msg.HTML, `<section id="customized-card">`) {
|
||||
t.Errorf("HTML has no customized card:\n%s", msg.HTML)
|
||||
}
|
||||
if strings.Contains(msg.HTML, "lender-card") {
|
||||
t.Errorf("HTML still has the file layout:\n%s", msg.HTML)
|
||||
}
|
||||
})
|
||||
|
||||
t.Run("editor_validate_and_preview", func(t *testing.T) {
|
||||
t.Parallel()
|
||||
cat := borrowerCatalog(t)
|
||||
m := NewMailer(cat, NewMemoryDriver(), Options{})
|
||||
ed := &Editor{m: m.(*mailer)}
|
||||
ctx := context.Background()
|
||||
draft := Draft{Kind: KindTemplate, Name: borrowerCode, Subject: "Code {{ .code }}", Body: "Your code is **{{ .code }}**.", Layout: lenderCardLayout}
|
||||
if derr := ed.Validate(ctx, draft); derr != nil {
|
||||
t.Errorf("Validate with the lender layout: %v", derr)
|
||||
}
|
||||
draft.Layout = ""
|
||||
if derr := ed.Validate(ctx, draft); derr != nil {
|
||||
t.Errorf("Validate keeping the file layout: %v", derr)
|
||||
}
|
||||
pv, err := ed.Preview(ctx, draft)
|
||||
if err != nil {
|
||||
t.Fatalf("Preview: %v", err)
|
||||
}
|
||||
if !strings.Contains(pv.HTML, lenderCardHTML) {
|
||||
t.Errorf("Preview HTML has no lender card:\n%s", pv.HTML)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// TestCrossPluginLayoutSync checks that SyncIndex points a borrower
|
||||
// template's row at the lender's layout row, and keeps it there on a second
|
||||
// run. It needs Docker.
|
||||
func TestCrossPluginLayoutSync(t *testing.T) {
|
||||
gdb := mailPostgres(t)
|
||||
cat := borrowerCatalog(t)
|
||||
ctx := context.Background()
|
||||
|
||||
layoutsOf := func() map[string]string {
|
||||
t.Helper()
|
||||
var rows []struct {
|
||||
Code string
|
||||
LayoutCode *string
|
||||
}
|
||||
err := gdb.Raw(`SELECT t.code, l.code AS layout_code
|
||||
FROM `+TemplatesTable+` t LEFT JOIN `+LayoutsTable+` l ON l.id = t.layout_id
|
||||
WHERE t.code IN (?, ?)`, borrowerCode, borrowerCodeEn).Scan(&rows).Error
|
||||
if err != nil {
|
||||
t.Fatalf("query: %v", err)
|
||||
}
|
||||
got := make(map[string]string, len(rows))
|
||||
for _, r := range rows {
|
||||
if r.LayoutCode != nil {
|
||||
got[r.Code] = *r.LayoutCode
|
||||
} else {
|
||||
got[r.Code] = ""
|
||||
}
|
||||
}
|
||||
return got
|
||||
}
|
||||
layoutIDs := func() map[string]int64 {
|
||||
t.Helper()
|
||||
var rows []struct {
|
||||
Code string
|
||||
LayoutID *int64
|
||||
}
|
||||
if err := gdb.Raw(`SELECT code, layout_id FROM `+TemplatesTable+` WHERE code IN (?, ?)`, borrowerCode, borrowerCodeEn).Scan(&rows).Error; err != nil {
|
||||
t.Fatalf("query: %v", err)
|
||||
}
|
||||
got := make(map[string]int64, len(rows))
|
||||
for _, r := range rows {
|
||||
if r.LayoutID != nil {
|
||||
got[r.Code] = *r.LayoutID
|
||||
}
|
||||
}
|
||||
return got
|
||||
}
|
||||
|
||||
if err := SyncIndex(ctx, gdb, cat, []string{"en"}); err != nil {
|
||||
t.Fatalf("SyncIndex: %v", err)
|
||||
}
|
||||
got := layoutsOf()
|
||||
for _, name := range []string{borrowerCode, borrowerCodeEn} {
|
||||
if got[name] != lenderCardLayout {
|
||||
t.Errorf("%s row layout = %q, want %q (rows %v)", name, got[name], lenderCardLayout, got)
|
||||
}
|
||||
}
|
||||
first := layoutIDs()
|
||||
if err := SyncIndex(ctx, gdb, cat, []string{"en"}); err != nil {
|
||||
t.Fatalf("second SyncIndex: %v", err)
|
||||
}
|
||||
second := layoutIDs()
|
||||
for _, name := range []string{borrowerCode, borrowerCodeEn} {
|
||||
if first[name] == 0 || first[name] != second[name] {
|
||||
t.Errorf("%s layout_id %d after the first run, %d after the second", name, first[name], second[name])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user