99 lines
3.8 KiB
Go
99 lines
3.8 KiB
Go
package outbound_test
|
|
|
|
import (
|
|
"bytes"
|
|
"errors"
|
|
"fmt"
|
|
"log/slog"
|
|
"strings"
|
|
"testing"
|
|
|
|
"lincodex/backend/internal/domain"
|
|
"lincodex/backend/internal/port/outbound"
|
|
)
|
|
|
|
const (
|
|
testRecipient = "Private.Person@example.com"
|
|
testSubject = "Confirm your address"
|
|
testTextBody = "Open https://lincodex.example/verify?token=raw-token-value"
|
|
testHTMLBody = "<a href=\"https://lincodex.example/verify?token=raw-token-value\">Confirm</a>"
|
|
)
|
|
|
|
func validOutgoingMail(t *testing.T) outbound.OutgoingMail {
|
|
t.Helper()
|
|
recipient, err := domain.NewEmailAddress(testRecipient)
|
|
if err != nil {
|
|
t.Fatalf("NewEmailAddress() error = %v", err)
|
|
}
|
|
return outbound.OutgoingMail{
|
|
To: recipient,
|
|
Subject: testSubject,
|
|
TextBody: testTextBody,
|
|
HTMLBody: testHTMLBody,
|
|
IdempotencyKey: "email_verification/0190d3f4-0000-7000-8000-000000000001",
|
|
}
|
|
}
|
|
|
|
func TestOutgoingMailValidateAcceptsOneBody(t *testing.T) {
|
|
textOnly := validOutgoingMail(t)
|
|
textOnly.HTMLBody = ""
|
|
htmlOnly := validOutgoingMail(t)
|
|
htmlOnly.TextBody = ""
|
|
longestKey := validOutgoingMail(t)
|
|
longestKey.IdempotencyKey = strings.Repeat("k", 256)
|
|
for name, mail := range map[string]outbound.OutgoingMail{"text only": textOnly, "HTML only": htmlOnly, "256-character key": longestKey} {
|
|
t.Run(name, func(t *testing.T) {
|
|
if err := mail.Validate(); err != nil {
|
|
t.Fatalf("Validate() error = %v", err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOutgoingMailValidateRejectsUnsendableMail(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
change func(*outbound.OutgoingMail)
|
|
}{
|
|
{name: "missing recipient", change: func(mail *outbound.OutgoingMail) { mail.To = domain.EmailAddress{} }},
|
|
{name: "blank subject", change: func(mail *outbound.OutgoingMail) { mail.Subject = " " }},
|
|
{name: "subject header injection", change: func(mail *outbound.OutgoingMail) { mail.Subject = testSubject + "\r\nBcc: other@example.com" }},
|
|
{name: "no body", change: func(mail *outbound.OutgoingMail) { mail.TextBody, mail.HTMLBody = " ", "" }},
|
|
{name: "missing idempotency key", change: func(mail *outbound.OutgoingMail) { mail.IdempotencyKey = "" }},
|
|
{name: "idempotency key too long", change: func(mail *outbound.OutgoingMail) { mail.IdempotencyKey = strings.Repeat("k", 257) }},
|
|
{name: "idempotency key with space", change: func(mail *outbound.OutgoingMail) { mail.IdempotencyKey = "email verification" }},
|
|
{name: "idempotency key non-ASCII", change: func(mail *outbound.OutgoingMail) { mail.IdempotencyKey = "письмо/1" }},
|
|
}
|
|
for _, test := range tests {
|
|
t.Run(test.name, func(t *testing.T) {
|
|
mail := validOutgoingMail(t)
|
|
test.change(&mail)
|
|
err := mail.Validate()
|
|
if !errors.Is(err, outbound.ErrInvalidOutgoingMail) {
|
|
t.Fatalf("Validate() error = %v, want ErrInvalidOutgoingMail", err)
|
|
}
|
|
if !strings.HasPrefix(err.Error(), "OutgoingMail.Validate - ") {
|
|
t.Fatalf("Validate() error = %q, want the OutgoingMail.Validate location", err)
|
|
}
|
|
for _, private := range []string{testRecipient, testSubject, "raw-token-value", "other@example.com"} {
|
|
if strings.Contains(err.Error(), private) {
|
|
t.Fatalf("Validate() error exposes message content: %v", err)
|
|
}
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestOutgoingMailIsRedacted(t *testing.T) {
|
|
mail := validOutgoingMail(t)
|
|
wrapper := struct{ Mail outbound.OutgoingMail }{Mail: mail}
|
|
var logs bytes.Buffer
|
|
slog.New(slog.NewJSONHandler(&logs, nil)).Info("redaction", "mail", mail, slog.Group("nested", "mail", mail))
|
|
formatted := fmt.Sprintf("%v %+v %#v %s %q %x %d %+v %#v", mail, mail, mail, mail, mail, mail, mail, wrapper, wrapper) + logs.String()
|
|
for _, private := range []string{"Private.Person", "private.person", testSubject, "raw-token-value", "Confirm"} {
|
|
if strings.Contains(formatted, private) {
|
|
t.Fatalf("formatted output exposes message content: %s", formatted)
|
|
}
|
|
}
|
|
}
|