| new file mode 100644 |
| index 000000000..b12b8294f |
| --- /dev/null |
| +++ b/internal/console/planmail_test.go |
| @@ -0,0 +1,198 @@ |
| +package console |
| + |
| +import ( |
| + "context" |
| + "strings" |
| + "testing" |
| + "time" |
| + |
| + "github.com/carlosframework/platform/internal/mail" |
| + "github.com/carlosframework/platform/internal/plans" |
| + "github.com/carlosframework/platform/internal/release" |
| +) |
| + |
| +var planMailNow = time.Date(2026, 8, 19, 12, 0, 0, 0, time.UTC) |
| + |
| +// addMailApp gives the ops account an app with a provisioned, active app |
| +// sender whose own daily cap (1000) is well above the plan's. |
| +func addMailApp(t *testing.T, fx *consoleFixture, app string) { |
| + t.Helper() |
| + ctx := context.Background() |
| + if _, err := fx.st.CreateAppIn(OpsAccountID, app, "paul@keymail.dev", planMailNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := mail.Declare(ctx, fx.rel, release.OpsAccount, app, strings.ToUpper(app)+"_SMTP", "noreply@x.com", "owner@example.test", planMailNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := mail.ApplyGrant(ctx, fx.rel, release.OpsAccount, app, []string{"x.com"}, 1000, "tier:free", "tier", planMailNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + rec, etag, _ := mail.Get(ctx, fx.rel, release.OpsAccount, app) |
| + rec.Provisioned = &mail.Provisioned{IAMUser: "u-" + app, KeyID: "AKIA" + app, Active: true, At: planMailNow} |
| + if _, err := mail.Put(ctx, fx.rel, release.OpsAccount, app, rec, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + p := fx.srv.cfg.MailProvisioner.(*mail.MemProvisioner) |
| + p.EnsureUser(ctx, "u-"+app, mail.UserPath, []string{"arn:aws:ses:eu-west-1:1:identity/x.com"}, nil, []string{"x.com"}) |
| +} |
| + |
| +// planMailFixture: plans on (free: 50 emails a day), two mail apps. |
| +func planMailFixture(t *testing.T) *consoleFixture { |
| + t.Helper() |
| + fx := newConsoleFixture(t) |
| + fx.srv.cfg.MailProvisioner = mail.NewMemProvisioner("1") |
| + fx.srv.cfg.PlanCatalog = examplePlanCatalog(t) |
| + addMailApp(t, fx, "blog") |
| + addMailApp(t, fx, "shop") |
| + return fx |
| +} |
| + |
| +func sends(t *testing.T, fx *consoleFixture, app string, n int, day time.Time) { |
| + t.Helper() |
| + for i := 0; i < n; i++ { |
| + if err := fx.st.RecordMailEvent(context.Background(), release.OpsAccount, app, mail.SenderAppID, day.UTC().Format("2006-01-02"), mail.EventSend); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| +} |
| + |
| +func senderState(t *testing.T, fx *consoleFixture, app string) *mail.Provisioned { |
| + t.Helper() |
| + rec, _, err := mail.Get(context.Background(), fx.rel, release.OpsAccount, app) |
| + if err != nil || rec == nil || rec.Provisioned == nil { |
| + t.Fatalf("mail record %s: %v", app, err) |
| + } |
| + return rec.Provisioned |
| +} |
| + |
| +func TestPlanMailPauseAt(t *testing.T) { |
| + for _, c := range []struct { |
| + limit, ceiling int |
| + want int64 |
| + }{{50, 10000, 100}, {8000, 10000, 10000}, {50, 0, 100}, {0, 10000, 0}} { |
| + if got := planMailPauseAt(&plans.Limits{EmailPerDay: c.limit}, plans.Limits{EmailPerDay: c.ceiling}); got != c.want { |
| + t.Errorf("limit %d ceiling %d = %d, want %d", c.limit, c.ceiling, got, c.want) |
| + } |
| + } |
| +} |
| + |
| +func TestAccountMailPausesEverySenderAt2x(t *testing.T) { |
| + fx := planMailFixture(t) |
| + sends(t, fx, "blog", 60, planMailNow) |
| + sends(t, fx, "shop", 40, planMailNow) |
| + paused, err := fx.srv.enforceAccountMail(context.Background(), release.OpsAccount, planMailNow) |
| + if err != nil || !paused { |
| + t.Fatalf("enforceAccountMail = %v, %v, want a pause", paused, err) |
| + } |
| + for _, app := range []string{"blog", "shop"} { |
| + if p := senderState(t, fx, app); p.Active || p.PauseKind != mail.PauseKindPlan { |
| + t.Errorf("%s sender = active %v kind %q, want paused for the plan", app, p.Active, p.PauseKind) |
| + } |
| + } |
| + acct, _ := fx.st.AccountBySqid(fx.sqid) |
| + events, _ := fx.st.Events(acct.ID, "blog", 10) |
| + found := false |
| + for _, e := range events { |
| + if e.Action == "mail.paused" && strings.Contains(e.Message+e.Detail, "100 of 50") { |
| + found = true |
| + } |
| + } |
| + if !found { |
| + t.Errorf("no mail.paused audit naming the figures: %+v", events) |
| + } |
| +} |
| + |
| +func TestAccountMailBelow2xLeavesSenders(t *testing.T) { |
| + fx := planMailFixture(t) |
| + sends(t, fx, "blog", 99, planMailNow) |
| + if paused, err := fx.srv.enforceAccountMail(context.Background(), release.OpsAccount, planMailNow); err != nil || paused { |
| + t.Fatalf("enforceAccountMail at 99 = %v, %v", paused, err) |
| + } |
| + if !senderState(t, fx, "blog").Active { |
| + t.Fatal("paused below 2x") |
| + } |
| +} |
| + |
| +func TestAccountMailWithoutACatalogUnchanged(t *testing.T) { |
| + fx := planMailFixture(t) |
| + fx.srv.cfg.PlanCatalog = nil |
| + sends(t, fx, "blog", 500, planMailNow) |
| + if paused, _ := fx.srv.enforceAccountMail(context.Background(), release.OpsAccount, planMailNow); paused { |
| + t.Fatal("paused with plans off") |
| + } |
| + if !senderState(t, fx, "shop").Active { |
| + t.Fatal("a sender paused with plans off") |
| + } |
| +} |
| + |
| +func TestAccountMailSkipsPerInstanceSenders(t *testing.T) { |
| + fx := planMailFixture(t) |
| + ctx := context.Background() |
| + sn, etag, err := mail.GetSender(ctx, fx.rel, release.OpsAccount, "shop", mail.SenderAppID) |
| + if err != nil || sn == nil { |
| + t.Fatalf("sender: %v", err) |
| + } |
| + sn.PerInstance = true |
| + if _, err := mail.PutSender(ctx, fx.rel, release.OpsAccount, "shop", sn, etag); err != nil { |
| + t.Fatal(err) |
| + } |
| + sends(t, fx, "blog", 100, planMailNow) |
| + fx.srv.enforceAccountMail(ctx, release.OpsAccount, planMailNow) |
| + if !senderState(t, fx, "shop").Active { |
| + t.Fatal("a per-instance sender was paused") |
| + } |
| + if senderState(t, fx, "blog").Active { |
| + t.Fatal("the app sender was not paused") |
| + } |
| +} |
| + |
| +func TestPlanMailPauseResumesOnDayRoll(t *testing.T) { |
| + fx := planMailFixture(t) |
| + ctx := context.Background() |
| + sends(t, fx, "blog", 100, planMailNow) |
| + fx.srv.enforceAccountMail(ctx, release.OpsAccount, planMailNow) |
| + acct, _ := fx.st.AccountBySqid(fx.sqid) |
| + if err := fx.srv.resumePlanMail(ctx, acct, planMailNow.Add(time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if senderState(t, fx, "blog").Active { |
| + t.Fatal("resumed the same day") |
| + } |
| + if err := fx.srv.resumePlanMail(ctx, acct, planMailNow.Add(13*time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if !senderState(t, fx, "blog").Active || !senderState(t, fx, "shop").Active { |
| + t.Fatal("plan pause not lifted on the UTC day roll") |
| + } |
| +} |
| + |
| +func TestPlanResumeLeavesOtherPausesAlone(t *testing.T) { |
| + fx := planMailFixture(t) |
| + ctx := context.Background() |
| + if _, err := mail.SetSenderPaused(ctx, fx.rel, fx.srv.cfg.MailProvisioner, release.OpsAccount, "blog", mail.SenderAppID, true, mail.PauseKindRate, planMailNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := mail.SetSenderPaused(ctx, fx.rel, fx.srv.cfg.MailProvisioner, release.OpsAccount, "shop", mail.SenderAppID, true, "", planMailNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + acct, _ := fx.st.AccountBySqid(fx.sqid) |
| + if err := fx.srv.resumePlanMail(ctx, acct, planMailNow.Add(48*time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + if senderState(t, fx, "blog").Active || senderState(t, fx, "shop").Active { |
| + t.Fatal("a rate or manual pause was lifted by the plan resume") |
| + } |
| +} |
| + |
| +// The SES event path runs the account check: one sender's event pauses |
| +// every sender of the account. |
| +func TestSESEventTriggersAccountPause(t *testing.T) { |
| + fx := planMailFixture(t) |
| + sends(t, fx, "blog", 100, planMailNow) |
| + if _, _, err := fx.srv.enforceMailLimits(context.Background(), release.OpsAccount, "blog", mail.SenderAppID, planMailNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + if senderState(t, fx, "shop").Active { |
| + t.Fatal("an SES event at 2x left a sibling sender sending") |
| + } |
| +} |