Skip to content

Commit 4dbdc64

Browse files
kolaentetink-bot
authored andcommitted
test: merge overlapping refresh rate limit subtests
1 parent 4864678 commit 4dbdc64

1 file changed

Lines changed: 15 additions & 24 deletions

File tree

pkg/webtests/token_refresh_rate_limit_test.go

Lines changed: 15 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package webtests
1818

1919
import (
2020
"net/http"
21+
"strconv"
2122
"testing"
2223

2324
"code.vikunja.io/api/pkg/config"
@@ -74,36 +75,26 @@ func TestTokenRefreshRateLimit(t *testing.T) {
7475
assert.Equal(t, "0", rec.Header().Get("X-RateLimit-Remaining"))
7576
})
7677

77-
// v2 counts against the same per-IP "tokenrefresh" budget as v1, so this
78-
// needs its own routes instance to start from a fresh one.
79-
t.Run("v2 renewals stay bounded", func(t *testing.T) {
80-
e := routes.NewEcho()
81-
routes.RegisterRoutes(e)
82-
83-
for i := 0; i < 4; i++ {
84-
rec := humaRequest(t, e, http.MethodPost, "/api/v2/user/token/refresh", "", "", "")
85-
require.Equal(t, http.StatusUnauthorized, rec.Code, "request %d, body: %s", i, rec.Body.String())
86-
assert.Equal(t, "4", rec.Header().Get("X-RateLimit-Limit"))
87-
}
88-
89-
rec := humaRequest(t, e, http.MethodPost, "/api/v2/user/token/refresh", "", "", "")
90-
assert.Equal(t, http.StatusTooManyRequests, rec.Code, "body: %s", rec.Body.String())
91-
assert.Equal(t, "0", rec.Header().Get("X-RateLimit-Remaining"))
92-
})
93-
94-
// Building the limiter per API version would give each its own counters,
95-
// doubling the effective budget with the default in-memory store.
78+
// Fresh instance: v1 and v2 count against the same per-IP budget, so reusing
79+
// the outer e would carry over budget already spent above.
9680
t.Run("v1 and v2 share the renewal budget", func(t *testing.T) {
9781
e := routes.NewEcho()
9882
routes.RegisterRoutes(e)
9983

100-
for i := 0; i < 3; i++ {
101-
rec := humaRequest(t, e, http.MethodPost, "/api/v2/user/token/refresh", "", "", "")
102-
require.NotEqual(t, http.StatusTooManyRequests, rec.Code, "request %d, body: %s", i, rec.Body.String())
84+
paths := []string{
85+
"/api/v2/user/token/refresh",
86+
"/api/v1/user/token/refresh",
87+
"/api/v2/user/token/refresh",
88+
"/api/v1/user/token/refresh",
89+
}
90+
for i, path := range paths {
91+
rec := humaRequest(t, e, http.MethodPost, path, "", "", "")
92+
require.NotEqual(t, http.StatusTooManyRequests, rec.Code, "request %d (%s), body: %s", i, path, rec.Body.String())
93+
assert.Equal(t, strconv.Itoa(len(paths)-i-1), rec.Header().Get("X-RateLimit-Remaining"), "request %d (%s)", i, path)
10394
}
10495

10596
rec := humaRequest(t, e, http.MethodPost, "/api/v1/user/token/refresh", "", "", "")
106-
require.NotEqual(t, http.StatusTooManyRequests, rec.Code, "body: %s", rec.Body.String())
107-
assert.Equal(t, "0", rec.Header().Get("X-RateLimit-Remaining"), "v1 must see the three v2 renewals already counted")
97+
assert.Equal(t, http.StatusTooManyRequests, rec.Code, "body: %s", rec.Body.String())
98+
assert.Equal(t, "0", rec.Header().Get("X-RateLimit-Remaining"))
10899
})
109100
}

0 commit comments

Comments
 (0)