@@ -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}
0 commit comments