8
8
"github.com/golang-jwt/jwt/v4"
9
9
"github.com/google/uuid"
10
10
"github.com/stretchr/testify/require"
11
+ "golang.org/x/exp/slices"
11
12
12
13
"cdr.dev/slog/sloggers/slogtest"
13
14
"github.com/coder/coder/v2/coderd/coderdtest"
@@ -152,9 +153,26 @@ func TestGroupSyncTable(t *testing.T) {
152
153
AutoCreateMissingGroups : true ,
153
154
},
154
155
Groups : map [uuid.UUID ]bool {},
155
- ExpectedGroups : []uuid.UUID {
156
- ids .ID ("create-bar" ),
157
- ids .ID ("create-baz" ),
156
+ ExpectedGroupNames : []string {
157
+ "create-bar" ,
158
+ "create-baz" ,
159
+ },
160
+ },
161
+ {
162
+ Name : "GroupNamesNoMapping" ,
163
+ Settings : & idpsync.GroupSyncSettings {
164
+ GroupField : "groups" ,
165
+ RegexFilter : regexp .MustCompile (".*" ),
166
+ AutoCreateMissingGroups : false ,
167
+ },
168
+ GroupNames : map [string ]bool {
169
+ "foo" : false ,
170
+ "bar" : false ,
171
+ "goob" : true ,
172
+ },
173
+ ExpectedGroupNames : []string {
174
+ "foo" ,
175
+ "bar" ,
158
176
},
159
177
},
160
178
{
@@ -219,10 +237,12 @@ func SetupOrganization(t *testing.T, s *idpsync.AGPLIDPSync, db database.Store,
219
237
org := dbgen .Organization (t , db , database.Organization {
220
238
ID : def .OrgID ,
221
239
})
240
+ _ , err := db .InsertAllUsersGroup (context .Background (), org .ID )
241
+ require .NoError (t , err , "Everyone group for an org" )
222
242
223
243
manager := runtimeconfig .NewStoreManager (db )
224
244
orgResolver := manager .Scoped (org .ID .String ())
225
- err : = s .Group .SetRuntimeValue (context .Background (), orgResolver , def .Settings )
245
+ err = s .Group .SetRuntimeValue (context .Background (), orgResolver , def .Settings )
226
246
require .NoError (t , err )
227
247
228
248
if ! def .NotMember {
@@ -243,47 +263,76 @@ func SetupOrganization(t *testing.T, s *idpsync.AGPLIDPSync, db database.Store,
243
263
})
244
264
}
245
265
}
266
+ for groupName , in := range def .GroupNames {
267
+ group := dbgen .Group (t , db , database.Group {
268
+ Name : groupName ,
269
+ OrganizationID : org .ID ,
270
+ })
271
+ if in {
272
+ dbgen .GroupMember (t , db , database.GroupMemberTable {
273
+ UserID : user .ID ,
274
+ GroupID : group .ID ,
275
+ })
276
+ }
277
+ }
246
278
}
247
279
248
280
type orgSetupDefinition struct {
249
281
Name string
250
282
OrgID uuid.UUID
251
283
// True if the user is a member of the group
252
- Groups map [uuid.UUID ]bool
253
- NotMember bool
284
+ Groups map [uuid.UUID ]bool
285
+ GroupNames map [string ]bool
286
+ NotMember bool
254
287
255
- Settings * idpsync.GroupSyncSettings
256
- ExpectedGroups []uuid.UUID
288
+ Settings * idpsync.GroupSyncSettings
289
+ ExpectedGroups []uuid.UUID
290
+ ExpectedGroupNames []string
257
291
}
258
292
259
293
func (o orgSetupDefinition ) Assert (t * testing.T , orgID uuid.UUID , db database.Store , user database.User ) {
260
294
t .Helper ()
261
295
262
- t .Run (o .Name + "-Assert" , func (t * testing.T ) {
263
- ctx := context .Background ()
296
+ ctx := context .Background ()
264
297
265
- members , err := db .OrganizationMembers (ctx , database.OrganizationMembersParams {
266
- OrganizationID : orgID ,
267
- UserID : user .ID ,
268
- })
269
- require .NoError (t , err )
270
- if o .NotMember {
271
- require .Len (t , members , 0 , "should not be a member" )
272
- } else {
273
- require .Len (t , members , 1 , "should be a member" )
274
- }
298
+ members , err := db .OrganizationMembers (ctx , database.OrganizationMembersParams {
299
+ OrganizationID : orgID ,
300
+ UserID : user .ID ,
301
+ })
302
+ require .NoError (t , err )
303
+ if o .NotMember {
304
+ require .Len (t , members , 0 , "should not be a member" )
305
+ } else {
306
+ require .Len (t , members , 1 , "should be a member" )
307
+ }
308
+
309
+ userGroups , err := db .GetGroups (ctx , database.GetGroupsParams {
310
+ OrganizationID : orgID ,
311
+ HasMemberID : user .ID ,
312
+ })
313
+ require .NoError (t , err )
314
+ if o .ExpectedGroups == nil {
315
+ o .ExpectedGroups = make ([]uuid.UUID , 0 )
316
+ }
317
+ if len (o .ExpectedGroupNames ) > 0 && len (o .ExpectedGroups ) > 0 {
318
+ t .Fatal ("ExpectedGroups and ExpectedGroupNames are mutually exclusive" )
319
+ }
320
+
321
+ // Everyone groups mess up our asserts
322
+ userGroups = slices .DeleteFunc (userGroups , func (row database.GetGroupsRow ) bool {
323
+ return row .Group .ID == row .Group .OrganizationID
324
+ })
275
325
276
- userGroups , err := db . GetGroups ( ctx , database. GetGroupsParams {
277
- OrganizationID : orgID ,
278
- HasMemberID : user . ID ,
326
+ if len ( o . ExpectedGroupNames ) > 0 {
327
+ found := db2sdk . List ( userGroups , func ( g database. GetGroupsRow ) string {
328
+ return g . Group . Name
279
329
})
280
- require .NoError (t , err )
281
- if o .ExpectedGroups == nil {
282
- o .ExpectedGroups = make ([]uuid.UUID , 0 )
283
- }
330
+ require .ElementsMatch (t , o .ExpectedGroupNames , found , "user groups by name" )
331
+ } else {
332
+ // Check by ID, recommended
284
333
found := db2sdk .List (userGroups , func (g database.GetGroupsRow ) uuid.UUID {
285
334
return g .Group .ID
286
335
})
287
336
require .ElementsMatch (t , o .ExpectedGroups , found , "user groups" )
288
- })
337
+ }
289
338
}
0 commit comments