package tasks import ( "context" "errors" "strings" "testing" "time" "github.com/google/uuid" "github.com/warmbly/warmbly/internal/app/unsublink" "github.com/warmbly/warmbly/internal/models" "github.com/warmbly/warmbly/internal/repository" ) type fakeUnsubTickets struct { err error stored string calls int } func (f *fakeUnsubTickets) Mint(_ context.Context, token string, _, _, _ uuid.UUID, _ time.Time) (string, error) { f.calls++ if f.err != nil { return "", f.err } if f.stored != "" { return f.stored, nil } return token, nil } func (f *fakeUnsubTickets) Resolve(context.Context, string) (*repository.UnsubscribeTicket, error) { return nil, nil } func TestMintUnsubscribeLinkPrefersAStoredTicket(t *testing.T) { tickets := &fakeUnsubTickets{} s := &tasksService{ unsubLinks: unsublink.New("secret", "https://api.example.com"), unsubTickets: tickets, } got := s.mintUnsubscribeLink(context.Background(), "https://t.customer.com", uuid.New(), uuid.New(), uuid.New()) if tickets.calls != 1 { t.Fatalf("minted %d tickets, want 1", tickets.calls) } token := strings.TrimPrefix(got, "https://t.customer.com"+unsublink.Path) if token == got || !unsublink.IsTicket(token) { t.Fatalf("not a ticket link: %q", got) } } // A follow-up reuses the address the first email carried, so a recipient who // kept the first message and clicks its link months later reaches the same // place as one clicking the latest. func TestMintUnsubscribeLinkReturnsTheStoredToken(t *testing.T) { tickets := &fakeUnsubTickets{stored: "firstsend0000000000000"} s := &tasksService{ unsubLinks: unsublink.New("secret", "https://api.example.com"), unsubTickets: tickets, } got := s.mintUnsubscribeLink(context.Background(), "", uuid.New(), uuid.New(), uuid.New()) if want := "https://api.example.com" + unsublink.Path + "firstsend0000000000000"; got != want { t.Fatalf("got %q, want %q", got, want) } } // The store failing must cost the recipient a long address, never the opt-out // itself: a signed link needs no row and works the same. func TestMintUnsubscribeLinkFallsBackToTheSignedLink(t *testing.T) { signer := unsublink.New("secret", "https://api.example.com") s := &tasksService{ unsubLinks: signer, unsubTickets: &fakeUnsubTickets{err: errors.New("postgres is down")}, } org, campaign, contact := uuid.New(), uuid.New(), uuid.New() got := s.mintUnsubscribeLink(context.Background(), "", org, campaign, contact) token := strings.TrimPrefix(got, "https://api.example.com"+unsublink.Path) if token == got { t.Fatalf("unexpected link %q", got) } claims, err := signer.Verify(token, time.Now()) if err != nil { t.Fatalf("the fallback link does not verify: %v", err) } if claims.OrgID != org || claims.CampaignID != campaign || claims.ContactID != contact { t.Fatalf("the fallback link names the wrong recipient: %+v", claims) } } // No store at all (a deployment wired without one) is the same fallback. func TestMintUnsubscribeLinkWithoutAStore(t *testing.T) { s := &tasksService{unsubLinks: unsublink.New("secret", "https://api.example.com")} got := s.mintUnsubscribeLink(context.Background(), "", uuid.New(), uuid.New(), uuid.New()) if token := strings.TrimPrefix(got, "https://api.example.com"+unsublink.Path); unsublink.IsTicket(token) { t.Fatalf("minted a ticket with no store: %q", got) } } // visibleText is the HTML with every tag removed: what a reader sees, as // opposed to what an href holds. mailhtml.ToPlainText is not usable here // because it deliberately renders a link as "label (url)". func visibleText(html string) string { var b strings.Builder depth := 0 for _, r := range html { switch { case r == '<': depth++ case r == '>' && depth > 0: depth-- case depth == 0: b.WriteRune(r) } } return b.String() } // The HTML part must never show the opt-out address as text. Issue #498 was // reported against the plain-text alternative, where an address has nowhere // to hide; this is the guard that the HTML half never regresses into the same // thing, in each of the three shapes a step can carry an opt-out. func TestHTMLEmailNeverShowsTheOptOutAddressAsText(t *testing.T) { const url = "https://t.acme.com/unsubscribe/Xk3mP9qR2tLwAb7dEfGhIj" link := models.UnsubscribeSettings{Mode: models.UnsubscribeModeLink}.Effective("") account := &models.Email{SignatureSync: true, SignatureHTML: "
Karan
", SignaturePlain: "Karan"} for _, tc := range []struct { name, body, wantLabel string }{ { name: "footer only", body: `I ended up trying the idea I had for your ad.
Not interested? {{.UnsubscribeLink}}
Quick one.