Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc267137

Browse files
committed
add some test noise
1 parentc1cc257 commitc267137

File tree

2 files changed

+31
-6
lines changed

2 files changed

+31
-6
lines changed

‎coderd/idpsync/organization.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u
127127
// Find the difference in the expected and the existing orgs, and
128128
// correct the set of orgs the user is a member of.
129129
add,remove:=slice.SymmetricDifference(existingOrgIDs,finalExpected)
130-
notExists:=make([]uuid.UUID,0)
130+
// notExists is purely for debugging. It logs when the settings want
131+
// a user in an organization, but the organization does not exist.
132+
notExists:=slice.DifferenceFunc(expectedOrgIDs,finalExpected,func(a,b uuid.UUID)bool {
133+
returna==b
134+
})
131135
for_,orgID:=rangeadd {
132136
_,err:=tx.InsertOrganizationMember(ctx, database.InsertOrganizationMemberParams{
133137
OrganizationID:orgID,
@@ -138,9 +142,26 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u
138142
})
139143
iferr!=nil {
140144
ifxerrors.Is(err,sql.ErrNoRows) {
145+
// This should not happen because we check the org existance
146+
// beforehand.
141147
notExists=append(notExists,orgID)
142148
continue
143149
}
150+
151+
ifdatabase.IsUniqueViolation(err,database.UniqueOrganizationMembersPkey) {
152+
// If we hit this error we have a bug. The user already exists in the
153+
// organization, but was not detected to be at the start of this function.
154+
// Instead of failing the function, an error will be logged. This is to not bring
155+
// down the entire syncing behavior from a single failed org. Failing this can
156+
// prevent user logins, so only fatal non-recoverable errors should be returned.
157+
s.Logger.Error(ctx,"syncing user to organization failed as they are already a member, please report this failure to Coder",
158+
slog.F("user_id",user.ID),
159+
slog.F("username",user.Username),
160+
slog.F("organization_id",orgID),
161+
slog.Error(err),
162+
)
163+
continue
164+
}
144165
returnxerrors.Errorf("add user to organization: %w",err)
145166
}
146167
}
@@ -156,6 +177,7 @@ func (s AGPLIDPSync) SyncOrganizations(ctx context.Context, tx database.Store, u
156177
}
157178

158179
iflen(notExists)>0 {
180+
notExists=slice.Unique(notExists)// Remove dupes
159181
s.Logger.Debug(ctx,"organizations do not exist but attempted to use in org sync",
160182
slog.F("not_found",notExists),
161183
slog.F("user_id",user.ID),

‎coderd/idpsync/organizations_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,17 +49,19 @@ func TestSyncOrganizations(t *testing.T) {
4949
t.Run("SyncUserToDeletedOrg",func(t*testing.T) {
5050
ctx:=testutil.Context(t,testutil.WaitMedium)
5151
db,_:=dbtestutil.NewDB(t)
52+
user:=dbgen.User(t,db, database.User{})
53+
extra:=dbgen.Organization(t,db, database.Organization{})
54+
dbgen.OrganizationMember(t,db, database.OrganizationMember{
55+
UserID:user.ID,
56+
OrganizationID:extra.ID,
57+
})
5258

5359
// Create a new organization, add in the user as a member, then delete
5460
// the org.
5561
org:=dbgen.Organization(t,db, database.Organization{})
56-
user:=dbgen.User(t,db, database.User{})
5762
dbgen.OrganizationMember(t,db, database.OrganizationMember{
5863
UserID:user.ID,
5964
OrganizationID:org.ID,
60-
CreatedAt:dbtime.Now(),
61-
UpdatedAt:dbtime.Now(),
62-
Roles:nil,
6365
})
6466

6567
err:=db.UpdateOrganizationDeletedByID(ctx, database.UpdateOrganizationDeletedByIDParams{
@@ -76,6 +78,7 @@ func TestSyncOrganizations(t *testing.T) {
7678
OrganizationField:"orgs",
7779
OrganizationMapping:map[string][]uuid.UUID{
7880
"random": {org.ID},
81+
"noise": {uuid.New()},
7982
},
8083
OrganizationAssignDefault:false,
8184
},
@@ -84,7 +87,7 @@ func TestSyncOrganizations(t *testing.T) {
8487
err=s.SyncOrganizations(ctx,db,user, idpsync.OrganizationParams{
8588
SyncEntitled:true,
8689
MergedClaims:map[string]interface{}{
87-
"orgs": []string{"random"},
90+
"orgs": []string{"random","noise"},
8891
},
8992
})
9093
require.NoError(t,err)

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp