Merge pull request '[GITEA] Make pagination test more robust (squash)' (#1885) from Gusted/forgejo:forgejo-bp-1880 into v1.21/forgejo

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/1885
This commit is contained in:
Gusted 2023-12-05 22:50:42 +00:00
commit 69221d84cf
2 changed files with 20 additions and 19 deletions

View file

@ -295,6 +295,10 @@ func Milestones(ctx *context.Context) {
return !showRepoIds.Contains(v) return !showRepoIds.Contains(v)
}) })
// This is to ensure that testing the expected pagination link is stable.
// As there's no guarantee of order.
slices.Sort(repoIDs)
var pagerCount int var pagerCount int
if isShowClosed { if isShowClosed {
ctx.Data["State"] = "closed" ctx.Data["State"] = "closed"

View file

@ -6,6 +6,7 @@ package integration
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/http/httptest"
"net/url" "net/url"
"testing" "testing"
@ -307,6 +308,15 @@ func TestPagination(t *testing.T) {
session := loginUser(t, "user2") session := loginUser(t, "user2")
assertHref := func(t *testing.T, resp *httptest.ResponseRecorder, expectedHref string) {
t.Helper()
htmlDoc := NewHTMLParser(t, resp.Body)
href, ok := htmlDoc.Find(".pagination a.item").Not(".active").Not(".navigation").Attr("href")
assert.True(t, ok)
assert.EqualValues(t, expectedHref, href)
}
// Pagination links can be seen multiple times, due to 'last' or 'next' having // Pagination links can be seen multiple times, due to 'last' or 'next' having
// the same link, so take that into consideration when writing asserts. // the same link, so take that into consideration when writing asserts.
t.Run("Issues", func(t *testing.T) { t.Run("Issues", func(t *testing.T) {
@ -316,9 +326,7 @@ func TestPagination(t *testing.T) {
req := NewRequest(t, "GET", "/issues") req := NewRequest(t, "GET", "/issues")
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body) assertHref(t, resp, "/issues?page=2&q=&type=your_repositories&repos=%5B%5D&sort=recentupdate&state=open&labels=")
sel := htmlDoc.Find(`.pagination a.navigation[href="/issues?page=2&q=&type=your_repositories&repos=%5B%5D&sort=recentupdate&state=open&labels="]`)
assert.GreaterOrEqual(t, sel.Length(), 1)
}) })
t.Run("Selected repositories", func(t *testing.T) { t.Run("Selected repositories", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
@ -327,9 +335,7 @@ func TestPagination(t *testing.T) {
req := NewRequest(t, "GET", "/issues?repos="+escapedQuery) req := NewRequest(t, "GET", "/issues?repos="+escapedQuery)
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body) assertHref(t, resp, fmt.Sprintf(`/issues?page=2&q=&type=your_repositories&repos=%s&sort=recentupdate&state=open&labels=`, escapedQuery))
sel := htmlDoc.Find(fmt.Sprintf(`.pagination a.navigation[href="/issues?page=2&q=&type=your_repositories&repos=%s&sort=recentupdate&state=open&labels="]`, escapedQuery))
assert.GreaterOrEqual(t, sel.Length(), 1)
}) })
}) })
@ -340,9 +346,7 @@ func TestPagination(t *testing.T) {
req := NewRequest(t, "GET", "/pulls") req := NewRequest(t, "GET", "/pulls")
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body) assertHref(t, resp, "/pulls?page=2&q=&type=your_repositories&repos=%5B%5D&sort=recentupdate&state=open&labels=")
sel := htmlDoc.Find(`.pagination a.navigation[href="/pulls?page=2&q=&type=your_repositories&repos=%5B%5D&sort=recentupdate&state=open&labels="]`)
assert.GreaterOrEqual(t, sel.Length(), 1)
}) })
t.Run("Selected repositories", func(t *testing.T) { t.Run("Selected repositories", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
@ -351,9 +355,7 @@ func TestPagination(t *testing.T) {
req := NewRequest(t, "GET", "/pulls?repos="+escapedQuery) req := NewRequest(t, "GET", "/pulls?repos="+escapedQuery)
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body) assertHref(t, resp, fmt.Sprintf("/pulls?page=2&q=&type=your_repositories&repos=%s&sort=recentupdate&state=open&labels=", escapedQuery))
sel := htmlDoc.Find(fmt.Sprintf(`.pagination a.navigation[href="/pulls?page=2&q=&type=your_repositories&repos=%s&sort=recentupdate&state=open&labels="]`, escapedQuery))
assert.GreaterOrEqual(t, sel.Length(), 1)
}) })
}) })
@ -361,13 +363,10 @@ func TestPagination(t *testing.T) {
t.Run("No selected repositories", func(t *testing.T) { t.Run("No selected repositories", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
escapedQuery := url.QueryEscape("[42,1]")
req := NewRequest(t, "GET", "/milestones") req := NewRequest(t, "GET", "/milestones")
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body) assertHref(t, resp, fmt.Sprintf(`/milestones?page=2&q=&repos=%s&sort=&state=open`, url.QueryEscape("[1,42]")))
sel := htmlDoc.Find(fmt.Sprintf(`.pagination a.navigation[href="/milestones?page=2&q=&repos=%s&sort=&state=open"]`, escapedQuery))
assert.GreaterOrEqual(t, sel.Length(), 1)
}) })
t.Run("Selected repositories", func(t *testing.T) { t.Run("Selected repositories", func(t *testing.T) {
defer tests.PrintCurrentTest(t)() defer tests.PrintCurrentTest(t)()
@ -376,9 +375,7 @@ func TestPagination(t *testing.T) {
req := NewRequest(t, "GET", "/milestones?repos="+escapedQuery) req := NewRequest(t, "GET", "/milestones?repos="+escapedQuery)
resp := session.MakeRequest(t, req, http.StatusOK) resp := session.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body) assertHref(t, resp, fmt.Sprintf(`/milestones?page=2&q=&repos=%s&sort=&state=open`, escapedQuery))
sel := htmlDoc.Find(fmt.Sprintf(`.pagination a.navigation[href="/milestones?page=2&q=&repos=%s&sort=&state=open"]`, escapedQuery))
assert.GreaterOrEqual(t, sel.Length(), 1)
}) })
}) })
} }