package user import ( "context" "strings" "testing" "git.golem15.com/golem15/summercms/modules/postcard" ) // TestPolishMailSubjects renders every Polish template next to its -en twin // and requires a subject of its own, so a Polish template with a copied // English subject does not slip through. func TestPolishMailSubjects(t *testing.T) { p := &Plugin{} cat := postcard.NewCatalog() if err := cat.Register(p.ID(), p.MailTemplatesFS(), p.MailTemplates(), p.MailLayouts()); err != nil { t.Fatal(err) } names := map[string]bool{} for _, name := range p.MailTemplates() { names[name] = true } vars := map[string]any{ "name": "Ada", "Name": "Ada", "link": "https://example.com/link", "Link": "https://example.com/link", "code": "1!abc", "Code": "1!abc", "hours": 72, "Hours": 72, "login": "ada@example.test", "Login": "ada@example.test", } subject := func(t *testing.T, template string) string { t.Helper() mem := postcard.NewMemoryDriver() mailer := postcard.NewMailer(cat, mem, postcard.Options{From: "noreply@example.test"}) if err := mailer.Send(context.Background(), postcard.Message{Template: template, To: []string{"ada@example.test"}, Vars: vars}); err != nil { t.Fatalf("%s: %v", template, err) } msgs := mem.Messages() if len(msgs) != 1 { t.Fatalf("%s: %d messages", template, len(msgs)) } return msgs[0].Subject } pairs := 0 for _, name := range p.MailTemplates() { if strings.HasSuffix(name, "-en") || !names[name+"-en"] { continue } pairs++ pl, en := subject(t, name), subject(t, name+"-en") if pl == "" || pl == en { t.Errorf("%s: Polish subject %q, English subject %q", name, pl, en) } } if pairs == 0 { t.Fatal("no Polish/English template pairs") } }