|
| 1 | +package integration |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/base64" |
| 5 | + "net/url" |
| 6 | + "testing" |
| 7 | + |
| 8 | + actions_model "code.gitea.io/gitea/models/actions" |
| 9 | + "code.gitea.io/gitea/models/unittest" |
| 10 | + "code.gitea.io/gitea/modules/structs" |
| 11 | + "github.com/stretchr/testify/require" |
| 12 | +) |
| 13 | + |
| 14 | +func TestActionsJobTokenAccess(t *testing.T) { |
| 15 | + onGiteaRun(t, func(t *testing.T, u *url.URL) { |
| 16 | + t.Run("Write Access", testActionsJobTokenAccess(u, false)) |
| 17 | + t.Run("Read Access", testActionsJobTokenAccess(u, true)) |
| 18 | + }) |
| 19 | +} |
| 20 | + |
| 21 | +func testActionsJobTokenAccess(u *url.URL, isFork bool) func(t *testing.T) { |
| 22 | + return func(t *testing.T) { |
| 23 | + task := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 47}) |
| 24 | + require.NoError(t, task.GenerateToken()) |
| 25 | + task.Status = actions_model.StatusRunning |
| 26 | + task.IsForkPullRequest = isFork |
| 27 | + err := actions_model.UpdateTask(t.Context(), task, "token_hash", "token_salt", "token_last_eight", "status", "is_fork_pull_request") |
| 28 | + require.NoError(t, err) |
| 29 | + session := emptyTestSession(t) |
| 30 | + context := APITestContext{ |
| 31 | + Session: session, |
| 32 | + Token: task.Token, |
| 33 | + Username: "user5", |
| 34 | + Reponame: "repo4", |
| 35 | + } |
| 36 | + dstPath := t.TempDir() |
| 37 | + |
| 38 | + u.Path = context.GitPath() |
| 39 | + u.User = url.UserPassword("gitea-actions", task.Token) |
| 40 | + |
| 41 | + t.Run("Git Clone", doGitClone(dstPath, u)) |
| 42 | + |
| 43 | + t.Run("API Get Repository", doAPIGetRepository(context, func(t *testing.T, r structs.Repository) { |
| 44 | + require.EqualValues(t, "repo4", r.Name) |
| 45 | + require.EqualValues(t, "user5", r.Owner.UserName) |
| 46 | + })) |
| 47 | + |
| 48 | + if isFork { |
| 49 | + context.ExpectedCode = 403 |
| 50 | + } |
| 51 | + t.Run("API Create File", doAPICreateFile(context, "test.txt", &structs.CreateFileOptions{ |
| 52 | + FileOptions: structs.FileOptions{ |
| 53 | + NewBranchName: "new-branch", |
| 54 | + Message: "Create File", |
| 55 | + }, |
| 56 | + ContentBase64: base64.StdEncoding.EncodeToString([]byte(`This is a test file created using job token.`)), |
| 57 | + })) |
| 58 | + |
| 59 | + context.ExpectedCode = 500 |
| 60 | + t.Run("Fail to Create Repository", doAPICreateRepository(context, true)) |
| 61 | + |
| 62 | + context.ExpectedCode = 403 |
| 63 | + t.Run("Fail to Delete Repository", doAPIDeleteRepository(context)) |
| 64 | + |
| 65 | + t.Run("Fail to Create Organization", doAPICreateOrganization(context, &structs.CreateOrgOption{ |
| 66 | + UserName: "actions", |
| 67 | + FullName: "Gitea Actions", |
| 68 | + })) |
| 69 | + } |
| 70 | +} |
0 commit comments