From 7005edcb9bea6df12faee06b5ff65a4c1789ea84 Mon Sep 17 00:00:00 2001 From: tgiachi Date: Mon, 9 Nov 2026 20:43:41 +0100 Subject: [PATCH] test: pin the documented limits and close gaps found by mutation checks - Test the lockout, the confirmation cap and Retry-After with literal numbers, not the constants - Test that failed deliveries are listed before the most retried ones --- internal/api/limit_test.go | 34 ++++++++++++++++++++++++++++++++++ internal/store/m2_test.go | 3 ++- internal/store/outbox_test.go | 18 ++++++++++++++++++ 3 files changed, 54 insertions(+), 1 deletion(-) diff --git a/internal/api/limit_test.go b/internal/api/limit_test.go index 4aa4a4e..8687d16 100644 --- a/internal/api/limit_test.go +++ b/internal/api/limit_test.go @@ -180,3 +180,37 @@ func TestTheLimiterForgetsOldAddresses(t *testing.T) { t.Fatalf("the limiter keeps %d expired addresses: memory grows without bound", n) } } + +func TestTheLimitIsTenWrongTokensInFiveMinutes(t *testing.T) { + // The numbers are part of the documented behaviour: do not derive them from the constants. + h, clk := limited(t) + for i := 1; i <= 10; i++ { + if rec := attempt(h, "203.0.113.7", "wrong"); rec.Code != 401 { + t.Fatalf("wrong token number %d must still be a plain 401, got %d", i, rec.Code) + } + } + if rec := attempt(h, "203.0.113.7", token); rec.Code != 429 { + t.Fatalf("the 11th attempt is locked out: %d", rec.Code) + } + clk.advance(4*time.Minute + 59*time.Second) + if rec := attempt(h, "203.0.113.7", token); rec.Code != 429 { + t.Fatalf("still locked just before 5 minutes: %d", rec.Code) + } + clk.advance(2 * time.Second) + if rec := attempt(h, "203.0.113.7", token); rec.Code != 200 { + t.Fatalf("free after 5 minutes: %d", rec.Code) + } +} + +func TestRetryAfterCountsDown(t *testing.T) { + h, clk := limited(t) + for i := 0; i < 10; i++ { + attempt(h, "203.0.113.7", "wrong") + } + first, _ := strconv.Atoi(attempt(h, "203.0.113.7", token).Header().Get("Retry-After")) + clk.advance(2 * time.Minute) + later, _ := strconv.Atoi(attempt(h, "203.0.113.7", token).Header().Get("Retry-After")) + if first != 300 || later != 180 { + t.Fatalf("Retry-After is the time left: %d then %d, want 300 then 180", first, later) + } +} diff --git a/internal/store/m2_test.go b/internal/store/m2_test.go index e86e452..7ac36c8 100644 --- a/internal/store/m2_test.go +++ b/internal/store/m2_test.go @@ -252,11 +252,12 @@ func TestConfirmationMailIsCappedPerAddressAcrossLists(t *testing.T) { } func TestConfirmationMailHasAGlobalCap(t *testing.T) { + // 300 an hour, as documented: not derived from the constant. s := newTestStore(t) now := time.Unix(1_000_000, 0) s.Now = func() time.Time { return now } l, _ := s.CreateList("dev", "example.org", "Dev") - for i := 0; i < maxPendingPerHour; i++ { + for i := 0; i < 300; i++ { if _, created, err := s.CreatePending(l.ID, fmt.Sprintf("u%d@x.com", i), "join"); err != nil || !created { t.Fatalf("request %d: %v %v", i+1, created, err) } diff --git a/internal/store/outbox_test.go b/internal/store/outbox_test.go index bc11393..70d95a9 100644 --- a/internal/store/outbox_test.go +++ b/internal/store/outbox_test.go @@ -350,3 +350,21 @@ func TestMigrationAddsTheFailedColumn(t *testing.T) { t.Fatalf("an existing delivery stays waiting: %+v", due) } } + +func TestFailedComeBeforeDeliveriesThatRetriedMoreOften(t *testing.T) { + s := newTestStore(t) + s.Enqueue([]byte("body"), items(3)) + due, _ := s.DueOutbox(10) + for i := 0; i < 4; i++ { // a delivery that has been retried 4 times + s.RetryOutbox(due[1].ID, time.Now().Add(time.Hour), "down") + } + s.RetryOutbox(due[2].ID, time.Now().Add(time.Hour), "down") // retried once + s.FailOutbox(due[0].ID, "refused") // failed, with fewer attempts than the others + list, _ := s.ListOutbox(10) + if list[0].ID != due[0].ID || !list[0].Failed { + t.Fatalf("a failed delivery is listed first whatever the others' attempts: %+v", list) + } + if list[1].ID != due[1].ID { + t.Fatalf("then the most retried: %+v", list) + } +}