2022-10-18 03:24:12 +02:00
|
|
|
// Copyright 2022 Woodpecker Authors
|
2018-02-20 00:24:10 +02:00
|
|
|
// Copyright 2018 Drone.IO Inc.
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2018-03-21 15:02:17 +02:00
|
|
|
//
|
2018-02-20 00:24:10 +02: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.
|
|
|
|
|
2015-10-22 01:14:02 +02:00
|
|
|
package datastore
|
2015-09-30 03:21:17 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2021-11-13 21:18:06 +02:00
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-12 09:25:13 +02:00
|
|
|
|
2024-12-22 11:44:34 +02:00
|
|
|
"go.woodpecker-ci.org/woodpecker/v3/server/model"
|
2015-09-30 03:21:17 +02:00
|
|
|
)
|
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
func TestCreateRepo(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Repo))
|
2021-11-13 21:18:06 +02:00
|
|
|
defer closer()
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
repo := model.Repo{
|
|
|
|
UserID: 1,
|
|
|
|
FullName: "bradrydzewski/test",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
|
|
|
}
|
|
|
|
err := store.CreateRepo(&repo)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NotZero(t, repo.ID)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
err2 := store.UpdateRepo(&repo)
|
|
|
|
getRepo, err3 := store.GetRepo(repo.ID)
|
2016-04-23 13:27:28 +02:00
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
assert.NoError(t, err2)
|
|
|
|
assert.NoError(t, err3)
|
|
|
|
assert.Equal(t, repo.ID, getRepo.ID)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
// test that repo has name/owner/fullname
|
|
|
|
assert.Error(t, store.CreateRepo(&model.Repo{
|
|
|
|
UserID: 1,
|
|
|
|
FullName: "bradrydzewski/",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "",
|
|
|
|
}))
|
|
|
|
assert.Error(t, store.CreateRepo(&model.Repo{
|
|
|
|
UserID: 1,
|
|
|
|
FullName: "/test",
|
|
|
|
Owner: "",
|
|
|
|
Name: "test",
|
|
|
|
}))
|
|
|
|
assert.Error(t, store.CreateRepo(&model.Repo{
|
|
|
|
UserID: 1,
|
|
|
|
FullName: "",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
|
|
|
}))
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
// test unique name
|
|
|
|
repo2 := model.Repo{
|
|
|
|
UserID: 2,
|
|
|
|
FullName: "bradrydzewski/test",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
|
|
|
}
|
|
|
|
err2 = store.CreateRepo(&repo2)
|
|
|
|
assert.Error(t, err2)
|
|
|
|
}
|
2023-12-19 07:03:56 +02:00
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
func TestGetRepo(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Repo))
|
|
|
|
defer closer()
|
|
|
|
repo := model.Repo{
|
|
|
|
UserID: 1,
|
|
|
|
FullName: "bradrydzewski/test",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
|
|
|
}
|
|
|
|
assert.NoError(t, store.CreateRepo(&repo))
|
|
|
|
getrepo, err := store.GetRepo(repo.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, repo.ID, getrepo.ID)
|
|
|
|
assert.Equal(t, repo.UserID, getrepo.UserID)
|
|
|
|
assert.Equal(t, repo.Owner, getrepo.Owner)
|
|
|
|
assert.Equal(t, repo.Name, getrepo.Name)
|
|
|
|
}
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
func TestGetRepoName(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t, new(model.Repo))
|
|
|
|
defer closer()
|
|
|
|
repo := model.Repo{
|
|
|
|
UserID: 1,
|
|
|
|
FullName: "bradrydzewski/TEST",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "TEST",
|
|
|
|
}
|
2023-03-19 14:52:58 +02:00
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
assert.NoError(t, store.CreateRepo(&repo))
|
|
|
|
getrepo, err := store.GetRepoName(repo.FullName)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, repo.ID, getrepo.ID)
|
|
|
|
assert.Equal(t, repo.UserID, getrepo.UserID)
|
|
|
|
assert.Equal(t, repo.Owner, getrepo.Owner)
|
|
|
|
assert.Equal(t, repo.Name, getrepo.Name)
|
2015-09-30 03:21:17 +02:00
|
|
|
|
2024-12-30 08:08:53 +02:00
|
|
|
// case-insensitive
|
|
|
|
getrepo, err = store.GetRepoName("Bradrydzewski/test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, repo.ID, getrepo.ID)
|
|
|
|
assert.Equal(t, repo.UserID, getrepo.UserID)
|
|
|
|
assert.Equal(t, repo.Owner, getrepo.Owner)
|
|
|
|
assert.Equal(t, repo.Name, getrepo.Name)
|
2015-09-30 03:21:17 +02:00
|
|
|
}
|
2017-07-14 21:58:38 +02:00
|
|
|
|
2017-07-15 18:51:02 +02:00
|
|
|
func TestRepoList(t *testing.T) {
|
2023-08-21 15:04:12 +02:00
|
|
|
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm), new(model.Org))
|
2021-11-13 21:18:06 +02:00
|
|
|
defer closer()
|
2017-07-15 18:51:02 +02:00
|
|
|
|
|
|
|
user := &model.User{
|
2024-11-22 20:12:43 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
AccessToken: "e42080dddf012c718e476da161d21ad5",
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
assert.NoError(t, store.CreateUser(user))
|
2017-07-15 18:51:02 +02:00
|
|
|
|
|
|
|
repo1 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "bradrydzewski/test",
|
|
|
|
ForgeRemoteID: "1",
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
|
|
|
repo2 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "test/test",
|
|
|
|
ForgeRemoteID: "2",
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
|
|
|
repo3 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "octocat",
|
|
|
|
Name: "hello-world",
|
|
|
|
FullName: "octocat/hello-world",
|
|
|
|
ForgeRemoteID: "3",
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
assert.NoError(t, store.CreateRepo(repo1))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo2))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo3))
|
2017-07-15 18:51:02 +02:00
|
|
|
|
2021-11-15 01:55:37 +02:00
|
|
|
for _, perm := range []*model.Perm{
|
2022-09-05 17:08:51 +02:00
|
|
|
{UserID: user.ID, Repo: repo1},
|
|
|
|
{UserID: user.ID, Repo: repo2},
|
2021-11-15 01:55:37 +02:00
|
|
|
} {
|
|
|
|
assert.NoError(t, store.PermUpsert(perm))
|
|
|
|
}
|
2017-07-15 18:51:02 +02:00
|
|
|
|
2023-04-30 03:40:13 +02:00
|
|
|
repos, err := store.RepoList(user, false, false)
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, repos, 2)
|
|
|
|
assert.Equal(t, repo1.ID, repos[0].ID)
|
|
|
|
assert.Equal(t, repo2.ID, repos[1].ID)
|
2021-10-26 21:29:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestOwnedRepoList(t *testing.T) {
|
2023-08-21 15:04:12 +02:00
|
|
|
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm), new(model.Org))
|
2021-11-13 21:18:06 +02:00
|
|
|
defer closer()
|
2021-10-26 21:29:30 +02:00
|
|
|
|
|
|
|
user := &model.User{
|
2024-11-22 20:12:43 +02:00
|
|
|
Login: "joe",
|
|
|
|
Email: "foo@bar.com",
|
|
|
|
AccessToken: "e42080dddf012c718e476da161d21ad5",
|
2021-10-26 21:29:30 +02:00
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
assert.NoError(t, store.CreateUser(user))
|
2021-10-26 21:29:30 +02:00
|
|
|
|
|
|
|
repo1 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "bradrydzewski/test",
|
|
|
|
ForgeRemoteID: "1",
|
2021-10-26 21:29:30 +02:00
|
|
|
}
|
|
|
|
repo2 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "test/test",
|
|
|
|
ForgeRemoteID: "2",
|
2021-10-26 21:29:30 +02:00
|
|
|
}
|
|
|
|
repo3 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "octocat",
|
|
|
|
Name: "hello-world",
|
|
|
|
FullName: "octocat/hello-world",
|
|
|
|
ForgeRemoteID: "3",
|
2021-10-26 21:29:30 +02:00
|
|
|
}
|
|
|
|
repo4 := &model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
Owner: "demo",
|
|
|
|
Name: "demo",
|
|
|
|
FullName: "demo/demo",
|
|
|
|
ForgeRemoteID: "4",
|
2021-10-26 21:29:30 +02:00
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
assert.NoError(t, store.CreateRepo(repo1))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo2))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo3))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo4))
|
2021-10-26 21:29:30 +02:00
|
|
|
|
2021-11-15 01:55:37 +02:00
|
|
|
for _, perm := range []*model.Perm{
|
2022-09-05 17:08:51 +02:00
|
|
|
{UserID: user.ID, Repo: repo1, Push: true, Admin: false},
|
|
|
|
{UserID: user.ID, Repo: repo2, Push: false, Admin: true},
|
|
|
|
{UserID: user.ID, Repo: repo3},
|
|
|
|
{UserID: user.ID, Repo: repo4},
|
2021-11-15 01:55:37 +02:00
|
|
|
} {
|
|
|
|
assert.NoError(t, store.PermUpsert(perm))
|
|
|
|
}
|
2021-10-26 21:29:30 +02:00
|
|
|
|
2023-04-30 03:40:13 +02:00
|
|
|
repos, err := store.RepoList(user, true, false)
|
2024-01-14 20:33:58 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Len(t, repos, 2)
|
|
|
|
assert.Equal(t, repo1.ID, repos[0].ID)
|
|
|
|
assert.Equal(t, repo2.ID, repos[1].ID)
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoCount(t *testing.T) {
|
2021-11-13 21:18:06 +02:00
|
|
|
store, closer := newTestStore(t, new(model.Repo))
|
|
|
|
defer closer()
|
2017-07-15 18:51:02 +02:00
|
|
|
|
|
|
|
repo1 := &model.Repo{
|
|
|
|
Owner: "bradrydzewski",
|
2021-10-02 10:59:34 +02:00
|
|
|
Name: "test",
|
|
|
|
FullName: "bradrydzewski/test",
|
2017-07-25 21:32:22 +02:00
|
|
|
IsActive: true,
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
|
|
|
repo2 := &model.Repo{
|
2021-10-02 10:59:34 +02:00
|
|
|
Owner: "test",
|
|
|
|
Name: "test",
|
|
|
|
FullName: "test/test",
|
2017-07-25 21:32:22 +02:00
|
|
|
IsActive: true,
|
|
|
|
}
|
|
|
|
repo3 := &model.Repo{
|
2021-10-02 10:59:34 +02:00
|
|
|
Owner: "test",
|
|
|
|
Name: "test-ui",
|
|
|
|
FullName: "test/test-ui",
|
2017-07-25 21:32:22 +02:00
|
|
|
IsActive: false,
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
assert.NoError(t, store.CreateRepo(repo1))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo2))
|
|
|
|
assert.NoError(t, store.CreateRepo(repo3))
|
2017-07-15 18:51:02 +02:00
|
|
|
|
2024-01-14 20:33:58 +02:00
|
|
|
count, err := store.GetRepoCount()
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.EqualValues(t, 2, count)
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestRepoCrud(t *testing.T) {
|
2021-12-11 17:03:14 +02:00
|
|
|
store, closer := newTestStore(t,
|
|
|
|
new(model.Repo),
|
|
|
|
new(model.User),
|
|
|
|
new(model.Perm),
|
2022-10-18 03:24:12 +02:00
|
|
|
new(model.Pipeline),
|
|
|
|
new(model.PipelineConfig),
|
2023-06-06 09:52:08 +02:00
|
|
|
new(model.LogEntry),
|
2022-10-28 17:38:53 +02:00
|
|
|
new(model.Step),
|
2021-12-11 17:03:14 +02:00
|
|
|
new(model.Secret),
|
|
|
|
new(model.Registry),
|
2022-09-05 17:08:51 +02:00
|
|
|
new(model.Config),
|
2024-01-18 23:50:29 +02:00
|
|
|
new(model.Redirection),
|
|
|
|
new(model.Workflow))
|
2021-11-13 21:18:06 +02:00
|
|
|
defer closer()
|
2017-07-15 18:51:02 +02:00
|
|
|
|
|
|
|
repo := model.Repo{
|
|
|
|
UserID: 1,
|
2021-10-02 10:59:34 +02:00
|
|
|
FullName: "bradrydzewski/test",
|
2017-07-15 18:51:02 +02:00
|
|
|
Owner: "bradrydzewski",
|
2021-10-02 10:59:34 +02:00
|
|
|
Name: "test",
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
2021-11-13 21:18:06 +02:00
|
|
|
assert.NoError(t, store.CreateRepo(&repo))
|
2022-10-18 03:24:12 +02:00
|
|
|
pipeline := model.Pipeline{
|
2021-12-11 17:03:14 +02:00
|
|
|
RepoID: repo.ID,
|
|
|
|
}
|
2022-10-28 17:38:53 +02:00
|
|
|
step := model.Step{
|
|
|
|
Name: "a step",
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
2022-10-28 17:38:53 +02:00
|
|
|
assert.NoError(t, store.CreatePipeline(&pipeline, &step))
|
2021-12-11 17:03:14 +02:00
|
|
|
|
|
|
|
// create unrelated
|
|
|
|
repoUnrelated := model.Repo{
|
|
|
|
UserID: 2,
|
|
|
|
FullName: "x/x",
|
|
|
|
Owner: "x",
|
|
|
|
Name: "x",
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
2021-12-11 17:03:14 +02:00
|
|
|
assert.NoError(t, store.CreateRepo(&repoUnrelated))
|
2022-10-18 03:24:12 +02:00
|
|
|
pipelineUnrelated := model.Pipeline{
|
2021-12-11 17:03:14 +02:00
|
|
|
RepoID: repoUnrelated.ID,
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
2022-10-28 17:38:53 +02:00
|
|
|
stepUnrelated := model.Step{
|
2023-06-06 09:52:08 +02:00
|
|
|
UUID: "44c0de71-a6be-41c9-b860-e3716d1dfcef",
|
2022-10-28 17:38:53 +02:00
|
|
|
Name: "a unrelated step",
|
2021-12-11 17:03:14 +02:00
|
|
|
}
|
2022-10-28 17:38:53 +02:00
|
|
|
assert.NoError(t, store.CreatePipeline(&pipelineUnrelated, &stepUnrelated))
|
2021-12-11 17:03:14 +02:00
|
|
|
|
|
|
|
_, err := store.GetRepo(repo.ID)
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.NoError(t, store.DeleteRepo(&repo))
|
|
|
|
_, err = store.GetRepo(repo.ID)
|
|
|
|
assert.Error(t, err)
|
|
|
|
|
2022-10-28 17:38:53 +02:00
|
|
|
stepCount, err := store.engine.Count(new(model.Step))
|
2021-12-11 17:03:14 +02:00
|
|
|
assert.NoError(t, err)
|
2022-10-28 17:38:53 +02:00
|
|
|
assert.EqualValues(t, 1, stepCount)
|
2022-10-18 03:24:12 +02:00
|
|
|
pipelineCount, err := store.engine.Count(new(model.Pipeline))
|
2021-12-11 17:03:14 +02:00
|
|
|
assert.NoError(t, err)
|
2022-10-18 03:24:12 +02:00
|
|
|
assert.EqualValues(t, 1, pipelineCount)
|
2017-07-15 18:51:02 +02:00
|
|
|
}
|
2022-09-05 17:08:51 +02:00
|
|
|
|
|
|
|
func TestRepoRedirection(t *testing.T) {
|
|
|
|
store, closer := newTestStore(t,
|
|
|
|
new(model.Repo),
|
|
|
|
new(model.Redirection))
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
repo := model.Repo{
|
2022-11-15 16:01:23 +02:00
|
|
|
UserID: 1,
|
|
|
|
ForgeRemoteID: "1",
|
|
|
|
FullName: "bradrydzewski/test",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test",
|
2022-09-05 17:08:51 +02:00
|
|
|
}
|
|
|
|
assert.NoError(t, store.CreateRepo(&repo))
|
|
|
|
|
|
|
|
repoUpdated := model.Repo{
|
2023-03-22 00:01:59 +02:00
|
|
|
ID: repo.ID,
|
2022-11-15 16:01:23 +02:00
|
|
|
ForgeRemoteID: "1",
|
|
|
|
FullName: "bradrydzewski/test-renamed",
|
|
|
|
Owner: "bradrydzewski",
|
|
|
|
Name: "test-renamed",
|
2022-09-05 17:08:51 +02:00
|
|
|
}
|
|
|
|
|
2023-03-22 00:01:59 +02:00
|
|
|
assert.NoError(t, store.UpdateRepo(&repoUpdated))
|
|
|
|
assert.NoError(t, store.CreateRedirection(&model.Redirection{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
FullName: repo.FullName,
|
|
|
|
}))
|
2022-09-05 17:08:51 +02:00
|
|
|
|
|
|
|
// test redirection from old repo name
|
|
|
|
repoFromStore, err := store.GetRepoNameFallback("1", "bradrydzewski/test")
|
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, repoFromStore.FullName, repoUpdated.FullName)
|
|
|
|
|
2022-11-05 01:35:06 +02:00
|
|
|
// test getting repo without forge ID (use name fallback)
|
2022-09-05 17:08:51 +02:00
|
|
|
repo = model.Repo{
|
|
|
|
UserID: 1,
|
2022-11-05 01:35:06 +02:00
|
|
|
FullName: "bradrydzewski/test-no-forge-id",
|
2022-09-05 17:08:51 +02:00
|
|
|
Owner: "bradrydzewski",
|
2022-11-05 01:35:06 +02:00
|
|
|
Name: "test-no-forge-id",
|
2022-09-05 17:08:51 +02:00
|
|
|
}
|
|
|
|
assert.NoError(t, store.CreateRepo(&repo))
|
|
|
|
|
2022-11-05 01:35:06 +02:00
|
|
|
repoFromStore, err = store.GetRepoNameFallback("", "bradrydzewski/test-no-forge-id")
|
2022-09-05 17:08:51 +02:00
|
|
|
assert.NoError(t, err)
|
|
|
|
assert.Equal(t, repoFromStore.FullName, repo.FullName)
|
|
|
|
}
|