You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

api_repo_variables_test.go 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // Copyright 2024 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package integration
  4. import (
  5. "fmt"
  6. "net/http"
  7. "testing"
  8. auth_model "code.gitea.io/gitea/models/auth"
  9. repo_model "code.gitea.io/gitea/models/repo"
  10. "code.gitea.io/gitea/models/unittest"
  11. user_model "code.gitea.io/gitea/models/user"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "code.gitea.io/gitea/tests"
  14. )
  15. func TestAPIRepoVariables(t *testing.T) {
  16. defer tests.PrepareTestEnv(t)()
  17. repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
  18. user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
  19. session := loginUser(t, user.Name)
  20. token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
  21. t.Run("CreateRepoVariable", func(t *testing.T) {
  22. cases := []struct {
  23. Name string
  24. ExpectedStatus int
  25. }{
  26. {
  27. Name: "-",
  28. ExpectedStatus: http.StatusBadRequest,
  29. },
  30. {
  31. Name: "_",
  32. ExpectedStatus: http.StatusNoContent,
  33. },
  34. {
  35. Name: "TEST_VAR",
  36. ExpectedStatus: http.StatusNoContent,
  37. },
  38. {
  39. Name: "test_var",
  40. ExpectedStatus: http.StatusConflict,
  41. },
  42. {
  43. Name: "ci",
  44. ExpectedStatus: http.StatusBadRequest,
  45. },
  46. {
  47. Name: "123var",
  48. ExpectedStatus: http.StatusBadRequest,
  49. },
  50. {
  51. Name: "var@test",
  52. ExpectedStatus: http.StatusBadRequest,
  53. },
  54. {
  55. Name: "github_var",
  56. ExpectedStatus: http.StatusBadRequest,
  57. },
  58. {
  59. Name: "gitea_var",
  60. ExpectedStatus: http.StatusBadRequest,
  61. },
  62. }
  63. for _, c := range cases {
  64. req := NewRequestWithJSON(t, "POST", fmt.Sprintf("/api/v1/repos/%s/actions/variables/%s", repo.FullName(), c.Name), api.CreateVariableOption{
  65. Value: "value",
  66. }).AddTokenAuth(token)
  67. MakeRequest(t, req, c.ExpectedStatus)
  68. }
  69. })
  70. t.Run("UpdateRepoVariable", func(t *testing.T) {
  71. variableName := "test_update_var"
  72. url := fmt.Sprintf("/api/v1/repos/%s/actions/variables/%s", repo.FullName(), variableName)
  73. req := NewRequestWithJSON(t, "POST", url, api.CreateVariableOption{
  74. Value: "initial_val",
  75. }).AddTokenAuth(token)
  76. MakeRequest(t, req, http.StatusNoContent)
  77. cases := []struct {
  78. Name string
  79. UpdateName string
  80. ExpectedStatus int
  81. }{
  82. {
  83. Name: "not_found_var",
  84. ExpectedStatus: http.StatusNotFound,
  85. },
  86. {
  87. Name: variableName,
  88. UpdateName: "1invalid",
  89. ExpectedStatus: http.StatusBadRequest,
  90. },
  91. {
  92. Name: variableName,
  93. UpdateName: "invalid@name",
  94. ExpectedStatus: http.StatusBadRequest,
  95. },
  96. {
  97. Name: variableName,
  98. UpdateName: "ci",
  99. ExpectedStatus: http.StatusBadRequest,
  100. },
  101. {
  102. Name: variableName,
  103. UpdateName: "updated_var_name",
  104. ExpectedStatus: http.StatusNoContent,
  105. },
  106. {
  107. Name: variableName,
  108. ExpectedStatus: http.StatusNotFound,
  109. },
  110. {
  111. Name: "updated_var_name",
  112. ExpectedStatus: http.StatusNoContent,
  113. },
  114. }
  115. for _, c := range cases {
  116. req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/actions/variables/%s", repo.FullName(), c.Name), api.UpdateVariableOption{
  117. Name: c.UpdateName,
  118. Value: "updated_val",
  119. }).AddTokenAuth(token)
  120. MakeRequest(t, req, c.ExpectedStatus)
  121. }
  122. })
  123. t.Run("DeleteRepoVariable", func(t *testing.T) {
  124. variableName := "test_delete_var"
  125. url := fmt.Sprintf("/api/v1/repos/%s/actions/variables/%s", repo.FullName(), variableName)
  126. req := NewRequestWithJSON(t, "POST", url, api.CreateVariableOption{
  127. Value: "initial_val",
  128. }).AddTokenAuth(token)
  129. MakeRequest(t, req, http.StatusNoContent)
  130. req = NewRequest(t, "DELETE", url).AddTokenAuth(token)
  131. MakeRequest(t, req, http.StatusNoContent)
  132. req = NewRequest(t, "DELETE", url).AddTokenAuth(token)
  133. MakeRequest(t, req, http.StatusNotFound)
  134. })
  135. }