mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-24 08:02:18 +02:00
added tests for repositories
This commit is contained in:
parent
54caa88281
commit
d36f82784c
@ -40,7 +40,7 @@ func Connect(driver, datasource string) (*sql.DB, error) {
|
|||||||
return migration.Open(driver, datasource, migrations)
|
return migration.Open(driver, datasource, migrations)
|
||||||
}
|
}
|
||||||
|
|
||||||
// MustConnect is a helper function that create a
|
// MustConnect is a helper function that creates a
|
||||||
// new database commention and auto-generates the
|
// new database commention and auto-generates the
|
||||||
// database schema. An error causes a panic.
|
// database schema. An error causes a panic.
|
||||||
func MustConnect(driver, datasource string) *sql.DB {
|
func MustConnect(driver, datasource string) *sql.DB {
|
||||||
|
@ -15,24 +15,24 @@ func NewPermstore(db meddler.DB) *Permstore {
|
|||||||
|
|
||||||
// GetPerm retrieves the User's permission from
|
// GetPerm retrieves the User's permission from
|
||||||
// the datastore for the given repository.
|
// the datastore for the given repository.
|
||||||
func (db *Repostore) GetPerm(user *model.User, repo *model.Repo) (*model.Perm, error) {
|
func (db *Permstore) GetPerm(user *model.User, repo *model.Repo) (*model.Perm, error) {
|
||||||
var perm = new(model.Perm)
|
var perm = new(model.Perm)
|
||||||
var err = meddler.QueryRow(db, perm, rebind(permQuery), user.ID, repo.ID)
|
var err = meddler.QueryRow(db, perm, rebind(permQuery), user.ID, repo.ID)
|
||||||
return perm, err
|
return perm, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostPerm saves permission in the datastore.
|
// PostPerm saves permission in the datastore.
|
||||||
func (db *Repostore) PostPerm(perm *model.Perm) error {
|
func (db *Permstore) PostPerm(perm *model.Perm) error {
|
||||||
return meddler.Save(db, permTable, perm)
|
return meddler.Save(db, permTable, perm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// PutPerm saves permission in the datastore.
|
// PutPerm saves permission in the datastore.
|
||||||
func (db *Repostore) PutPerm(perm *model.Perm) error {
|
func (db *Permstore) PutPerm(perm *model.Perm) error {
|
||||||
return meddler.Save(db, permTable, perm)
|
return meddler.Save(db, permTable, perm)
|
||||||
}
|
}
|
||||||
|
|
||||||
// DelPerm removes permission from the datastore.
|
// DelPerm removes permission from the datastore.
|
||||||
func (db *Repostore) DelPerm(perm *model.Perm) error {
|
func (db *Permstore) DelPerm(perm *model.Perm) error {
|
||||||
var _, err = db.Exec(rebind(permDeleteStmt), perm.ID)
|
var _, err = db.Exec(rebind(permDeleteStmt), perm.ID)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -33,7 +33,7 @@ func (db *Repostore) GetRepoName(remote, owner, name string) (*model.Repo, error
|
|||||||
// the datastore accessible by the given user ID.
|
// the datastore accessible by the given user ID.
|
||||||
func (db *Repostore) GetRepoList(user *model.User) ([]*model.Repo, error) {
|
func (db *Repostore) GetRepoList(user *model.User) ([]*model.Repo, error) {
|
||||||
var repos []*model.Repo
|
var repos []*model.Repo
|
||||||
var err = meddler.QueryAll(db, &repos, rebind(repoListQuery))
|
var err = meddler.QueryAll(db, &repos, rebind(repoListQuery), user.ID)
|
||||||
return repos, err
|
return repos, err
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -80,6 +80,6 @@ WHERE repo_id IN (
|
|||||||
|
|
||||||
// SQL statement to delete a User by ID.
|
// SQL statement to delete a User by ID.
|
||||||
const repoDeleteStmt = `
|
const repoDeleteStmt = `
|
||||||
DELETE FROM users
|
DELETE FROM repos
|
||||||
WHERE user_id = ?
|
WHERE repo_id = ?
|
||||||
`
|
`
|
||||||
|
@ -1 +1,163 @@
|
|||||||
package database
|
package database
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/drone/drone/shared/model"
|
||||||
|
"github.com/franela/goblin"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestRepostore(t *testing.T) {
|
||||||
|
db := MustConnect("sqlite3", ":memory:")
|
||||||
|
rs := NewRepostore(db)
|
||||||
|
ps := NewPermstore(db)
|
||||||
|
defer db.Close()
|
||||||
|
|
||||||
|
g := goblin.Goblin(t)
|
||||||
|
g.Describe("Repostore", func() {
|
||||||
|
|
||||||
|
// before each test be sure to purge the package
|
||||||
|
// table data from the database.
|
||||||
|
g.BeforeEach(func() {
|
||||||
|
db.Exec("DELETE FROM perms")
|
||||||
|
db.Exec("DELETE FROM repos")
|
||||||
|
db.Exec("DELETE FROM users")
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should Put a Repo", func() {
|
||||||
|
repo := model.Repo{
|
||||||
|
UserID: 1,
|
||||||
|
Remote: "enterprise.github.com",
|
||||||
|
Host: "github.drone.io",
|
||||||
|
Owner: "bradrydzewski",
|
||||||
|
Name: "drone",
|
||||||
|
}
|
||||||
|
err := rs.PostRepo(&repo)
|
||||||
|
g.Assert(err == nil).IsTrue()
|
||||||
|
g.Assert(repo.ID != 0).IsTrue()
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should Post a Repo", func() {
|
||||||
|
repo := model.Repo{
|
||||||
|
UserID: 1,
|
||||||
|
Remote: "enterprise.github.com",
|
||||||
|
Host: "github.drone.io",
|
||||||
|
Owner: "bradrydzewski",
|
||||||
|
Name: "drone",
|
||||||
|
}
|
||||||
|
err := rs.PostRepo(&repo)
|
||||||
|
g.Assert(err == nil).IsTrue()
|
||||||
|
g.Assert(repo.ID != 0).IsTrue()
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should Get a Repo by ID", func() {
|
||||||
|
repo := model.Repo{
|
||||||
|
UserID: 1,
|
||||||
|
Remote: "enterprise.github.com",
|
||||||
|
Host: "github.drone.io",
|
||||||
|
Owner: "bradrydzewski",
|
||||||
|
Name: "drone",
|
||||||
|
}
|
||||||
|
rs.PostRepo(&repo)
|
||||||
|
getrepo, err := rs.GetRepo(repo.ID)
|
||||||
|
g.Assert(err == nil).IsTrue()
|
||||||
|
g.Assert(repo.ID).Equal(getrepo.ID)
|
||||||
|
g.Assert(repo.UserID).Equal(getrepo.UserID)
|
||||||
|
g.Assert(repo.Remote).Equal(getrepo.Remote)
|
||||||
|
g.Assert(repo.Host).Equal(getrepo.Host)
|
||||||
|
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{
|
||||||
|
UserID: 1,
|
||||||
|
Remote: "enterprise.github.com",
|
||||||
|
Host: "github.drone.io",
|
||||||
|
Owner: "bradrydzewski",
|
||||||
|
Name: "drone",
|
||||||
|
}
|
||||||
|
rs.PostRepo(&repo)
|
||||||
|
getrepo, err := rs.GetRepoName(repo.Host, repo.Owner, repo.Name)
|
||||||
|
g.Assert(err == nil).IsTrue()
|
||||||
|
g.Assert(repo.ID).Equal(getrepo.ID)
|
||||||
|
g.Assert(repo.UserID).Equal(getrepo.UserID)
|
||||||
|
g.Assert(repo.Remote).Equal(getrepo.Remote)
|
||||||
|
g.Assert(repo.Host).Equal(getrepo.Host)
|
||||||
|
g.Assert(repo.Owner).Equal(getrepo.Owner)
|
||||||
|
g.Assert(repo.Name).Equal(getrepo.Name)
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should Get a Repo List by User", func() {
|
||||||
|
repo1 := model.Repo{
|
||||||
|
UserID: 1,
|
||||||
|
Remote: "enterprise.github.com",
|
||||||
|
Host: "github.drone.io",
|
||||||
|
Owner: "bradrydzewski",
|
||||||
|
Name: "drone",
|
||||||
|
}
|
||||||
|
repo2 := model.Repo{
|
||||||
|
UserID: 1,
|
||||||
|
Remote: "enterprise.github.com",
|
||||||
|
Host: "github.drone.io",
|
||||||
|
Owner: "bradrydzewski",
|
||||||
|
Name: "drone-dart",
|
||||||
|
}
|
||||||
|
rs.PostRepo(&repo1)
|
||||||
|
rs.PostRepo(&repo2)
|
||||||
|
ps.PostPerm(&model.Perm{
|
||||||
|
RepoID: repo1.ID,
|
||||||
|
UserID: 1,
|
||||||
|
Read: true,
|
||||||
|
Write: true,
|
||||||
|
Admin: true,
|
||||||
|
})
|
||||||
|
repos, err := rs.GetRepoList(&model.User{ID: 1})
|
||||||
|
g.Assert(err == nil).IsTrue()
|
||||||
|
g.Assert(len(repos)).Equal(1)
|
||||||
|
g.Assert(repos[0].UserID).Equal(repo1.UserID)
|
||||||
|
g.Assert(repos[0].Remote).Equal(repo1.Remote)
|
||||||
|
g.Assert(repos[0].Host).Equal(repo1.Host)
|
||||||
|
g.Assert(repos[0].Owner).Equal(repo1.Owner)
|
||||||
|
g.Assert(repos[0].Name).Equal(repo1.Name)
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should Delete a Repo", func() {
|
||||||
|
repo := model.Repo{
|
||||||
|
UserID: 1,
|
||||||
|
Remote: "enterprise.github.com",
|
||||||
|
Host: "github.drone.io",
|
||||||
|
Owner: "bradrydzewski",
|
||||||
|
Name: "drone",
|
||||||
|
}
|
||||||
|
rs.PostRepo(&repo)
|
||||||
|
_, err1 := rs.GetRepo(repo.ID)
|
||||||
|
err2 := rs.DelRepo(&repo)
|
||||||
|
_, err3 := rs.GetRepo(repo.ID)
|
||||||
|
g.Assert(err1 == nil).IsTrue()
|
||||||
|
g.Assert(err2 == nil).IsTrue()
|
||||||
|
g.Assert(err3 == nil).IsFalse()
|
||||||
|
})
|
||||||
|
|
||||||
|
g.It("Should Enforce Unique Repo Name", func() {
|
||||||
|
repo1 := model.Repo{
|
||||||
|
UserID: 1,
|
||||||
|
Remote: "enterprise.github.com",
|
||||||
|
Host: "github.drone.io",
|
||||||
|
Owner: "bradrydzewski",
|
||||||
|
Name: "drone",
|
||||||
|
}
|
||||||
|
repo2 := model.Repo{
|
||||||
|
UserID: 2,
|
||||||
|
Remote: "enterprise.github.com",
|
||||||
|
Host: "github.drone.io",
|
||||||
|
Owner: "bradrydzewski",
|
||||||
|
Name: "drone",
|
||||||
|
}
|
||||||
|
err1 := rs.PostRepo(&repo1)
|
||||||
|
err2 := rs.PostRepo(&repo2)
|
||||||
|
g.Assert(err1 == nil).IsTrue()
|
||||||
|
g.Assert(err2 == nil).IsFalse()
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
@ -7,7 +7,7 @@ import (
|
|||||||
"github.com/franela/goblin"
|
"github.com/franela/goblin"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestPackagestore(t *testing.T) {
|
func TestUserstore(t *testing.T) {
|
||||||
db := MustConnect("sqlite3", ":memory:")
|
db := MustConnect("sqlite3", ":memory:")
|
||||||
us := NewUserstore(db)
|
us := NewUserstore(db)
|
||||||
defer db.Close()
|
defer db.Close()
|
||||||
|
Loading…
Reference in New Issue
Block a user