code.gitea.io/gitea@v1.21.7/tests/integration/api_repo_secrets_test.go (about)

     1  // Copyright 2023 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 TestAPIRepoSecrets(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("Create", func(t *testing.T) {
    28  		cases := []struct {
    29  			Name           string
    30  			ExpectedStatus int
    31  		}{
    32  			{
    33  				Name:           "",
    34  				ExpectedStatus: http.StatusNotFound,
    35  			},
    36  			{
    37  				Name:           "-",
    38  				ExpectedStatus: http.StatusBadRequest,
    39  			},
    40  			{
    41  				Name:           "_",
    42  				ExpectedStatus: http.StatusCreated,
    43  			},
    44  			{
    45  				Name:           "secret",
    46  				ExpectedStatus: http.StatusCreated,
    47  			},
    48  			{
    49  				Name:           "2secret",
    50  				ExpectedStatus: http.StatusBadRequest,
    51  			},
    52  			{
    53  				Name:           "GITEA_secret",
    54  				ExpectedStatus: http.StatusBadRequest,
    55  			},
    56  			{
    57  				Name:           "GITHUB_secret",
    58  				ExpectedStatus: http.StatusBadRequest,
    59  			},
    60  		}
    61  
    62  		for _, c := range cases {
    63  			req := NewRequestWithJSON(t, "PUT", fmt.Sprintf("/api/v1/repos/%s/actions/secrets/%s?token=%s", repo.FullName(), c.Name, token), api.CreateOrUpdateSecretOption{
    64  				Data: "data",
    65  			})
    66  			MakeRequest(t, req, c.ExpectedStatus)
    67  		}
    68  	})
    69  
    70  	t.Run("Update", func(t *testing.T) {
    71  		name := "update_secret"
    72  		url := fmt.Sprintf("/api/v1/repos/%s/actions/secrets/%s?token=%s", repo.FullName(), name, token)
    73  
    74  		req := NewRequestWithJSON(t, "PUT", url, api.CreateOrUpdateSecretOption{
    75  			Data: "initial",
    76  		})
    77  		MakeRequest(t, req, http.StatusCreated)
    78  
    79  		req = NewRequestWithJSON(t, "PUT", url, api.CreateOrUpdateSecretOption{
    80  			Data: "changed",
    81  		})
    82  		MakeRequest(t, req, http.StatusNoContent)
    83  	})
    84  
    85  	t.Run("Delete", func(t *testing.T) {
    86  		name := "delete_secret"
    87  		url := fmt.Sprintf("/api/v1/repos/%s/actions/secrets/%s?token=%s", repo.FullName(), name, token)
    88  
    89  		req := NewRequestWithJSON(t, "PUT", url, api.CreateOrUpdateSecretOption{
    90  			Data: "initial",
    91  		})
    92  		MakeRequest(t, req, http.StatusCreated)
    93  
    94  		req = NewRequest(t, "DELETE", url)
    95  		MakeRequest(t, req, http.StatusNoContent)
    96  
    97  		req = NewRequest(t, "DELETE", url)
    98  		MakeRequest(t, req, http.StatusNotFound)
    99  
   100  		req = NewRequest(t, "DELETE", fmt.Sprintf("/api/v1/repos/%s/actions/secrets/000?token=%s", repo.FullName(), token))
   101  		MakeRequest(t, req, http.StatusBadRequest)
   102  	})
   103  }