mail: Task 1 review fixes (Fable P2-1, P2-2, P3-1, P3-2, P3-3)
- P2-1: TestEnableEnvironmentUnderAPausedApp. An owned environment enabled under a paused app gets an inactive key and record, for the legacy shape and for the Pause field alone. Minting it always active, or from Provisioned.Active, now fails. - P2-2: TestSetSenderPausedWithoutProvisioned gives the switched sender a standalone and an owned environment; both keys go inactive on pause and active on resume. Skipping the standalone toggle now fails. - P3-1: enforceMailLimits' automatic pause goes through SetSenderPausedIf with "can send and not paused", as the plan pause does, so a manual pause taken meanwhile is not rewritten as a cap pause that lifts at the day roll (TestEnforceKeepsAManualPauseTakenMeanwhile). - P3-2: TestPlanPauseRereadsTheSender, the pause direction of TestPlanResumeRereadsTheSender. - P3-3: TestPauseKindsSurvive gains the legacy shape. Each new test was watched to fail against the mutation it names.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+175
−6
internal/console/mailcounts.go+11 −1internal/console/mailcounts_test.go+63 −5internal/console/planmail_test.go+19 −0internal/mail/pauseif_test.go+34 −0internal/mail/walk_environment_test.go+48 −0
diff --git a/internal/console/mailcounts.go b/internal/console/mailcounts.go| index a852bd35c..cd0185576 100644 |
| --- a/internal/console/mailcounts.go |
| +++ b/internal/console/mailcounts.go |
| @@ -342,7 +342,17 @@ func (s *Server) enforceMailLimits(ctx context.Context, acct, app, sender string |
| return false, "", nil |
| } |
| - if _, err := mail.SetSenderPaused(ctx, s.claimMailStore(), s.cfg.MailProvisioner, acct, app, sender, true, kind, now); err != nil { |
| + // Asked again of the sender as read inside the mutation: the read above |
| + // may be stale, and a pause taken since (a manual one, say) must not be |
| + // rewritten as this automatic one, whose kind could lift it at the day |
| + // roll. The same rule as the plan pause (planmail.go). |
| + stillUnpaused := func(cur *mail.Sender) bool { |
| + return mail.CanSend(cur) && mail.EffectivePause(cur) == nil |
| + } |
| + if _, err := mail.SetSenderPausedIf(ctx, s.claimMailStore(), s.cfg.MailProvisioner, acct, app, sender, true, kind, now, stillUnpaused); err != nil { |
| + if errors.Is(err, mail.ErrNotEligible) { |
| + return false, "", nil |
| + } |
| return false, "", err |
| } |
| // Audit AFTER the write it announces, and never before — a pause the |
diff --git a/internal/console/mailcounts_test.go b/internal/console/mailcounts_test.go| index 383a13a93..f63d74f02 100644 |
| --- a/internal/console/mailcounts_test.go |
| +++ b/internal/console/mailcounts_test.go |
| @@ -833,10 +833,11 @@ func TestResumeAuditDetailIsByteIdenticalForTheAppSender(t *testing.T) { |
| // Test 8.13 as gap G1 reads it: with the pause on the sender record |
| // (decision 8), a cap pause and a plan pause from yesterday lift on today's |
| // enforceMailLimits; a rate, a manual and a per-instance pause from |
| -// yesterday do not. Each kind runs twice: written by the pause walk (Pause |
| -// and, in the overlap week, Provisioned too), and as a Pause field alone |
| -// over a Provisioned block that says active, which only a reader of the |
| -// Pause field gets right. |
| +// yesterday do not. Each kind runs in three shapes: written by the pause |
| +// walk (Pause and, in the overlap week, Provisioned too); as a Pause field |
| +// alone over a Provisioned block that says active, which only a reader of |
| +// the Pause field gets right; and the legacy shape, in Provisioned alone, |
| +// which only a reader that falls back to Provisioned gets right. |
| func TestPauseKindsSurvive(t *testing.T) { |
| for _, c := range []struct { |
| kind string |
| @@ -848,7 +849,7 @@ func TestPauseKindsSurvive(t *testing.T) { |
| {"", false}, // manual |
| {mail.PauseKindPerInstance, false}, |
| } { |
| - for _, shape := range []string{"walk", "pause-field-only"} { |
| + for _, shape := range []string{"walk", "pause-field-only", "legacy"} { |
| name := c.kind |
| if name == "" { |
| name = "manual" |
| @@ -863,6 +864,10 @@ func TestPauseKindsSurvive(t *testing.T) { |
| mail.SenderAppID, true, c.kind, yesterday); err != nil { |
| t.Fatal(err) |
| } |
| + case "legacy": |
| + // A record from before the Pause field: the pause is in |
| + // Provisioned alone. |
| + pauseDirectly(t, fx, "harbourmail", c.kind, yesterday) |
| case "pause-field-only": |
| sn, etag, err := mail.GetSender(ctx, fx.rel, release.OpsAccount, "harbourmail", mail.SenderAppID) |
| if err != nil { |
| @@ -1003,3 +1008,56 @@ func TestMailCallerSendsCounts(t *testing.T) { |
| t.Errorf("a forgotten app still has %v", got) |
| } |
| } |
| + |
| +// pauseOnFirstRead is a release.Store that answers the first read of one key |
| +// with the object as it was, and pauses that sender by hand (manual, kind |
| +// "") underneath straight after: a caller holding the read now holds a stale |
| +// "not paused" snapshot, as one would if an operator paused in between. |
| +type pauseOnFirstRead struct { |
| + release.Store |
| + key string |
| + fired bool |
| + inject func() |
| +} |
| + |
| +func (w *pauseOnFirstRead) Get(ctx context.Context, key string) ([]byte, string, error) { |
| + body, etag, err := w.Store.Get(ctx, key) |
| + if key == w.key && !w.fired { |
| + w.fired = true |
| + w.inject() |
| + } |
| + return body, etag, err |
| +} |
| + |
| +func manualPauseBetween(t *testing.T, fx *consoleFixture, app string, at time.Time) { |
| + t.Helper() |
| + fx.srv.cfg.Releases = &pauseOnFirstRead{Store: fx.rel, key: mail.SenderKey(release.OpsAccount, app, mail.SenderAppID), inject: func() { |
| + if _, err := mail.SetSenderPaused(context.Background(), fx.rel, fx.srv.cfg.MailProvisioner, release.OpsAccount, app, mail.SenderAppID, true, "", at); err != nil { |
| + t.Errorf("manual pause: %v", err) |
| + } |
| + }} |
| +} |
| + |
| +// The automatic cap and rate pause asks its condition again of the sender |
| +// as read inside the mutation: a manual pause taken after enforceMailLimits |
| +// read the sender must stay a manual pause, not be rewritten as a cap pause |
| +// that would lift at the next day roll. |
| +func TestEnforceKeepsAManualPauseTakenMeanwhile(t *testing.T) { |
| + fx, now := mailFixture(t) |
| + seedCounts(t, fx, now.UTC().Format("2006-01-02"), MailCounts{Sends: 50}) |
| + manualPauseBetween(t, fx, "harbourmail", now) |
| + paused, _, err := fx.srv.enforceMailLimits(context.Background(), release.OpsAccount, "harbourmail", mail.SenderAppID, now) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if paused { |
| + t.Fatal("enforce reports its own pause over a manual one taken meanwhile") |
| + } |
| + sn, _, err := mail.GetSender(context.Background(), fx.rel, release.OpsAccount, "harbourmail", mail.SenderAppID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if pz := mail.EffectivePause(sn); pz == nil || pz.Kind != "" { |
| + t.Fatalf("pause = %+v, want the manual (kind-less) pause kept", pz) |
| + } |
| +} |
diff --git a/internal/console/planmail_test.go b/internal/console/planmail_test.go| index b79c59047..cb23635f2 100644 |
| --- a/internal/console/planmail_test.go |
| +++ b/internal/console/planmail_test.go |
| @@ -251,3 +251,22 @@ func TestAccountMailContinuesPastABrokenApp(t *testing.T) { |
| t.Fatal("an unreadable app stopped the others pausing") |
| } |
| } |
| + |
| +// The pause direction of TestPlanResumeRereadsTheSender: the plan pause |
| +// lists the senders first, and a manual pause taken after the listing must |
| +// not be rewritten as a plan pause, which would lift at the day roll. |
| +func TestPlanPauseRereadsTheSender(t *testing.T) { |
| + fx := planMailFixture(t) |
| + sends(t, fx, "blog", 60, planMailNow) |
| + sends(t, fx, "shop", 40, planMailNow) |
| + manualPauseBetween(t, fx, "blog", planMailNow) |
| + if _, err := fx.srv.enforceAccountMail(context.Background(), release.OpsAccount, planMailNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if p := senderState(t, fx, "blog"); p.Active || p.PauseKind != "" { |
| + t.Fatalf("blog = active %v kind %q, want the manual pause kept", p.Active, p.PauseKind) |
| + } |
| + if p := senderState(t, fx, "shop"); p.Active || p.PauseKind != mail.PauseKindPlan { |
| + t.Fatalf("shop = active %v kind %q, want paused for the plan", p.Active, p.PauseKind) |
| + } |
| +} |
diff --git a/internal/mail/pauseif_test.go b/internal/mail/pauseif_test.go| index 9fd0800d3..ec088cce2 100644 |
| --- a/internal/mail/pauseif_test.go |
| +++ b/internal/mail/pauseif_test.go |
| @@ -141,8 +141,34 @@ func TestSetSenderPausedWithoutProvisioned(t *testing.T) { |
| t.Fatalf("pause of a sender nothing can send as = %v, want ErrNotProvisioned", err) |
| } |
| + // The switched sender still has a standalone credential and an owned |
| + // environment's user: each is its own IAM user, and the abuse brake |
| + // must stop them too even though the app-level user is gone. |
| + arns := []string{"arn:aws:ses:eu-west-1:1:identity/x.com"} |
| + keys := map[string]string{} |
| + for _, user := range []string{"u-laptop", "u-env"} { |
| + if _, err := p.EnsureUser(ctx, user, UserPath, arns, nil, []string{"x.com"}); err != nil { |
| + t.Fatal(err) |
| + } |
| + id, _, err := p.MintKey(ctx, user) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + keys[user] = id |
| + } |
| + wantKeys := func(active bool) { |
| + t.Helper() |
| + for user, id := range keys { |
| + if got := p.Users[user].Keys[id].Active; got != active { |
| + t.Errorf("key %s of %s active = %v, want %v", id, user, got, active) |
| + } |
| + } |
| + } |
| + |
| sn, etag, _ := GetSender(ctx, s, "bab", "blog", SenderAppID) |
| sn.Submission = &mailwire.Submission{On: true, At: now, By: "o@example.test"} |
| + sn.Standalone = []*Standalone{{Name: "laptop", IAMUser: "u-laptop", KeyID: keys["u-laptop"], At: now}} |
| + sn.Environments = map[string]*Provisioned{"team": {IAMUser: "u-env", KeyID: keys["u-env"], Active: true, At: now}} |
| if _, err := PutSender(ctx, s, "bab", "blog", sn, etag); err != nil { |
| t.Fatal(err) |
| } |
| @@ -156,6 +182,10 @@ func TestSetSenderPausedWithoutProvisioned(t *testing.T) { |
| if pz := EffectivePause(sn); pz == nil || pz.Kind != PauseKindRate { |
| t.Fatalf("EffectivePause = %+v, want the rate pause", pz) |
| } |
| + if sn.Environments["team"].Active { |
| + t.Fatal("the owned environment's record is still active") |
| + } |
| + wantKeys(false) |
| if _, err := SetSenderPaused(ctx, s, p, "bab", "blog", SenderAppID, false, "", now); err != nil { |
| t.Fatal(err) |
| } |
| @@ -163,4 +193,8 @@ func TestSetSenderPausedWithoutProvisioned(t *testing.T) { |
| if sn.Pause != nil || !Switched(sn) { |
| t.Fatalf("after a resume: Pause = %+v, switched = %v", sn.Pause, Switched(sn)) |
| } |
| + if !sn.Environments["team"].Active { |
| + t.Fatal("the owned environment's record is still paused after a resume") |
| + } |
| + wantKeys(true) |
| } |
diff --git a/internal/mail/walk_environment_test.go b/internal/mail/walk_environment_test.go| index c9c9be7bf..105c23ca8 100644 |
| --- a/internal/mail/walk_environment_test.go |
| +++ b/internal/mail/walk_environment_test.go |
| @@ -14,6 +14,7 @@ import ( |
| "github.com/carlosframework/platform/internal/appconfig" |
| "github.com/carlosframework/platform/internal/dnsrec" |
| + "github.com/carlosframework/platform/internal/mailwire" |
| "github.com/carlosframework/platform/internal/release" |
| ) |
| @@ -247,3 +248,50 @@ func TestEnvironmentUserNameFitsIAMAtTheMaxima(t *testing.T) { |
| } |
| } |
| } |
| + |
| +// Decision 8: an owned environment enabled while its app is paused gets a |
| +// key that starts INACTIVE, whichever shape the app's pause has: the legacy |
| +// shape (Provisioned.Active false, no Pause) and the Pause field alone (over |
| +// a Provisioned block that still says active). The second is what only a |
| +// reader of EffectivePause gets right. |
| +func TestEnableEnvironmentUnderAPausedApp(t *testing.T) { |
| + for _, shape := range []string{"legacy", "pause-field-only"} { |
| + t.Run(shape, func(t *testing.T) { |
| + ctx, s, p, pub, now := enableFixture(t) |
| + if _, err := Enable(ctx, s, p, mailTestSealer(pub, "bag", "harbourmail"), testDeployment(), "bag", "harbourmail", now); err != nil { |
| + t.Fatal(err) |
| + } |
| + sn, etag, err := GetSender(ctx, s, "bag", "harbourmail", SenderAppID) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + switch shape { |
| + case "legacy": |
| + sn.Provisioned.Active, sn.Provisioned.PauseKind = false, PauseKindRate |
| + case "pause-field-only": |
| + sn.Pause = &mailwire.Pause{Kind: PauseKindRate, At: now} |
| + } |
| + if _, err := PutSender(ctx, s, "bag", "harbourmail", sn, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := appconfig.ApplyEnvironmentOwned(ctx, s, "bag", "harbourmail", "team", "alta", |
| + map[string]appconfig.Var{"TEAM_ONLY": {Kind: "env", Value: "1"}}, nil); err != nil { |
| + t.Fatal(err) |
| + } |
| + rec, err := EnableEnvironment(ctx, s, p, mailTestSealer(pub, "bag", "harbourmail"), testDeployment(), "bag", "harbourmail", "team", now) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + ep := rec.Environments["team"] |
| + if ep == nil || ep.KeyID == "" { |
| + t.Fatalf("no environment credential minted: %+v", ep) |
| + } |
| + if ep.Active { |
| + t.Fatal("the environment's record says active under a paused app") |
| + } |
| + if p.Users[ep.IAMUser].Keys[ep.KeyID].Active { |
| + t.Fatal("the environment's key is active at the provisioner under a paused app") |
| + } |
| + }) |
| + } |
| +} |