Count downloads for tag archives

This commit is contained in:
JakobDev 2024-04-02 16:34:57 +02:00
parent f8a5d6872c
commit 613e5387c5
22 changed files with 469 additions and 95 deletions

View file

@ -319,3 +319,39 @@ func TestAPIUploadAssetRelease(t *testing.T) {
assert.EqualValues(t, 104, attachment.Size)
})
}
func TestAPIGetReleaseArchiveDownloadCount(t *testing.T) {
defer tests.PrepareTestEnv(t)()
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
session := loginUser(t, owner.LowerName)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
name := "ReleaseDownloadCount"
createNewReleaseUsingAPI(t, session, token, owner, repo, name, "", name, "test")
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/releases/tags/%s", owner.Name, repo.Name, name)
req := NewRequest(t, "GET", urlStr)
resp := MakeRequest(t, req, http.StatusOK)
var release *api.Release
DecodeJSON(t, resp, &release)
// Check if everything defaults to 0
assert.Equal(t, int64(0), release.ArchiveDownloadCount.TarGz)
assert.Equal(t, int64(0), release.ArchiveDownloadCount.Zip)
// Download the tarball to increase the count
MakeRequest(t, NewRequest(t, "GET", release.TarURL), http.StatusOK)
// Check if the count has increased
resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &release)
assert.Equal(t, int64(1), release.ArchiveDownloadCount.TarGz)
assert.Equal(t, int64(0), release.ArchiveDownloadCount.Zip)
}

View file

@ -85,3 +85,39 @@ func createNewTagUsingAPI(t *testing.T, session *TestSession, token, ownerName,
DecodeJSON(t, resp, &respObj)
return &respObj
}
func TestAPIGetTagArchiveDownloadCount(t *testing.T) {
defer tests.PrepareTestEnv(t)()
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
// Login as User2.
session := loginUser(t, user.Name)
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
repoName := "repo1"
tagName := "TagDownloadCount"
createNewTagUsingAPI(t, session, token, user.Name, repoName, tagName, "", "")
urlStr := fmt.Sprintf("/api/v1/repos/%s/%s/tags/%s?token=%s", user.Name, repoName, tagName, token)
req := NewRequest(t, "GET", urlStr)
resp := MakeRequest(t, req, http.StatusOK)
var tagInfo *api.Tag
DecodeJSON(t, resp, &tagInfo)
// Check if everything defaults to 0
assert.Equal(t, int64(0), tagInfo.ArchiveDownloadCount.TarGz)
assert.Equal(t, int64(0), tagInfo.ArchiveDownloadCount.Zip)
// Download the tarball to increase the count
MakeRequest(t, NewRequest(t, "GET", tagInfo.TarballURL), http.StatusOK)
// Check if the count has increased
resp = MakeRequest(t, req, http.StatusOK)
DecodeJSON(t, resp, &tagInfo)
assert.Equal(t, int64(1), tagInfo.ArchiveDownloadCount.TarGz)
assert.Equal(t, int64(0), tagInfo.ArchiveDownloadCount.Zip)
}