Skip to content

Commit a0a75e6

Browse files
committed
test: add unit test to excercise bug when idp sync hits deleted orgs
Deleted organizations are still attempting to sync members. This causes an error on inserting the member, and would likely cause issues later in the sync process even if that member is inserted. Deleted orgs should be skipped.
1 parent 0cd531d commit a0a75e6

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

coderd/idpsync/organizations_test.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ import (
88
"github.com/stretchr/testify/require"
99

1010
"cdr.dev/slog/sloggers/slogtest"
11+
"github.com/coder/coder/v2/coderd/database"
12+
"github.com/coder/coder/v2/coderd/database/dbgen"
13+
"github.com/coder/coder/v2/coderd/database/dbtestutil"
14+
"github.com/coder/coder/v2/coderd/database/dbtime"
1115
"github.com/coder/coder/v2/coderd/idpsync"
1216
"github.com/coder/coder/v2/coderd/runtimeconfig"
1317
"github.com/coder/coder/v2/testutil"
@@ -38,3 +42,59 @@ func TestParseOrganizationClaims(t *testing.T) {
3842
require.False(t, params.SyncEntitled)
3943
})
4044
}
45+
46+
func TestSyncOrganizations(t *testing.T) {
47+
t.Parallel()
48+
49+
t.Run("SyncUserToDeletedOrg", func(t *testing.T) {
50+
ctx := testutil.Context(t, testutil.WaitMedium)
51+
db, _ := dbtestutil.NewDB(t)
52+
53+
// Create a new organization, add in the user as a member, then delete
54+
// the org.
55+
org := dbgen.Organization(t, db, database.Organization{})
56+
user := dbgen.User(t, db, database.User{})
57+
dbgen.OrganizationMember(t, db, database.OrganizationMember{
58+
UserID: user.ID,
59+
OrganizationID: org.ID,
60+
CreatedAt: dbtime.Now(),
61+
UpdatedAt: dbtime.Now(),
62+
Roles: nil,
63+
})
64+
65+
err := db.UpdateOrganizationDeletedByID(ctx, database.UpdateOrganizationDeletedByIDParams{
66+
UpdatedAt: dbtime.Now(),
67+
ID: org.ID,
68+
})
69+
require.NoError(t, err)
70+
71+
// Now sync the user to the deleted organization
72+
s := idpsync.NewAGPLSync(
73+
slogtest.Make(t, &slogtest.Options{}),
74+
runtimeconfig.NewManager(),
75+
idpsync.DeploymentSyncSettings{
76+
OrganizationField: "orgs",
77+
OrganizationMapping: map[string][]uuid.UUID{
78+
"random": {org.ID},
79+
},
80+
OrganizationAssignDefault: false,
81+
},
82+
)
83+
84+
err = s.SyncOrganizations(ctx, db, user, idpsync.OrganizationParams{
85+
SyncEntitled: true,
86+
MergedClaims: map[string]interface{}{
87+
"orgs": []string{"random"},
88+
},
89+
})
90+
require.NoError(t, err)
91+
92+
mems, err := db.OrganizationMembers(ctx, database.OrganizationMembersParams{
93+
OrganizationID: org.ID,
94+
UserID: user.ID,
95+
IncludeSystem: false,
96+
})
97+
require.NoError(t, err)
98+
require.Len(t, mems, 1)
99+
})
100+
}

0 commit comments

Comments
 (0)