Skip to content

Commit 1d482e9

Browse files
committed
fixup duplicate assignments
1 parent d4aece9 commit 1d482e9

File tree

2 files changed

+93
-3
lines changed

2 files changed

+93
-3
lines changed

enterprise/coderd/enidpsync/organizations.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"cdr.dev/slog"
1111
"github.com/coder/coder/v2/coderd/database/dbauthz"
1212
"github.com/coder/coder/v2/coderd/idpsync"
13+
"github.com/coder/coder/v2/coderd/util/slice"
1314
"github.com/coder/coder/v2/codersdk"
1415
)
1516

@@ -62,6 +63,7 @@ func (e EnterpriseIDPSync) ParseOrganizationClaims(ctx context.Context, mergedCl
6263
// If the field is not set, then sync is not enabled.
6364
SyncEnabled: e.OrganizationField != "",
6465
IncludeDefault: e.OrganizationAssignDefault,
65-
Organizations: userOrganizations,
66+
// Do not return duplicates
67+
Organizations: slice.Unique(userOrganizations),
6668
}, nil
6769
}

enterprise/coderd/enidpsync/organizations_test.go

Lines changed: 90 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,92 @@ func TestOrganizationSync(t *testing.T) {
132132
}
133133
},
134134
},
135+
{
136+
Name: "MultiOrgWithDefault",
137+
Case: func(t *testing.T, db database.Store) OrganizationSyncTestCase {
138+
def, _ := db.GetDefaultOrganization(context.Background())
139+
one := dbgen.Organization(t, db, database.Organization{})
140+
two := dbgen.Organization(t, db, database.Organization{})
141+
three := dbgen.Organization(t, db, database.Organization{})
142+
return OrganizationSyncTestCase{
143+
Entitlements: entitled,
144+
Settings: idpsync.SyncSettings{
145+
OrganizationField: "organizations",
146+
OrganizationMapping: map[string][]uuid.UUID{
147+
"first": {one.ID},
148+
"second": {two.ID},
149+
"third": {three.ID},
150+
},
151+
OrganizationAssignDefault: true,
152+
},
153+
Exps: []Expectations{
154+
{
155+
Name: "NoOrganizations",
156+
Claims: jwt.MapClaims{},
157+
ExpectedParams: idpsync.OrganizationParams{
158+
SyncEnabled: true,
159+
IncludeDefault: true,
160+
Organizations: []uuid.UUID{},
161+
},
162+
Sync: ExpectedUser{
163+
Organizations: []uuid.UUID{def.ID},
164+
},
165+
},
166+
{
167+
Name: "AlreadyInOrgs",
168+
Claims: jwt.MapClaims{
169+
"organizations": []string{"second", "extra"},
170+
},
171+
ExpectedParams: idpsync.OrganizationParams{
172+
SyncEnabled: true,
173+
IncludeDefault: true,
174+
Organizations: []uuid.UUID{two.ID},
175+
},
176+
Mutate: func(t *testing.T, db database.Store, user database.User) {
177+
dbgen.OrganizationMember(t, db, database.OrganizationMember{
178+
UserID: user.ID,
179+
OrganizationID: def.ID,
180+
})
181+
dbgen.OrganizationMember(t, db, database.OrganizationMember{
182+
UserID: user.ID,
183+
OrganizationID: one.ID,
184+
})
185+
},
186+
Sync: ExpectedUser{
187+
Organizations: []uuid.UUID{def.ID, two.ID},
188+
},
189+
},
190+
{
191+
Name: "ManyClaims",
192+
Claims: jwt.MapClaims{
193+
// Add some repeats
194+
"organizations": []string{"second", "extra", "first", "third", "second", "second"},
195+
},
196+
ExpectedParams: idpsync.OrganizationParams{
197+
SyncEnabled: true,
198+
IncludeDefault: true,
199+
Organizations: []uuid.UUID{
200+
two.ID, one.ID, three.ID,
201+
},
202+
},
203+
Mutate: func(t *testing.T, db database.Store, user database.User) {
204+
dbgen.OrganizationMember(t, db, database.OrganizationMember{
205+
UserID: user.ID,
206+
OrganizationID: def.ID,
207+
})
208+
dbgen.OrganizationMember(t, db, database.OrganizationMember{
209+
UserID: user.ID,
210+
OrganizationID: one.ID,
211+
})
212+
},
213+
Sync: ExpectedUser{
214+
Organizations: []uuid.UUID{def.ID, one.ID, two.ID, three.ID},
215+
},
216+
},
217+
},
218+
}
219+
},
220+
},
135221
}
136222

137223
for _, tc := range testCases {
@@ -157,6 +243,7 @@ func TestOrganizationSync(t *testing.T) {
157243
exp.ParseError(t, httpErr)
158244
return
159245
}
246+
require.Nil(t, httpErr, "no parse error")
160247

161248
require.Equal(t, exp.ExpectedParams.SyncEnabled, params.SyncEnabled, "match enabled")
162249
require.Equal(t, exp.ExpectedParams.IncludeDefault, params.IncludeDefault, "match include default")
@@ -167,14 +254,15 @@ func TestOrganizationSync(t *testing.T) {
167254

168255
user := dbgen.User(t, db, database.User{})
169256
if exp.Mutate != nil {
170-
exp.Mutate(t, db, user)
257+
exp.Mutate(t, rdb, user)
171258
}
172259

173-
err := sync.SyncOrganizations(ctx, db, user, params)
260+
err := sync.SyncOrganizations(ctx, rdb, user, params)
174261
if exp.Sync.SyncError {
175262
require.Error(t, err)
176263
return
177264
}
265+
require.NoError(t, err)
178266
requireUserOrgs(t, db, user, exp.Sync.Organizations)
179267
})
180268
}

0 commit comments

Comments
 (0)