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