test(web): trimming, Retry-After rounding and the exact edges of the limit window

- the typed address is trimmed before it is used
- Retry-After rounds up to whole seconds
- the hour ends after exactly an hour, and the sweep forgets such an address
- found by mutation testing: these edges survived the first tests
This commit is contained in:
tgiachi 2026-11-10 12:45:49 +01:00
commit 5ca091ccbf

View file

@ -667,3 +667,50 @@ func TestSubscribeLinkOnlyOnTheArchivePageOfAQualifyingList(t *testing.T) {
wantNoBody(t, get(t, f.h, fmt.Sprintf("/archive/dev@example.org/%d", id)), "/subscribe/") wantNoBody(t, get(t, f.h, fmt.Sprintf("/archive/dev@example.org/%d", id)), "/subscribe/")
wantNoBody(t, get(t, f.h, "/unsubscribe/"+f.bobTok), "/subscribe/") wantNoBody(t, get(t, f.h, "/unsubscribe/"+f.bobTok), "/subscribe/")
} }
func TestSubscribeTrimsTheTypedAddress(t *testing.T) {
f := newFixture(t)
f.s.AddMember(f.pub.ID, "member@x.com", "")
wantBody(t, subscribe(t, f, "dev@example.org", "192.0.2.1", email(" carol@x.com\t")), neutralText)
wantBody(t, subscribe(t, f, "dev@example.org", "192.0.2.2", email(" MEMBER@x.com ")), neutralText)
sent := f.flush(t)
if len(sent) != 1 || sent[0].To[0] != "carol@x.com" {
t.Fatalf("one mail, to the trimmed address: %+v", sent)
}
}
func TestSubscribeRetryAfterRoundsUp(t *testing.T) {
f := newFixture(t)
for i := 0; i < 10; i++ {
subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com"))
}
f.clock = f.clock.Add(10*time.Minute + 500*time.Millisecond)
rec := subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com"))
wantStatus(t, rec, 429)
if got := rec.Header().Get("Retry-After"); got != "3000" { // 2999.5 s left
t.Errorf("Retry-After = %q, want 3000", got)
}
}
// The hour is over when an hour has passed, not one tick later.
func TestSubscribeWindowEndsAfterExactlyAnHour(t *testing.T) {
f := newFixture(t)
for i := 0; i < 10; i++ {
subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com"))
}
f.clock = f.clock.Add(time.Hour - time.Nanosecond)
wantStatus(t, subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com")), 429)
f.clock = f.clock.Add(time.Nanosecond)
wantStatus(t, subscribe(t, f, "dev@example.org", "198.51.100.7", email("a@x.com")), 200)
}
func TestSubscribeLimiterSweepsAnAddressWhoseHourIsOver(t *testing.T) {
now := time.Date(2026, 1, 1, 0, 0, 0, 0, time.UTC)
l := newSubscribeLimiter(func() time.Time { return now })
l.allow("192.0.2.1")
now = now.Add(time.Hour) // exactly an hour since the first request
l.allow("192.0.2.2")
if l.size() != 1 {
t.Fatalf("size = %d, want 1 (the first address is forgotten)", l.size())
}
}