2022-10-18 01:24:12 +00:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-19 22:24:10 +00:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 13:02:17 +00:00
|
|
|
//
|
2018-02-19 22:24:10 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2017-04-11 17:06:45 +00:00
|
|
|
package datastore
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2023-12-08 07:15:08 +00:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v2/server/model"
|
2017-04-11 17:06:45 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestSecretFind(t *testing.T) {
|
2021-11-13 19:18:06 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
2017-04-11 17:06:45 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
err := store.SecretCreate(&model.Secret{
|
2017-04-11 17:06:45 +00:00
|
|
|
RepoID: 1,
|
|
|
|
Name: "password",
|
|
|
|
Value: "correct-horse-battery-staple",
|
|
|
|
Images: []string{"golang", "node"},
|
2021-12-11 01:41:28 +00:00
|
|
|
Events: []model.WebhookEvent{"push", "tag"},
|
2017-04-11 17:06:45 +00:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert secret: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
secret, err := store.SecretFind(&model.Repo{ID: 1}, "password")
|
2017-04-11 17:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := secret.RepoID, int64(1); got != want {
|
|
|
|
t.Errorf("Want repo id %d, got %d", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Name, "password"; got != want {
|
|
|
|
t.Errorf("Want secret name %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Value, "correct-horse-battery-staple"; got != want {
|
|
|
|
t.Errorf("Want secret value %s, got %s", want, got)
|
|
|
|
}
|
2021-12-11 01:41:28 +00:00
|
|
|
if got, want := secret.Events[0], model.EventPush; got != want {
|
2017-04-11 17:06:45 +00:00
|
|
|
t.Errorf("Want secret event %s, got %s", want, got)
|
|
|
|
}
|
2021-12-11 01:41:28 +00:00
|
|
|
if got, want := secret.Events[1], model.EventTag; got != want {
|
2017-04-11 17:06:45 +00:00
|
|
|
t.Errorf("Want secret event %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Images[0], "golang"; got != want {
|
|
|
|
t.Errorf("Want secret image %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Images[1], "node"; got != want {
|
|
|
|
t.Errorf("Want secret image %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSecretList(t *testing.T) {
|
2021-11-13 19:18:06 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
2017-04-11 17:06:45 +00:00
|
|
|
|
2022-08-14 11:48:53 +00:00
|
|
|
createTestSecrets(t, store)
|
2017-04-11 17:06:45 +00:00
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
list, err := store.SecretList(&model.Repo{ID: 1, OrgID: 12}, false, &model.ListOptions{Page: 1, PerPage: 50})
|
2021-11-13 19:18:06 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, list, 2)
|
2017-04-11 17:06:45 +00:00
|
|
|
}
|
|
|
|
|
2023-01-12 19:59:07 +00:00
|
|
|
func TestSecretListAll(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
createTestSecrets(t, store)
|
|
|
|
|
|
|
|
list, err := store.SecretListAll()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, list, 4)
|
|
|
|
}
|
|
|
|
|
2022-10-18 01:24:12 +00:00
|
|
|
func TestSecretPipelineList(t *testing.T) {
|
2022-08-14 11:48:53 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
createTestSecrets(t, store)
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
list, err := store.SecretList(&model.Repo{ID: 1, OrgID: 12}, true, &model.ListOptions{Page: 1, PerPage: 50})
|
2022-08-14 11:48:53 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, list, 4)
|
|
|
|
}
|
|
|
|
|
2017-04-11 17:06:45 +00:00
|
|
|
func TestSecretUpdate(t *testing.T) {
|
2021-11-13 19:18:06 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
2017-04-11 17:06:45 +00:00
|
|
|
|
|
|
|
secret := &model.Secret{
|
|
|
|
RepoID: 1,
|
|
|
|
Name: "foo",
|
|
|
|
Value: "baz",
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.SecretCreate(secret); err != nil {
|
2017-04-11 17:06:45 +00:00
|
|
|
t.Errorf("Unexpected error: insert secret: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
secret.Value = "qux"
|
2021-11-13 19:18:06 +00:00
|
|
|
assert.EqualValues(t, 1, secret.ID)
|
|
|
|
if err := store.SecretUpdate(secret); err != nil {
|
2017-04-11 17:06:45 +00:00
|
|
|
t.Errorf("Unexpected error: update secret: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
updated, err := store.SecretFind(&model.Repo{ID: 1}, "foo")
|
2017-04-11 17:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := updated.Value, "qux"; got != want {
|
|
|
|
t.Errorf("Want secret value %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-15 16:51:02 +00:00
|
|
|
func TestSecretDelete(t *testing.T) {
|
2021-11-13 19:18:06 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
2017-07-15 16:51:02 +00:00
|
|
|
|
|
|
|
secret := &model.Secret{
|
|
|
|
RepoID: 1,
|
|
|
|
Name: "foo",
|
|
|
|
Value: "baz",
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.SecretCreate(secret); err != nil {
|
2017-07-15 16:51:02 +00:00
|
|
|
t.Errorf("Unexpected error: insert secret: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.SecretDelete(secret); err != nil {
|
2017-07-15 16:51:02 +00:00
|
|
|
t.Errorf("Unexpected error: delete secret: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2021-11-13 19:18:06 +00:00
|
|
|
_, err := store.SecretFind(&model.Repo{ID: 1}, "foo")
|
2017-07-15 16:51:02 +00:00
|
|
|
if err == nil {
|
|
|
|
t.Errorf("Expect error: sql.ErrNoRows")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-11 17:06:45 +00:00
|
|
|
func TestSecretIndexes(t *testing.T) {
|
2021-11-13 19:18:06 +00:00
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
2017-04-11 17:06:45 +00:00
|
|
|
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.SecretCreate(&model.Secret{
|
2017-04-11 17:06:45 +00:00
|
|
|
RepoID: 1,
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
}); err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert secret: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// fail due to duplicate name
|
2021-11-13 19:18:06 +00:00
|
|
|
if err := store.SecretCreate(&model.Secret{
|
2017-04-11 17:06:45 +00:00
|
|
|
RepoID: 1,
|
|
|
|
Name: "foo",
|
|
|
|
Value: "baz",
|
|
|
|
}); err == nil {
|
2021-10-08 16:35:56 +00:00
|
|
|
t.Errorf("Unexpected error: duplicate name")
|
2017-04-11 17:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
2022-08-14 11:48:53 +00:00
|
|
|
|
|
|
|
func createTestSecrets(t *testing.T, store *storage) {
|
|
|
|
assert.NoError(t, store.SecretCreate(&model.Secret{
|
2023-07-21 17:45:32 +00:00
|
|
|
OrgID: 12,
|
2022-08-14 11:48:53 +00:00
|
|
|
Name: "usr",
|
|
|
|
Value: "sec",
|
|
|
|
}))
|
|
|
|
assert.NoError(t, store.SecretCreate(&model.Secret{
|
|
|
|
RepoID: 1,
|
|
|
|
Name: "foo",
|
|
|
|
Value: "bar",
|
|
|
|
}))
|
|
|
|
assert.NoError(t, store.SecretCreate(&model.Secret{
|
|
|
|
RepoID: 1,
|
|
|
|
Name: "baz",
|
|
|
|
Value: "qux",
|
|
|
|
}))
|
|
|
|
assert.NoError(t, store.SecretCreate(&model.Secret{
|
|
|
|
Name: "global",
|
|
|
|
Value: "val",
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOrgSecretFind(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
err := store.SecretCreate(&model.Secret{
|
2023-07-21 17:45:32 +00:00
|
|
|
OrgID: 12,
|
2022-08-14 11:48:53 +00:00
|
|
|
Name: "password",
|
|
|
|
Value: "correct-horse-battery-staple",
|
|
|
|
Images: []string{"golang", "node"},
|
|
|
|
Events: []model.WebhookEvent{"push", "tag"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert secret: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
secret, err := store.OrgSecretFind(12, "password")
|
2022-08-14 11:48:53 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2023-07-21 17:45:32 +00:00
|
|
|
if got, want := secret.OrgID, int64(12); got != want {
|
|
|
|
t.Errorf("Want org_id %d, got %d", want, got)
|
2022-08-14 11:48:53 +00:00
|
|
|
}
|
|
|
|
if got, want := secret.Name, "password"; got != want {
|
|
|
|
t.Errorf("Want secret name %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Value, "correct-horse-battery-staple"; got != want {
|
|
|
|
t.Errorf("Want secret value %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Events[0], model.EventPush; got != want {
|
|
|
|
t.Errorf("Want secret event %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Events[1], model.EventTag; got != want {
|
|
|
|
t.Errorf("Want secret event %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Images[0], "golang"; got != want {
|
|
|
|
t.Errorf("Want secret image %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Images[1], "node"; got != want {
|
|
|
|
t.Errorf("Want secret image %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestOrgSecretList(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
createTestSecrets(t, store)
|
|
|
|
|
2023-07-21 17:45:32 +00:00
|
|
|
list, err := store.OrgSecretList(12, &model.ListOptions{All: true})
|
2022-08-14 11:48:53 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, list, 1)
|
|
|
|
|
2023-12-16 09:29:13 +00:00
|
|
|
assert.True(t, list[0].IsOrganization())
|
2022-08-14 11:48:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobalSecretFind(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
err := store.SecretCreate(&model.Secret{
|
|
|
|
Name: "password",
|
|
|
|
Value: "correct-horse-battery-staple",
|
|
|
|
Images: []string{"golang", "node"},
|
|
|
|
Events: []model.WebhookEvent{"push", "tag"},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("Unexpected error: insert secret: %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
secret, err := store.GlobalSecretFind("password")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if got, want := secret.Name, "password"; got != want {
|
|
|
|
t.Errorf("Want secret name %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Value, "correct-horse-battery-staple"; got != want {
|
|
|
|
t.Errorf("Want secret value %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Events[0], model.EventPush; got != want {
|
|
|
|
t.Errorf("Want secret event %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Events[1], model.EventTag; got != want {
|
|
|
|
t.Errorf("Want secret event %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Images[0], "golang"; got != want {
|
|
|
|
t.Errorf("Want secret image %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
if got, want := secret.Images[1], "node"; got != want {
|
|
|
|
t.Errorf("Want secret image %s, got %s", want, got)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGlobalSecretList(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Secret))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
createTestSecrets(t, store)
|
|
|
|
|
2023-04-30 01:40:13 +00:00
|
|
|
list, err := store.GlobalSecretList(&model.ListOptions{All: true})
|
2022-08-14 11:48:53 +00:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, list, 1)
|
|
|
|
|
2023-12-16 09:29:13 +00:00
|
|
|
assert.True(t, list[0].IsGlobal())
|
2022-08-14 11:48:53 +00:00
|
|
|
}
|