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.
89 lines
2.4 KiB
Go
89 lines
2.4 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/jackc/pgx/v5"
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"lambdamail/protocols/internal/application/port"
|
|
)
|
|
|
|
// MailboxRepository implements port.MailboxRepository against Postgres.
|
|
type MailboxRepository struct {
|
|
pool *pgxpool.Pool
|
|
}
|
|
|
|
func NewMailboxRepository(pool *pgxpool.Pool) *MailboxRepository {
|
|
return &MailboxRepository{pool: pool}
|
|
}
|
|
|
|
func (r *MailboxRepository) FindActiveByAddress(ctx context.Context, address string) (*port.MailboxRecord, error) {
|
|
row := r.pool.QueryRow(ctx, `
|
|
SELECT m.id, d.max_message_bytes, m.quota_bytes, m.used_bytes
|
|
FROM mailboxes m
|
|
JOIN domains d ON d.id = m.domain_id
|
|
WHERE m.email_address = $1
|
|
AND m.is_active = true
|
|
AND d.is_active = true
|
|
`, address)
|
|
|
|
var rec port.MailboxRecord
|
|
var id uuid.UUID
|
|
if err := row.Scan(&id, &rec.MaxMessageBytes, &rec.QuotaBytes, &rec.UsedBytes); err != nil {
|
|
if err == pgx.ErrNoRows {
|
|
return nil, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
rec.ID = id
|
|
return &rec, nil
|
|
}
|
|
|
|
// ResolveDeliveryTargets tries a direct mailbox match first, then falls back
|
|
// to aliases: an exact source_address match takes precedence over a
|
|
// domain-wide catch-all. Destination addresses that don't resolve to an
|
|
// internal active mailbox (e.g. external forwarding) are silently skipped -
|
|
// outbound delivery doesn't exist yet in this sub-project's scope.
|
|
func (r *MailboxRepository) ResolveDeliveryTargets(ctx context.Context, address string) ([]port.MailboxRecord, error) {
|
|
direct, err := r.FindActiveByAddress(ctx, address)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if direct != nil {
|
|
return []port.MailboxRecord{*direct}, nil
|
|
}
|
|
|
|
var destinations []string
|
|
err = r.pool.QueryRow(ctx, `
|
|
SELECT a.destination_addresses
|
|
FROM aliases a
|
|
JOIN domains d ON d.id = a.domain_id
|
|
WHERE d.is_active = true
|
|
AND a.is_active = true
|
|
AND d.name = split_part($1, '@', 2)
|
|
AND (a.source_address = $1 OR (a.is_catch_all AND a.source_address = '*@' || split_part($1, '@', 2)))
|
|
ORDER BY a.is_catch_all ASC
|
|
LIMIT 1
|
|
`, address).Scan(&destinations)
|
|
if err != nil {
|
|
if err == pgx.ErrNoRows {
|
|
return []port.MailboxRecord{}, nil
|
|
}
|
|
return nil, err
|
|
}
|
|
|
|
targets := []port.MailboxRecord{}
|
|
for _, dest := range destinations {
|
|
rec, err := r.FindActiveByAddress(ctx, dest)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if rec != nil {
|
|
targets = append(targets, *rec)
|
|
}
|
|
}
|
|
return targets, nil
|
|
}
|