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
This commit is contained in:
tgiachi 2026-11-09 20:43:41 +01:00
commit 7005edcb9b
3 changed files with 54 additions and 1 deletions

View file

@ -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)
}
}

View file

@ -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)
}

View file

@ -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)
}
}