|
| 1 | +package enidpsync |
| 2 | + |
| 3 | +import ( |
| 4 | +"context" |
| 5 | +"testing" |
| 6 | + |
| 7 | +"github.com/golang-jwt/jwt/v4" |
| 8 | +"github.com/google/uuid" |
| 9 | +"github.com/prometheus/client_golang/prometheus" |
| 10 | +"github.com/stretchr/testify/require" |
| 11 | + |
| 12 | +"cdr.dev/slog/sloggers/slogtest" |
| 13 | +"github.com/coder/coder/v2/coderd/coderdtest" |
| 14 | +"github.com/coder/coder/v2/coderd/database" |
| 15 | +"github.com/coder/coder/v2/coderd/database/db2sdk" |
| 16 | +"github.com/coder/coder/v2/coderd/database/dbauthz" |
| 17 | +"github.com/coder/coder/v2/coderd/database/dbgen" |
| 18 | +"github.com/coder/coder/v2/coderd/database/dbtestutil" |
| 19 | +"github.com/coder/coder/v2/coderd/entitlements" |
| 20 | +"github.com/coder/coder/v2/coderd/idpsync" |
| 21 | +"github.com/coder/coder/v2/coderd/rbac" |
| 22 | +"github.com/coder/coder/v2/codersdk" |
| 23 | +"github.com/coder/coder/v2/testutil" |
| 24 | +) |
| 25 | + |
| 26 | +typeExpectedUserstruct { |
| 27 | +SyncErrorbool |
| 28 | +Organizations []uuid.UUID |
| 29 | +} |
| 30 | + |
| 31 | +typeExpectationsstruct { |
| 32 | +Namestring |
| 33 | +Claims jwt.MapClaims |
| 34 | +// Parse |
| 35 | +ParseErrorfunc(t*testing.T,httpErr*idpsync.HttpError) |
| 36 | +ExpectedParams idpsync.OrganizationParams |
| 37 | +// Mutate allows mutating the user before syncing |
| 38 | +Mutatefunc(t*testing.T,db database.Store,user database.User) |
| 39 | +SyncExpectedUser |
| 40 | +} |
| 41 | + |
| 42 | +typeOrganizationSyncTestCasestruct { |
| 43 | +Settings idpsync.SyncSettings |
| 44 | +Entitlements*entitlements.Set |
| 45 | +Exps []Expectations |
| 46 | +} |
| 47 | + |
| 48 | +funcTestOrganizationSync(t*testing.T) { |
| 49 | +t.Parallel() |
| 50 | + |
| 51 | +ifdbtestutil.WillUsePostgres() { |
| 52 | +t.Skip("Skipping test because it populates a lot of db entries, which is slow on postgres") |
| 53 | +} |
| 54 | + |
| 55 | +requireUserOrgs:=func(t*testing.T,db database.Store,user database.User,expected []uuid.UUID) { |
| 56 | +t.Helper() |
| 57 | + |
| 58 | +// nolint:gocritic // in testing |
| 59 | +members,err:=db.OrganizationMembers(dbauthz.AsSystemRestricted(context.Background()), database.OrganizationMembersParams{ |
| 60 | +UserID:user.ID, |
| 61 | +}) |
| 62 | +require.NoError(t,err) |
| 63 | + |
| 64 | +foundIDs:=db2sdk.List(members,func(m database.OrganizationMembersRow) uuid.UUID { |
| 65 | +returnm.OrganizationMember.OrganizationID |
| 66 | +}) |
| 67 | +require.ElementsMatch(t,expected,foundIDs,"match user organizations") |
| 68 | +} |
| 69 | + |
| 70 | +entitled:=entitlements.New() |
| 71 | +entitled.Update(func(entitlements*codersdk.Entitlements) { |
| 72 | +entitlements.Features[codersdk.FeatureMultipleOrganizations]= codersdk.Feature{ |
| 73 | +Entitlement:codersdk.EntitlementEntitled, |
| 74 | +Enabled:true, |
| 75 | +Limit:nil, |
| 76 | +Actual:nil, |
| 77 | +} |
| 78 | +}) |
| 79 | + |
| 80 | +testCases:= []struct { |
| 81 | +Namestring |
| 82 | +Casefunc(t*testing.T,db database.Store)OrganizationSyncTestCase |
| 83 | +}{ |
| 84 | +{ |
| 85 | +Name:"SingleOrgDeployment", |
| 86 | +Case:func(t*testing.T,db database.Store)OrganizationSyncTestCase { |
| 87 | +def,_:=db.GetDefaultOrganization(context.Background()) |
| 88 | +other:=dbgen.Organization(t,db, database.Organization{}) |
| 89 | +returnOrganizationSyncTestCase{ |
| 90 | +Entitlements:entitled, |
| 91 | +Settings: idpsync.SyncSettings{ |
| 92 | +OrganizationField:"", |
| 93 | +OrganizationMapping:nil, |
| 94 | +OrganizationAssignDefault:true, |
| 95 | +}, |
| 96 | +Exps: []Expectations{ |
| 97 | +{ |
| 98 | +Name:"NoOrganizations", |
| 99 | +Claims: jwt.MapClaims{}, |
| 100 | +ExpectedParams: idpsync.OrganizationParams{ |
| 101 | +SyncEnabled:false, |
| 102 | +IncludeDefault:true, |
| 103 | +Organizations: []uuid.UUID{}, |
| 104 | +}, |
| 105 | +Sync:ExpectedUser{ |
| 106 | +Organizations: []uuid.UUID{}, |
| 107 | +}, |
| 108 | +}, |
| 109 | +{ |
| 110 | +Name:"AlreadyInOrgs", |
| 111 | +Claims: jwt.MapClaims{}, |
| 112 | +ExpectedParams: idpsync.OrganizationParams{ |
| 113 | +SyncEnabled:false, |
| 114 | +IncludeDefault:true, |
| 115 | +Organizations: []uuid.UUID{}, |
| 116 | +}, |
| 117 | +Mutate:func(t*testing.T,db database.Store,user database.User) { |
| 118 | +dbgen.OrganizationMember(t,db, database.OrganizationMember{ |
| 119 | +UserID:user.ID, |
| 120 | +OrganizationID:def.ID, |
| 121 | +}) |
| 122 | +dbgen.OrganizationMember(t,db, database.OrganizationMember{ |
| 123 | +UserID:user.ID, |
| 124 | +OrganizationID:other.ID, |
| 125 | +}) |
| 126 | +}, |
| 127 | +Sync:ExpectedUser{ |
| 128 | +Organizations: []uuid.UUID{def.ID,other.ID}, |
| 129 | +}, |
| 130 | +}, |
| 131 | +}, |
| 132 | +} |
| 133 | +}, |
| 134 | +}, |
| 135 | +} |
| 136 | + |
| 137 | +for_,tc:=rangetestCases { |
| 138 | +tc:=tc |
| 139 | +t.Run(tc.Name,func(t*testing.T) { |
| 140 | +t.Parallel() |
| 141 | +ctx:=testutil.Context(t,testutil.WaitMedium) |
| 142 | +logger:=slogtest.Make(t,&slogtest.Options{}) |
| 143 | + |
| 144 | +rdb,_:=dbtestutil.NewDB(t) |
| 145 | +db:=dbauthz.New(rdb,rbac.NewAuthorizer(prometheus.NewRegistry()),logger,coderdtest.AccessControlStorePointer()) |
| 146 | +caseData:=tc.Case(t,rdb) |
| 147 | +ifcaseData.Entitlements==nil { |
| 148 | +caseData.Entitlements=entitlements.New() |
| 149 | +} |
| 150 | + |
| 151 | +// Create a new sync object |
| 152 | +sync:=NewSync(logger,caseData.Entitlements,caseData.Settings) |
| 153 | +for_,exp:=rangecaseData.Exps { |
| 154 | +t.Run(exp.Name,func(t*testing.T) { |
| 155 | +params,httpErr:=sync.ParseOrganizationClaims(ctx,exp.Claims) |
| 156 | +ifexp.ParseError!=nil { |
| 157 | +exp.ParseError(t,httpErr) |
| 158 | +return |
| 159 | +} |
| 160 | + |
| 161 | +require.Equal(t,exp.ExpectedParams.SyncEnabled,params.SyncEnabled,"match enabled") |
| 162 | +require.Equal(t,exp.ExpectedParams.IncludeDefault,params.IncludeDefault,"match include default") |
| 163 | +ifexp.ExpectedParams.Organizations==nil { |
| 164 | +exp.ExpectedParams.Organizations= []uuid.UUID{} |
| 165 | +} |
| 166 | +require.ElementsMatch(t,exp.ExpectedParams.Organizations,params.Organizations,"match organizations") |
| 167 | + |
| 168 | +user:=dbgen.User(t,db, database.User{}) |
| 169 | +ifexp.Mutate!=nil { |
| 170 | +exp.Mutate(t,db,user) |
| 171 | +} |
| 172 | + |
| 173 | +err:=sync.SyncOrganizations(ctx,db,user,params) |
| 174 | +ifexp.Sync.SyncError { |
| 175 | +require.Error(t,err) |
| 176 | +return |
| 177 | +} |
| 178 | +requireUserOrgs(t,db,user,exp.Sync.Organizations) |
| 179 | +}) |
| 180 | +} |
| 181 | +}) |
| 182 | +} |
| 183 | +} |