From 114f18f10594e41a500c135de81be776c18cef2a Mon Sep 17 00:00:00 2001 From: Gusted Date: Mon, 4 Dec 2023 19:27:57 +0100 Subject: [PATCH] [GITEA] Make pagination test more robust (squash) - Backport #1882 - Be more specific of which element we want and also don't include the href into the selector, so if the value changes, it will show the value that was rendered. - Ensure stable order of passed repository IDs. - Resolves codeberg.org/forgejo/forgejo/issues/1880 (cherry picked from commit 79bc4cffe5437179543ce5f0e8ebe0f1e2301216) --- routers/web/user/home.go | 4 ++++ tests/integration/user_test.go | 35 ++++++++++++++++------------------ 2 files changed, 20 insertions(+), 19 deletions(-) diff --git a/routers/web/user/home.go b/routers/web/user/home.go index 4c4a57734c..73ece7c1cc 100644 --- a/routers/web/user/home.go +++ b/routers/web/user/home.go @@ -295,6 +295,10 @@ func Milestones(ctx *context.Context) { 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 if isShowClosed { ctx.Data["State"] = "closed" diff --git a/tests/integration/user_test.go b/tests/integration/user_test.go index 2144e7f92b..d7fcad322e 100644 --- a/tests/integration/user_test.go +++ b/tests/integration/user_test.go @@ -6,6 +6,7 @@ package integration import ( "fmt" "net/http" + "net/http/httptest" "net/url" "testing" @@ -307,6 +308,15 @@ func TestPagination(t *testing.T) { 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 // the same link, so take that into consideration when writing asserts. t.Run("Issues", func(t *testing.T) { @@ -316,9 +326,7 @@ func TestPagination(t *testing.T) { req := NewRequest(t, "GET", "/issues") resp := session.MakeRequest(t, req, http.StatusOK) - htmlDoc := NewHTMLParser(t, resp.Body) - 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) + assertHref(t, resp, "/issues?page=2&q=&type=your_repositories&repos=%5B%5D&sort=recentupdate&state=open&labels=") }) t.Run("Selected repositories", func(t *testing.T) { defer tests.PrintCurrentTest(t)() @@ -327,9 +335,7 @@ func TestPagination(t *testing.T) { req := NewRequest(t, "GET", "/issues?repos="+escapedQuery) resp := session.MakeRequest(t, req, http.StatusOK) - htmlDoc := NewHTMLParser(t, resp.Body) - 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) + assertHref(t, resp, fmt.Sprintf(`/issues?page=2&q=&type=your_repositories&repos=%s&sort=recentupdate&state=open&labels=`, escapedQuery)) }) }) @@ -340,9 +346,7 @@ func TestPagination(t *testing.T) { req := NewRequest(t, "GET", "/pulls") resp := session.MakeRequest(t, req, http.StatusOK) - htmlDoc := NewHTMLParser(t, resp.Body) - 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) + assertHref(t, resp, "/pulls?page=2&q=&type=your_repositories&repos=%5B%5D&sort=recentupdate&state=open&labels=") }) t.Run("Selected repositories", func(t *testing.T) { defer tests.PrintCurrentTest(t)() @@ -351,9 +355,7 @@ func TestPagination(t *testing.T) { req := NewRequest(t, "GET", "/pulls?repos="+escapedQuery) resp := session.MakeRequest(t, req, http.StatusOK) - htmlDoc := NewHTMLParser(t, resp.Body) - 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) + assertHref(t, resp, fmt.Sprintf("/pulls?page=2&q=&type=your_repositories&repos=%s&sort=recentupdate&state=open&labels=", escapedQuery)) }) }) @@ -361,13 +363,10 @@ func TestPagination(t *testing.T) { t.Run("No selected repositories", func(t *testing.T) { defer tests.PrintCurrentTest(t)() - escapedQuery := url.QueryEscape("[42,1]") req := NewRequest(t, "GET", "/milestones") resp := session.MakeRequest(t, req, http.StatusOK) - htmlDoc := NewHTMLParser(t, resp.Body) - 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) + assertHref(t, resp, fmt.Sprintf(`/milestones?page=2&q=&repos=%s&sort=&state=open`, url.QueryEscape("[1,42]"))) }) t.Run("Selected repositories", func(t *testing.T) { defer tests.PrintCurrentTest(t)() @@ -376,9 +375,7 @@ func TestPagination(t *testing.T) { req := NewRequest(t, "GET", "/milestones?repos="+escapedQuery) resp := session.MakeRequest(t, req, http.StatusOK) - htmlDoc := NewHTMLParser(t, resp.Body) - 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) + assertHref(t, resp, fmt.Sprintf(`/milestones?page=2&q=&repos=%s&sort=&state=open`, escapedQuery)) }) }) }