@@ -84,7 +84,7 @@ func TestGroupSyncTable(t *testing.T) {
84
84
testCases := []orgSetupDefinition {
85
85
{
86
86
Name : "SwitchGroups" ,
87
- Settings : & idpsync.GroupSyncSettings {
87
+ GroupSettings : & idpsync.GroupSyncSettings {
88
88
Field : "groups" ,
89
89
Mapping : map [string ][]uuid.UUID {
90
90
"foo" : {ids .ID ("sg-foo" ), ids .ID ("sg-foo-2" )},
@@ -101,16 +101,18 @@ func TestGroupSyncTable(t *testing.T) {
101
101
ids .ID ("sg-bar" ): false ,
102
102
ids .ID ("sg-baz" ): false ,
103
103
},
104
- ExpectedGroups : []uuid.UUID {
105
- ids .ID ("sg-foo" ),
106
- ids .ID ("sg-foo-2" ),
107
- ids .ID ("sg-bar" ),
108
- ids .ID ("sg-baz" ),
104
+ assertGroups : & orgGroupAssert {
105
+ ExpectedGroups : []uuid.UUID {
106
+ ids .ID ("sg-foo" ),
107
+ ids .ID ("sg-foo-2" ),
108
+ ids .ID ("sg-bar" ),
109
+ ids .ID ("sg-baz" ),
110
+ },
109
111
},
110
112
},
111
113
{
112
114
Name : "StayInGroup" ,
113
- Settings : & idpsync.GroupSyncSettings {
115
+ GroupSettings : & idpsync.GroupSyncSettings {
114
116
Field : "groups" ,
115
117
// Only match foo, so bar does not map
116
118
RegexFilter : regexp .MustCompile ("^foo$" ),
@@ -124,13 +126,15 @@ func TestGroupSyncTable(t *testing.T) {
124
126
ids .ID ("gg-foo" ): true ,
125
127
ids .ID ("gg-bar" ): false ,
126
128
},
127
- ExpectedGroups : []uuid.UUID {
128
- ids .ID ("gg-foo" ),
129
+ assertGroups : & orgGroupAssert {
130
+ ExpectedGroups : []uuid.UUID {
131
+ ids .ID ("gg-foo" ),
132
+ },
129
133
},
130
134
},
131
135
{
132
136
Name : "UserJoinsGroups" ,
133
- Settings : & idpsync.GroupSyncSettings {
137
+ GroupSettings : & idpsync.GroupSyncSettings {
134
138
Field : "groups" ,
135
139
Mapping : map [string ][]uuid.UUID {
136
140
"foo" : {ids .ID ("ng-foo" ), uuid .New ()},
@@ -144,29 +148,33 @@ func TestGroupSyncTable(t *testing.T) {
144
148
ids .ID ("ng-bar-2" ): false ,
145
149
ids .ID ("ng-baz" ): false ,
146
150
},
147
- ExpectedGroups : []uuid.UUID {
148
- ids .ID ("ng-foo" ),
149
- ids .ID ("ng-bar" ),
150
- ids .ID ("ng-bar-2" ),
151
- ids .ID ("ng-baz" ),
151
+ assertGroups : & orgGroupAssert {
152
+ ExpectedGroups : []uuid.UUID {
153
+ ids .ID ("ng-foo" ),
154
+ ids .ID ("ng-bar" ),
155
+ ids .ID ("ng-bar-2" ),
156
+ ids .ID ("ng-baz" ),
157
+ },
152
158
},
153
159
},
154
160
{
155
161
Name : "CreateGroups" ,
156
- Settings : & idpsync.GroupSyncSettings {
162
+ GroupSettings : & idpsync.GroupSyncSettings {
157
163
Field : "groups" ,
158
164
RegexFilter : regexp .MustCompile ("^create" ),
159
165
AutoCreateMissing : true ,
160
166
},
161
167
Groups : map [uuid.UUID ]bool {},
162
- ExpectedGroupNames : []string {
163
- "create-bar" ,
164
- "create-baz" ,
168
+ assertGroups : & orgGroupAssert {
169
+ ExpectedGroupNames : []string {
170
+ "create-bar" ,
171
+ "create-baz" ,
172
+ },
165
173
},
166
174
},
167
175
{
168
176
Name : "GroupNamesNoMapping" ,
169
- Settings : & idpsync.GroupSyncSettings {
177
+ GroupSettings : & idpsync.GroupSyncSettings {
170
178
Field : "groups" ,
171
179
RegexFilter : regexp .MustCompile (".*" ),
172
180
AutoCreateMissing : false ,
@@ -176,14 +184,16 @@ func TestGroupSyncTable(t *testing.T) {
176
184
"bar" : false ,
177
185
"goob" : true ,
178
186
},
179
- ExpectedGroupNames : []string {
180
- "foo" ,
181
- "bar" ,
187
+ assertGroups : & orgGroupAssert {
188
+ ExpectedGroupNames : []string {
189
+ "foo" ,
190
+ "bar" ,
191
+ },
182
192
},
183
193
},
184
194
{
185
195
Name : "NoUser" ,
186
- Settings : & idpsync.GroupSyncSettings {
196
+ GroupSettings : & idpsync.GroupSyncSettings {
187
197
Field : "groups" ,
188
198
Mapping : map [string ][]uuid.UUID {
189
199
// Extra ID that does not map to a group
@@ -199,13 +209,13 @@ func TestGroupSyncTable(t *testing.T) {
199
209
},
200
210
},
201
211
{
202
- Name : "NoSettingsNoUser" ,
203
- Settings : nil ,
204
- Groups : map [uuid.UUID ]bool {},
212
+ Name : "NoSettingsNoUser" ,
213
+ GroupSettings : nil ,
214
+ Groups : map [uuid.UUID ]bool {},
205
215
},
206
216
{
207
217
Name : "LegacyMapping" ,
208
- Settings : & idpsync.GroupSyncSettings {
218
+ GroupSettings : & idpsync.GroupSyncSettings {
209
219
Field : "groups" ,
210
220
RegexFilter : regexp .MustCompile ("^legacy" ),
211
221
LegacyNameMapping : map [string ]string {
@@ -223,9 +233,11 @@ func TestGroupSyncTable(t *testing.T) {
223
233
"extra" : true ,
224
234
"legacy-bop" : true ,
225
235
},
226
- ExpectedGroupNames : []string {
227
- "legacy-bar" ,
228
- "legacy-foo" ,
236
+ assertGroups : & orgGroupAssert {
237
+ ExpectedGroupNames : []string {
238
+ "legacy-bar" ,
239
+ "legacy-foo" ,
240
+ },
229
241
},
230
242
},
231
243
}
@@ -299,9 +311,10 @@ func TestGroupSyncTable(t *testing.T) {
299
311
"random" : true ,
300
312
},
301
313
// No settings, because they come from the deployment values
302
- Settings : nil ,
303
- ExpectedGroups : nil ,
304
- ExpectedGroupNames : []string {"legacy-foo" , "legacy-baz" , "legacy-bar" },
314
+ GroupSettings : nil ,
315
+ assertGroups : & orgGroupAssert {
316
+ ExpectedGroupNames : []string {"legacy-foo" , "legacy-baz" , "legacy-bar" },
317
+ },
305
318
}
306
319
307
320
//nolint:gocritic // testing
@@ -373,16 +386,18 @@ func TestSyncDisabled(t *testing.T) {
373
386
ids .ID ("baz" ): false ,
374
387
ids .ID ("bop" ): false ,
375
388
},
376
- Settings : & idpsync.GroupSyncSettings {
389
+ GroupSettings : & idpsync.GroupSyncSettings {
377
390
Field : "groups" ,
378
391
Mapping : map [string ][]uuid.UUID {
379
392
"foo" : {ids .ID ("foo" )},
380
393
"baz" : {ids .ID ("baz" )},
381
394
},
382
395
},
383
- ExpectedGroups : []uuid.UUID {
384
- ids .ID ("foo" ),
385
- ids .ID ("bar" ),
396
+ assertGroups : & orgGroupAssert {
397
+ ExpectedGroups : []uuid.UUID {
398
+ ids .ID ("foo" ),
399
+ ids .ID ("bar" ),
400
+ },
386
401
},
387
402
}
388
403
@@ -717,7 +732,10 @@ func SetupOrganization(t *testing.T, s *idpsync.AGPLIDPSync, db database.Store,
717
732
718
733
manager := runtimeconfig .NewManager ()
719
734
orgResolver := manager .OrganizationResolver (db , org .ID )
720
- err = s .Group .SetRuntimeValue (context .Background (), orgResolver , def .Settings )
735
+ err = s .Group .SetRuntimeValue (context .Background (), orgResolver , def .GroupSettings )
736
+ require .NoError (t , err )
737
+
738
+ err = s .Role .SetRuntimeValue (context .Background (), orgResolver , def .RoleSettings )
721
739
require .NoError (t , err )
722
740
723
741
if ! def .NotMember {
@@ -726,6 +744,14 @@ func SetupOrganization(t *testing.T, s *idpsync.AGPLIDPSync, db database.Store,
726
744
OrganizationID : org .ID ,
727
745
})
728
746
}
747
+ if len (def .OrganizationRoles ) > 0 {
748
+ _ , err := db .UpdateMemberRoles (context .Background (), database.UpdateMemberRolesParams {
749
+ GrantedRoles : def .OrganizationRoles ,
750
+ UserID : user .ID ,
751
+ OrgID : org .ID ,
752
+ })
753
+ require .NoError (t , err )
754
+ }
729
755
for groupID , in := range def .Groups {
730
756
dbgen .Group (t , db , database.Group {
731
757
ID : groupID ,
@@ -757,9 +783,23 @@ type orgSetupDefinition struct {
757
783
// True if the user is a member of the group
758
784
Groups map [uuid.UUID ]bool
759
785
GroupNames map [string ]bool
760
- NotMember bool
761
786
762
- Settings * idpsync.GroupSyncSettings
787
+ OrganizationRoles []string
788
+ // NotMember if true will ensure the user is not a member of the organization.
789
+ NotMember bool
790
+
791
+ GroupSettings * idpsync.GroupSyncSettings
792
+ RoleSettings * idpsync.RoleSyncSettings
793
+
794
+ assertGroups * orgGroupAssert
795
+ assertRoles * orgRoleAssert
796
+ }
797
+
798
+ type orgRoleAssert struct {
799
+ ExpectedOrgRoles []string
800
+ }
801
+
802
+ type orgGroupAssert struct {
763
803
ExpectedGroups []uuid.UUID
764
804
ExpectedGroupNames []string
765
805
}
@@ -780,6 +820,24 @@ func (o orgSetupDefinition) Assert(t *testing.T, orgID uuid.UUID, db database.St
780
820
require .Len (t , members , 1 , "should be a member" )
781
821
}
782
822
823
+ if o .assertGroups != nil {
824
+ o .assertGroups .Assert (t , orgID , db , user )
825
+ }
826
+ if o .assertRoles != nil {
827
+ o .assertRoles .Assert (t , orgID , db , o .NotMember , user )
828
+ }
829
+
830
+ if o .assertGroups == nil && o .assertRoles == nil {
831
+ t .Errorf ("no group or role asserts present, must have at least one" )
832
+ t .FailNow ()
833
+ }
834
+ }
835
+
836
+ func (o orgGroupAssert ) Assert (t * testing.T , orgID uuid.UUID , db database.Store , user database.User ) {
837
+ t .Helper ()
838
+
839
+ ctx := context .Background ()
840
+
783
841
userGroups , err := db .GetGroups (ctx , database.GetGroupsParams {
784
842
OrganizationID : orgID ,
785
843
HasMemberID : user .ID ,
@@ -812,3 +870,22 @@ func (o orgSetupDefinition) Assert(t *testing.T, orgID uuid.UUID, db database.St
812
870
require .Len (t , o .ExpectedGroupNames , 0 , "ExpectedGroupNames should be empty" )
813
871
}
814
872
}
873
+
874
+ func (o orgRoleAssert ) Assert (t * testing.T , orgID uuid.UUID , db database.Store , notMember bool , user database.User ) {
875
+ t .Helper ()
876
+
877
+ ctx := context .Background ()
878
+
879
+ members , err := db .OrganizationMembers (ctx , database.OrganizationMembersParams {
880
+ OrganizationID : orgID ,
881
+ UserID : user .ID ,
882
+ })
883
+ if notMember {
884
+ require .ErrorIs (t , err , sql .ErrNoRows )
885
+ return
886
+ }
887
+ require .NoError (t , err )
888
+ require .Len (t , members , 1 )
889
+ member := members [0 ]
890
+ require .ElementsMatch (t , member .OrganizationMember .Roles , o .ExpectedOrgRoles )
891
+ }
0 commit comments