package tasks import ( "strings" "testing" "github.com/google/uuid" "github.com/warmbly/warmbly/internal/models" ) func TestUnsubscribeLinkIsNeverTracked(t *testing.T) { body := `
Hi pricing and unsubscribe
` out, links := WrapLinksForTracking(body, uuid.New(), uuid.New(), "t.example.com") if len(links) != 1 || links[0].Destination != "https://acme.com/pricing" { t.Fatalf("expected only the pricing link to be ticketed, got %+v", links) } if !strings.Contains(out, `href="https://api.example.com/unsubscribe/abc123"`) { t.Fatalf("unsubscribe link was rewritten: %s", out) } } func TestOptOutFooter(t *testing.T) { text := models.UnsubscribeSettings{Mode: models.UnsubscribeModeText, Text: "Reply and I'll stop."} h, p := appendOptOut("Hi
", "Hi", text, "") if !strings.Contains(h, "Reply and I'll stop.") || !strings.HasSuffix(p, "Reply and I'll stop.") { t.Fatalf("text footer missing: %q / %q", h, p) } link := models.UnsubscribeSettings{Mode: models.UnsubscribeModeLink, Text: "fallback", LinkIntro: "Not interested?", LinkText: "Unsubscribe"} h, p = appendOptOut("Hi
", "Hi", link, "https://api.example.com/unsubscribe/tok") if !strings.Contains(h, `href="https://api.example.com/unsubscribe/tok"`) || !strings.Contains(p, "Unsubscribe: https://api.example.com/unsubscribe/tok") { t.Fatalf("link footer missing: %q / %q", h, p) } // No link to mint: link mode degrades to the text line, never to nothing. h, _ = appendOptOut("Hi
", "Hi", link, "") if !strings.Contains(h, "fallback") { t.Fatalf("link mode without a link should fall back to text: %q", h) } h, p = appendOptOut("Hi
", "Hi", models.UnsubscribeSettings{Mode: models.UnsubscribeModeOff}, "x") if h != "Hi
" || p != "Hi" { t.Fatalf("off mode changed the body: %q / %q", h, p) } } func TestLinkifyUnsubscribeURL(t *testing.T) { const url = "https://api.example.com/unsubscribe/tok123" // The variable chip the composer saves: a bare URL in a text node. got := linkifyUnsubscribeURL(`Not interested? `+url+`
`, url, "Unsubscribe") want := `Not interested? Unsubscribe
` if got != want { t.Fatalf("bare token not linkified:\n got %s\nwant %s", got, want) } // The workspace's own link wording is what the anchor says. if got := linkifyUnsubscribeURL(""+url+"
", url, "Opt out"); !strings.Contains(got, `>Opt out`) { t.Fatalf("link text ignored: %s", got) } if got := linkifyUnsubscribeURL(""+url+"
", url, " "); !strings.Contains(got, ">Unsubscribe") { t.Fatalf("blank link text should fall back to the default: %s", got) } if got := linkifyUnsubscribeURL(""+url+"
", url, `Bill & "Ted"`); !strings.Contains(got, `>Bill & "Ted"`) { t.Fatalf("link text not escaped: %s", got) } // An author's own anchor is left exactly as written: no second link, no // rewritten href. own := `` if got := linkifyUnsubscribeURL(own, url, "Unsubscribe"); got != own { t.Fatalf("author's anchor was rewritten: %s", got) } // The URL as the text of an existing anchor becomes its label rather than // a nested link. nested := `` + url + `` if got := linkifyUnsubscribeURL(nested, url, "Unsubscribe"); got != `Unsubscribe` { t.Fatalf("URL inside an anchor should become the label: %s", got) } // Nothing to do cases. if got := linkifyUnsubscribeURL("Hi
", url, "Unsubscribe"); got != "Hi
" { t.Fatalf("body without the link changed: %s", got) } if got := linkifyUnsubscribeURL(""+url+"
", "", "Unsubscribe"); got != ""+url+"
" { t.Fatalf("no link to mint should be a no-op: %s", got) } if got := linkifyUnsubscribeURL("", url, "Unsubscribe"); got != "" { t.Fatalf("empty body changed: %q", got) } // A ">" inside a quoted attribute closes nothing. Reading one as the end of // the tag turned the href that followed it into a dead link. quoted := `read this` if got := linkifyUnsubscribeURL(quoted, url, "Unsubscribe"); got != quoted { t.Fatalf("a quoted \">\" broke the tag scan: %s", got) } if got := linkifyUnsubscribeURL(`Bye. `+url+`
`, url, "Unsubscribe"); got != `Bye. Unsubscribe
` { t.Fatalf("text after a quoted \">\" should still linkify: %s", got) } // A malformed body (unterminated tag) is copied through, never truncated. if got := linkifyUnsubscribeURL("hihi closes , so what follows is
// loose text again.
mixed := `x or ` + url
if got := linkifyUnsubscribeURL(mixed, url, "Unsubscribe"); got != `x or Unsubscribe` {
t.Fatalf("mixed-case anchors mishandled: %s", got)
}
}
func TestLinkifiedUnsubscribeLinkSurvivesTracking(t *testing.T) {
const url = "https://api.example.com/unsubscribe/tok123"
body := linkifyUnsubscribeURL(` Or `+url+` to stop. pricing Bye. `+url+` `+url+` Explore the work. Karan Barad
`
// In send order: the open-tracking pixel goes on first, at the end of the
// document. It is a child of from then on, and reading it as
// layout would stop the descent there and put everything after it back
// outside the card.
body = AddOpenTrackingPixel(body, uuid.New(), "t.example.com")
withSig := AddSignature(body, "` +
` ` +
` Built by Ayonix Design