Files
warmbly/internal/repository/pool_link_live_test.go
T

324 lines
13 KiB
Go

package repository
import (
"context"
"errors"
"testing"
"time"
"github.com/google/uuid"
"github.com/jackc/pgx/v5/pgxpool"
)
// The pool link's two queries that a second reader of the same mailbox depends
// on. An enrolled mailbox is synced by the instance AND by Warmbly Cloud, so
// whichever consumes the token first must not decide what the other sees.
//
// WARMBLY_TEST_DB=postgres://warmbly:warmbly@localhost:15432/warmbly_dev?sslmode=disable \
// go test ./internal/repository/ -run LivePoolLink -v
type poolLinkFixture struct {
pool *pgxpool.Pool
user uuid.UUID
org uuid.UUID
recipient uuid.UUID
sender uuid.UUID
senderTo string
task uuid.UUID
warmup WarmupRepository
}
func newPoolLinkFixture(t *testing.T) *poolLinkFixture {
t.Helper()
_, pool := liveContactDB(t)
ctx := context.Background()
f := &poolLinkFixture{
pool: pool, user: uuid.New(), org: uuid.New(),
recipient: uuid.New(), sender: uuid.New(), task: uuid.New(),
warmup: NewWarmupRepository(pool),
}
exec := func(sql string, args ...any) {
t.Helper()
if _, err := pool.Exec(ctx, sql, args...); err != nil {
t.Fatalf("fixture %q: %v", sql[:min(60, len(sql))], err)
}
}
exec(`INSERT INTO users (id, email, first_name, last_name, password_hash) VALUES ($1, $2, 'Pool', 'Link', 'x')`,
f.user, "pl-"+f.user.String()[:8]+"@test.local")
exec(`INSERT INTO organizations (id, name, slug, owner_user_id) VALUES ($1, 'Pool Link', $2, $3)`,
f.org, "pl-"+f.org.String()[:8], f.user)
for _, id := range []uuid.UUID{f.recipient, f.sender} {
addr := "pl-" + id.String()[:8] + "@test.local"
exec(`INSERT INTO email_accounts (id, user_id, organization_id, email, name, signature_plain,
signature_html, provider, status, campaign_limit, min_wait_time, timezone,test_mode)
VALUES ($1, $2, $3, $4, 'PL', '', '', 'smtp_imap', 'active', 50, 600, 'UTC',NULL)`,
id, f.user, f.org, addr)
if id == f.sender {
f.senderTo = addr
}
}
exec(`INSERT INTO tasks (id, task_type, email_account_id, status, message_id)
VALUES ($1, 'warmup', $2, 'completed', '')`, f.task, f.sender)
t.Cleanup(func() {
c := context.Background()
for _, sql := range []string{
`DELETE FROM warmup_received WHERE email_account_id IN (SELECT id FROM email_accounts WHERE organization_id = $1)`,
`DELETE FROM warmup_tokens WHERE recipient_account_id IN (SELECT id FROM email_accounts WHERE organization_id = $1)`,
`DELETE FROM tasks WHERE email_account_id IN (SELECT id FROM email_accounts WHERE organization_id = $1)`,
`DELETE FROM email_accounts WHERE organization_id = $1`,
`DELETE FROM organizations WHERE id = $1`,
`DELETE FROM users WHERE id = $1`,
} {
if _, err := pool.Exec(c, sql, f.org); err != nil {
t.Errorf("cleanup %q: %v", sql[:min(50, len(sql))], err)
}
}
})
return f
}
// token writes one warmup token addressed to the recipient.
func (f *poolLinkFixture) token(t *testing.T, messageID, subject string, consumed bool) uuid.UUID {
t.Helper()
id := uuid.New()
var consumedAt any
if consumed {
consumedAt = time.Now()
}
_, err := f.pool.Exec(context.Background(),
`INSERT INTO warmup_tokens (token, task_id, sender_account_id, recipient_account_id, sent_message_id, subject, consumed_at)
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
id, f.task, f.sender, f.recipient, messageID, subject, consumedAt)
if err != nil {
t.Fatalf("insert token: %v", err)
}
return id
}
// A mailbox Warmbly Cloud warms is read by the cloud and by the instance. The
// cloud consumes the token when it syncs first, and the instance's question
// ("is this yours?") must still answer yes, or the cloud's warmup mail lands in
// the owner's unibox.
func TestLivePoolLinkWarmupDeliverySurvivesAConsumedToken(t *testing.T) {
f := newPoolLinkFixture(t)
ctx := context.Background()
const messageID = "<pool-link-consumed@test.local>"
f.token(t, messageID, "Quick question", true)
found, err := f.warmup.FindDeliveredWarmupToken(ctx, f.recipient, f.senderTo, messageID, "Quick question")
if err != nil {
t.Fatalf("FindDeliveredWarmupToken: %v", err)
}
if found != nil {
t.Fatal("a consumed token should not be resolvable for local verification")
}
ok, err := f.warmup.IsWarmupDelivery(ctx, f.recipient, f.senderTo, messageID, "Quick question")
if err != nil {
t.Fatalf("IsWarmupDelivery: %v", err)
}
if !ok {
t.Fatal("consumed warmup mail must still be recognised as warmup for a linked instance")
}
}
func TestLivePoolLinkWarmupDeliveryMatchesAndRefuses(t *testing.T) {
f := newPoolLinkFixture(t)
ctx := context.Background()
// Pending token, matched on the sender/subject pair rather than the id.
pending := f.token(t, "", "Following up", false)
ok, err := f.warmup.IsWarmupDelivery(ctx, f.recipient, f.senderTo, "", "Following up")
if err != nil {
t.Fatalf("IsWarmupDelivery pair: %v", err)
}
if !ok {
t.Fatal("a pending token for this sender and subject should match")
}
// Its send confirmed, as HandleEmailSent does, so the cases below are not
// held behind it.
if _, err := f.pool.Exec(ctx, `UPDATE warmup_tokens SET sent_message_id = '<warm-1@test.local>' WHERE token = $1`, pending); err != nil {
t.Fatal(err)
}
// A delivery already recorded, whose token has since been cleaned up.
const receivedID = "<pool-link-received@test.local>"
if err := f.warmup.RecordWarmupReceived(ctx, f.recipient, uuid.New(), receivedID, f.sender, false); err != nil {
t.Fatalf("RecordWarmupReceived: %v", err)
}
ok, err = f.warmup.IsWarmupDelivery(ctx, f.recipient, "someone@elsewhere.test", receivedID, "Anything")
if err != nil {
t.Fatalf("IsWarmupDelivery received: %v", err)
}
if !ok {
t.Fatal("a recorded warmup delivery should be recognised by its message id")
}
// A pool partner's real email, sharing a subject with a token whose own
// Message-ID is known and different. Two known ids that differ are a
// different message, so the sender/subject fallback must not claim it.
f.token(t, "<warm-2@test.local>", "Quick sync", false)
ok, err = f.warmup.IsWarmupDelivery(ctx, f.recipient, f.senderTo, "<genuine-reply@test.local>", "Quick sync")
if err != nil {
t.Fatalf("IsWarmupDelivery conflicting ids: %v", err)
}
if ok {
t.Fatal("a message whose id differs from the token's must not match on sender and subject")
}
// Real mail from someone else must never be dropped.
ok, err = f.warmup.IsWarmupDelivery(ctx, f.recipient, "prospect@elsewhere.test", "<real-mail@elsewhere.test>", "Are you free Thursday?")
if err != nil {
t.Fatalf("IsWarmupDelivery stranger: %v", err)
}
if ok {
t.Fatal("ordinary mail must not be taken for warmup")
}
}
func TestLivePoolLinkWarmupDeliveryRecognizesSentAndOldCopies(t *testing.T) {
f := newPoolLinkFixture(t)
ctx := context.Background()
const messageID = "<warmup-sent@test.local>"
token := f.token(t, messageID, "Quick review", true)
if _, err := f.pool.Exec(ctx, `UPDATE warmup_tokens SET created_at = NOW() - INTERVAL '30 days' WHERE token = $1`, token); err != nil {
t.Fatal(err)
}
for _, tc := range []struct {
name string
account uuid.UUID
sender, messageID string
want bool
}{
{"Sent copy", f.sender, f.senderTo, messageID, true},
{"older inbound copy", f.recipient, f.senderTo, messageID, true},
{"unrelated mailbox", uuid.New(), f.senderTo, messageID, false},
{"different message", f.sender, f.senderTo, "<human@test.local>", false},
} {
t.Run(tc.name, func(t *testing.T) {
got, err := f.warmup.IsWarmupDelivery(ctx, tc.account, tc.sender, tc.messageID, "Quick review")
if err != nil || got != tc.want {
t.Fatalf("warmup = %v, error = %v; want %v", got, err, tc.want)
}
})
}
}
// A reply typed by hand in a warmup thread names the send it answers and
// nothing else; the turn after it names that reply. Both have to resolve, from
// either mailbox in the pair.
func TestLivePoolLinkWarmupThreadReplyByAncestry(t *testing.T) {
f := newPoolLinkFixture(t)
requireSchemaVersion(t, f.pool, 180)
ctx := context.Background()
const sent = "<warm-thread@test.local>"
f.token(t, sent, "Quick question", true)
for _, tc := range []struct {
name string
account uuid.UUID
parents []string
want bool
}{
{"the recipient's copy of the reply", f.recipient, []string{sent}, true},
{"the sender's copy of the reply", f.sender, []string{sent}, true},
{"bare id, several parents", f.recipient, []string{"unrelated@test.local", "warm-thread@test.local"}, true},
{"a mailbox outside the pair", uuid.New(), []string{sent}, false},
{"a reply to ordinary mail", f.recipient, []string{"<real@prospect.test>"}, false},
{"nothing to answer", f.recipient, nil, false},
} {
t.Run(tc.name, func(t *testing.T) {
got, err := f.warmup.IsWarmupThreadReply(ctx, tc.account, tc.parents)
if err != nil || got != tc.want {
t.Fatalf("thread reply = %v, error = %v; want %v", got, err, tc.want)
}
})
}
// The next turn answers the reply, and is asked about from the other
// mailbox in the pair, whose own record of the reply is its Sent copy that
// may never have synced.
if err := f.warmup.RecordWarmupThreadMessage(ctx, f.recipient, "<reply-1@gmail.test>"); err != nil {
t.Fatalf("RecordWarmupThreadMessage: %v", err)
}
if err := f.warmup.RecordWarmupThreadMessage(ctx, f.recipient, "<reply-1@gmail.test>"); err != nil {
t.Fatalf("a re-synced turn must record idempotently: %v", err)
}
got, err := f.warmup.IsWarmupThreadReply(ctx, f.sender, []string{"<reply-1@gmail.test>"})
if err != nil || !got {
t.Fatalf("second turn = %v, error = %v; want it recognised through the recorded reply", got, err)
}
}
// An approved code the instance never came back for must not keep a usable
// bearer token in plaintext once it expires.
func TestLivePoolLinkExpiredCodeLosesItsToken(t *testing.T) {
_, pool := liveContactDB(t)
ctx := context.Background()
repo := NewPoolLinkRepository(pool, testSessionConnector(pool))
id := uuid.New()
suffix := id.String()[:8]
if _, err := pool.Exec(ctx,
`INSERT INTO pool_link_codes (id, device_code_hash, user_code, instance_name, status, instance_token, expires_at)
VALUES ($1, $2, $3, 'Abandoned', 'approved', 'wpl_secret', NOW() - INTERVAL '1 minute')`,
id, "hash-"+suffix, "PL"+suffix[:2]+"-"+suffix[2:6]); err != nil {
t.Fatalf("insert code: %v", err)
}
t.Cleanup(func() {
if _, err := pool.Exec(context.Background(), `DELETE FROM pool_link_codes WHERE id = $1`, id); err != nil {
t.Errorf("cleanup code: %v", err)
}
})
if err := repo.DeleteExpiredCodes(ctx); err != nil {
t.Fatalf("DeleteExpiredCodes: %v", err)
}
var token *string
if err := pool.QueryRow(ctx, `SELECT instance_token FROM pool_link_codes WHERE id = $1`, id).Scan(&token); err != nil {
t.Fatalf("read back: %v", err)
}
if token != nil {
t.Fatalf("expired code kept its plaintext instance token: %q", *token)
}
}
// Until a warmup send is confirmed, a copy of it whose subject did not survive
// delivery cannot be told apart from ordinary mail by that sender. It is held
// for the confirmed id rather than stored and announced (#659).
func TestLivePoolLinkUnconfirmedSendHoldsThatSendersMail(t *testing.T) {
f := newPoolLinkFixture(t)
ctx := context.Background()
token := f.token(t, "", "Following up", false)
_, err := f.warmup.IsWarmupDelivery(ctx, f.recipient, f.senderTo, "<exchange-1@test.local>", "[EXTERNAL] Following up")
if !errors.Is(err, ErrWarmupDeliveryPending) {
t.Fatalf("error = %v, want the arrival held until the send is confirmed", err)
}
// Someone else's mail is never held behind it.
ok, err := f.warmup.IsWarmupDelivery(ctx, f.recipient, "prospect@elsewhere.test", "<real@elsewhere.test>", "Following up")
if err != nil || ok {
t.Fatalf("stranger: warmup = %v, error = %v; want ordinary mail", ok, err)
}
// Confirmed: the id decides, both ways.
if _, err := f.pool.Exec(ctx, `UPDATE warmup_tokens SET sent_message_id = '<exchange-1@test.local>' WHERE token = $1`, token); err != nil {
t.Fatal(err)
}
if ok, err := f.warmup.IsWarmupDelivery(ctx, f.recipient, f.senderTo, "<exchange-1@test.local>", "[EXTERNAL] Following up"); err != nil || !ok {
t.Fatalf("confirmed copy: warmup = %v, error = %v; want warmup", ok, err)
}
if ok, err := f.warmup.IsWarmupDelivery(ctx, f.recipient, f.senderTo, "<human@test.local>", "Lunch?"); err != nil || ok {
t.Fatalf("real mail after confirmation: warmup = %v, error = %v; want ordinary mail", ok, err)
}
// A send never confirmed stops holding anything after half an hour.
if _, err := f.pool.Exec(ctx, `UPDATE warmup_tokens SET sent_message_id = '', created_at = NOW() - INTERVAL '31 minutes' WHERE token = $1`, token); err != nil {
t.Fatal(err)
}
if ok, err := f.warmup.IsWarmupDelivery(ctx, f.recipient, f.senderTo, "<human@test.local>", "Lunch?"); err != nil || ok {
t.Fatalf("stale send: warmup = %v, error = %v; want ordinary mail", ok, err)
}
}