Skip to content

Commit 8fefc9f

Browse files
committed
test legacy params
1 parent b379725 commit 8fefc9f

File tree

2 files changed

+104
-14
lines changed

2 files changed

+104
-14
lines changed

coderd/idpsync/group.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
7878
if err != nil {
7979
return xerrors.Errorf("resolve group sync settings: %w", err)
8080
}
81-
orgSettings[orgID] = *settings
8281

8382
// Legacy deployment settings will override empty settings.
8483
if orgID == defaultOrgID && settings.GroupField == "" {
@@ -89,6 +88,7 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
8988
AutoCreateMissingGroups: s.Legacy.CreateMissingGroups,
9089
}
9190
}
91+
orgSettings[orgID] = *settings
9292
}
9393

9494
// collect all diffs to do 1 sql update for all orgs
@@ -280,6 +280,8 @@ func (s GroupSyncSettings) ParseClaims(orgID uuid.UUID, mergedClaims jwt.MapClai
280280

281281
groups := make([]ExpectedGroup, 0)
282282
for _, group := range parsedGroups {
283+
group := group
284+
283285
// Legacy group mappings happen before the regex filter.
284286
mappedGroupName, ok := s.LegacyGroupNameMapping[group]
285287
if ok {
@@ -302,7 +304,6 @@ func (s GroupSyncSettings) ParseClaims(orgID uuid.UUID, mergedClaims jwt.MapClai
302304
continue
303305
}
304306

305-
group := group
306307
groups = append(groups, ExpectedGroup{OrganizationID: orgID, GroupName: &group})
307308
}
308309

coderd/idpsync/group_test.go

+101-12
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/coder/coder/v2/coderd/coderdtest"
1515
"github.com/coder/coder/v2/coderd/database"
1616
"github.com/coder/coder/v2/coderd/database/db2sdk"
17+
"github.com/coder/coder/v2/coderd/database/dbauthz"
1718
"github.com/coder/coder/v2/coderd/database/dbgen"
1819
"github.com/coder/coder/v2/coderd/database/dbtestutil"
1920
"github.com/coder/coder/v2/coderd/idpsync"
@@ -71,6 +72,7 @@ func TestGroupSyncTable(t *testing.T) {
7172
"groups": []string{
7273
"foo", "bar", "baz",
7374
"create-bar", "create-baz",
75+
"legacy-bar",
7476
},
7577
}
7678

@@ -229,10 +231,6 @@ func TestGroupSyncTable(t *testing.T) {
229231
t.Run(tc.Name, func(t *testing.T) {
230232
t.Parallel()
231233

232-
if tc.OrgID == uuid.Nil {
233-
tc.OrgID = uuid.New()
234-
}
235-
236234
db, _ := dbtestutil.NewDB(t)
237235
manager := runtimeconfig.NewStoreManager()
238236
s := idpsync.NewAGPLSync(slogtest.Make(t, &slogtest.Options{}),
@@ -242,9 +240,10 @@ func TestGroupSyncTable(t *testing.T) {
242240
},
243241
)
244242

245-
ctx := testutil.Context(t, testutil.WaitMedium)
243+
ctx := testutil.Context(t, testutil.WaitSuperLong)
246244
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)
248247

249248
// Do the group sync!
250249
err := s.SyncGroups(ctx, db, user, idpsync.GroupParams{
@@ -253,17 +252,106 @@ func TestGroupSyncTable(t *testing.T) {
253252
})
254253
require.NoError(t, err)
255254

256-
tc.Assert(t, tc.OrgID, db, user)
255+
tc.Assert(t, orgID, db, user)
257256
})
258257
}
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+
})
259343
}
260344

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+
262348
org := dbgen.Organization(t, db, database.Organization{
263-
ID: def.OrgID,
349+
ID: orgID,
264350
})
265351
_, 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+
}
267355

268356
manager := runtimeconfig.NewStoreManager()
269357
orgResolver := manager.OrganizationResolver(db, org.ID)
@@ -303,8 +391,7 @@ func SetupOrganization(t *testing.T, s *idpsync.AGPLIDPSync, db database.Store,
303391
}
304392

305393
type orgSetupDefinition struct {
306-
Name string
307-
OrgID uuid.UUID
394+
Name string
308395
// True if the user is a member of the group
309396
Groups map[uuid.UUID]bool
310397
GroupNames map[string]bool
@@ -353,11 +440,13 @@ func (o orgSetupDefinition) Assert(t *testing.T, orgID uuid.UUID, db database.St
353440
return g.Group.Name
354441
})
355442
require.ElementsMatch(t, o.ExpectedGroupNames, found, "user groups by name")
443+
require.Len(t, o.ExpectedGroups, 0, "ExpectedGroups should be empty")
356444
} else {
357445
// Check by ID, recommended
358446
found := db2sdk.List(userGroups, func(g database.GetGroupsRow) uuid.UUID {
359447
return g.Group.ID
360448
})
361449
require.ElementsMatch(t, o.ExpectedGroups, found, "user groups")
450+
require.Len(t, o.ExpectedGroupNames, 0, "ExpectedGroupNames should be empty")
362451
}
363452
}

0 commit comments

Comments
 (0)