func TestTeams(t *testing.T) { gin.SetMode(gin.TestMode) logrus.SetOutput(ioutil.Discard) g := goblin.Goblin(t) g.Describe("Team endpoint", func() { g.It("Should return the team list", func() { cache := new(cache.Cache) cache.On("Get", "teams:octocat").Return(fakeTeams, nil).Once() e := gin.New() e.NoRoute(GetTeams) e.Use(func(c *gin.Context) { c.Set("user", fakeUser) c.Set("cache", cache) }) w := httptest.NewRecorder() r, _ := http.NewRequest("GET", "/", nil) e.ServeHTTP(w, r) // the user is appended to the team list so we retrieve a full list of // accounts to which the user has access. teams := append(fakeTeams, &model.Team{ Login: fakeUser.Login, }) want, _ := json.Marshal(teams) got := strings.TrimSpace(w.Body.String()) g.Assert(got).Equal(string(want)) g.Assert(w.Code).Equal(200) }) g.It("Should return a 500 error", func() { remote := new(remote.Remote) cache := new(cache.Cache) cache.On("Get", "teams:octocat").Return(nil, fmt.Errorf("Not Found")).Once() remote.On("GetTeams", fakeUser).Return(nil, fmt.Errorf("Not Found")).Once() e := gin.New() e.NoRoute(GetTeams) e.Use(func(c *gin.Context) { c.Set("user", fakeUser) c.Set("cache", cache) c.Set("remote", remote) }) w := httptest.NewRecorder() r, _ := http.NewRequest("GET", "/", nil) e.ServeHTTP(w, r) got := strings.TrimSpace(w.Body.String()) g.Assert(got).Equal("Error getting team list") g.Assert(w.Code).Equal(500) }) }) }
func TestHelper(t *testing.T) { g := goblin.Goblin(t) g.Describe("Cache helpers", func() { var c *gin.Context var r *mock.Remote g.BeforeEach(func() { c = new(gin.Context) ToContext(c, Default()) r = new(mock.Remote) remote.ToContext(c, r) }) g.It("Should get permissions from remote", func() { r.On("GetPerm", fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(fakePerm, nil).Once() 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, ) Set(c, key, fakePerm) r.On("GetPerm", fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(nil, fakeErr).Once() 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() { r.On("GetPerm", fakeUser, fakeRepo.Owner, fakeRepo.Name).Return(nil, fakeErr).Once() p, err := GetPerm(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("GetRepos", fakeUser).Return(fakeRepos, nil).Once() 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, ) Set(c, key, fakeRepos) r.On("GetRepos", fakeUser).Return(nil, fakeErr).Once() p, err := GetRepos(c, fakeUser) g.Assert(p).Equal(fakeRepos) g.Assert(err).Equal(nil) }) g.It("Should get repos error", func() { r.On("GetRepos", fakeUser).Return(nil, fakeErr).Once() p, err := GetRepos(c, fakeUser) g.Assert(p == nil).IsTrue() g.Assert(err).Equal(fakeErr) }) g.It("Should set and get teams", func() { r.On("GetTeams", fakeUser).Return(fakeTeams, nil).Once() 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, ) Set(c, key, fakeTeams) r.On("GetTeams", fakeUser).Return(nil, fakeErr).Once() p, err := GetTeams(c, fakeUser) g.Assert(p).Equal(fakeTeams) g.Assert(err).Equal(nil) }) g.It("Should get team error", func() { r.On("GetTeams", fakeUser).Return(nil, fakeErr).Once() p, err := GetTeams(c, fakeUser) g.Assert(p == nil).IsTrue() g.Assert(err).Equal(fakeErr) }) g.It("Should set and get members", func() { r.On("GetMembers", fakeUser, "drone").Return(fakeMembers, nil).Once() 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" Set(c, key, fakeMembers) r.On("GetMembers", fakeUser, "drone").Return(nil, fakeErr).Once() p, err := GetMembers(c, fakeUser, "drone") g.Assert(p).Equal(fakeMembers) g.Assert(err).Equal(nil) }) g.It("Should get member error", func() { r.On("GetMembers", fakeUser, "drone").Return(nil, fakeErr).Once() p, err := GetMembers(c, fakeUser, "drone") g.Assert(p == nil).IsTrue() g.Assert(err).Equal(fakeErr) }) }) }