@@ -14,6 +14,7 @@ import (
14
14
"github.com/coder/coder/v2/coderd/coderdtest"
15
15
"github.com/coder/coder/v2/coderd/database"
16
16
"github.com/coder/coder/v2/coderd/database/db2sdk"
17
+ "github.com/coder/coder/v2/coderd/database/dbauthz"
17
18
"github.com/coder/coder/v2/coderd/database/dbgen"
18
19
"github.com/coder/coder/v2/coderd/database/dbtestutil"
19
20
"github.com/coder/coder/v2/coderd/idpsync"
@@ -71,6 +72,7 @@ func TestGroupSyncTable(t *testing.T) {
71
72
"groups" : []string {
72
73
"foo" , "bar" , "baz" ,
73
74
"create-bar" , "create-baz" ,
75
+ "legacy-bar" ,
74
76
},
75
77
}
76
78
@@ -229,10 +231,6 @@ func TestGroupSyncTable(t *testing.T) {
229
231
t .Run (tc .Name , func (t * testing.T ) {
230
232
t .Parallel ()
231
233
232
- if tc .OrgID == uuid .Nil {
233
- tc .OrgID = uuid .New ()
234
- }
235
-
236
234
db , _ := dbtestutil .NewDB (t )
237
235
manager := runtimeconfig .NewStoreManager ()
238
236
s := idpsync .NewAGPLSync (slogtest .Make (t , & slogtest.Options {}),
@@ -242,9 +240,10 @@ func TestGroupSyncTable(t *testing.T) {
242
240
},
243
241
)
244
242
245
- ctx := testutil .Context (t , testutil .WaitMedium )
243
+ ctx := testutil .Context (t , testutil .WaitSuperLong )
246
244
user := dbgen .User (t , db , database.User {})
247
- SetupOrganization (t , s , db , user , tc )
245
+ orgID := uuid .New ()
246
+ SetupOrganization (t , s , db , user , orgID , tc )
248
247
249
248
// Do the group sync!
250
249
err := s .SyncGroups (ctx , db , user , idpsync.GroupParams {
@@ -253,17 +252,106 @@ func TestGroupSyncTable(t *testing.T) {
253
252
})
254
253
require .NoError (t , err )
255
254
256
- tc .Assert (t , tc . OrgID , db , user )
255
+ tc .Assert (t , orgID , db , user )
257
256
})
258
257
}
258
+
259
+ // AllTogether runs the entire tabled test as a singular user and
260
+ // deployment. This tests all organizations being synced together.
261
+ // The reason we do them individually, is that it is much easier to
262
+ // debug a single test case.
263
+ t .Run ("AllTogether" , func (t * testing.T ) {
264
+ t .Parallel ()
265
+
266
+ db , _ := dbtestutil .NewDB (t )
267
+ manager := runtimeconfig .NewStoreManager ()
268
+ s := idpsync .NewAGPLSync (slogtest .Make (t , & slogtest.Options {}),
269
+ manager ,
270
+ // Also sync the default org!
271
+ idpsync.DeploymentSyncSettings {
272
+ GroupField : "groups" ,
273
+ Legacy : idpsync.DefaultOrgLegacySettings {
274
+ GroupField : "groups" ,
275
+ GroupMapping : map [string ]string {
276
+ "foo" : "legacy-foo" ,
277
+ "baz" : "legacy-baz" ,
278
+ },
279
+ GroupFilter : regexp .MustCompile ("^legacy" ),
280
+ CreateMissingGroups : true ,
281
+ },
282
+ },
283
+ )
284
+
285
+ ctx := testutil .Context (t , testutil .WaitSuperLong )
286
+ user := dbgen .User (t , db , database.User {})
287
+
288
+ var asserts []func (t * testing.T )
289
+ // The default org is also going to do something
290
+ def := orgSetupDefinition {
291
+ Name : "DefaultOrg" ,
292
+ GroupNames : map [string ]bool {
293
+ "legacy-foo" : false ,
294
+ "legacy-baz" : true ,
295
+ "random" : true ,
296
+ },
297
+ // No settings, because they come from the deployment values
298
+ Settings : nil ,
299
+ ExpectedGroups : nil ,
300
+ ExpectedGroupNames : []string {"legacy-foo" , "legacy-baz" , "legacy-bar" },
301
+ }
302
+
303
+ //nolint:gocritic // testing
304
+ defOrg , err := db .GetDefaultOrganization (dbauthz .AsSystemRestricted (ctx ))
305
+ require .NoError (t , err )
306
+ SetupOrganization (t , s , db , user , defOrg .ID , def )
307
+ asserts = append (asserts , func (t * testing.T ) {
308
+ t .Run (def .Name , func (t * testing.T ) {
309
+ t .Parallel ()
310
+ def .Assert (t , defOrg .ID , db , user )
311
+ })
312
+ })
313
+
314
+ for _ , tc := range testCases {
315
+ tc := tc
316
+
317
+ orgID := uuid .New ()
318
+ SetupOrganization (t , s , db , user , orgID , tc )
319
+ asserts = append (asserts , func (t * testing.T ) {
320
+ t .Run (tc .Name , func (t * testing.T ) {
321
+ t .Parallel ()
322
+ tc .Assert (t , orgID , db , user )
323
+ })
324
+ })
325
+ }
326
+
327
+ asserts = append (asserts , func (t * testing.T ) {
328
+ t .Helper ()
329
+ def .Assert (t , defOrg .ID , db , user )
330
+ })
331
+
332
+ // Do the group sync!
333
+ err = s .SyncGroups (ctx , db , user , idpsync.GroupParams {
334
+ SyncEnabled : true ,
335
+ MergedClaims : userClaims ,
336
+ })
337
+ require .NoError (t , err )
338
+
339
+ for _ , assert := range asserts {
340
+ assert (t )
341
+ }
342
+ })
259
343
}
260
344
261
- func SetupOrganization (t * testing.T , s * idpsync.AGPLIDPSync , db database.Store , user database.User , def orgSetupDefinition ) {
345
+ func SetupOrganization (t * testing.T , s * idpsync.AGPLIDPSync , db database.Store , user database.User , orgID uuid.UUID , def orgSetupDefinition ) {
346
+ t .Helper ()
347
+
262
348
org := dbgen .Organization (t , db , database.Organization {
263
- ID : def . OrgID ,
349
+ ID : orgID ,
264
350
})
265
351
_ , err := db .InsertAllUsersGroup (context .Background (), org .ID )
266
- require .NoError (t , err , "Everyone group for an org" )
352
+ if ! database .IsUniqueViolation (err ) {
353
+ require .NoError (t , err , "Everyone group for an org" )
354
+ }
267
355
268
356
manager := runtimeconfig .NewStoreManager ()
269
357
orgResolver := manager .OrganizationResolver (db , org .ID )
@@ -303,8 +391,7 @@ func SetupOrganization(t *testing.T, s *idpsync.AGPLIDPSync, db database.Store,
303
391
}
304
392
305
393
type orgSetupDefinition struct {
306
- Name string
307
- OrgID uuid.UUID
394
+ Name string
308
395
// True if the user is a member of the group
309
396
Groups map [uuid.UUID ]bool
310
397
GroupNames map [string ]bool
@@ -353,11 +440,13 @@ func (o orgSetupDefinition) Assert(t *testing.T, orgID uuid.UUID, db database.St
353
440
return g .Group .Name
354
441
})
355
442
require .ElementsMatch (t , o .ExpectedGroupNames , found , "user groups by name" )
443
+ require .Len (t , o .ExpectedGroups , 0 , "ExpectedGroups should be empty" )
356
444
} else {
357
445
// Check by ID, recommended
358
446
found := db2sdk .List (userGroups , func (g database.GetGroupsRow ) uuid.UUID {
359
447
return g .Group .ID
360
448
})
361
449
require .ElementsMatch (t , o .ExpectedGroups , found , "user groups" )
450
+ require .Len (t , o .ExpectedGroupNames , 0 , "ExpectedGroupNames should be empty" )
362
451
}
363
452
}
0 commit comments