Per feature/3-f0-f1-validation's own gap analysis (commit 097fa37):
- InboundMessageRepository.Persist now updates folders.unread_count,
folders.total_count, and mailboxes.used_bytes in the same transaction
as the message insert (PLAN.md section 6.1 "++counters" was a no-op
before this).
- MailboxRecord carries QuotaBytes/UsedBytes; ResolveRecipient rejects
RCPT with 452 4.2.2 when a target mailbox has already reached its
quota (used_bytes >= quota_bytes), before DATA - never silently
accepting mail into a full mailbox.
- MailboxRepository.ResolveDeliveryTargets tries a direct mailbox match
first, then falls back to the aliases table (exact source_address
match takes precedence over a domain catch-all). One RCPT TO can now
fan out to multiple mailboxes; alias destinations that don't resolve
to an internal active mailbox are silently skipped, since outbound/
external forwarding doesn't exist yet. postmaster@/abuse@/dmarc@-style
aliases now actually deliver.
The E2E test's counters_updated subtest flips from a documented KNOWN
GAP (t.Skipf) to a hard assertion now that it passes, and gains two new
subtests (quota_rejected_when_mailbox_full, alias_resolves_to_destination_mailbox)
covering the other two fixes end-to-end. New unit and Postgres
integration test coverage added for all three fixes at every layer.
Verified: full suite green on Windows (unit + E2E via embedded
Postgres, no Docker needed) and via WSL Docker against the real
docker-compose Postgres (all infrastructure/postgres and diskstorage
integration tests, not just E2E). lang-lint clean.
174 lines
5.8 KiB
Go
174 lines
5.8 KiB
Go
package usecase
|
|
|
|
import (
|
|
"bytes"
|
|
"context"
|
|
"errors"
|
|
"io"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"lambdamail/protocols/internal/application/port"
|
|
)
|
|
|
|
type fakeMailboxRepository struct {
|
|
byAddress map[string]port.MailboxRecord
|
|
}
|
|
|
|
func (f *fakeMailboxRepository) FindActiveByAddress(_ context.Context, address string) (*port.MailboxRecord, error) {
|
|
rec, ok := f.byAddress[address]
|
|
if !ok {
|
|
return nil, nil
|
|
}
|
|
return &rec, nil
|
|
}
|
|
|
|
func (f *fakeMailboxRepository) ResolveDeliveryTargets(_ context.Context, address string) ([]port.MailboxRecord, error) {
|
|
rec, ok := f.byAddress[address]
|
|
if !ok {
|
|
return []port.MailboxRecord{}, nil
|
|
}
|
|
return []port.MailboxRecord{rec}, nil
|
|
}
|
|
|
|
func TestUseCase_ResolveRecipient_ReturnsRecordForActiveMailbox(t *testing.T) {
|
|
mailboxID := uuid.New()
|
|
mailboxes := &fakeMailboxRepository{byAddress: map[string]port.MailboxRecord{
|
|
"user@example.test": {ID: mailboxID, MaxMessageBytes: 1000, QuotaBytes: 1000},
|
|
}}
|
|
uc := NewProcessInboundEmailUseCase(mailboxes, nil, nil)
|
|
|
|
targets, err := uc.ResolveRecipient(context.Background(), "user@example.test")
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
if len(targets) != 1 || targets[0].ID != mailboxID {
|
|
t.Errorf("targets = %+v, want single record with ID %v", targets, mailboxID)
|
|
}
|
|
}
|
|
|
|
func TestUseCase_ResolveRecipient_ReturnsErrRecipientNotFoundForUnknownAddress(t *testing.T) {
|
|
mailboxes := &fakeMailboxRepository{byAddress: map[string]port.MailboxRecord{}}
|
|
uc := NewProcessInboundEmailUseCase(mailboxes, nil, nil)
|
|
|
|
_, err := uc.ResolveRecipient(context.Background(), "nobody@example.test")
|
|
if !errors.Is(err, ErrRecipientNotFound) {
|
|
t.Fatalf("error = %v, want ErrRecipientNotFound", err)
|
|
}
|
|
}
|
|
|
|
func TestUseCase_ResolveRecipient_ReturnsErrMailboxQuotaExceededWhenFull(t *testing.T) {
|
|
mailboxes := &fakeMailboxRepository{byAddress: map[string]port.MailboxRecord{
|
|
"full@example.test": {ID: uuid.New(), QuotaBytes: 1000, UsedBytes: 1000},
|
|
}}
|
|
uc := NewProcessInboundEmailUseCase(mailboxes, nil, nil)
|
|
|
|
_, err := uc.ResolveRecipient(context.Background(), "full@example.test")
|
|
if !errors.Is(err, ErrMailboxQuotaExceeded) {
|
|
t.Fatalf("error = %v, want ErrMailboxQuotaExceeded", err)
|
|
}
|
|
}
|
|
|
|
func TestUseCase_ResolveRecipient_ReturnsErrMailboxQuotaExceededWhenAnyFanOutTargetIsFull(t *testing.T) {
|
|
mailboxes := &fakeAliasFanOutRepository{targets: []port.MailboxRecord{
|
|
{ID: uuid.New(), QuotaBytes: 1000, UsedBytes: 0},
|
|
{ID: uuid.New(), QuotaBytes: 1000, UsedBytes: 1000},
|
|
}}
|
|
uc := NewProcessInboundEmailUseCase(mailboxes, nil, nil)
|
|
|
|
_, err := uc.ResolveRecipient(context.Background(), "alias@example.test")
|
|
if !errors.Is(err, ErrMailboxQuotaExceeded) {
|
|
t.Fatalf("error = %v, want ErrMailboxQuotaExceeded when any fan-out target is full", err)
|
|
}
|
|
}
|
|
|
|
// fakeAliasFanOutRepository simulates an alias resolving to multiple mailboxes.
|
|
type fakeAliasFanOutRepository struct {
|
|
targets []port.MailboxRecord
|
|
}
|
|
|
|
func (f *fakeAliasFanOutRepository) FindActiveByAddress(_ context.Context, _ string) (*port.MailboxRecord, error) {
|
|
return nil, nil
|
|
}
|
|
|
|
func (f *fakeAliasFanOutRepository) ResolveDeliveryTargets(_ context.Context, _ string) ([]port.MailboxRecord, error) {
|
|
return f.targets, nil
|
|
}
|
|
|
|
type stubBlobStorage struct {
|
|
ref port.BlobRef
|
|
err error
|
|
}
|
|
|
|
func (s *stubBlobStorage) Store(_ context.Context, r io.Reader) (port.BlobRef, error) {
|
|
_, _ = io.Copy(io.Discard, r)
|
|
return s.ref, s.err
|
|
}
|
|
|
|
type recordingMessageRepository struct {
|
|
persisted []port.PersistInboundMessageInput
|
|
nextUID int64
|
|
}
|
|
|
|
func (r *recordingMessageRepository) Persist(_ context.Context, input port.PersistInboundMessageInput) (int64, error) {
|
|
r.persisted = append(r.persisted, input)
|
|
r.nextUID++
|
|
return r.nextUID, nil
|
|
}
|
|
|
|
func TestUseCase_Handle_StoresBlobOnceAndPersistsOncePerRecipient(t *testing.T) {
|
|
blobRef := port.BlobRef{ID: uuid.New(), SHA256: "abc123", SizeBytes: 42}
|
|
blobs := &stubBlobStorage{ref: blobRef}
|
|
messages := &recordingMessageRepository{}
|
|
uc := NewProcessInboundEmailUseCase(nil, blobs, messages)
|
|
|
|
recipients := []port.MailboxRecord{
|
|
{ID: uuid.New(), MaxMessageBytes: 1000},
|
|
{ID: uuid.New(), MaxMessageBytes: 1000},
|
|
}
|
|
addresses := []string{"a@example.test", "b@example.test"}
|
|
|
|
err := uc.Handle(context.Background(), ProcessInboundEmailInput{
|
|
Sender: "sender@example.test",
|
|
Recipients: recipients,
|
|
RecipientAddresses: addresses,
|
|
Body: bytes.NewBufferString("Subject: hi\r\n\r\nbody"),
|
|
})
|
|
if err != nil {
|
|
t.Fatalf("unexpected error: %v", err)
|
|
}
|
|
|
|
if len(messages.persisted) != 2 {
|
|
t.Fatalf("persisted %d messages, want 2 (one per recipient, blob stored once)", len(messages.persisted))
|
|
}
|
|
for i, p := range messages.persisted {
|
|
if p.Blob.ID != blobRef.ID {
|
|
t.Errorf("persisted[%d].Blob.ID = %v, want %v (same blob for every recipient)", i, p.Blob.ID, blobRef.ID)
|
|
}
|
|
if p.MailboxID != recipients[i].ID {
|
|
t.Errorf("persisted[%d].MailboxID = %v, want %v", i, p.MailboxID, recipients[i].ID)
|
|
}
|
|
if p.RecipientAddress != addresses[i] {
|
|
t.Errorf("persisted[%d].RecipientAddress = %q, want %q", i, p.RecipientAddress, addresses[i])
|
|
}
|
|
if p.SPFResult != "none" || p.DKIMResult != "none" || p.DMARCResult != "none" {
|
|
t.Errorf("persisted[%d] auth results = %q/%q/%q, want \"none\"/\"none\"/\"none\" (verification lands in a later sub-project)", i, p.SPFResult, p.DKIMResult, p.DMARCResult)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestUseCase_Handle_PropagatesBlobStorageError(t *testing.T) {
|
|
blobs := &stubBlobStorage{err: errors.New("disk full")}
|
|
uc := NewProcessInboundEmailUseCase(nil, blobs, &recordingMessageRepository{})
|
|
|
|
err := uc.Handle(context.Background(), ProcessInboundEmailInput{
|
|
Sender: "sender@example.test",
|
|
Recipients: []port.MailboxRecord{{ID: uuid.New()}},
|
|
RecipientAddresses: []string{"a@example.test"},
|
|
Body: bytes.NewBufferString("x"),
|
|
})
|
|
if err == nil {
|
|
t.Fatal("expected error when blob storage fails, got nil")
|
|
}
|
|
}
|