diff options
author | 6543 <6543@obermui.de> | 2021-06-17 10:58:10 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-06-17 10:58:10 +0200 |
commit | 6ad5d0a3062966515730aa1f8d62db5d2a7704ee (patch) | |
tree | 711b5814e4eb096e88b6f2ccbe597b0b00029275 /integrations | |
parent | c9d053f0caa14e6fec4d04c009c79c9a0940c450 (diff) | |
download | gitea-6ad5d0a3062966515730aa1f8d62db5d2a7704ee.tar.gz gitea-6ad5d0a3062966515730aa1f8d62db5d2a7704ee.zip |
[API] ListReleases add filter for draft and pre-releases (#16175)
* invent ctx.QueryOptionalBool
* [API] ListReleases add draft and pre-release filter
* Add X-Total-Count header
* Add a release to fixtures
* Add TEST for API ListReleases
Diffstat (limited to 'integrations')
-rw-r--r-- | integrations/api_releases_test.go | 53 | ||||
-rw-r--r-- | integrations/api_repo_test.go | 2 | ||||
-rw-r--r-- | integrations/release_test.go | 23 |
3 files changed, 67 insertions, 11 deletions
diff --git a/integrations/api_releases_test.go b/integrations/api_releases_test.go index 26bf752cca..027b282036 100644 --- a/integrations/api_releases_test.go +++ b/integrations/api_releases_test.go @@ -7,6 +7,7 @@ package integrations import ( "fmt" "net/http" + "net/url" "testing" "code.gitea.io/gitea/models" @@ -16,6 +17,58 @@ import ( "github.com/stretchr/testify/assert" ) +func TestAPIListReleases(t *testing.T) { + defer prepareTestEnv(t)() + + repo := models.AssertExistsAndLoadBean(t, &models.Repository{ID: 1}).(*models.Repository) + user2 := models.AssertExistsAndLoadBean(t, &models.User{ID: 2}).(*models.User) + session := loginUser(t, user2.LowerName) + token := getTokenForLoggedInUser(t, session) + + link, _ := url.Parse(fmt.Sprintf("/api/v1/repos/%s/%s/releases", user2.Name, repo.Name)) + link.RawQuery = url.Values{"token": {token}}.Encode() + resp := session.MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK) + var apiReleases []*api.Release + DecodeJSON(t, resp, &apiReleases) + if assert.Len(t, apiReleases, 3) { + for _, release := range apiReleases { + switch release.ID { + case 1: + assert.False(t, release.IsDraft) + assert.False(t, release.IsPrerelease) + case 4: + assert.True(t, release.IsDraft) + assert.False(t, release.IsPrerelease) + case 5: + assert.False(t, release.IsDraft) + assert.True(t, release.IsPrerelease) + default: + assert.NoError(t, fmt.Errorf("unexpected release: %v", release)) + } + } + } + + // test filter + testFilterByLen := func(auth bool, query url.Values, expectedLength int, msgAndArgs ...string) { + link.RawQuery = query.Encode() + if auth { + query.Set("token", token) + resp = session.MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK) + } else { + resp = MakeRequest(t, NewRequest(t, "GET", link.String()), http.StatusOK) + } + DecodeJSON(t, resp, &apiReleases) + assert.Len(t, apiReleases, expectedLength, msgAndArgs) + } + + testFilterByLen(false, url.Values{"draft": {"true"}}, 0, "anon should not see drafts") + testFilterByLen(true, url.Values{"draft": {"true"}}, 1, "repo owner should see drafts") + testFilterByLen(true, url.Values{"draft": {"false"}}, 2, "exclude drafts") + testFilterByLen(true, url.Values{"draft": {"false"}, "pre-release": {"false"}}, 1, "exclude drafts and pre-releases") + testFilterByLen(true, url.Values{"pre-release": {"true"}}, 1, "only get pre-release") + testFilterByLen(true, url.Values{"draft": {"true"}, "pre-release": {"true"}}, 0, "there is no pre-release draft") +} + func createNewReleaseUsingAPI(t *testing.T, session *TestSession, token string, owner *models.User, repo *models.Repository, name, target, title, desc string) *api.Release { urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases?token=%s", owner.Name, repo.Name, token) diff --git a/integrations/api_repo_test.go b/integrations/api_repo_test.go index cfd3b58d64..1ca4575508 100644 --- a/integrations/api_repo_test.go +++ b/integrations/api_repo_test.go @@ -223,7 +223,7 @@ func TestAPIViewRepo(t *testing.T) { DecodeJSON(t, resp, &repo) assert.EqualValues(t, 1, repo.ID) assert.EqualValues(t, "repo1", repo.Name) - assert.EqualValues(t, 2, repo.Releases) + assert.EqualValues(t, 3, repo.Releases) assert.EqualValues(t, 1, repo.OpenIssues) assert.EqualValues(t, 3, repo.OpenPulls) diff --git a/integrations/release_test.go b/integrations/release_test.go index 365bc04d87..ac5df315d5 100644 --- a/integrations/release_test.go +++ b/integrations/release_test.go @@ -85,7 +85,7 @@ func TestCreateRelease(t *testing.T) { session := loginUser(t, "user2") createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, false) - checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 3) + checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.stable"), 4) } func TestCreateReleasePreRelease(t *testing.T) { @@ -94,7 +94,7 @@ func TestCreateReleasePreRelease(t *testing.T) { session := loginUser(t, "user2") createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", true, false) - checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 3) + checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.prerelease"), 4) } func TestCreateReleaseDraft(t *testing.T) { @@ -103,7 +103,7 @@ func TestCreateReleaseDraft(t *testing.T) { session := loginUser(t, "user2") createNewRelease(t, session, "/user2/repo1", "v0.0.1", "v0.0.1", false, true) - checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 3) + checkLatestReleaseAndCount(t, session, "/user2/repo1", "v0.0.1", i18n.Tr("en", "repo.release.draft"), 4) } func TestCreateReleasePaging(t *testing.T) { @@ -142,7 +142,7 @@ func TestViewReleaseListNoLogin(t *testing.T) { htmlDoc := NewHTMLParser(t, rsp.Body) releases := htmlDoc.Find("#release-list li.ui.grid") - assert.Equal(t, 1, releases.Length()) + assert.Equal(t, 2, releases.Length()) links := make([]string, 0, 5) releases.Each(func(i int, s *goquery.Selection) { @@ -153,7 +153,7 @@ func TestViewReleaseListNoLogin(t *testing.T) { links = append(links, link) }) - assert.EqualValues(t, []string{"/user2/repo1/releases/tag/v1.1"}, links) + assert.EqualValues(t, []string{"/user2/repo1/releases/tag/v1.0", "/user2/repo1/releases/tag/v1.1"}, links) } func TestViewReleaseListLogin(t *testing.T) { @@ -169,7 +169,7 @@ func TestViewReleaseListLogin(t *testing.T) { htmlDoc := NewHTMLParser(t, rsp.Body) releases := htmlDoc.Find("#release-list li.ui.grid") - assert.Equal(t, 2, releases.Length()) + assert.Equal(t, 3, releases.Length()) links := make([]string, 0, 5) releases.Each(func(i int, s *goquery.Selection) { @@ -180,8 +180,11 @@ func TestViewReleaseListLogin(t *testing.T) { links = append(links, link) }) - assert.EqualValues(t, []string{"/user2/repo1/releases/tag/draft-release", - "/user2/repo1/releases/tag/v1.1"}, links) + assert.EqualValues(t, []string{ + "/user2/repo1/releases/tag/draft-release", + "/user2/repo1/releases/tag/v1.0", + "/user2/repo1/releases/tag/v1.1", + }, links) } func TestViewTagsList(t *testing.T) { @@ -197,12 +200,12 @@ func TestViewTagsList(t *testing.T) { htmlDoc := NewHTMLParser(t, rsp.Body) tags := htmlDoc.Find(".tag-list tr") - assert.Equal(t, 2, tags.Length()) + assert.Equal(t, 3, tags.Length()) tagNames := make([]string, 0, 5) tags.Each(func(i int, s *goquery.Selection) { tagNames = append(tagNames, s.Find(".tag a.df.ac").Text()) }) - assert.EqualValues(t, []string{"delete-tag", "v1.1"}, tagNames) + assert.EqualValues(t, []string{"v1.0", "delete-tag", "v1.1"}, tagNames) } |