gitea源码

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