|
| 1 | +// Copyright 2025 The Gitea Authors. All rights reserved. |
| 2 | +// SPDX-License-Identifier: MIT |
| 3 | + |
| 4 | +package integration |
| 5 | + |
| 6 | +import ( |
| 7 | + "context" |
| 8 | + "fmt" |
| 9 | + "net/http" |
| 10 | + "testing" |
| 11 | + |
| 12 | + actions_model "code.gitea.io/gitea/models/actions" |
| 13 | + "code.gitea.io/gitea/models/db" |
| 14 | + repo_model "code.gitea.io/gitea/models/repo" |
| 15 | + "code.gitea.io/gitea/models/unittest" |
| 16 | + user_model "code.gitea.io/gitea/models/user" |
| 17 | + "code.gitea.io/gitea/tests" |
| 18 | + |
| 19 | + "github.com/stretchr/testify/assert" |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | +) |
| 22 | + |
| 23 | +func TestActionsRunnerModify(t *testing.T) { |
| 24 | + defer tests.PrepareTestEnv(t)() |
| 25 | + |
| 26 | + ctx := context.Background() |
| 27 | + |
| 28 | + require.NoError(t, db.DeleteAllRecords("action_runner")) |
| 29 | + |
| 30 | + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
| 31 | + _ = actions_model.CreateRunner(ctx, &actions_model.ActionRunner{OwnerID: user2.ID, Name: "user2-runner", TokenHash: "a", UUID: "a"}) |
| 32 | + user2Runner := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunner{OwnerID: user2.ID, Name: "user2-runner"}) |
| 33 | + userWebURL := "/user/settings/actions/runners" |
| 34 | + |
| 35 | + org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3, Type: user_model.UserTypeOrganization}) |
| 36 | + require.NoError(t, actions_model.CreateRunner(ctx, &actions_model.ActionRunner{OwnerID: org3.ID, Name: "org3-runner", TokenHash: "b", UUID: "b"})) |
| 37 | + org3Runner := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunner{OwnerID: org3.ID, Name: "org3-runner"}) |
| 38 | + orgWebURL := "/org/org3/settings/actions/runners" |
| 39 | + |
| 40 | + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
| 41 | + _ = actions_model.CreateRunner(ctx, &actions_model.ActionRunner{RepoID: repo1.ID, Name: "repo1-runner", TokenHash: "c", UUID: "c"}) |
| 42 | + repo1Runner := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunner{RepoID: repo1.ID, Name: "repo1-runner"}) |
| 43 | + repoWebURL := "/user2/repo1/settings/actions/runners" |
| 44 | + |
| 45 | + _ = actions_model.CreateRunner(ctx, &actions_model.ActionRunner{Name: "global-runner", TokenHash: "d", UUID: "d"}) |
| 46 | + globalRunner := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunner{Name: "global-runner"}) |
| 47 | + adminWebURL := "/-/admin/actions/runners" |
| 48 | + |
| 49 | + sessionAdmin := loginUser(t, "user1") |
| 50 | + sessionUser2 := loginUser(t, user2.Name) |
| 51 | + |
| 52 | + doUpdate := func(t *testing.T, sess *TestSession, baseURL string, id int64, description string, expectedStatus int) { |
| 53 | + req := NewRequestWithValues(t, "POST", fmt.Sprintf("%s/%d", baseURL, id), map[string]string{ |
| 54 | + "_csrf": GetUserCSRFToken(t, sess), |
| 55 | + "description": description, |
| 56 | + }) |
| 57 | + sess.MakeRequest(t, req, expectedStatus) |
| 58 | + } |
| 59 | + |
| 60 | + doDelete := func(t *testing.T, sess *TestSession, baseURL string, id int64, expectedStatus int) { |
| 61 | + req := NewRequestWithValues(t, "POST", fmt.Sprintf("%s/%d/delete", baseURL, id), map[string]string{ |
| 62 | + "_csrf": GetUserCSRFToken(t, sess), |
| 63 | + }) |
| 64 | + sess.MakeRequest(t, req, expectedStatus) |
| 65 | + } |
| 66 | + |
| 67 | + assertDenied := func(t *testing.T, sess *TestSession, baseURL string, id int64) { |
| 68 | + doUpdate(t, sess, baseURL, id, "ChangedDescription", http.StatusNotFound) |
| 69 | + doDelete(t, sess, baseURL, id, http.StatusNotFound) |
| 70 | + v := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunner{ID: id}) |
| 71 | + assert.Empty(t, v.Description) |
| 72 | + } |
| 73 | + |
| 74 | + assertSuccess := func(t *testing.T, sess *TestSession, baseURL string, id int64) { |
| 75 | + doUpdate(t, sess, baseURL, id, "ChangedDescription", http.StatusSeeOther) |
| 76 | + v := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionRunner{ID: id}) |
| 77 | + assert.Equal(t, "ChangedDescription", v.Description) |
| 78 | + doDelete(t, sess, baseURL, id, http.StatusOK) |
| 79 | + unittest.AssertNotExistsBean(t, &actions_model.ActionRunner{ID: id}) |
| 80 | + } |
| 81 | + |
| 82 | + t.Run("UpdateUserRunner", func(t *testing.T) { |
| 83 | + theRunner := user2Runner |
| 84 | + t.Run("FromOrg", func(t *testing.T) { |
| 85 | + assertDenied(t, sessionAdmin, orgWebURL, theRunner.ID) |
| 86 | + }) |
| 87 | + t.Run("FromRepo", func(t *testing.T) { |
| 88 | + assertDenied(t, sessionAdmin, repoWebURL, theRunner.ID) |
| 89 | + }) |
| 90 | + t.Run("FromAdmin", func(t *testing.T) { |
| 91 | + t.Skip("Admin can update any runner (not right but not too bad)") |
| 92 | + assertDenied(t, sessionAdmin, adminWebURL, theRunner.ID) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("UpdateOrgRunner", func(t *testing.T) { |
| 97 | + theRunner := org3Runner |
| 98 | + t.Run("FromRepo", func(t *testing.T) { |
| 99 | + assertDenied(t, sessionAdmin, repoWebURL, theRunner.ID) |
| 100 | + }) |
| 101 | + t.Run("FromUser", func(t *testing.T) { |
| 102 | + assertDenied(t, sessionAdmin, userWebURL, theRunner.ID) |
| 103 | + }) |
| 104 | + t.Run("FromAdmin", func(t *testing.T) { |
| 105 | + t.Skip("Admin can update any runner (not right but not too bad)") |
| 106 | + assertDenied(t, sessionAdmin, adminWebURL, theRunner.ID) |
| 107 | + }) |
| 108 | + }) |
| 109 | + |
| 110 | + t.Run("UpdateRepoRunner", func(t *testing.T) { |
| 111 | + theRunner := repo1Runner |
| 112 | + t.Run("FromOrg", func(t *testing.T) { |
| 113 | + assertDenied(t, sessionAdmin, orgWebURL, theRunner.ID) |
| 114 | + }) |
| 115 | + t.Run("FromUser", func(t *testing.T) { |
| 116 | + assertDenied(t, sessionAdmin, userWebURL, theRunner.ID) |
| 117 | + }) |
| 118 | + t.Run("FromAdmin", func(t *testing.T) { |
| 119 | + t.Skip("Admin can update any runner (not right but not too bad)") |
| 120 | + assertDenied(t, sessionAdmin, adminWebURL, theRunner.ID) |
| 121 | + }) |
| 122 | + }) |
| 123 | + |
| 124 | + t.Run("UpdateGlobalRunner", func(t *testing.T) { |
| 125 | + theRunner := globalRunner |
| 126 | + t.Run("FromOrg", func(t *testing.T) { |
| 127 | + assertDenied(t, sessionAdmin, orgWebURL, theRunner.ID) |
| 128 | + }) |
| 129 | + t.Run("FromUser", func(t *testing.T) { |
| 130 | + assertDenied(t, sessionAdmin, userWebURL, theRunner.ID) |
| 131 | + }) |
| 132 | + t.Run("FromRepo", func(t *testing.T) { |
| 133 | + assertDenied(t, sessionAdmin, repoWebURL, theRunner.ID) |
| 134 | + }) |
| 135 | + }) |
| 136 | + |
| 137 | + t.Run("UpdateSuccess", func(t *testing.T) { |
| 138 | + t.Run("User", func(t *testing.T) { |
| 139 | + assertSuccess(t, sessionUser2, userWebURL, user2Runner.ID) |
| 140 | + }) |
| 141 | + t.Run("Org", func(t *testing.T) { |
| 142 | + assertSuccess(t, sessionAdmin, orgWebURL, org3Runner.ID) |
| 143 | + }) |
| 144 | + t.Run("Repo", func(t *testing.T) { |
| 145 | + assertSuccess(t, sessionUser2, repoWebURL, repo1Runner.ID) |
| 146 | + }) |
| 147 | + t.Run("Admin", func(t *testing.T) { |
| 148 | + assertSuccess(t, sessionAdmin, adminWebURL, globalRunner.ID) |
| 149 | + }) |
| 150 | + }) |
| 151 | +} |
0 commit comments