2015-10-21 01:45:24 +02:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2016-03-05 07:15:50 +02:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2015-10-21 01:45:24 +02:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/drone/drone/model"
|
2016-03-05 07:15:50 +02:00
|
|
|
"github.com/drone/drone/remote"
|
|
|
|
"github.com/drone/drone/remote/mock"
|
2015-10-21 01:45:24 +02:00
|
|
|
"github.com/franela/goblin"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHelper(t *testing.T) {
|
|
|
|
|
|
|
|
g := goblin.Goblin(t)
|
2016-03-05 07:15:50 +02:00
|
|
|
|
2015-10-21 01:45:24 +02:00
|
|
|
g.Describe("Cache helpers", func() {
|
|
|
|
|
|
|
|
var c *gin.Context
|
2016-03-05 07:15:50 +02:00
|
|
|
var r *mock.Remote
|
|
|
|
|
2015-10-21 01:45:24 +02:00
|
|
|
g.BeforeEach(func() {
|
|
|
|
c = new(gin.Context)
|
|
|
|
ToContext(c, Default())
|
2016-03-05 07:15:50 +02:00
|
|
|
|
|
|
|
r = new(mock.Remote)
|
|
|
|
remote.ToContext(c, r)
|
2015-10-21 01:45:24 +02:00
|
|
|
})
|
|
|
|
|
2016-03-05 07:15:50 +02:00
|
|
|
g.It("Should get permissions from remote", func() {
|
|
|
|
r.On("Perm", fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(fakePerm, nil).Once()
|
|
|
|
p, err := GetPerms(c, fakeUser, fakeRepo.Owner, fakeRepo.Name)
|
|
|
|
g.Assert(p).Equal(fakePerm)
|
|
|
|
g.Assert(err).Equal(nil)
|
2015-10-21 01:45:24 +02:00
|
|
|
|
|
|
|
})
|
|
|
|
|
2016-03-05 07:15:50 +02:00
|
|
|
g.It("Should get permissions from cache", func() {
|
|
|
|
key := fmt.Sprintf("perms:%s:%s/%s",
|
|
|
|
fakeUser.Login,
|
|
|
|
fakeRepo.Owner,
|
|
|
|
fakeRepo.Name,
|
|
|
|
)
|
|
|
|
|
|
|
|
Set(c, key, fakePerm)
|
|
|
|
r.On("Perm", fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(nil, fakeErr).Once()
|
|
|
|
p, err := GetPerms(c, fakeUser, fakeRepo.Owner, fakeRepo.Name)
|
|
|
|
g.Assert(p).Equal(fakePerm)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get permissions error", func() {
|
|
|
|
r.On("Perm", fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(nil, fakeErr).Once()
|
|
|
|
p, err := GetPerms(c, fakeUser, fakeRepo.Owner, fakeRepo.Name)
|
|
|
|
g.Assert(p == nil).IsTrue()
|
|
|
|
g.Assert(err).Equal(fakeErr)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should set and get repos", func() {
|
|
|
|
|
|
|
|
r.On("Repos", fakeUser).Return(fakeRepos, nil).Once()
|
|
|
|
p, err := GetRepos(c, fakeUser)
|
|
|
|
g.Assert(p).Equal(fakeRepos)
|
|
|
|
g.Assert(err).Equal(nil)
|
2015-10-21 01:45:24 +02:00
|
|
|
})
|
|
|
|
|
2016-03-05 07:15:50 +02:00
|
|
|
g.It("Should get repos", func() {
|
|
|
|
key := fmt.Sprintf("repos:%s",
|
|
|
|
fakeUser.Login,
|
|
|
|
)
|
2015-10-21 01:45:24 +02:00
|
|
|
|
2016-03-05 07:15:50 +02:00
|
|
|
Set(c, key, fakeRepos)
|
|
|
|
r.On("Repos", fakeUser).Return(nil, fakeErr).Once()
|
|
|
|
p, err := GetRepos(c, fakeUser)
|
|
|
|
g.Assert(p).Equal(fakeRepos)
|
|
|
|
g.Assert(err).Equal(nil)
|
2015-10-21 01:45:24 +02:00
|
|
|
})
|
|
|
|
|
2016-03-05 07:15:50 +02:00
|
|
|
g.It("Should get repos error", func() {
|
|
|
|
r.On("Repos", fakeUser).Return(nil, fakeErr).Once()
|
|
|
|
p, err := GetRepos(c, fakeUser)
|
|
|
|
g.Assert(p == nil).IsTrue()
|
|
|
|
g.Assert(err).Equal(fakeErr)
|
2015-10-21 01:45:24 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-03-05 07:15:50 +02:00
|
|
|
fakeErr = errors.New("Not Found")
|
2015-10-21 01:45:24 +02:00
|
|
|
fakeUser = &model.User{Login: "octocat"}
|
|
|
|
fakePerm = &model.Perm{true, true, true}
|
2016-03-05 07:15:50 +02:00
|
|
|
fakeRepo = &model.RepoLite{Owner: "octocat", Name: "Hello-World"}
|
2015-10-21 01:45:24 +02:00
|
|
|
fakeRepos = []*model.RepoLite{
|
|
|
|
{Owner: "octocat", Name: "Hello-World"},
|
|
|
|
{Owner: "octocat", Name: "hello-world"},
|
|
|
|
{Owner: "octocat", Name: "Spoon-Knife"},
|
|
|
|
}
|
|
|
|
)
|