diff options
author | Anthony Wang | 2023-03-04 00:03:38 +0000 |
---|---|---|
committer | Anthony Wang | 2023-03-04 00:03:38 +0000 |
commit | b4640101f3c8938ed689743606a79601201141ca (patch) | |
tree | 8469fc5ef817560db646837b41417d50e2f8ebfc /tests | |
parent | dc20c2832871f6462990751ea802e14b02bf41b0 (diff) | |
parent | 8540fc45b11eff9a73753ca139f8ea5c38509bf5 (diff) |
Merge remote-tracking branch 'origin/main' into forgejo-federation
Diffstat (limited to 'tests')
-rw-r--r-- | tests/integration/api_packages_chef_test.go | 2 | ||||
-rw-r--r-- | tests/integration/api_packages_composer_test.go | 1 | ||||
-rw-r--r-- | tests/integration/api_packages_conan_test.go | 1 | ||||
-rw-r--r-- | tests/integration/api_packages_container_test.go | 4 | ||||
-rw-r--r-- | tests/integration/api_packages_generic_test.go | 1 | ||||
-rw-r--r-- | tests/integration/api_packages_helm_test.go | 1 | ||||
-rw-r--r-- | tests/integration/api_packages_maven_test.go | 1 | ||||
-rw-r--r-- | tests/integration/api_packages_npm_test.go | 1 | ||||
-rw-r--r-- | tests/integration/api_packages_pub_test.go | 1 | ||||
-rw-r--r-- | tests/integration/api_packages_pypi_test.go | 1 | ||||
-rw-r--r-- | tests/integration/api_packages_rubygems_test.go | 1 | ||||
-rw-r--r-- | tests/integration/api_packages_test.go | 28 | ||||
-rw-r--r-- | tests/integration/git_test.go | 15 | ||||
-rw-r--r-- | tests/integration/pull_status_test.go | 21 | ||||
-rw-r--r-- | tests/integration/repo_commits_test.go | 58 | ||||
-rw-r--r-- | tests/test_utils.go | 14 |
16 files changed, 127 insertions, 24 deletions
diff --git a/tests/integration/api_packages_chef_test.go b/tests/integration/api_packages_chef_test.go index 14baddca9..0ee43e174 100644 --- a/tests/integration/api_packages_chef_test.go +++ b/tests/integration/api_packages_chef_test.go @@ -11,7 +11,6 @@ import ( "crypto/rand" "crypto/rsa" "crypto/sha1" - "crypto/sha256" "crypto/x509" "encoding/base64" "encoding/pem" @@ -34,6 +33,7 @@ import ( chef_router "code.gitea.io/gitea/routers/api/packages/chef" "code.gitea.io/gitea/tests" + "github.com/minio/sha256-simd" "github.com/stretchr/testify/assert" ) diff --git a/tests/integration/api_packages_composer_test.go b/tests/integration/api_packages_composer_test.go index 87647cc47..ac1259056 100644 --- a/tests/integration/api_packages_composer_test.go +++ b/tests/integration/api_packages_composer_test.go @@ -25,6 +25,7 @@ import ( func TestPackageComposer(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) vendorName := "gitea" diff --git a/tests/integration/api_packages_conan_test.go b/tests/integration/api_packages_conan_test.go index 5ced388b4..209aeecd1 100644 --- a/tests/integration/api_packages_conan_test.go +++ b/tests/integration/api_packages_conan_test.go @@ -205,6 +205,7 @@ func uploadConanPackageV2(t *testing.T, baseURL, token, name, version, user, cha func TestPackageConan(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) name := "ConanPackage" diff --git a/tests/integration/api_packages_container_test.go b/tests/integration/api_packages_container_test.go index 3d9319f37..d925fd164 100644 --- a/tests/integration/api_packages_container_test.go +++ b/tests/integration/api_packages_container_test.go @@ -5,7 +5,6 @@ package integration import ( "bytes" - "crypto/sha256" "encoding/base64" "fmt" "net/http" @@ -24,6 +23,7 @@ import ( api "code.gitea.io/gitea/modules/structs" "code.gitea.io/gitea/tests" + "github.com/minio/sha256-simd" oci "github.com/opencontainers/image-spec/specs-go/v1" "github.com/stretchr/testify/assert" ) @@ -640,7 +640,7 @@ func TestPackageContainer(t *testing.T) { checkCatalog := func(owner string) func(t *testing.T) { return func(t *testing.T) { - defer tests.PrepareTestEnv(t)() + defer tests.PrintCurrentTest(t)() req := NewRequest(t, "GET", fmt.Sprintf("%sv2/_catalog", setting.AppURL)) addTokenAuthHeader(req, userToken) diff --git a/tests/integration/api_packages_generic_test.go b/tests/integration/api_packages_generic_test.go index 875b9ed96..765d11fd8 100644 --- a/tests/integration/api_packages_generic_test.go +++ b/tests/integration/api_packages_generic_test.go @@ -21,6 +21,7 @@ import ( func TestPackageGeneric(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) packageName := "te-st_pac.kage" diff --git a/tests/integration/api_packages_helm_test.go b/tests/integration/api_packages_helm_test.go index 4a7d56343..4f6145207 100644 --- a/tests/integration/api_packages_helm_test.go +++ b/tests/integration/api_packages_helm_test.go @@ -26,6 +26,7 @@ import ( func TestPackageHelm(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) packageName := "test-chart" diff --git a/tests/integration/api_packages_maven_test.go b/tests/integration/api_packages_maven_test.go index 5c0dbfc99..81112f305 100644 --- a/tests/integration/api_packages_maven_test.go +++ b/tests/integration/api_packages_maven_test.go @@ -22,6 +22,7 @@ import ( func TestPackageMaven(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) groupID := "com.gitea" diff --git a/tests/integration/api_packages_npm_test.go b/tests/integration/api_packages_npm_test.go index 39d1c91e8..6c4503f7e 100644 --- a/tests/integration/api_packages_npm_test.go +++ b/tests/integration/api_packages_npm_test.go @@ -24,6 +24,7 @@ import ( func TestPackageNpm(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) token := fmt.Sprintf("Bearer %s", getTokenForLoggedInUser(t, loginUser(t, user.Name))) diff --git a/tests/integration/api_packages_pub_test.go b/tests/integration/api_packages_pub_test.go index 325401fe7..24975ab62 100644 --- a/tests/integration/api_packages_pub_test.go +++ b/tests/integration/api_packages_pub_test.go @@ -27,6 +27,7 @@ import ( func TestPackagePub(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) token := "Bearer " + getUserToken(t, user.Name) diff --git a/tests/integration/api_packages_pypi_test.go b/tests/integration/api_packages_pypi_test.go index 71197f92a..c7c466e73 100644 --- a/tests/integration/api_packages_pypi_test.go +++ b/tests/integration/api_packages_pypi_test.go @@ -25,6 +25,7 @@ import ( func TestPackagePyPI(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) packageName := "test-package" diff --git a/tests/integration/api_packages_rubygems_test.go b/tests/integration/api_packages_rubygems_test.go index c85ff9aaf..2099357cb 100644 --- a/tests/integration/api_packages_rubygems_test.go +++ b/tests/integration/api_packages_rubygems_test.go @@ -23,6 +23,7 @@ import ( func TestPackageRubyGems(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) packageName := "gitea" diff --git a/tests/integration/api_packages_test.go b/tests/integration/api_packages_test.go index 39852e212..4228003e2 100644 --- a/tests/integration/api_packages_test.go +++ b/tests/integration/api_packages_test.go @@ -5,7 +5,6 @@ package integration import ( "bytes" - "crypto/sha256" "fmt" "net/http" "strings" @@ -20,10 +19,12 @@ import ( user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/util" packages_service "code.gitea.io/gitea/services/packages" packages_cleanup_service "code.gitea.io/gitea/services/packages/cleanup" "code.gitea.io/gitea/tests" + "github.com/minio/sha256-simd" "github.com/stretchr/testify/assert" ) @@ -235,16 +236,35 @@ func TestPackageQuota(t *testing.T) { func TestPackageCleanup(t *testing.T) { defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + duration, _ := time.ParseDuration("-1h") t.Run("Common", func(t *testing.T) { defer tests.PrintCurrentTest(t)() + // Upload and delete a generic package and upload a container blob + data, _ := util.CryptoRandomBytes(5) + url := fmt.Sprintf("/api/packages/%s/generic/cleanup-test/1.1.1/file.bin", user.Name) + req := NewRequestWithBody(t, "PUT", url, bytes.NewReader(data)) + AddBasicAuthHeader(req, user.Name) + MakeRequest(t, req, http.StatusCreated) + + req = NewRequest(t, "DELETE", url) + AddBasicAuthHeader(req, user.Name) + MakeRequest(t, req, http.StatusNoContent) + + data, _ = util.CryptoRandomBytes(5) + url = fmt.Sprintf("/v2/%s/cleanup-test/blobs/uploads?digest=sha256:%x", user.Name, sha256.Sum256(data)) + req = NewRequestWithBody(t, "POST", url, bytes.NewReader(data)) + AddBasicAuthHeader(req, user.Name) + MakeRequest(t, req, http.StatusCreated) + pbs, err := packages_model.FindExpiredUnreferencedBlobs(db.DefaultContext, duration) assert.NoError(t, err) assert.NotEmpty(t, pbs) - _, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, 2, packages_model.TypeContainer, "test", container_model.UploadVersion) + _, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, "cleanup-test", container_model.UploadVersion) assert.NoError(t, err) err = packages_cleanup_service.Cleanup(db.DefaultContext, duration) @@ -254,15 +274,13 @@ func TestPackageCleanup(t *testing.T) { assert.NoError(t, err) assert.Empty(t, pbs) - _, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, 2, packages_model.TypeContainer, "test", container_model.UploadVersion) + _, err = packages_model.GetInternalVersionByNameAndVersion(db.DefaultContext, user.ID, packages_model.TypeContainer, "cleanup-test", container_model.UploadVersion) assert.ErrorIs(t, err, packages_model.ErrPackageNotExist) }) t.Run("CleanupRules", func(t *testing.T) { defer tests.PrintCurrentTest(t)() - user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) - type version struct { Version string ShouldExist bool diff --git a/tests/integration/git_test.go b/tests/integration/git_test.go index 420a8676b..d21f3994a 100644 --- a/tests/integration/git_test.go +++ b/tests/integration/git_test.go @@ -630,8 +630,17 @@ func doAutoPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) { commitID := path.Base(commitURL) + addCommitStatus := func(status api.CommitStatusState) func(*testing.T) { + return doAPICreateCommitStatus(ctx, commitID, api.CreateStatusOption{ + State: status, + TargetURL: "http://test.ci/", + Description: "", + Context: "testci", + }) + } + // Call API to add Pending status for commit - t.Run("CreateStatus", doAPICreateCommitStatus(ctx, commitID, api.CommitStatusPending)) + t.Run("CreateStatus", addCommitStatus(api.CommitStatusPending)) // Cancel not existing auto merge ctx.ExpectedCode = http.StatusNotFound @@ -660,7 +669,7 @@ func doAutoPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) { assert.False(t, pr.HasMerged) // Call API to add Failure status for commit - t.Run("CreateStatus", doAPICreateCommitStatus(ctx, commitID, api.CommitStatusFailure)) + t.Run("CreateStatus", addCommitStatus(api.CommitStatusFailure)) // Check pr status pr, err = doAPIGetPullRequest(ctx, baseCtx.Username, baseCtx.Reponame, pr.Index)(t) @@ -668,7 +677,7 @@ func doAutoPRMerge(baseCtx *APITestContext, dstPath string) func(t *testing.T) { assert.False(t, pr.HasMerged) // Call API to add Success status for commit - t.Run("CreateStatus", doAPICreateCommitStatus(ctx, commitID, api.CommitStatusSuccess)) + t.Run("CreateStatus", addCommitStatus(api.CommitStatusSuccess)) // wait to let gitea merge stuff time.Sleep(time.Second) diff --git a/tests/integration/pull_status_test.go b/tests/integration/pull_status_test.go index e60d17edc..736d1ee4f 100644 --- a/tests/integration/pull_status_test.go +++ b/tests/integration/pull_status_test.go @@ -70,7 +70,12 @@ func TestPullCreate_CommitStatus(t *testing.T) { for _, status := range statusList { // Call API to add status for commit - t.Run("CreateStatus", doAPICreateCommitStatus(testCtx, commitID, status)) + t.Run("CreateStatus", doAPICreateCommitStatus(testCtx, commitID, api.CreateStatusOption{ + State: status, + TargetURL: "http://test.ci/", + Description: "", + Context: "testci", + })) req = NewRequestf(t, "GET", "/user1/repo1/pulls/1/commits") resp = session.MakeRequest(t, req, http.StatusOK) @@ -88,15 +93,13 @@ func TestPullCreate_CommitStatus(t *testing.T) { }) } -func doAPICreateCommitStatus(ctx APITestContext, commitID string, status api.CommitStatusState) func(*testing.T) { +func doAPICreateCommitStatus(ctx APITestContext, commitID string, data api.CreateStatusOption) func(*testing.T) { return func(t *testing.T) { - req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/statuses/%s?token=%s", ctx.Username, ctx.Reponame, commitID, ctx.Token), - api.CreateStatusOption{ - State: status, - TargetURL: "http://test.ci/", - Description: "", - Context: "testci", - }, + req := NewRequestWithJSON( + t, + http.MethodPost, + fmt.Sprintf("/api/v1/repos/%s/%s/statuses/%s?token=%s", ctx.Username, ctx.Reponame, commitID, ctx.Token), + data, ) if ctx.ExpectedCode != 0 { ctx.Session.MakeRequest(t, req, ctx.ExpectedCode) diff --git a/tests/integration/repo_commits_test.go b/tests/integration/repo_commits_test.go index cbd83c6de..e74e3867f 100644 --- a/tests/integration/repo_commits_test.go +++ b/tests/integration/repo_commits_test.go @@ -52,14 +52,19 @@ func doTestRepoCommitWithStatus(t *testing.T, state string, classes ...string) { // Call API to add status for commit ctx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeRepo) - t.Run("CreateStatus", doAPICreateCommitStatus(ctx, path.Base(commitURL), api.CommitStatusState(state))) + t.Run("CreateStatus", doAPICreateCommitStatus(ctx, path.Base(commitURL), api.CreateStatusOption{ + State: api.CommitStatusState(state), + TargetURL: "http://test.ci/", + Description: "", + Context: "testci", + })) req = NewRequest(t, "GET", "/user2/repo1/commits/branch/master") resp = session.MakeRequest(t, req, http.StatusOK) doc = NewHTMLParser(t, resp.Body) - // Check if commit status is displayed in message column - sel := doc.doc.Find("#commits-table tbody tr td.message a.commit-statuses-trigger .commit-status") + // Check if commit status is displayed in message column (.tippy-target to ignore the tippy trigger) + sel := doc.doc.Find("#commits-table tbody tr td.message .tippy-target .commit-status") assert.Equal(t, 1, sel.Length()) for _, class := range classes { assert.True(t, sel.HasClass(class)) @@ -145,7 +150,12 @@ func TestRepoCommitsStatusParallel(t *testing.T) { go func(parentT *testing.T, i int) { parentT.Run(fmt.Sprintf("ParallelCreateStatus_%d", i), func(t *testing.T) { ctx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeRepoStatus) - runBody := doAPICreateCommitStatus(ctx, path.Base(commitURL), api.CommitStatusState("pending")) + runBody := doAPICreateCommitStatus(ctx, path.Base(commitURL), api.CreateStatusOption{ + State: api.CommitStatusPending, + TargetURL: "http://test.ci/", + Description: "", + Context: "testci", + }) runBody(t) wg.Done() }) @@ -153,3 +163,43 @@ func TestRepoCommitsStatusParallel(t *testing.T) { } wg.Wait() } + +func TestRepoCommitsStatusMultiple(t *testing.T) { + defer tests.PrepareTestEnv(t)() + + session := loginUser(t, "user2") + + // Request repository commits page + req := NewRequest(t, "GET", "/user2/repo1/commits/branch/master") + resp := session.MakeRequest(t, req, http.StatusOK) + + doc := NewHTMLParser(t, resp.Body) + // Get first commit URL + commitURL, exists := doc.doc.Find("#commits-table tbody tr td.sha a").Attr("href") + assert.True(t, exists) + assert.NotEmpty(t, commitURL) + + // Call API to add status for commit + ctx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeRepo) + t.Run("CreateStatus", doAPICreateCommitStatus(ctx, path.Base(commitURL), api.CreateStatusOption{ + State: api.CommitStatusSuccess, + TargetURL: "http://test.ci/", + Description: "", + Context: "testci", + })) + + t.Run("CreateStatus", doAPICreateCommitStatus(ctx, path.Base(commitURL), api.CreateStatusOption{ + State: api.CommitStatusSuccess, + TargetURL: "http://test.ci/", + Description: "", + Context: "other_context", + })) + + req = NewRequest(t, "GET", "/user2/repo1/commits/branch/master") + resp = session.MakeRequest(t, req, http.StatusOK) + + doc = NewHTMLParser(t, resp.Body) + // Check that the data-tippy="commit-statuses" (for trigger) and commit-status (svg) are present + sel := doc.doc.Find("#commits-table tbody tr td.message [data-tippy=\"commit-statuses\"] .commit-status") + assert.Equal(t, 1, sel.Length()) +} diff --git a/tests/test_utils.go b/tests/test_utils.go index 9e9f97a5f..5cc31b814 100644 --- a/tests/test_utils.go +++ b/tests/test_utils.go @@ -13,6 +13,8 @@ import ( "runtime" "testing" + "code.gitea.io/gitea/models/db" + packages_model "code.gitea.io/gitea/models/packages" "code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/modules/base" "code.gitea.io/gitea/modules/git" @@ -204,6 +206,18 @@ func PrepareTestEnv(t testing.TB, skip ...int) func() { return err })) + // clear all package data + assert.NoError(t, db.TruncateBeans(db.DefaultContext, + &packages_model.Package{}, + &packages_model.PackageVersion{}, + &packages_model.PackageFile{}, + &packages_model.PackageBlob{}, + &packages_model.PackageProperty{}, + &packages_model.PackageBlobUpload{}, + &packages_model.PackageCleanupRule{}, + )) + assert.NoError(t, storage.Clean(storage.Packages)) + return deferFn } |