From 61f36020cdfb27ad1ad3b62bf011e5b801091d2f Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Mon, 22 Apr 2024 23:46:26 +0200 Subject: [PATCH] Fix user search paging When searching for users, page the results by default, and respect the default paging limits. This makes queries like '/api/v1/users/search?limit=1' actually work. Signed-off-by: Gergely Nagy (cherry picked from commit 9b85f97835a9b2f8e6af97bbec27c59210c2c94e) --- models/user/search.go | 2 +- tests/integration/api_user_search_test.go | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/models/user/search.go b/models/user/search.go index 45b051187e..a710b70287 100644 --- a/models/user/search.go +++ b/models/user/search.go @@ -140,7 +140,7 @@ func SearchUsers(ctx context.Context, opts *SearchUserOptions) (users []*User, _ sessQuery := opts.toSearchQueryBase(ctx).OrderBy(opts.OrderBy.String()) defer sessQuery.Close() - if opts.Page != 0 { + if opts.PageSize > 0 { sessQuery = db.SetSessionPagination(sessQuery, opts) } diff --git a/tests/integration/api_user_search_test.go b/tests/integration/api_user_search_test.go index f776b35325..0e01b504cc 100644 --- a/tests/integration/api_user_search_test.go +++ b/tests/integration/api_user_search_test.go @@ -10,7 +10,9 @@ import ( auth_model "code.gitea.io/gitea/models/auth" "code.gitea.io/gitea/models/unittest" user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/test" "code.gitea.io/gitea/tests" "github.com/stretchr/testify/assert" @@ -57,6 +59,25 @@ func TestAPIUserSearchNotLoggedIn(t *testing.T) { } } +func TestAPIUserSearchPaged(t *testing.T) { + defer tests.PrepareTestEnv(t)() + defer test.MockVariableValue(&setting.API.DefaultPagingNum, 5)() + + req := NewRequest(t, "GET", "/api/v1/users/search?limit=1") + resp := MakeRequest(t, req, http.StatusOK) + + var limitedResults SearchResults + DecodeJSON(t, resp, &limitedResults) + assert.Len(t, limitedResults.Data, 1) + + req = NewRequest(t, "GET", "/api/v1/users/search") + resp = MakeRequest(t, req, http.StatusOK) + + var results SearchResults + DecodeJSON(t, resp, &results) + assert.Len(t, results.Data, 5) +} + func TestAPIUserSearchSystemUsers(t *testing.T) { defer tests.PrepareTestEnv(t)() for _, systemUser := range []*user_model.User{