summaryrefslogtreecommitdiffstats
path: root/tests/integration/forgejo_git_test.go
blob: ebad074e14aa0756a27e88cf12a19bc13f15491d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright Earl Warren <contact@earl-warren.org>
// SPDX-License-Identifier: MIT

package integration

import (
	"net/http"
	"net/url"
	"os"
	"path"
	"testing"
	"time"

	actions_model "code.gitea.io/gitea/models/actions"
	auth_model "code.gitea.io/gitea/models/auth"
	"code.gitea.io/gitea/models/db"
	repo_model "code.gitea.io/gitea/models/repo"
	"code.gitea.io/gitea/models/unittest"
	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/git"
	api "code.gitea.io/gitea/modules/structs"
	"code.gitea.io/gitea/tests"

	"github.com/stretchr/testify/require"
)

func TestActionsUserGit(t *testing.T) {
	onGiteaRun(t, testActionsUserGit)
}

func NewActionsUserTestContext(t *testing.T, username, reponame string) APITestContext {
	t.Helper()

	repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame})
	repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: username})

	task := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 47})
	task.RepoID = repo.ID
	task.OwnerID = repoOwner.ID
	task.GenerateToken()

	actions_model.UpdateTask(db.DefaultContext, task)
	return APITestContext{
		Session:  emptyTestSession(t),
		Token:    task.Token,
		Username: username,
		Reponame: reponame,
	}
}

func testActionsUserGit(t *testing.T, u *url.URL) {
	username := "user2"
	reponame := "repo1"
	httpContext := NewAPITestContext(t, username, reponame, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)

	for _, testCase := range []struct {
		name string
		head string
		ctx  APITestContext
	}{
		{
			name: "UserTypeIndividual",
			head: "individualhead",
			ctx:  httpContext,
		},
		{
			name: "ActionsUser",
			head: "actionsuserhead",
			ctx:  NewActionsUserTestContext(t, username, reponame),
		},
	} {
		t.Run("CreatePR "+testCase.name, func(t *testing.T) {
			defer tests.PrintCurrentTest(t)()

			dstPath := t.TempDir()
			u.Path = httpContext.GitPath()
			u.User = url.UserPassword(httpContext.Username, userPassword)
			t.Run("Clone", doGitClone(dstPath, u))
			t.Run("PopulateBranch", doActionsUserPopulateBranch(dstPath, &httpContext, "master", testCase.head))
			t.Run("CreatePR", doActionsUserPR(httpContext, testCase.ctx, "master", testCase.head))
		})
	}
}

func doActionsUserPopulateBranch(dstPath string, ctx *APITestContext, baseBranch, headBranch string) func(t *testing.T) {
	return func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()

		t.Run("CreateHeadBranch", doGitCreateBranch(dstPath, headBranch))

		t.Run("AddCommit", func(t *testing.T) {
			err := os.WriteFile(path.Join(dstPath, "test_file"), []byte("## test content"), 0o666)
			require.NoError(t, err)

			err = git.AddChanges(dstPath, true)
			require.NoError(t, err)

			err = git.CommitChanges(dstPath, git.CommitChangesOptions{
				Committer: &git.Signature{
					Email: "user2@example.com",
					Name:  "user2",
					When:  time.Now(),
				},
				Author: &git.Signature{
					Email: "user2@example.com",
					Name:  "user2",
					When:  time.Now(),
				},
				Message: "Testing commit 1",
			})
			require.NoError(t, err)
		})

		t.Run("Push", func(t *testing.T) {
			err := git.NewCommand(git.DefaultContext, "push", "origin").AddDynamicArguments("HEAD:refs/heads/" + headBranch).Run(&git.RunOpts{Dir: dstPath})
			require.NoError(t, err)
		})
	}
}

func doActionsUserPR(ctx, doerCtx APITestContext, baseBranch, headBranch string) func(t *testing.T) {
	return func(t *testing.T) {
		defer tests.PrintCurrentTest(t)()
		var pr api.PullRequest
		var err error

		// Create a test pullrequest
		t.Run("CreatePullRequest", func(t *testing.T) {
			pr, err = doAPICreatePullRequest(doerCtx, ctx.Username, ctx.Reponame, baseBranch, headBranch)(t)
			require.NoError(t, err)
		})
		doerCtx.ExpectedCode = http.StatusCreated
		t.Run("AutoMergePR", doAPIAutoMergePullRequest(doerCtx, ctx.Username, ctx.Reponame, pr.Index))
		// Ensure the PR page works
		t.Run("EnsureCanSeePull", doEnsureCanSeePull(ctx, pr, true))
	}
}