|
| 1 | +// Copyright 2024 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 TestActionsVariables(t *testing.T) { |
| 24 | + defer tests.PrepareTestEnv(t)() |
| 25 | + |
| 26 | + ctx := context.Background() |
| 27 | + |
| 28 | + require.NoError(t, db.DeleteAllRecords("action_variable")) |
| 29 | + |
| 30 | + user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) |
| 31 | + _, _ = actions_model.InsertVariable(ctx, user2.ID, 0, "VAR", "user2-var") |
| 32 | + user2Var := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{OwnerID: user2.ID, Name: "VAR"}) |
| 33 | + userWebURL := "/user/settings/actions/variables" |
| 34 | + |
| 35 | + org3 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 3, Type: user_model.UserTypeOrganization}) |
| 36 | + _, _ = actions_model.InsertVariable(ctx, org3.ID, 0, "VAR", "org3-var") |
| 37 | + org3Var := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{OwnerID: org3.ID, Name: "VAR"}) |
| 38 | + orgWebURL := "/org/org3/settings/actions/variables" |
| 39 | + |
| 40 | + repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1}) |
| 41 | + _, _ = actions_model.InsertVariable(ctx, 0, repo1.ID, "VAR", "repo1-var") |
| 42 | + repo1Var := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{RepoID: repo1.ID, Name: "VAR"}) |
| 43 | + repoWebURL := "/user2/repo1/settings/actions/variables" |
| 44 | + |
| 45 | + _, _ = actions_model.InsertVariable(ctx, 0, 0, "VAR", "global-var") |
| 46 | + globalVar := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{Name: "VAR", Data: "global-var"}) |
| 47 | + adminWebURL := "/-/admin/actions/variables" |
| 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, data string, expectedStatus int) { |
| 53 | + req := NewRequestWithValues(t, "POST", fmt.Sprintf("%s/%d/edit", baseURL, id), map[string]string{ |
| 54 | + "_csrf": GetUserCSRFToken(t, sess), |
| 55 | + "name": "VAR", |
| 56 | + "data": data, |
| 57 | + }) |
| 58 | + sess.MakeRequest(t, req, expectedStatus) |
| 59 | + } |
| 60 | + |
| 61 | + doDelete := func(t *testing.T, sess *TestSession, baseURL string, id int64, expectedStatus int) { |
| 62 | + req := NewRequestWithValues(t, "POST", fmt.Sprintf("%s/%d/delete", baseURL, id), map[string]string{ |
| 63 | + "_csrf": GetUserCSRFToken(t, sess), |
| 64 | + }) |
| 65 | + sess.MakeRequest(t, req, expectedStatus) |
| 66 | + } |
| 67 | + |
| 68 | + assertDenied := func(t *testing.T, sess *TestSession, baseURL string, id int64) { |
| 69 | + doUpdate(t, sess, baseURL, id, "ChangedData", http.StatusNotFound) |
| 70 | + doDelete(t, sess, baseURL, id, http.StatusNotFound) |
| 71 | + v := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{ID: id}) |
| 72 | + assert.Contains(t, v.Data, "-var") |
| 73 | + } |
| 74 | + |
| 75 | + assertSuccess := func(t *testing.T, sess *TestSession, baseURL string, id int64) { |
| 76 | + doUpdate(t, sess, baseURL, id, "ChangedData", http.StatusOK) |
| 77 | + v := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionVariable{ID: id}) |
| 78 | + assert.Equal(t, "ChangedData", v.Data) |
| 79 | + doDelete(t, sess, baseURL, id, http.StatusOK) |
| 80 | + unittest.AssertNotExistsBean(t, &actions_model.ActionVariable{ID: id}) |
| 81 | + } |
| 82 | + |
| 83 | + t.Run("UpdateUserVar", func(t *testing.T) { |
| 84 | + theVar := user2Var |
| 85 | + t.Run("FromOrg", func(t *testing.T) { |
| 86 | + assertDenied(t, sessionAdmin, orgWebURL, theVar.ID) |
| 87 | + }) |
| 88 | + t.Run("FromRepo", func(t *testing.T) { |
| 89 | + assertDenied(t, sessionAdmin, repoWebURL, theVar.ID) |
| 90 | + }) |
| 91 | + t.Run("FromAdmin", func(t *testing.T) { |
| 92 | + assertDenied(t, sessionAdmin, adminWebURL, theVar.ID) |
| 93 | + }) |
| 94 | + }) |
| 95 | + |
| 96 | + t.Run("UpdateOrgVar", func(t *testing.T) { |
| 97 | + theVar := org3Var |
| 98 | + t.Run("FromRepo", func(t *testing.T) { |
| 99 | + assertDenied(t, sessionAdmin, repoWebURL, theVar.ID) |
| 100 | + }) |
| 101 | + t.Run("FromUser", func(t *testing.T) { |
| 102 | + assertDenied(t, sessionAdmin, userWebURL, theVar.ID) |
| 103 | + }) |
| 104 | + t.Run("FromAdmin", func(t *testing.T) { |
| 105 | + assertDenied(t, sessionAdmin, adminWebURL, theVar.ID) |
| 106 | + }) |
| 107 | + }) |
| 108 | + |
| 109 | + t.Run("UpdateRepoVar", func(t *testing.T) { |
| 110 | + theVar := repo1Var |
| 111 | + t.Run("FromOrg", func(t *testing.T) { |
| 112 | + assertDenied(t, sessionAdmin, orgWebURL, theVar.ID) |
| 113 | + }) |
| 114 | + t.Run("FromUser", func(t *testing.T) { |
| 115 | + assertDenied(t, sessionAdmin, userWebURL, theVar.ID) |
| 116 | + }) |
| 117 | + t.Run("FromAdmin", func(t *testing.T) { |
| 118 | + assertDenied(t, sessionAdmin, adminWebURL, theVar.ID) |
| 119 | + }) |
| 120 | + }) |
| 121 | + |
| 122 | + t.Run("UpdateGlobalVar", func(t *testing.T) { |
| 123 | + theVar := globalVar |
| 124 | + t.Run("FromOrg", func(t *testing.T) { |
| 125 | + assertDenied(t, sessionAdmin, orgWebURL, theVar.ID) |
| 126 | + }) |
| 127 | + t.Run("FromUser", func(t *testing.T) { |
| 128 | + assertDenied(t, sessionAdmin, userWebURL, theVar.ID) |
| 129 | + }) |
| 130 | + t.Run("FromRepo", func(t *testing.T) { |
| 131 | + assertDenied(t, sessionAdmin, repoWebURL, theVar.ID) |
| 132 | + }) |
| 133 | + }) |
| 134 | + |
| 135 | + t.Run("UpdateSuccess", func(t *testing.T) { |
| 136 | + t.Run("User", func(t *testing.T) { |
| 137 | + assertSuccess(t, sessionUser2, userWebURL, user2Var.ID) |
| 138 | + }) |
| 139 | + t.Run("Org", func(t *testing.T) { |
| 140 | + assertSuccess(t, sessionAdmin, orgWebURL, org3Var.ID) |
| 141 | + }) |
| 142 | + t.Run("Repo", func(t *testing.T) { |
| 143 | + assertSuccess(t, sessionUser2, repoWebURL, repo1Var.ID) |
| 144 | + }) |
| 145 | + t.Run("Admin", func(t *testing.T) { |
| 146 | + assertSuccess(t, sessionAdmin, adminWebURL, globalVar.ID) |
| 147 | + }) |
| 148 | + }) |
| 149 | +} |
0 commit comments