Files
gitea/routers/api/v1/utils/sort_test.go
Matt Schoen a564f0587a feat(api): add sort and order query parameters to job list endpoints (#37672)
Adds `sort` and `order` query parameters to all action job list API
endpoints (`/admin/actions/jobs`, `/repos/{owner}/{repo}/actions/jobs`,
`/repos/{owner}/{repo}/actions/runs/{run}/jobs`, `/user/actions/jobs`),
following the existing `OrderByMap` pattern used by repo/user search
endpoints.

- Default is `id` / `asc` (backwards compatible — matches previous DB
natural order)
- Only `id` sort field for now; the map is extensible for future fields
- Returns 422 for invalid sort/order values
- `ToOrders()` returns empty string when `OrderBy` is unset, so internal
callers (webhook dispatch, concurrency checks) are unaffected

Closes: #37666
Supersedes: #37667
Co-authored-by: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Co-authored-by: silverwind <me@silverwind.io>
2026-05-13 13:11:02 +00:00

45 lines
1.1 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package utils
import (
"net/http"
"testing"
"code.gitea.io/gitea/models/db"
"code.gitea.io/gitea/services/contexttest"
"github.com/stretchr/testify/assert"
)
func TestResolveSortOrder(t *testing.T) {
m := map[string]map[string]db.SearchOrderBy{
"asc": {"id": "id ASC"},
"desc": {"id": "id DESC"},
}
defaultOrder := db.SearchOrderBy("default")
cases := []struct {
path string
wantOK bool
wantOrder db.SearchOrderBy
wantStatus int
}{
{"GET /", true, defaultOrder, 0},
{"GET /?sort=id", true, "id ASC", 0},
{"GET /?sort=id&order=desc", true, "id DESC", 0},
{"GET /?sort=bogus", false, "", http.StatusUnprocessableEntity},
{"GET /?sort=id&order=bogus", false, "", http.StatusUnprocessableEntity},
}
for _, tc := range cases {
t.Run(tc.path, func(t *testing.T) {
ctx, _ := contexttest.MockAPIContext(t, tc.path)
got, ok := ResolveSortOrder(ctx, m, defaultOrder)
assert.Equal(t, tc.wantOK, ok)
assert.Equal(t, tc.wantOrder, got)
assert.Equal(t, tc.wantStatus, ctx.Resp.WrittenStatus())
})
}
}