Skip to content

Commit a76b437

Browse files
committed
Add pagination support for teams and users in org role tests. Enhanced mock GitHub server to handle paginated responses for teams and users, and updated tests to verify correct handling of grants and pagination scenarios.
1 parent 03d1e5d commit a76b437

File tree

2 files changed

+123
-6
lines changed

2 files changed

+123
-6
lines changed

pkg/connector/org_role_test.go

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ func TestOrgRole(t *testing.T) {
2222

2323
githubOrganization, _, _, githubUser, _ := mgh.Seed()
2424

25+
// Add user to org role
26+
roleId := int64(1)
27+
mgh.AddUserToOrgRole(roleId, *githubUser.ID)
28+
2529
githubClient := github.NewClient(mgh.Server())
2630
cache := newOrgNameCache(githubClient)
2731
client := orgRoleBuilder(githubClient, cache)
@@ -39,16 +43,19 @@ func TestOrgRole(t *testing.T) {
3943
Resource: role,
4044
}
4145

46+
// Grant the role to the user
4247
grantAnnotations, err := client.Grant(ctx, user, &entitlement)
4348
require.Nil(t, err)
4449
require.Empty(t, grantAnnotations)
4550

51+
// Check that we can see both teams and users in the grants list
4652
grants, nextToken, grantsAnnotations, err := client.Grants(ctx, role, &pagination.Token{})
4753
require.Nil(t, err)
4854
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
49-
require.Equal(t, "", nextToken)
50-
require.Len(t, grants, 1)
55+
require.Empty(t, nextToken) // No next token since we don't have a full page
56+
require.Len(t, grants, 2) // Should get both the team and user
5157

58+
// Revoke the role from the user
5259
grant := v2.Grant{
5360
Entitlement: &entitlement,
5461
Principal: user,
@@ -91,4 +98,41 @@ func TestOrgRole(t *testing.T) {
9198
require.Empty(t, nextToken)
9299
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
93100
})
101+
102+
t.Run("should handle pagination for teams and users", func(t *testing.T) {
103+
mockGithub := mocks.NewMockGitHub()
104+
githubOrganization, _, _, githubUser, _ := mockGithub.Seed()
105+
106+
// Add more teams to trigger pagination
107+
for i := 0; i < 3; i++ {
108+
teamId := int64(100 + i)
109+
team := github.Team{
110+
ID: &teamId,
111+
Organization: githubOrganization,
112+
}
113+
mockGithub.AddTeam(team)
114+
}
115+
116+
// Add user to org role
117+
roleId := int64(1)
118+
mockGithub.AddUserToOrgRole(roleId, *githubUser.ID)
119+
120+
githubClient := github.NewClient(mockGithub.Server())
121+
cache := newOrgNameCache(githubClient)
122+
client := orgRoleBuilder(githubClient, cache)
123+
124+
organization, _ := organizationResource(ctx, githubOrganization, nil)
125+
role, _ := orgRoleResource(ctx, &OrganizationRole{
126+
ID: 1,
127+
Name: "Test Role",
128+
Description: "Test Role Description",
129+
}, organization)
130+
131+
// Test first page (should get all teams and users)
132+
grants, nextToken, annotations, err := client.Grants(ctx, role, &pagination.Token{Size: 5})
133+
require.Nil(t, err)
134+
require.Empty(t, nextToken) // No next token since we got all results
135+
require.Len(t, grants, 5) // Should get all 4 teams (3 added + 1 from Seed) and 1 user
136+
test.AssertNoRatelimitAnnotations(t, annotations)
137+
})
94138
}

test/mocks/github.go

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -545,8 +545,36 @@ func (mgh MockGitHub) getOrgRoleTeams(
545545
return
546546
}
547547

548-
// Return empty teams list for now
549-
_, _ = w.Write(mock.MustMarshal([]*github.Team{}))
548+
// Get pagination parameters
549+
page := 1
550+
perPage := 30
551+
if pageStr, ok := variables["page"]; ok {
552+
if p, err := strconv.Atoi(pageStr); err == nil {
553+
page = p
554+
}
555+
}
556+
if perPageStr, ok := variables["per_page"]; ok {
557+
if pp, err := strconv.Atoi(perPageStr); err == nil {
558+
perPage = pp
559+
}
560+
}
561+
562+
// Return paginated teams
563+
teams := make([]*github.Team, 0)
564+
start := (page - 1) * perPage
565+
end := start + perPage
566+
i := 0
567+
for _, team := range mgh.teams {
568+
if i >= start && i < end {
569+
teams = append(teams, &team)
570+
}
571+
i++
572+
if i >= end {
573+
break
574+
}
575+
}
576+
577+
_, _ = w.Write(mock.MustMarshal(teams))
550578
}
551579

552580
func (mgh MockGitHub) getOrgRoleUsers(
@@ -560,10 +588,34 @@ func (mgh MockGitHub) getOrgRoleUsers(
560588
return
561589
}
562590

591+
// Get pagination parameters
592+
page := 1
593+
perPage := 30
594+
if pageStr, ok := variables["page"]; ok {
595+
if p, err := strconv.Atoi(pageStr); err == nil {
596+
page = p
597+
}
598+
}
599+
if perPageStr, ok := variables["per_page"]; ok {
600+
if pp, err := strconv.Atoi(perPageStr); err == nil {
601+
perPage = pp
602+
}
603+
}
604+
605+
// Return paginated users
563606
users := make([]github.User, 0)
607+
start := (page - 1) * perPage
608+
end := start + perPage
609+
i := 0
564610
for _, userID := range memberships.ToSlice() {
565-
if user, ok := mgh.users[userID]; ok {
566-
users = append(users, user)
611+
if i >= start && i < end {
612+
if user, ok := mgh.users[userID]; ok {
613+
users = append(users, user)
614+
}
615+
}
616+
i++
617+
if i >= end {
618+
break
567619
}
568620
}
569621

@@ -655,3 +707,24 @@ func (mgh MockGitHub) Server() *http.Client {
655707
}
656708
return mock.NewMockedHTTPClient(options...)
657709
}
710+
711+
// AddTeam adds a team to the mock server for testing purposes.
712+
func (mgh *MockGitHub) AddTeam(team github.Team) {
713+
mgh.teams[*team.ID] = team
714+
}
715+
716+
// AddUserToOrgRole adds a user to an org role for testing purposes.
717+
func (mgh *MockGitHub) AddUserToOrgRole(roleID int64, userID int64) {
718+
if _, ok := mgh.orgRoles[roleID]; !ok {
719+
mgh.orgRoles[roleID] = mapset.NewSet[int64]()
720+
}
721+
mgh.orgRoles[roleID].Add(userID)
722+
}
723+
724+
// AddTeamToOrgRole adds a team to an org role for testing purposes.
725+
func (mgh *MockGitHub) AddTeamToOrgRole(roleID int64, teamID int64) {
726+
if _, ok := mgh.orgRoles[roleID]; !ok {
727+
mgh.orgRoles[roleID] = mapset.NewSet[int64]()
728+
}
729+
mgh.orgRoles[roleID].Add(teamID)
730+
}

0 commit comments

Comments
 (0)