Files
opencloud/pkg/middleware/ratelimit_test.go
T
Pascal Bleser 0169f3b0a5 graph: answer the export rate limit in the graph error format
A request to export the personal data beyond the rate limit got the
plain text 429 of httprate, while the other errors of graph, also those
of the same route, are JSON with an error code.

RateLimitPerAuthenticatedUser takes a handler for the response beyond
the limit now, so that a service can answer in its own format; nil
keeps the plain text 429. Graph answers with activityLimitReached. The
Retry-After header is set before the handler is called, so it stays.

Assisted-by: Claude Code:claude-opus-5-5
2026-11-13 18:25:47 +01:00

74 lines
2.7 KiB
Go

package middleware
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"time"
userv1beta1 "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1"
revactx "github.com/opencloud-eu/reva/v2/pkg/ctx"
"github.com/stretchr/testify/assert"
)
func TestRateLimitPerAuthenticatedUser(t *testing.T) {
ok := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { w.WriteHeader(http.StatusOK) })
// do sends a request as the given user, or without one if user is empty
do := func(h http.Handler, user, path string) *httptest.ResponseRecorder {
ctx := context.Background()
if user != "" {
ctx = revactx.ContextSetUser(ctx, &userv1beta1.User{Id: &userv1beta1.UserId{OpaqueId: user}})
}
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodPost, path, nil).WithContext(ctx))
return rr
}
t.Run("limits each user on their own", func(t *testing.T) {
h := RateLimitPerAuthenticatedUser(2, time.Minute, nil)(ok)
assert.Equal(t, http.StatusOK, do(h, "alice", "/a").Code)
assert.Equal(t, http.StatusOK, do(h, "alice", "/a").Code)
rr := do(h, "alice", "/a")
assert.Equal(t, http.StatusTooManyRequests, rr.Code)
assert.NotEmpty(t, rr.Header().Get("Retry-After"))
assert.Equal(t, http.StatusOK, do(h, "bob", "/a").Code)
})
t.Run("counts by user, not by URL", func(t *testing.T) {
h := RateLimitPerAuthenticatedUser(2, time.Minute, nil)(ok)
assert.Equal(t, http.StatusOK, do(h, "alice", "/a").Code)
assert.Equal(t, http.StatusOK, do(h, "alice", "/a/").Code)
assert.Equal(t, http.StatusTooManyRequests, do(h, "alice", "/b?x=1").Code)
})
t.Run("rejects requests without an authenticated user", func(t *testing.T) {
h := RateLimitPerAuthenticatedUser(2, time.Minute, nil)(ok)
assert.Equal(t, http.StatusUnauthorized, do(h, "", "/a").Code)
})
t.Run("a limit of 0 disables it", func(t *testing.T) {
h := RateLimitPerAuthenticatedUser(0, time.Minute, nil)(ok)
for range 5 {
assert.Equal(t, http.StatusOK, do(h, "alice", "/a").Code)
}
})
t.Run("answers beyond the limit with the given handler", func(t *testing.T) {
limitReached := func(w http.ResponseWriter, _ *http.Request) {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusTooManyRequests)
_, _ = w.Write([]byte(`{"error":"slow down"}`))
}
h := RateLimitPerAuthenticatedUser(1, time.Minute, limitReached)(ok)
assert.Equal(t, http.StatusOK, do(h, "alice", "/a").Code)
rr := do(h, "alice", "/a")
assert.Equal(t, http.StatusTooManyRequests, rr.Code)
assert.Equal(t, "application/json", rr.Header().Get("Content-Type"))
assert.JSONEq(t, `{"error":"slow down"}`, rr.Body.String())
// set before the handler is called
assert.NotEmpty(t, rr.Header().Get("Retry-After"))
})
}