@@ -132,6 +132,92 @@ func TestOrganizationSync(t *testing.T) {
132
132
}
133
133
},
134
134
},
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
+ },
135
221
}
136
222
137
223
for _ ,tc := range testCases {
@@ -157,6 +243,7 @@ func TestOrganizationSync(t *testing.T) {
157
243
exp .ParseError (t ,httpErr )
158
244
return
159
245
}
246
+ require .Nil (t ,httpErr ,"no parse error" )
160
247
161
248
require .Equal (t ,exp .ExpectedParams .SyncEnabled ,params .SyncEnabled ,"match enabled" )
162
249
require .Equal (t ,exp .ExpectedParams .IncludeDefault ,params .IncludeDefault ,"match include default" )
@@ -167,14 +254,15 @@ func TestOrganizationSync(t *testing.T) {
167
254
168
255
user := dbgen .User (t ,db , database.User {})
169
256
if exp .Mutate != nil {
170
- exp .Mutate (t ,db ,user )
257
+ exp .Mutate (t ,rdb ,user )
171
258
}
172
259
173
- err := sync .SyncOrganizations (ctx ,db ,user ,params )
260
+ err := sync .SyncOrganizations (ctx ,rdb ,user ,params )
174
261
if exp .Sync .SyncError {
175
262
require .Error (t ,err )
176
263
return
177
264
}
265
+ require .NoError (t ,err )
178
266
requireUserOrgs (t ,db ,user ,exp .Sync .Organizations )
179
267
})
180
268
}