Add end-to-end tests for TLS RPT report ingestion and WebSocket event handling

- Implemented tests for ingesting TLS RPT reports from emails and ensuring they are stored correctly in the database.
- Added tests to verify that the same report can be ingested multiple times without duplication.
- Created tests for backfilling reports that were previously delivered to the inbox.
- Developed WebSocket event handling tests to ensure events are correctly routed to subscribers based on mailbox ownership.
- Included tests for the mail API to validate session handling, mailbox isolation, and error mapping.
- Ensured that the API rejects unauthorized requests and handles malformed JSON gracefully.
This commit is contained in:
Lucas Rayan committed 2026-08-21 09:55:37 -03:00
1 parent 880dced9e4
commit f330c4324e
28 files changed
+2438 -6

No files matched your search

@@ -0,0 +1,50 @@
package main
import (
"context"
"log"
"time"
"lambdamail/protocols/internal/application/usecase"
"lambdamail/protocols/internal/infrastructure/diskstorage"
"lambdamail/protocols/internal/infrastructure/postgres"
)
// runBackfillReports parses the DMARC and TLS-RPT reports that were delivered
// before automatic ingestion existed.
//
// Run by hand, once, after deploying ingestion: every report received until
// then was filed in the admin's inbox as an attachment nothing ever read, so
// the tables the console shows start empty however long the server has been
// running. Safe to run more than once - storing a report is idempotent, and a
// message already filed in Reports is not picked up again.
func runBackfillReports(cfg config) {
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Minute)
defer cancel()
pool, err := postgres.NewPool(ctx, cfg.DatabaseURL)
if err != nil {
log.Fatalf("backfill: cannot reach the database: %v", err)
}
defer pool.Close()
// The reader alone: the backfill only ever reads stored messages back out.
blobs := diskstorage.NewLocalDiskBlobReader(pool)
store := postgres.NewDeliveredReportStore(pool)
uc := usecase.NewBackfillReportsUseCase(store, blobs,
usecase.NewIngestDeliveredReportsUseCase(
usecase.NewIngestReportsUseCase(postgres.NewReportRepository(pool))))
summary, err := uc.Run(ctx, 0)
if err != nil {
log.Fatalf("backfill: %v", err)
}
log.Printf("backfill: scanned %d stored reports, ingested %d, could not read %d",
summary.Scanned, summary.Ingested, summary.Failed)
if summary.Failed > 0 {
log.Printf("backfill: the unreadable ones are filed in Reports with their " +
"bytes intact, so this can be run again once the cause is fixed")
}
}