Skip to content

Commit 64f442e

Browse files
committed
add test for disabled sync
1 parent ae1e2eb commit 64f442e

File tree

7 files changed

+74
-20
lines changed

7 files changed

+74
-20
lines changed

coderd/idpsync/group.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,11 @@ func (s AGPLIDPSync) SyncGroups(ctx context.Context, db database.Store, user dat
8181
orgResolver := s.Manager.OrganizationResolver(tx, orgID)
8282
settings, err := s.SyncSettings.Group.Resolve(ctx, orgResolver)
8383
if err != nil {
84-
if xerrors.Is(err, runtimeconfig.ErrEntryNotFound) {
85-
// Default to not being configured
86-
settings = &GroupSyncSettings{}
87-
} else {
84+
if !xerrors.Is(err, runtimeconfig.ErrEntryNotFound) {
8885
return xerrors.Errorf("resolve group sync settings: %w", err)
8986
}
87+
// Default to not being configured
88+
settings = &GroupSyncSettings{}
9089
}
9190

9291
// Legacy deployment settings will override empty settings.
@@ -273,15 +272,7 @@ func (s *GroupSyncSettings) Set(v string) error {
273272
}
274273

275274
func (s *GroupSyncSettings) String() string {
276-
v, err := json.Marshal(s)
277-
if err != nil {
278-
return "decode failed: " + err.Error()
279-
}
280-
return string(v)
281-
}
282-
283-
func (s *GroupSyncSettings) Type() string {
284-
return "GroupSyncSettings"
275+
return runtimeconfig.JSONString(s)
285276
}
286277

287278
type ExpectedGroup struct {

coderd/idpsync/group_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,60 @@ func TestGroupSyncTable(t *testing.T) {
342342
})
343343
}
344344

345+
func TestSyncDisabled(t *testing.T) {
346+
t.Parallel()
347+
348+
if dbtestutil.WillUsePostgres() {
349+
t.Skip("Skipping test because it populates a lot of db entries, which is slow on postgres.")
350+
}
351+
352+
db, _ := dbtestutil.NewDB(t)
353+
manager := runtimeconfig.NewManager()
354+
s := idpsync.NewAGPLSync(slogtest.Make(t, &slogtest.Options{}),
355+
manager,
356+
idpsync.DeploymentSyncSettings{},
357+
)
358+
359+
ids := coderdtest.NewDeterministicUUIDGenerator()
360+
ctx := testutil.Context(t, testutil.WaitSuperLong)
361+
user := dbgen.User(t, db, database.User{})
362+
orgID := uuid.New()
363+
364+
def := orgSetupDefinition{
365+
Name: "SyncDisabled",
366+
Groups: map[uuid.UUID]bool{
367+
ids.ID("foo"): true,
368+
ids.ID("bar"): true,
369+
ids.ID("baz"): false,
370+
ids.ID("bop"): false,
371+
},
372+
Settings: &idpsync.GroupSyncSettings{
373+
Field: "groups",
374+
Mapping: map[string][]uuid.UUID{
375+
"foo": {ids.ID("foo")},
376+
"baz": {ids.ID("baz")},
377+
},
378+
},
379+
ExpectedGroups: []uuid.UUID{
380+
ids.ID("foo"),
381+
ids.ID("bar"),
382+
},
383+
}
384+
385+
SetupOrganization(t, s, db, user, orgID, def)
386+
387+
// Do the group sync!
388+
err := s.SyncGroups(ctx, db, user, idpsync.GroupParams{
389+
SyncEnabled: false,
390+
MergedClaims: jwt.MapClaims{
391+
"groups": []string{"baz", "bop"},
392+
},
393+
})
394+
require.NoError(t, err)
395+
396+
def.Assert(t, orgID, db, user)
397+
}
398+
345399
// TestApplyGroupDifference is mainly testing the database functions
346400
func TestApplyGroupDifference(t *testing.T) {
347401
t.Parallel()

coderd/idpsync/organizations_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ func TestParseOrganizationClaims(t *testing.T) {
2020
t.Parallel()
2121

2222
s := idpsync.NewAGPLSync(slogtest.Make(t, &slogtest.Options{}),
23-
runtimeconfig.NewStoreManager(),
23+
runtimeconfig.NewManager(),
2424
idpsync.DeploymentSyncSettings{
2525
OrganizationField: "",
2626
OrganizationMapping: nil,
@@ -42,7 +42,7 @@ func TestParseOrganizationClaims(t *testing.T) {
4242

4343
// AGPL has limited behavior
4444
s := idpsync.NewAGPLSync(slogtest.Make(t, &slogtest.Options{}),
45-
runtimeconfig.NewStoreManager(),
45+
runtimeconfig.NewManager(),
4646
idpsync.DeploymentSyncSettings{
4747
OrganizationField: "orgs",
4848
OrganizationMapping: map[string][]uuid.UUID{

coderd/runtimeconfig/entry.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package runtimeconfig
22

33
import (
44
"context"
5+
"encoding/json"
56
"fmt"
67

78
"golang.org/x/xerrors"
@@ -93,3 +94,11 @@ func (e *RuntimeEntry[T]) name() (string, error) {
9394

9495
return e.n, nil
9596
}
97+
98+
func JSONString(v any) string {
99+
s, err := json.Marshal(v)
100+
if err != nil {
101+
return "decode failed: " + err.Error()
102+
}
103+
return string(s)
104+
}

enterprise/coderd/enidpsync/enidpsync.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ type EnterpriseIDPSync struct {
1717
*idpsync.AGPLIDPSync
1818
}
1919

20-
func NewSync(logger slog.Logger, manager runtimeconfig.Manager, set *entitlements.Set, settings idpsync.DeploymentSyncSettings) *EnterpriseIDPSync {
20+
func NewSync(logger slog.Logger, manager *runtimeconfig.Manager, set *entitlements.Set, settings idpsync.DeploymentSyncSettings) *EnterpriseIDPSync {
2121
return &EnterpriseIDPSync{
2222
entitlements: set,
2323
AGPLIDPSync: idpsync.NewAGPLSync(logger.With(slog.F("enterprise_capable", "true")), manager, settings),

enterprise/coderd/enidpsync/groups_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestEnterpriseParseGroupClaims(t *testing.T) {
3030
t.Parallel()
3131

3232
s := enidpsync.NewSync(slogtest.Make(t, &slogtest.Options{}),
33-
runtimeconfig.NewStoreManager(),
33+
runtimeconfig.NewManager(),
3434
entitlements.New(),
3535
idpsync.DeploymentSyncSettings{})
3636

@@ -46,7 +46,7 @@ func TestEnterpriseParseGroupClaims(t *testing.T) {
4646
t.Parallel()
4747

4848
s := enidpsync.NewSync(slogtest.Make(t, &slogtest.Options{}),
49-
runtimeconfig.NewStoreManager(),
49+
runtimeconfig.NewManager(),
5050
entitled,
5151
idpsync.DeploymentSyncSettings{
5252
GroupField: "groups",
@@ -74,7 +74,7 @@ func TestEnterpriseParseGroupClaims(t *testing.T) {
7474
t.Parallel()
7575

7676
s := enidpsync.NewSync(slogtest.Make(t, &slogtest.Options{}),
77-
runtimeconfig.NewStoreManager(),
77+
runtimeconfig.NewManager(),
7878
entitled,
7979
idpsync.DeploymentSyncSettings{
8080
GroupField: "groups",

enterprise/coderd/enidpsync/organizations_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ func TestOrganizationSync(t *testing.T) {
237237
}
238238

239239
// Create a new sync object
240-
sync := enidpsync.NewSync(logger, runtimeconfig.NewStoreManager(), caseData.Entitlements, caseData.Settings)
240+
sync := enidpsync.NewSync(logger, runtimeconfig.NewManager(), caseData.Entitlements, caseData.Settings)
241241
for _, exp := range caseData.Exps {
242242
t.Run(exp.Name, func(t *testing.T) {
243243
params, httpErr := sync.ParseOrganizationClaims(ctx, exp.Claims)

0 commit comments

Comments
 (0)