release gate / check (push) Failing after 6m56s
Transactional mail now goes out as the Superbrain HTML design
(internal/mailer/template.html, embedded verbatim) with a plain-text
alternative: multipart/alternative (plain first per RFC 2046), random
boundary, CRLF normalization and SMTP dot-stuffing. Render() fills the
{{placeholders}} with HTML-escaped values and removes optional sections
(CTA + fallback link, note block, footer links, address) when empty;
anchor drift fails the unit tests and degrades to plain text at send
time, never blocking delivery. Password reset gets the headline,
first-name greeting, "Choose a new password" button (Outlook VML
fallback) and expiry/single-use note; notification and agent emails
render title-as-headline with body paragraphs.
The template's footer links are now real pages:
- /help: public help center (10 topic sections + FAQ accordion), linked
from the login page.
- /unsubscribe?token=...: public confirmation page; the HMAC-signed user
id (notify.UnsubscribeToken, domain-separated, stable, no stored row)
IS the credential. POST /api/notifications/unsubscribe accepts the
RFC 8058 one-click shape (token in query, form body ignored) and the
browser JSON shape; it disables the user's enabled email channels and
pauses enabled agent email tasks addressed to their own email
(idempotent, re-enableable, per-client rate limited, audit logged).
- Email preferences deep-links to /settings?tab=notifications; Settings
now honors ?tab=<section> (unknown values fall back to profile).
notify.FooterLinks is the single eligibility point: preferences + help
whenever NEXTAUTH_URL is set; unsubscribe ONLY when the recipient is the
owner's own account email — third-party channel addresses and agent
recipients never receive a link that changes someone else's settings,
and transactional mail (password reset) never carries unsubscribe.
Owner-addressed mail also carries List-Unsubscribe +
List-Unsubscribe-Post headers.
Verified: Go build/vet/unit green; new TestUnsubscribeFlow integration
test pins footer + headers, blast radius (slack channel and third-party
task survive), one-click + JSON paths, idempotence, tampered tokens and
the third-party/transactional exclusions; notifications, phase-2,
password-reset and agent suites green; web lint/SSR-lint/build 0 errors;
full browser regressions pass; live smoke of /help, /unsubscribe states,
the settings deep-link auth gate and bad-token 400 through the proxy.
220 lines
7.3 KiB
Go
220 lines
7.3 KiB
Go
package mailer
|
|
|
|
import (
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
// The renderer and the embedded design file are one unit: these tests pin
|
|
// the anchors so a template edit that drops a marker fails loudly instead
|
|
// of silently shipping unfilled placeholders.
|
|
|
|
func TestRenderFull(t *testing.T) {
|
|
html, err := Render("Reset your Superbrain password", Content{
|
|
Preheader: "Choose a new password.",
|
|
Headline: "Reset your password",
|
|
FirstName: "Alice",
|
|
Body: "First paragraph.\nSecond paragraph.",
|
|
CTAURL: "https://superbrain.test/reset-password?token=abc",
|
|
CTALabel: "Choose a new password",
|
|
Note: "The link expires in 30 minutes.",
|
|
SignOff: "Cheers,",
|
|
PreferencesURL: "https://superbrain.test/settings",
|
|
UnsubscribeURL: "https://superbrain.test/unsubscribe",
|
|
HelpURL: "https://superbrain.test/help",
|
|
CompanyAddress: "1 Example Way, Testville",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, want := range []string{
|
|
"Reset your password", "Hi Alice,", "First paragraph.", "Second paragraph.",
|
|
"Choose a new password", "https://superbrain.test/reset-password?token=abc",
|
|
"v:roundrect", "If the button doesn't work", // Outlook fallback + plain link
|
|
"The link expires in 30 minutes.", "Cheers,", "The Superbrain team",
|
|
"Email preferences", "Unsubscribe", "Help center", "1 Example Way, Testville",
|
|
"Choose a new password.", // preheader
|
|
} {
|
|
if !strings.Contains(html, want) {
|
|
t.Errorf("rendered mail missing %q", want)
|
|
}
|
|
}
|
|
if strings.Contains(html, "{{") {
|
|
t.Errorf("unfilled placeholder remains:\n%s", html)
|
|
}
|
|
}
|
|
|
|
func TestRenderMinimal(t *testing.T) {
|
|
html, err := Render("Bookmark due: Due soon", Content{
|
|
Headline: "Bookmark due: Due soon",
|
|
Body: "Due soon is due in 6 hours.",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(html, "{{") {
|
|
t.Errorf("unfilled placeholder remains:\n%s", html)
|
|
}
|
|
// Optional sections are removed, not stubbed.
|
|
for _, absent := range []string{
|
|
"v:roundrect", "If the button doesn't work", // CTA + fallback rows
|
|
`<td class="bg-note`, // note block
|
|
"Email preferences", // footer links paragraph
|
|
"Superbrain, ", // address line
|
|
"Hi ,", // broken greeting
|
|
} {
|
|
if strings.Contains(html, absent) {
|
|
t.Errorf("minimal render still contains %q", absent)
|
|
}
|
|
}
|
|
for _, want := range []string{"Hi,", "Bookmark due: Due soon", "Due soon is due in 6 hours.", "Thanks,"} {
|
|
if !strings.Contains(html, want) {
|
|
t.Errorf("minimal render missing %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRenderEscaping(t *testing.T) {
|
|
html, err := Render(`Subject "<x>"`, Content{
|
|
Headline: `<script>alert(1)</script>`,
|
|
FirstName: `Ann<script>`,
|
|
Body: `<img src=x onerror=alert(1)> & "quotes"`,
|
|
CTAURL: `https://x.test/"onmouseover="alert(1)`,
|
|
Note: `<b>note</b>`,
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, absent := range []string{
|
|
"<script>alert(1)</script>", "Ann<script>", "<img src=x", `"onmouseover="`, "<b>note</b>",
|
|
} {
|
|
if strings.Contains(html, absent) {
|
|
t.Errorf("unescaped value %q survived rendering", absent)
|
|
}
|
|
}
|
|
for _, want := range []string{"<script>", "&", """} {
|
|
if !strings.Contains(html, want) {
|
|
t.Errorf("expected escaped form %q", want)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestRenderBodyParagraphs(t *testing.T) {
|
|
html, err := Render("s", Content{Headline: "h", Body: "one\n\n two \nthree\n"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
for _, want := range []string{
|
|
`<p style="margin:0 0 16px 0;">one</p>`,
|
|
`<p style="margin:0 0 16px 0;">two</p>`,
|
|
`<p style="margin:0;">three</p>`, // last paragraph loses the bottom margin
|
|
} {
|
|
if !strings.Contains(html, want) {
|
|
t.Errorf("missing paragraph %q", want)
|
|
}
|
|
}
|
|
// Empty body: greeting survives, no placeholder paragraphs remain.
|
|
html, err = Render("s", Content{Headline: "h"})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if strings.Contains(html, "{{") || !strings.Contains(html, "Hi,") {
|
|
t.Errorf("empty-body render broken:\n%s", html)
|
|
}
|
|
}
|
|
|
|
func TestRenderDefaults(t *testing.T) {
|
|
html, err := Render("Fallback subject", Content{
|
|
Body: strings.Repeat("x", 120),
|
|
CTAURL: "https://superbrain.test/",
|
|
})
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if !strings.Contains(html, "<h1") || !strings.Contains(html, "Fallback subject") {
|
|
t.Error("headline must fall back to the subject")
|
|
}
|
|
if !strings.Contains(html, "Open Superbrain") {
|
|
t.Error("CTA label must default to Open Superbrain")
|
|
}
|
|
// Preheader defaults to the first paragraph, truncated to 90 runes.
|
|
pre := html[strings.Index(html, `opacity:0;">`)+len(`opacity:0;">`):]
|
|
pre = strings.TrimSpace(pre[:strings.Index(pre, "͏")])
|
|
if pre != strings.Repeat("x", 90) {
|
|
t.Errorf("preheader = %q, want 90 x's", pre)
|
|
}
|
|
}
|
|
|
|
func TestRenderTemplateDrift(t *testing.T) {
|
|
saved := emailTemplate
|
|
defer func() { emailTemplate = saved }()
|
|
emailTemplate = "<html>{{headline}} only</html>"
|
|
if _, err := Render("s", Content{Headline: "h"}); err == nil {
|
|
t.Fatal("Render must error when the template loses its anchors")
|
|
}
|
|
}
|
|
|
|
func TestBuildMessagePlain(t *testing.T) {
|
|
msg := buildMessage("from@x.test", "to@x.test", "hi\r\nBcc: evil@x.test", "body", "", "")
|
|
if strings.Contains(msg, "\r\nBcc:") {
|
|
t.Fatal("header injection via subject survived")
|
|
}
|
|
if !strings.Contains(msg, "Content-Type: text/plain; charset=UTF-8") {
|
|
t.Fatal("plain message lost its content type")
|
|
}
|
|
if strings.Contains(msg, "multipart") {
|
|
t.Fatal("plain message must not be multipart")
|
|
}
|
|
if strings.Contains(msg, "List-Unsubscribe") {
|
|
t.Fatal("plain message without an unsubscribe URL must not carry the headers")
|
|
}
|
|
}
|
|
|
|
func TestBuildMessageMultipart(t *testing.T) {
|
|
text := "plain alternative\nwith a link https://x.test/"
|
|
html := "<html>\n.dot line\n</html>"
|
|
msg := buildMessage("from@x.test", "to@x.test", "s", text, html, "")
|
|
if !strings.Contains(msg, "Content-Type: multipart/alternative; boundary=\"sb-") {
|
|
t.Fatalf("missing multipart header:\n%s", msg)
|
|
}
|
|
boundary := msg[strings.Index(msg, `boundary="`)+len(`boundary="`):]
|
|
boundary = boundary[:strings.Index(boundary, `"`)]
|
|
if n := strings.Count(msg, "--"+boundary); n != 3 {
|
|
t.Fatalf("boundary appears %d times, want 3 (two parts + close)", n)
|
|
}
|
|
if !strings.HasSuffix(msg, "--"+boundary+"--\r\n") {
|
|
t.Fatal("multipart close delimiter missing")
|
|
}
|
|
if strings.Index(msg, "text/plain") > strings.Index(msg, "text/html") {
|
|
t.Fatal("RFC 2046: the plain alternative must come first")
|
|
}
|
|
if !strings.Contains(msg, "\r\n..dot line") {
|
|
t.Fatal("HTML part must be dot-stuffed")
|
|
}
|
|
if !strings.Contains(msg, smtpBody(text)) {
|
|
t.Fatal("plain alternative missing")
|
|
}
|
|
}
|
|
|
|
func TestSMTPBody(t *testing.T) {
|
|
got := smtpBody(".start\nmid\r\n.dot\n")
|
|
want := "..start\r\nmid\r\n..dot\r\n"
|
|
if got != want {
|
|
t.Fatalf("smtpBody = %q, want %q", got, want)
|
|
}
|
|
}
|
|
|
|
func TestBuildMessageListUnsubscribe(t *testing.T) {
|
|
msg := buildMessage("from@x.test", "to@x.test", "s", "text", "<html></html>",
|
|
"https://x.test/api/notifications/unsubscribe?token=abc\r\nBcc: evil@x.test")
|
|
if !strings.Contains(msg, "List-Unsubscribe: <https://x.test/api/notifications/unsubscribe?token=abc") {
|
|
t.Fatalf("List-Unsubscribe header missing:\n%s", msg)
|
|
}
|
|
if !strings.Contains(msg, "List-Unsubscribe-Post: List-Unsubscribe=One-Click\r\n") {
|
|
t.Fatal("RFC 8058 one-click header missing")
|
|
}
|
|
if strings.Contains(msg, "\r\nBcc:") {
|
|
t.Fatal("header injection via the unsubscribe URL survived")
|
|
}
|
|
}
|