Adds a real :143 IMAP listener to the Go protocols service: a mailbox owner authenticates over IMAP (Argon2id against mailboxes.password_hash, enumeration-resistant per PLAN.md section 14.6), lists folders, selects INBOX, fetches messages F1's SMTP path already persisted - including real ENVELOPE/BODY[] built from the actual stored MIME bytes via go-imap/v2's own ExtractEnvelope/ExtractBodySection helpers, not reconstructed from denormalized columns - and sets/reads flags via STORE. - ImapSessionUseCase orchestrates auth, folder listing/selection, message querying, and flag mutation against new Postgres repositories (auth, folders, message query, flags) and a local-disk blob reader. - A github.com/emersion/go-imap/v2 (pinned v2.0.0-beta.8) Backend/Session implements 7 real methods (Login, List, Select, Unselect, Status, Fetch, Store) plus a correct no-op Poll; the other 10 of the interface's 18 methods (Create/Delete/Rename/Subscribe/Unsubscribe/Append/Idle/Expunge/ Search/Copy) are explicit stubs returning a clear "not yet implemented" IMAP error - SEARCH/EXPUNGE/COPY/MOVE, IDLE/CONDSTORE, mailbox management, and APPEND are separate follow-up sub-projects per the design doc's agreed ordering with the user. Verified end-to-end with a real go-imap/v2 imapclient (embedded Postgres, no Docker needed) driving Login -> Select -> Fetch -> Store -> List against a message seeded through F1's real SMTP use case, proving true SMTP-write/IMAP-read interop, not just isolated unit tests. Built via 8 sequential tasks under subagent-driven development, each independently reviewed, plus a final whole-branch review. Two real bugs were found and fixed before merge, both exactly the kind unit tests with fakes cannot catch: - Store's UID-set resolution silently no-op'd on dynamic ranges like "UID STORE 1:*" (the common "mark all as read" operation) after an earlier draft bypassed the folder's real message list; restored to resolve against real messages via the same pattern Fetch already used correctly, with tests proving both dynamic ranges and non-UID STORE. - go-imap/v2's server calls Session.Poll() implicitly before the status response of nearly every authenticated command (FETCH/STORE/SEARCH/ APPEND/COPY), not just IDLE as assumed - a hard-stubbed Poll broke every real command through this path; fixed to a correct no-op. - List had zero test coverage despite being committed scope; added unit tests for wildcard/delimiter matching plus a real E2E LIST assertion. Known follow-up, not blocking: a pre-existing test-fixture hygiene gap (hardcoded content_sha256 blob hashes with no cleanup, shared across several Postgres integration test files including one new to this branch) causes local-dev test collisions on repeated runs against a persistent database - invisible to CI (fresh Postgres per run) but worth a cleanup pass.
110 lines
3.7 KiB
Go
110 lines
3.7 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"sort"
|
|
"testing"
|
|
|
|
"github.com/google/uuid"
|
|
"lambdamail/protocols/internal/application/port"
|
|
)
|
|
|
|
func TestFlagRepository_SetFlags_AddThenDelThenSet(t *testing.T) {
|
|
pool := testPool(t)
|
|
defer pool.Close()
|
|
ctx := context.Background()
|
|
|
|
domainID := uuid.New()
|
|
domainName := "flagrepo-test-" + domainID.String() + ".invalid"
|
|
_, err := pool.Exec(ctx, `INSERT INTO domains (id, name, punycode_name) VALUES ($1, $2, $3)`,
|
|
domainID, domainName, domainName)
|
|
if err != nil {
|
|
t.Fatalf("seed domain: %v", err)
|
|
}
|
|
defer pool.Exec(ctx, `DELETE FROM domains WHERE id = $1`, domainID)
|
|
|
|
mailboxID := uuid.New()
|
|
_, err = pool.Exec(ctx, `INSERT INTO mailboxes (id, domain_id, local_part, email_address, password_hash) VALUES ($1, $2, 'user', $3, '$argon2id$v=19$m=65536,t=3,p=4$c29tZXNhbHQ$RdescudvJCsgt3ub+b+dWRWJTmaaJObG')`,
|
|
mailboxID, domainID, "user@"+domainName)
|
|
if err != nil {
|
|
t.Fatalf("seed mailbox: %v", err)
|
|
}
|
|
folderID := uuid.New()
|
|
_, err = pool.Exec(ctx, `INSERT INTO folders (id, mailbox_id, name, special_use) VALUES ($1, $2, 'INBOX', 'inbox')`,
|
|
folderID, mailboxID)
|
|
if err != nil {
|
|
t.Fatalf("seed folder: %v", err)
|
|
}
|
|
blobID := uuid.New()
|
|
_, err = pool.Exec(ctx, `INSERT INTO message_blobs (id, content_sha256, storage_driver, storage_path, size_bytes) VALUES ($1, $2, 'local', '/tmp/x', 10)`,
|
|
blobID, "1111110000000000000000000000000000000000000000000000000000000000"[:64])
|
|
if err != nil {
|
|
t.Fatalf("seed blob: %v", err)
|
|
}
|
|
messageID := uuid.New()
|
|
_, err = pool.Exec(ctx, `
|
|
INSERT INTO email_messages (id, mailbox_id, folder_id, uid, blob_id, sender_address, recipient_addresses, size_bytes)
|
|
VALUES ($1, $2, $3, 7, $4, 'sender@example.test', $5, 10)
|
|
`, messageID, mailboxID, folderID, blobID, []string{"user@" + domainName})
|
|
if err != nil {
|
|
t.Fatalf("seed message: %v", err)
|
|
}
|
|
|
|
readFlags := func() []string {
|
|
t.Helper()
|
|
rows, err := pool.Query(ctx, `SELECT flag FROM message_flags WHERE message_id = $1 ORDER BY flag`, messageID)
|
|
if err != nil {
|
|
t.Fatalf("query flags: %v", err)
|
|
}
|
|
defer rows.Close()
|
|
var flags []string
|
|
for rows.Next() {
|
|
var f string
|
|
if err := rows.Scan(&f); err != nil {
|
|
t.Fatalf("scan flag: %v", err)
|
|
}
|
|
flags = append(flags, f)
|
|
}
|
|
return flags
|
|
}
|
|
|
|
repo := NewFlagRepository(pool)
|
|
|
|
if err := repo.SetFlags(ctx, folderID.String(), 7, port.FlagOpAdd, []string{"\\Seen", "\\Flagged"}); err != nil {
|
|
t.Fatalf("add: %v", err)
|
|
}
|
|
got := readFlags()
|
|
sort.Strings(got)
|
|
if len(got) != 2 || got[0] != "\\Flagged" || got[1] != "\\Seen" {
|
|
t.Fatalf("after add: flags = %v, want [\\Flagged \\Seen]", got)
|
|
}
|
|
|
|
if err := repo.SetFlags(ctx, folderID.String(), 7, port.FlagOpDel, []string{"\\Flagged"}); err != nil {
|
|
t.Fatalf("del: %v", err)
|
|
}
|
|
got = readFlags()
|
|
if len(got) != 1 || got[0] != "\\Seen" {
|
|
t.Fatalf("after del: flags = %v, want [\\Seen]", got)
|
|
}
|
|
|
|
if err := repo.SetFlags(ctx, folderID.String(), 7, port.FlagOpSet, []string{"\\Answered"}); err != nil {
|
|
t.Fatalf("set: %v", err)
|
|
}
|
|
got = readFlags()
|
|
if len(got) != 1 || got[0] != "\\Answered" {
|
|
t.Fatalf("after set: flags = %v, want [\\Answered] (Set replaces the full flag set)", got)
|
|
}
|
|
|
|
// A client sending "STORE 1 FLAGS (\Seen \Seen)" - a duplicate flag in a
|
|
// single command - must not fail the whole STORE with a primary key
|
|
// violation on the second INSERT for the same (message_id, received_at,
|
|
// flag).
|
|
if err := repo.SetFlags(ctx, folderID.String(), 7, port.FlagOpSet, []string{"\\Seen", "\\Seen"}); err != nil {
|
|
t.Fatalf("set with duplicate flags in input: %v", err)
|
|
}
|
|
got = readFlags()
|
|
if len(got) != 1 || got[0] != "\\Seen" {
|
|
t.Fatalf("after set with duplicate input flags: flags = %v, want [\\Seen]", got)
|
|
}
|
|
}
|