woodpecker/server/store/datastore/repo_test.go

406 lines
10 KiB
Go
Raw Normal View History

// 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.
package datastore
2015-09-30 01:21:17 +00:00
import (
"testing"
"github.com/franela/goblin"
"github.com/stretchr/testify/assert"
"go.woodpecker-ci.org/woodpecker/v2/server/model"
2015-09-30 01:21:17 +00:00
)
func TestRepos(t *testing.T) {
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Pipeline))
defer closer()
2015-09-30 01:21:17 +00:00
g := goblin.Goblin(t)
g.Describe("Repo", func() {
// before each test be sure to purge the package
// table data from the database.
g.BeforeEach(func() {
_, err := store.engine.Exec("DELETE FROM pipelines")
g.Assert(err).IsNil()
_, err = store.engine.Exec("DELETE FROM repos")
g.Assert(err).IsNil()
_, err = store.engine.Exec("DELETE FROM users")
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
})
g.It("Should Set a Repo", func() {
repo := model.Repo{
2015-09-30 01:21:17 +00:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 01:21:17 +00:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 01:21:17 +00:00
}
err1 := store.CreateRepo(&repo)
err2 := store.UpdateRepo(&repo)
getRepo, err3 := store.GetRepo(repo.ID)
2016-04-23 11:27:28 +00:00
g.Assert(err1).IsNil()
g.Assert(err2).IsNil()
g.Assert(err3).IsNil()
g.Assert(repo.ID).Equal(getRepo.ID)
2015-09-30 01:21:17 +00:00
})
g.It("Should Add a Repo", func() {
repo := model.Repo{
2015-09-30 01:21:17 +00:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 01:21:17 +00:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 01:21:17 +00:00
}
err := store.CreateRepo(&repo)
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(repo.ID != 0).IsTrue()
})
g.It("Should fail if repo has no name / owner / fullname", func() {
g.Assert(store.CreateRepo(&model.Repo{
UserID: 1,
FullName: "bradrydzewski/",
Owner: "bradrydzewski",
Name: "",
})).IsNotNil()
g.Assert(store.CreateRepo(&model.Repo{
UserID: 1,
FullName: "/test",
Owner: "",
Name: "test",
})).IsNotNil()
g.Assert(store.CreateRepo(&model.Repo{
UserID: 1,
FullName: "",
Owner: "bradrydzewski",
Name: "test",
})).IsNotNil()
})
2015-09-30 01:21:17 +00:00
g.It("Should Get a Repo by ID", func() {
repo := model.Repo{
2015-09-30 01:21:17 +00:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 01:21:17 +00:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 01:21:17 +00:00
}
g.Assert(store.CreateRepo(&repo)).IsNil()
getrepo, err := store.GetRepo(repo.ID)
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(repo.ID).Equal(getrepo.ID)
g.Assert(repo.UserID).Equal(getrepo.UserID)
g.Assert(repo.Owner).Equal(getrepo.Owner)
g.Assert(repo.Name).Equal(getrepo.Name)
})
g.It("Should Get a Repo by Name", func() {
repo := model.Repo{
2015-09-30 01:21:17 +00:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 01:21:17 +00:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 01:21:17 +00:00
}
g.Assert(store.CreateRepo(&repo)).IsNil()
getrepo, err := store.GetRepoName(repo.FullName)
g.Assert(err).IsNil()
2015-09-30 01:21:17 +00:00
g.Assert(repo.ID).Equal(getrepo.ID)
g.Assert(repo.UserID).Equal(getrepo.UserID)
g.Assert(repo.Owner).Equal(getrepo.Owner)
g.Assert(repo.Name).Equal(getrepo.Name)
})
g.It("Should Get a Repo by Name (case-insensitive)", func() {
repo := model.Repo{
UserID: 1,
FullName: "bradrydzewski/TEST",
Owner: "bradrydzewski",
Name: "TEST",
}
g.Assert(store.CreateRepo(&repo)).IsNil()
getrepo, err := store.GetRepoName("Bradrydzewski/test")
g.Assert(err).IsNil()
g.Assert(repo.ID).Equal(getrepo.ID)
g.Assert(repo.UserID).Equal(getrepo.UserID)
g.Assert(repo.Owner).Equal(getrepo.Owner)
g.Assert(repo.Name).Equal(getrepo.Name)
2015-09-30 01:21:17 +00:00
})
g.It("Should Enforce Unique Repo Name", func() {
repo1 := model.Repo{
2015-09-30 01:21:17 +00:00
UserID: 1,
FullName: "bradrydzewski/test",
2015-09-30 01:21:17 +00:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 01:21:17 +00:00
}
repo2 := model.Repo{
2015-09-30 01:21:17 +00:00
UserID: 2,
FullName: "bradrydzewski/test",
2015-09-30 01:21:17 +00:00
Owner: "bradrydzewski",
Name: "test",
2015-09-30 01:21:17 +00:00
}
err1 := store.CreateRepo(&repo1)
err2 := store.CreateRepo(&repo2)
g.Assert(err1).IsNil()
g.Assert(err2 == nil).IsFalse()
2015-09-30 01:21:17 +00:00
})
})
}
2017-07-14 19:58:38 +00:00
2017-07-15 16:51:02 +00:00
func TestRepoList(t *testing.T) {
2023-08-21 13:04:12 +00:00
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm), new(model.Org))
defer closer()
2017-07-15 16:51:02 +00:00
user := &model.User{
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
assert.NoError(t, store.CreateUser(user))
2017-07-15 16:51:02 +00:00
repo1 := &model.Repo{
Owner: "bradrydzewski",
Name: "test",
FullName: "bradrydzewski/test",
ForgeRemoteID: "1",
2017-07-15 16:51:02 +00:00
}
repo2 := &model.Repo{
Owner: "test",
Name: "test",
FullName: "test/test",
ForgeRemoteID: "2",
2017-07-15 16:51:02 +00:00
}
repo3 := &model.Repo{
Owner: "octocat",
Name: "hello-world",
FullName: "octocat/hello-world",
ForgeRemoteID: "3",
2017-07-15 16:51:02 +00:00
}
assert.NoError(t, store.CreateRepo(repo1))
assert.NoError(t, store.CreateRepo(repo2))
assert.NoError(t, store.CreateRepo(repo3))
2017-07-15 16:51:02 +00:00
for _, perm := range []*model.Perm{
{UserID: user.ID, Repo: repo1},
{UserID: user.ID, Repo: repo2},
} {
assert.NoError(t, store.PermUpsert(perm))
}
2017-07-15 16:51:02 +00:00
repos, err := store.RepoList(user, false, false)
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)
}
func TestOwnedRepoList(t *testing.T) {
2023-08-21 13:04:12 +00:00
store, closer := newTestStore(t, new(model.Repo), new(model.User), new(model.Perm), new(model.Org))
defer closer()
user := &model.User{
Login: "joe",
Email: "foo@bar.com",
Token: "e42080dddf012c718e476da161d21ad5",
}
assert.NoError(t, store.CreateUser(user))
repo1 := &model.Repo{
Owner: "bradrydzewski",
Name: "test",
FullName: "bradrydzewski/test",
ForgeRemoteID: "1",
}
repo2 := &model.Repo{
Owner: "test",
Name: "test",
FullName: "test/test",
ForgeRemoteID: "2",
}
repo3 := &model.Repo{
Owner: "octocat",
Name: "hello-world",
FullName: "octocat/hello-world",
ForgeRemoteID: "3",
}
repo4 := &model.Repo{
Owner: "demo",
Name: "demo",
FullName: "demo/demo",
ForgeRemoteID: "4",
}
assert.NoError(t, store.CreateRepo(repo1))
assert.NoError(t, store.CreateRepo(repo2))
assert.NoError(t, store.CreateRepo(repo3))
assert.NoError(t, store.CreateRepo(repo4))
for _, perm := range []*model.Perm{
{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},
} {
assert.NoError(t, store.PermUpsert(perm))
}
repos, err := store.RepoList(user, true, false)
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 16:51:02 +00:00
}
func TestRepoCount(t *testing.T) {
store, closer := newTestStore(t, new(model.Repo))
defer closer()
2017-07-15 16:51:02 +00:00
repo1 := &model.Repo{
Owner: "bradrydzewski",
Name: "test",
FullName: "bradrydzewski/test",
2017-07-25 19:32:22 +00:00
IsActive: true,
2017-07-15 16:51:02 +00:00
}
repo2 := &model.Repo{
Owner: "test",
Name: "test",
FullName: "test/test",
2017-07-25 19:32:22 +00:00
IsActive: true,
}
repo3 := &model.Repo{
Owner: "test",
Name: "test-ui",
FullName: "test/test-ui",
2017-07-25 19:32:22 +00:00
IsActive: false,
2017-07-15 16:51:02 +00:00
}
assert.NoError(t, store.CreateRepo(repo1))
assert.NoError(t, store.CreateRepo(repo2))
assert.NoError(t, store.CreateRepo(repo3))
2017-07-15 16:51:02 +00:00
count, err := store.GetRepoCount()
assert.NoError(t, err)
assert.EqualValues(t, 2, count)
2017-07-15 16:51:02 +00:00
}
func TestRepoCrud(t *testing.T) {
store, closer := newTestStore(t,
new(model.Repo),
new(model.User),
new(model.Perm),
new(model.Pipeline),
new(model.PipelineConfig),
new(model.LogEntry),
new(model.Step),
new(model.Secret),
new(model.Registry),
new(model.Config),
new(model.Redirection),
new(model.Workflow))
defer closer()
2017-07-15 16:51:02 +00:00
repo := model.Repo{
UserID: 1,
FullName: "bradrydzewski/test",
2017-07-15 16:51:02 +00:00
Owner: "bradrydzewski",
Name: "test",
2017-07-15 16:51:02 +00:00
}
assert.NoError(t, store.CreateRepo(&repo))
pipeline := model.Pipeline{
RepoID: repo.ID,
}
step := model.Step{
Name: "a step",
2017-07-15 16:51:02 +00:00
}
assert.NoError(t, store.CreatePipeline(&pipeline, &step))
// create unrelated
repoUnrelated := model.Repo{
UserID: 2,
FullName: "x/x",
Owner: "x",
Name: "x",
2017-07-15 16:51:02 +00:00
}
assert.NoError(t, store.CreateRepo(&repoUnrelated))
pipelineUnrelated := model.Pipeline{
RepoID: repoUnrelated.ID,
2017-07-15 16:51:02 +00:00
}
stepUnrelated := model.Step{
UUID: "44c0de71-a6be-41c9-b860-e3716d1dfcef",
Name: "a unrelated step",
}
assert.NoError(t, store.CreatePipeline(&pipelineUnrelated, &stepUnrelated))
_, err := store.GetRepo(repo.ID)
assert.NoError(t, err)
assert.NoError(t, store.DeleteRepo(&repo))
_, err = store.GetRepo(repo.ID)
assert.Error(t, err)
stepCount, err := store.engine.Count(new(model.Step))
assert.NoError(t, err)
assert.EqualValues(t, 1, stepCount)
pipelineCount, err := store.engine.Count(new(model.Pipeline))
assert.NoError(t, err)
assert.EqualValues(t, 1, pipelineCount)
2017-07-15 16:51:02 +00:00
}
func TestRepoRedirection(t *testing.T) {
store, closer := newTestStore(t,
new(model.Repo),
new(model.Redirection))
defer closer()
repo := model.Repo{
UserID: 1,
ForgeRemoteID: "1",
FullName: "bradrydzewski/test",
Owner: "bradrydzewski",
Name: "test",
}
assert.NoError(t, store.CreateRepo(&repo))
repoUpdated := model.Repo{
ID: repo.ID,
ForgeRemoteID: "1",
FullName: "bradrydzewski/test-renamed",
Owner: "bradrydzewski",
Name: "test-renamed",
}
assert.NoError(t, store.UpdateRepo(&repoUpdated))
assert.NoError(t, store.CreateRedirection(&model.Redirection{
RepoID: repo.ID,
FullName: repo.FullName,
}))
// test redirection from old repo name
repoFromStore, err := store.GetRepoNameFallback("1", "bradrydzewski/test")
assert.NoError(t, err)
assert.Equal(t, repoFromStore.FullName, repoUpdated.FullName)
// test getting repo without forge ID (use name fallback)
repo = model.Repo{
UserID: 1,
FullName: "bradrydzewski/test-no-forge-id",
Owner: "bradrydzewski",
Name: "test-no-forge-id",
}
assert.NoError(t, store.CreateRepo(&repo))
repoFromStore, err = store.GetRepoNameFallback("", "bradrydzewski/test-no-forge-id")
assert.NoError(t, err)
assert.Equal(t, repoFromStore.FullName, repo.FullName)
}