2016-04-19 07:22:23 +03:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
2016-11-14 15:32:32 +03:00
|
|
|
"github.com/go-gitea/lgtm/model"
|
|
|
|
"github.com/go-gitea/lgtm/remote"
|
2021-10-07 00:25:55 +03:00
|
|
|
mocks "github.com/go-gitea/lgtm/remote/mock"
|
2016-04-19 07:22:23 +03:00
|
|
|
|
|
|
|
"github.com/franela/goblin"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestHelper(t *testing.T) {
|
|
|
|
|
|
|
|
g := goblin.Goblin(t)
|
|
|
|
|
|
|
|
g.Describe("Cache helpers", func() {
|
|
|
|
|
|
|
|
var c *gin.Context
|
2016-11-15 12:36:25 +03:00
|
|
|
var r *mocks.Remote
|
2016-04-19 07:22:23 +03:00
|
|
|
|
|
|
|
g.BeforeEach(func() {
|
|
|
|
c = new(gin.Context)
|
|
|
|
ToContext(c, Default())
|
|
|
|
|
2016-11-15 12:36:25 +03:00
|
|
|
r = new(mocks.Remote)
|
2016-04-19 07:22:23 +03:00
|
|
|
remote.ToContext(c, r)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get permissions from remote", func() {
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetPerm", c, fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(fakePerm, nil).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetPerm(c, fakeUser, fakeRepo.Owner, fakeRepo.Name)
|
|
|
|
g.Assert(p).Equal(fakePerm)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get permissions from cache", func() {
|
|
|
|
key := fmt.Sprintf("perms:%s:%s/%s",
|
|
|
|
fakeUser.Login,
|
|
|
|
fakeRepo.Owner,
|
|
|
|
fakeRepo.Name,
|
|
|
|
)
|
|
|
|
|
2021-10-07 00:25:55 +03:00
|
|
|
err := Set(c, key, fakePerm)
|
|
|
|
g.Assert(err).IsNil()
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetPerm", c, fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(nil, errFake).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetPerm(c, fakeUser, fakeRepo.Owner, fakeRepo.Name)
|
|
|
|
g.Assert(p).Equal(fakePerm)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get permissions error", func() {
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetPerm", c, fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(nil, errFake).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetPerm(c, fakeUser, fakeRepo.Owner, fakeRepo.Name)
|
|
|
|
g.Assert(p == nil).IsTrue()
|
2016-11-15 12:36:25 +03:00
|
|
|
g.Assert(err).Equal(errFake)
|
2016-04-19 07:22:23 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should set and get repos", func() {
|
|
|
|
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetRepos", c, fakeUser).Return(fakeRepos, nil).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetRepos(c, fakeUser)
|
|
|
|
g.Assert(p).Equal(fakeRepos)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get repos", func() {
|
|
|
|
key := fmt.Sprintf("repos:%s",
|
|
|
|
fakeUser.Login,
|
|
|
|
)
|
|
|
|
|
2021-10-07 00:25:55 +03:00
|
|
|
err := Set(c, key, fakeRepos)
|
|
|
|
g.Assert(err).IsNil()
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetRepos", c, fakeUser).Return(nil, errFake).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetRepos(c, fakeUser)
|
|
|
|
g.Assert(p).Equal(fakeRepos)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get repos error", func() {
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetRepos", c, fakeUser).Return(nil, errFake).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetRepos(c, fakeUser)
|
|
|
|
g.Assert(p == nil).IsTrue()
|
2016-11-15 12:36:25 +03:00
|
|
|
g.Assert(err).Equal(errFake)
|
2016-04-19 07:22:23 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should set and get teams", func() {
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetTeams", c, fakeUser).Return(fakeTeams, nil).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetTeams(c, fakeUser)
|
|
|
|
g.Assert(p).Equal(fakeTeams)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get teams", func() {
|
|
|
|
key := fmt.Sprintf("teams:%s",
|
|
|
|
fakeUser.Login,
|
|
|
|
)
|
|
|
|
|
2021-10-07 00:25:55 +03:00
|
|
|
err := Set(c, key, fakeTeams)
|
|
|
|
g.Assert(err).IsNil()
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetTeams", c, fakeUser).Return(nil, errFake).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetTeams(c, fakeUser)
|
|
|
|
g.Assert(p).Equal(fakeTeams)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get team error", func() {
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetTeams", c, fakeUser).Return(nil, errFake).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetTeams(c, fakeUser)
|
|
|
|
g.Assert(p == nil).IsTrue()
|
2016-11-15 12:36:25 +03:00
|
|
|
g.Assert(err).Equal(errFake)
|
2016-04-19 07:22:23 +03:00
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should set and get members", func() {
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetMembers", c, fakeUser, "drone").Return(fakeMembers, nil).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetMembers(c, fakeUser, "drone")
|
|
|
|
g.Assert(p).Equal(fakeMembers)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get members", func() {
|
|
|
|
key := "members:drone"
|
|
|
|
|
2021-10-07 00:25:55 +03:00
|
|
|
err := Set(c, key, fakeMembers)
|
|
|
|
g.Assert(err).IsNil()
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetMembers", c, fakeUser, "drone").Return(nil, errFake).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetMembers(c, fakeUser, "drone")
|
|
|
|
g.Assert(p).Equal(fakeMembers)
|
|
|
|
g.Assert(err).Equal(nil)
|
|
|
|
})
|
|
|
|
|
|
|
|
g.It("Should get member error", func() {
|
2017-12-28 17:28:22 +03:00
|
|
|
r.On("GetMembers", c, fakeUser, "drone").Return(nil, errFake).Once()
|
2016-04-19 07:22:23 +03:00
|
|
|
p, err := GetMembers(c, fakeUser, "drone")
|
|
|
|
g.Assert(p == nil).IsTrue()
|
2016-11-15 12:36:25 +03:00
|
|
|
g.Assert(err).Equal(errFake)
|
2016-04-19 07:22:23 +03:00
|
|
|
})
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
var (
|
2016-11-15 11:51:11 +03:00
|
|
|
errFake = errors.New("Not Found")
|
2016-04-19 07:22:23 +03:00
|
|
|
fakeUser = &model.User{Login: "octocat"}
|
|
|
|
fakePerm = &model.Perm{Pull: true, Push: true, Admin: true}
|
|
|
|
fakeRepo = &model.Repo{Owner: "octocat", Name: "Hello-World"}
|
|
|
|
fakeRepos = []*model.Repo{
|
|
|
|
{Owner: "octocat", Name: "Hello-World"},
|
|
|
|
{Owner: "octocat", Name: "hello-world"},
|
|
|
|
{Owner: "octocat", Name: "Spoon-Knife"},
|
|
|
|
}
|
|
|
|
fakeTeams = []*model.Team{
|
|
|
|
{Login: "drone"},
|
|
|
|
{Login: "docker"},
|
|
|
|
}
|
|
|
|
fakeMembers = []*model.Member{
|
|
|
|
{Login: "octocat"},
|
|
|
|
}
|
|
|
|
)
|