feat(mail): add MailSender port and explicit provider selection
This commit is contained in:
1 parent
e7d0d92ef9
commit
6153887eb5
26 files changed
+1253
-29
No files matched your search
Whitespace-only changes.
@@ -0,0 +1,30 @@
|
||||
// Package disabled provides the mail transport for EMAIL_PROVIDER=disabled.
|
||||
package disabled
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
// Sender sends nothing. Every valid message is reported as
|
||||
// outbound.ErrMailChannelUnavailable, never as an accepted or delivered
|
||||
// message, so a caller cannot mistake the disabled mode for a delivery.
|
||||
type Sender struct{}
|
||||
|
||||
var _ outbound.MailSender = (*Sender)(nil)
|
||||
|
||||
func NewSender() *Sender {
|
||||
return &Sender{}
|
||||
}
|
||||
|
||||
// Send validates the message first so that local runs without a transport
|
||||
// still expose invalid messages, then reports the channel as unavailable. It
|
||||
// logs nothing and keeps no copy of the recipient or the content.
|
||||
func (sender *Sender) Send(_ context.Context, mail outbound.OutgoingMail) (outbound.SendResult, error) {
|
||||
if err := mail.Validate(); err != nil {
|
||||
return outbound.SendResult{}, fmt.Errorf("Sender.Send - validate mail: %w", err)
|
||||
}
|
||||
return outbound.SendResult{}, fmt.Errorf("Sender.Send - EMAIL_PROVIDER is disabled: %w", outbound.ErrMailChannelUnavailable)
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
package disabled_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"lincodex/backend/internal/adapter/secondary/mail/disabled"
|
||||
"lincodex/backend/internal/domain"
|
||||
"lincodex/backend/internal/port/outbound"
|
||||
)
|
||||
|
||||
const testRecipient = "candidate@example.com"
|
||||
|
||||
func TestSenderReportsChannelUnavailableAndNeverAcceptance(t *testing.T) {
|
||||
recipient, err := domain.NewEmailAddress(testRecipient)
|
||||
if err != nil {
|
||||
t.Fatalf("NewEmailAddress() error = %v", err)
|
||||
}
|
||||
mail := outbound.OutgoingMail{
|
||||
To: recipient,
|
||||
Subject: "Confirm your address",
|
||||
TextBody: "verification link with raw-token-value",
|
||||
IdempotencyKey: "email_verification/1",
|
||||
}
|
||||
canceled, cancel := context.WithCancel(context.Background())
|
||||
cancel()
|
||||
|
||||
for name, ctx := range map[string]context.Context{"active context": context.Background(), "canceled context": canceled} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
result, err := disabled.NewSender().Send(ctx, mail)
|
||||
if !errors.Is(err, outbound.ErrMailChannelUnavailable) {
|
||||
t.Fatalf("Send() error = %v, want ErrMailChannelUnavailable", err)
|
||||
}
|
||||
// Delivery and email verification code reads Outcome and
|
||||
// ProviderMessageID; neither may suggest that a provider took the
|
||||
// message.
|
||||
if result != (outbound.SendResult{}) || result.Outcome == outbound.MailOutcomeAccepted || result.ProviderMessageID != "" {
|
||||
t.Fatalf("Send() result = %+v, want the zero result", result)
|
||||
}
|
||||
if strings.Contains(err.Error(), testRecipient) || strings.Contains(err.Error(), "raw-token-value") {
|
||||
t.Fatalf("Send() error exposes message content: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestSenderRejectsInvalidMailBeforeReportingUnavailability(t *testing.T) {
|
||||
result, err := disabled.NewSender().Send(context.Background(), outbound.OutgoingMail{})
|
||||
if !errors.Is(err, outbound.ErrInvalidOutgoingMail) || errors.Is(err, outbound.ErrMailChannelUnavailable) {
|
||||
t.Fatalf("Send() error = %v, want only ErrInvalidOutgoingMail", err)
|
||||
}
|
||||
if result != (outbound.SendResult{}) {
|
||||
t.Fatalf("Send() result = %+v, want the zero result", result)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user