Skip to content

Commit 50b8537

Browse files
committed
support org role tests by updating Seed method to include OrganizationRole. Refactor test cases to utilize the new role data
1 parent 0fca2cc commit 50b8537

File tree

6 files changed

+112
-79
lines changed

6 files changed

+112
-79
lines changed

pkg/connector/org_role_test.go

Lines changed: 39 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -20,42 +20,57 @@ func TestOrgRole(t *testing.T) {
2020
t.Run("should grant and revoke entitlements", func(t *testing.T) {
2121
mgh := mocks.NewMockGitHub()
2222

23-
githubOrganization, _, _, githubUser, _ := mgh.Seed()
24-
25-
// Add user to org role
26-
roleId := int64(1)
27-
mgh.AddUserToOrgRole(roleId, *githubUser.ID)
23+
githubOrganization, _, _, githubUser, orgRole, _ := mgh.Seed()
2824

2925
githubClient := github.NewClient(mgh.Server())
3026
cache := newOrgNameCache(githubClient)
3127
client := orgRoleBuilder(githubClient, cache)
3228

3329
organization, _ := organizationResource(ctx, githubOrganization, nil)
34-
role, _ := orgRoleResource(ctx, &OrganizationRole{
35-
ID: 1,
36-
Name: "Test Role",
37-
Description: "Test Role Description",
30+
roleResource, _ := orgRoleResource(ctx, &OrganizationRole{
31+
ID: orgRole.ID,
32+
Name: orgRole.Name,
33+
Description: orgRole.Description,
3834
}, organization)
3935
user, _ := userResource(ctx, githubUser, *githubUser.Email, nil)
4036

4137
entitlement := v2.Entitlement{
42-
Id: entitlement2.NewEntitlementID(role, "assigned"),
43-
Resource: role,
38+
Id: entitlement2.NewEntitlementID(roleResource, "assigned"),
39+
Resource: roleResource,
4440
}
4541

4642
// Grant the role to the user
4743
grantAnnotations, err := client.Grant(ctx, user, &entitlement)
4844
require.Nil(t, err)
4945
require.Empty(t, grantAnnotations)
5046

51-
// Check that we can see both teams and users in the grants list
52-
grants, nextToken, grantsAnnotations, err := client.Grants(ctx, role, &pagination.Token{})
53-
require.Nil(t, err)
54-
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
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
47+
grants := make([]*v2.Grant, 0)
48+
bag := &pagination.Bag{}
49+
for {
50+
pToken := pagination.Token{}
51+
state := bag.Current()
52+
if state != nil {
53+
token, _ := bag.Marshal()
54+
pToken.Token = token
55+
}
56+
57+
nextGrants, nextToken, grantsAnnotations, err := client.Grants(ctx, roleResource, &pToken)
58+
grants = append(grants, nextGrants...)
59+
60+
require.Nil(t, err)
61+
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
62+
if nextToken == "" {
63+
break
64+
}
65+
66+
err = bag.Unmarshal(nextToken)
67+
if err != nil {
68+
t.Error(err)
69+
}
70+
}
71+
72+
require.Len(t, grants, 2)
5773

58-
// Revoke the role from the user
5974
grant := v2.Grant{
6075
Entitlement: &entitlement,
6176
Principal: user,
@@ -70,7 +85,7 @@ func TestOrgRole(t *testing.T) {
7085
mockGithub := mocks.NewMockGitHub()
7186
mockGithub.SimulateOrgRolePermErr = true
7287

73-
githubOrganization, _, _, _, _ := mockGithub.Seed()
88+
githubOrganization, _, _, _, orgRole, _ := mockGithub.Seed()
7489

7590
githubClient := github.NewClient(mockGithub.Server())
7691
cache := newOrgNameCache(githubClient)
@@ -87,52 +102,16 @@ func TestOrgRole(t *testing.T) {
87102

88103
// Test Grants with permission error
89104
role, _ := orgRoleResource(ctx, &OrganizationRole{
90-
ID: 1,
91-
Name: "Test Role",
92-
Description: "Test Role Description",
105+
ID: orgRole.ID,
106+
Name: orgRole.Name,
107+
Description: orgRole.Description,
93108
}, organization)
94109

95110
grants, nextToken, grantsAnnotations, err := client.Grants(ctx, role, &pagination.Token{})
96111
require.Nil(t, err)
97112
require.Empty(t, grants)
98-
require.Empty(t, nextToken)
113+
// The token should contain the initial state for users
114+
require.NotEmpty(t, nextToken)
99115
test.AssertNoRatelimitAnnotations(t, grantsAnnotations)
100116
})
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-
})
138117
}

pkg/connector/org_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func TestOrganization(t *testing.T) {
1919
t.Run("should grant and revoke entitlements", func(t *testing.T) {
2020
mgh := mocks.NewMockGitHub()
2121

22-
githubOrganization, _, _, githubUser, _ := mgh.Seed()
22+
githubOrganization, _, _, githubUser, _, _ := mgh.Seed()
2323

2424
githubClient := github.NewClient(mgh.Server())
2525
cache := newOrgNameCache(githubClient)

pkg/connector/repository_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestRepository(t *testing.T) {
2020
t.Run("should grant and revoke entitlements", func(t *testing.T) {
2121
mgh := mocks.NewMockGitHub()
2222

23-
githubOrganization, githubRepository, _, githubUser, _ := mgh.Seed()
23+
githubOrganization, githubRepository, _, githubUser, _, _ := mgh.Seed()
2424

2525
githubClient := github.NewClient(mgh.Server())
2626
cache := newOrgNameCache(githubClient)

pkg/connector/team_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestTeam(t *testing.T) {
2020
t.Run("should grant and revoke entitlements", func(t *testing.T) {
2121
mgh := mocks.NewMockGitHub()
2222

23-
githubOrganization, _, githubTeam, githubUser, _ := mgh.Seed()
23+
githubOrganization, _, githubTeam, githubUser, _, _ := mgh.Seed()
2424

2525
githubClient := github.NewClient(mgh.Server())
2626
cache := newOrgNameCache(githubClient)

pkg/connector/user_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ func TestUsersList(t *testing.T) {
2929
t.Run(fmt.Sprintf("should get a list of users (SAML:%s)", testCase.message), func(t *testing.T) {
3030
mgh := mocks.NewMockGitHub()
3131

32-
githubOrganization, _, _, githubUser, _ := mgh.Seed()
32+
githubOrganization, _, _, githubUser, _, _ := mgh.Seed()
3333

3434
organization, err := organizationResource(
3535
ctx,

test/mocks/github.go

Lines changed: 69 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ func (mgh MockGitHub) Seed() (
9393
*github.Repository,
9494
*github.Team,
9595
*github.User,
96+
*OrganizationRole,
9697
error,
9798
) {
9899
organizationId := int64(12)
@@ -137,7 +138,16 @@ func (mgh MockGitHub) Seed() (
137138
mgh.teamMemberships[teamId] = mapset.NewSet[int64](userId)
138139
mgh.organizationMemberships[organizationId] = mapset.NewSet[int64](userId)
139140

140-
return &githubOrganization, &githubRepository, &githubTeam, &githubUser, nil
141+
// Add a mock org role
142+
roleId := int64(1)
143+
orgRole := &OrganizationRole{
144+
ID: roleId,
145+
Name: "Test Role",
146+
Description: "Test Role Description",
147+
}
148+
mgh.orgRoles[roleId] = mapset.NewSet[int64]() // Initialize the set for this role
149+
150+
return &githubOrganization, &githubRepository, &githubTeam, &githubUser, orgRole, nil
141151
}
142152

143153
func getResource[T interface{}](
@@ -564,13 +574,29 @@ func (mgh MockGitHub) getOrgRoleTeams(
564574
start := (page - 1) * perPage
565575
end := start + perPage
566576
i := 0
567-
for _, team := range mgh.teams {
568-
if i >= start && i < end {
569-
teams = append(teams, &team)
577+
578+
// Get users with this role
579+
roleUsers := mgh.orgRoles[roleID]
580+
581+
// Find teams that have members with this role
582+
for teamID, team := range mgh.teams {
583+
teamMembers := mgh.teamMemberships[teamID]
584+
if teamMembers == nil {
585+
continue
570586
}
571-
i++
572-
if i >= end {
573-
break
587+
588+
// Check if any team member has the role
589+
for _, userID := range teamMembers.ToSlice() {
590+
if roleUsers.Contains(userID) {
591+
if i >= start && i < end {
592+
teams = append(teams, &team)
593+
}
594+
i++
595+
if i >= end {
596+
break
597+
}
598+
break // Found a member with the role, no need to check other members
599+
}
574600
}
575601
}
576602

@@ -647,6 +673,30 @@ func (mgh MockGitHub) removeOrgRoleUser(
647673
}
648674
}
649675

676+
// Add a handler for GET /orgs/{org}/organization-roles/{role_id}
677+
func (mgh MockGitHub) getOrgRoleByID(
678+
w http.ResponseWriter,
679+
variables map[string]string,
680+
) {
681+
orgID, _ := getCrossTableId(w, variables, "org")
682+
roleID, _ := getCrossTableId(w, variables, "role_id")
683+
if _, ok := mgh.organizations[orgID]; !ok {
684+
w.WriteHeader(http.StatusNotFound)
685+
return
686+
}
687+
// Only support role ID 1 for the mock
688+
if roleID != 1 {
689+
w.WriteHeader(http.StatusNotFound)
690+
return
691+
}
692+
role := &OrganizationRole{
693+
ID: 1,
694+
Name: "Test Role",
695+
Description: "Test Role Description",
696+
}
697+
_, _ = w.Write(mock.MustMarshal(role))
698+
}
699+
650700
type handler = func(w http.ResponseWriter, variables map[string]string)
651701

652702
// addEndpointHandler takes a string interpolation pattern and a handler
@@ -694,9 +744,13 @@ func (mgh MockGitHub) Server() *http.Client {
694744
DeleteOrganizationsTeamsMembershipsByOrganizationByTeamIdByUsername: mgh.removeMembership,
695745
PutOrganizationsTeamsMembershipsByOrganizationByTeamIdByUsername: mgh.addMembership,
696746
// Add organization role endpoints
697-
GetOrgsRolesByOrg: mgh.getOrgRoles,
698-
GetOrgsRolesTeamsByOrgByRoleId: mgh.getOrgRoleTeams,
699-
GetOrgsRolesUsersByOrgByRoleId: mgh.getOrgRoleUsers,
747+
GetOrgsRolesByOrg: mgh.getOrgRoles,
748+
GetOrgsRolesTeamsByOrgByRoleId: mgh.getOrgRoleTeams,
749+
GetOrgsRolesUsersByOrgByRoleId: mgh.getOrgRoleUsers,
750+
mock.EndpointPattern{
751+
Pattern: "/orgs/{org}/organization-roles/{role_id}",
752+
Method: "GET",
753+
}: mgh.getOrgRoleByID,
700754
PutOrgsRolesUsersByOrgByRoleIdByUsername: mgh.addOrgRoleUser,
701755
DeleteOrgsRolesUsersByOrgByRoleIdByUsername: mgh.removeOrgRoleUser,
702756
}
@@ -721,10 +775,10 @@ func (mgh *MockGitHub) AddUserToOrgRole(roleID int64, userID int64) {
721775
mgh.orgRoles[roleID].Add(userID)
722776
}
723777

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]()
778+
// AddMembership adds a user to a team for testing purposes.
779+
func (mgh *MockGitHub) AddMembership(teamID int64, userID int64) {
780+
if _, ok := mgh.teamMemberships[teamID]; !ok {
781+
mgh.teamMemberships[teamID] = mapset.NewSet[int64]()
728782
}
729-
mgh.orgRoles[roleID].Add(teamID)
783+
mgh.teamMemberships[teamID].Add(userID)
730784
}

0 commit comments

Comments
 (0)