|
| 1 | +// Copyright Earl Warren <[email protected]> |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "os" |
| 10 | + "path" |
| 11 | + "testing" |
| 12 | + "time" |
| 13 | + |
| 14 | + actions_model "code.gitea.io/gitea/models/actions" |
| 15 | + auth_model "code.gitea.io/gitea/models/auth" |
| 16 | + "code.gitea.io/gitea/models/db" |
| 17 | + repo_model "code.gitea.io/gitea/models/repo" |
| 18 | + "code.gitea.io/gitea/models/unittest" |
| 19 | + user_model "code.gitea.io/gitea/models/user" |
| 20 | + "code.gitea.io/gitea/modules/git" |
| 21 | + api "code.gitea.io/gitea/modules/structs" |
| 22 | + "code.gitea.io/gitea/tests" |
| 23 | + |
| 24 | + "github.com/stretchr/testify/assert" |
| 25 | +) |
| 26 | + |
| 27 | +func TestActionsUserGit(t *testing.T) { |
| 28 | + onGiteaRun(t, testActionsUserGit) |
| 29 | +} |
| 30 | + |
| 31 | +func NewActionsUserTestContext(t *testing.T, username, reponame string) APITestContext { |
| 32 | + t.Helper() |
| 33 | + |
| 34 | + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}) |
| 35 | + repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: username}) |
| 36 | + |
| 37 | + task := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 47}) |
| 38 | + task.RepoID = repo.ID |
| 39 | + task.OwnerID = repoOwner.ID |
| 40 | + task.GenerateToken() |
| 41 | + |
| 42 | + actions_model.UpdateTask(db.DefaultContext, task) |
| 43 | + return APITestContext{ |
| 44 | + Session: emptyTestSession(t), |
| 45 | + Token: task.Token, |
| 46 | + Username: username, |
| 47 | + Reponame: reponame, |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func testActionsUserGit(t *testing.T, u *url.URL) { |
| 52 | + username := "user2" |
| 53 | + reponame := "repo1" |
| 54 | + httpContext := NewAPITestContext(t, username, reponame, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) |
| 55 | + |
| 56 | + for _, testCase := range []struct { |
| 57 | + name string |
| 58 | + head string |
| 59 | + ctx APITestContext |
| 60 | + }{ |
| 61 | + { |
| 62 | + name: "UserTypeIndividual", |
| 63 | + head: "individualhead", |
| 64 | + ctx: httpContext, |
| 65 | + }, |
| 66 | + { |
| 67 | + name: "ActionsUser", |
| 68 | + head: "actionsuserhead", |
| 69 | + ctx: NewActionsUserTestContext(t, username, reponame), |
| 70 | + }, |
| 71 | + } { |
| 72 | + t.Run("CreatePR "+testCase.name, func(t *testing.T) { |
| 73 | + defer tests.PrintCurrentTest(t)() |
| 74 | + |
| 75 | + dstPath := t.TempDir() |
| 76 | + u.Path = httpContext.GitPath() |
| 77 | + u.User = url.UserPassword(httpContext.Username, userPassword) |
| 78 | + t.Run("Clone", doGitClone(dstPath, u)) |
| 79 | + t.Run("PopulateBranch", doActionsUserPopulateBranch(dstPath, &httpContext, "master", testCase.head)) |
| 80 | + t.Run("CreatePR", doActionsUserPR(httpContext, testCase.ctx, "master", testCase.head)) |
| 81 | + }) |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +func doActionsUserPopulateBranch(dstPath string, ctx *APITestContext, baseBranch, headBranch string) func(t *testing.T) { |
| 86 | + return func(t *testing.T) { |
| 87 | + defer tests.PrintCurrentTest(t)() |
| 88 | + |
| 89 | + t.Run("CreateHeadBranch", doGitCreateBranch(dstPath, headBranch)) |
| 90 | + |
| 91 | + t.Run("AddCommit", func(t *testing.T) { |
| 92 | + err := os.WriteFile(path.Join(dstPath, "test_file"), []byte("## test content"), 0o666) |
| 93 | + if !assert.NoError(t, err) { |
| 94 | + return |
| 95 | + } |
| 96 | + |
| 97 | + err = git.AddChanges(dstPath, true) |
| 98 | + assert.NoError(t, err) |
| 99 | + |
| 100 | + err = git.CommitChanges(dstPath, git.CommitChangesOptions{ |
| 101 | + Committer: &git.Signature{ |
| 102 | + |
| 103 | + Name: "user2", |
| 104 | + When: time.Now(), |
| 105 | + }, |
| 106 | + Author: &git.Signature{ |
| 107 | + |
| 108 | + Name: "user2", |
| 109 | + When: time.Now(), |
| 110 | + }, |
| 111 | + Message: "Testing commit 1", |
| 112 | + }) |
| 113 | + assert.NoError(t, err) |
| 114 | + }) |
| 115 | + |
| 116 | + t.Run("Push", func(t *testing.T) { |
| 117 | + err := git.NewCommand(git.DefaultContext, "push", "origin").AddDynamicArguments("HEAD:refs/heads/" + headBranch).Run(&git.RunOpts{Dir: dstPath}) |
| 118 | + assert.NoError(t, err) |
| 119 | + }) |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +func doActionsUserPR(ctx, doerCtx APITestContext, baseBranch, headBranch string) func(t *testing.T) { |
| 124 | + return func(t *testing.T) { |
| 125 | + defer tests.PrintCurrentTest(t)() |
| 126 | + var pr api.PullRequest |
| 127 | + var err error |
| 128 | + |
| 129 | + // Create a test pullrequest |
| 130 | + t.Run("CreatePullRequest", func(t *testing.T) { |
| 131 | + pr, err = doAPICreatePullRequest(doerCtx, ctx.Username, ctx.Reponame, baseBranch, headBranch)(t) |
| 132 | + assert.NoError(t, err) |
| 133 | + }) |
| 134 | + doerCtx.ExpectedCode = http.StatusCreated |
| 135 | + t.Run("AutoMergePR", doAPIAutoMergePullRequest(doerCtx, ctx.Username, ctx.Reponame, pr.Index)) |
| 136 | + // Ensure the PR page works |
| 137 | + t.Run("EnsureCanSeePull", doEnsureCanSeePull(ctx, pr)) |
| 138 | + } |
| 139 | +} |
0 commit comments