2023-10-30 23:46:09 -04:00
|
|
|
// Copyright 2023 The Gitea Authors. All rights reserved.
|
|
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
|
|
|
|
package integration
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"fmt"
|
|
|
|
|
"net/http"
|
2026-01-13 22:19:21 -05:00
|
|
|
"strings"
|
2023-10-30 23:46:09 -04:00
|
|
|
"testing"
|
|
|
|
|
|
2025-03-27 15:40:14 -04:00
|
|
|
auth_model "forgejo.org/models/auth"
|
2026-01-13 22:19:21 -05:00
|
|
|
secret_model "forgejo.org/models/secret"
|
|
|
|
|
"forgejo.org/models/unittest"
|
|
|
|
|
user_model "forgejo.org/models/user"
|
2025-03-27 15:40:14 -04:00
|
|
|
api "forgejo.org/modules/structs"
|
|
|
|
|
"forgejo.org/tests"
|
2026-01-13 22:19:21 -05:00
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2023-10-30 23:46:09 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAPIUserSecrets(t *testing.T) {
|
|
|
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
|
|
2026-01-13 22:19:21 -05:00
|
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
|
|
|
|
|
session := loginUser(t, user.Name)
|
2023-10-30 23:46:09 -04:00
|
|
|
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteUser)
|
|
|
|
|
|
|
|
|
|
t.Run("Create", func(t *testing.T) {
|
|
|
|
|
cases := []struct {
|
|
|
|
|
Name string
|
|
|
|
|
ExpectedStatus int
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
Name: "",
|
|
|
|
|
ExpectedStatus: http.StatusNotFound,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "-",
|
|
|
|
|
ExpectedStatus: http.StatusBadRequest,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "_",
|
|
|
|
|
ExpectedStatus: http.StatusCreated,
|
|
|
|
|
},
|
2026-01-13 22:19:21 -05:00
|
|
|
{
|
|
|
|
|
Name: "ci",
|
|
|
|
|
ExpectedStatus: http.StatusCreated,
|
|
|
|
|
},
|
2023-10-30 23:46:09 -04:00
|
|
|
{
|
|
|
|
|
Name: "secret",
|
|
|
|
|
ExpectedStatus: http.StatusCreated,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "2secret",
|
|
|
|
|
ExpectedStatus: http.StatusBadRequest,
|
|
|
|
|
},
|
2026-01-13 22:19:21 -05:00
|
|
|
{
|
|
|
|
|
Name: "FORGEJO_secret",
|
|
|
|
|
ExpectedStatus: http.StatusBadRequest,
|
|
|
|
|
},
|
2023-10-30 23:46:09 -04:00
|
|
|
{
|
|
|
|
|
Name: "GITEA_secret",
|
|
|
|
|
ExpectedStatus: http.StatusBadRequest,
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
Name: "GITHUB_secret",
|
|
|
|
|
ExpectedStatus: http.StatusBadRequest,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for _, c := range cases {
|
2026-02-09 11:02:18 -05:00
|
|
|
url := fmt.Sprintf("/api/v1/user/actions/secrets/%s", c.Name)
|
|
|
|
|
req := NewRequestWithJSON(t, "PUT", url, api.CreateOrUpdateSecretOption{Data: " \r\ndàtä\t "})
|
|
|
|
|
req.AddTokenAuth(token)
|
2023-10-30 23:46:09 -04:00
|
|
|
MakeRequest(t, req, c.ExpectedStatus)
|
2026-02-09 11:02:18 -05:00
|
|
|
|
|
|
|
|
if c.ExpectedStatus < 300 {
|
|
|
|
|
secret := unittest.AssertExistsAndLoadBean(t, &secret_model.Secret{OwnerID: user.ID, Name: strings.ToUpper(c.Name)})
|
|
|
|
|
data, err := secret.GetDecryptedData()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
assert.Equal(t, " \ndàtä\t ", data)
|
|
|
|
|
}
|
2023-10-30 23:46:09 -04:00
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("Update", func(t *testing.T) {
|
2026-01-13 22:19:21 -05:00
|
|
|
name := "update_user_secret_and_test_data"
|
2023-12-21 18:59:59 -05:00
|
|
|
url := fmt.Sprintf("/api/v1/user/actions/secrets/%s", name)
|
2023-10-30 23:46:09 -04:00
|
|
|
|
2026-02-09 11:02:18 -05:00
|
|
|
req := NewRequestWithJSON(t, "PUT", url, api.CreateOrUpdateSecretOption{Data: "initial"})
|
|
|
|
|
req.AddTokenAuth(token)
|
2023-10-30 23:46:09 -04:00
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
2026-02-09 11:02:18 -05:00
|
|
|
req = NewRequestWithJSON(t, "PUT", url, api.CreateOrUpdateSecretOption{Data: " \r\nchängéd\t "})
|
|
|
|
|
req.AddTokenAuth(token)
|
2023-10-30 23:46:09 -04:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2026-01-13 22:19:21 -05:00
|
|
|
|
|
|
|
|
secret := unittest.AssertExistsAndLoadBean(t, &secret_model.Secret{Name: strings.ToUpper(name)})
|
2026-02-09 11:02:18 -05:00
|
|
|
data, err := secret.GetDecryptedData()
|
2026-01-13 22:19:21 -05:00
|
|
|
require.NoError(t, err)
|
2026-02-09 11:02:18 -05:00
|
|
|
assert.Equal(t, " \nchängéd\t ", data)
|
2023-10-30 23:46:09 -04:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("Delete", func(t *testing.T) {
|
|
|
|
|
name := "delete_secret"
|
2023-12-21 18:59:59 -05:00
|
|
|
url := fmt.Sprintf("/api/v1/user/actions/secrets/%s", name)
|
2023-10-30 23:46:09 -04:00
|
|
|
|
|
|
|
|
req := NewRequestWithJSON(t, "PUT", url, api.CreateOrUpdateSecretOption{
|
|
|
|
|
Data: "initial",
|
2023-12-21 18:59:59 -05:00
|
|
|
}).AddTokenAuth(token)
|
2023-10-30 23:46:09 -04:00
|
|
|
MakeRequest(t, req, http.StatusCreated)
|
|
|
|
|
|
2023-12-21 18:59:59 -05:00
|
|
|
req = NewRequest(t, "DELETE", url).
|
|
|
|
|
AddTokenAuth(token)
|
2023-10-30 23:46:09 -04:00
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
|
|
|
|
|
2023-12-21 18:59:59 -05:00
|
|
|
req = NewRequest(t, "DELETE", url).
|
|
|
|
|
AddTokenAuth(token)
|
2023-10-30 23:46:09 -04:00
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
2026-01-13 22:19:21 -05:00
|
|
|
})
|
|
|
|
|
|
|
|
|
|
t.Run("Delete with forbidden names", func(t *testing.T) {
|
|
|
|
|
secret, err := secret_model.InsertEncryptedSecret(t.Context(), user.ID, 0, "FORGEJO_FORBIDDEN", "illegal")
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
url := fmt.Sprintf("/api/v1/user/actions/secrets/%s", secret.Name)
|
|
|
|
|
|
|
|
|
|
req := NewRequest(t, "DELETE", url).
|
|
|
|
|
AddTokenAuth(token)
|
|
|
|
|
MakeRequest(t, req, http.StatusNoContent)
|
2023-10-30 23:46:09 -04:00
|
|
|
|
2026-01-13 22:19:21 -05:00
|
|
|
req = NewRequest(t, "DELETE", url).
|
2023-12-21 18:59:59 -05:00
|
|
|
AddTokenAuth(token)
|
2026-01-13 22:19:21 -05:00
|
|
|
MakeRequest(t, req, http.StatusNotFound)
|
2023-10-30 23:46:09 -04:00
|
|
|
})
|
|
|
|
|
}
|