2
2
// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
3
3
4
4
import { ClaimsService } from "./ClaimsService" ;
5
- import type { OidcClientSettingsStore } from "./OidcClientSettings" ;
5
+ import { OidcClientSettingsStore } from "./OidcClientSettings" ;
6
6
import type { UserProfile } from "./User" ;
7
7
8
8
describe ( "ClaimsService" , ( ) => {
9
9
let settings : OidcClientSettingsStore ;
10
10
let subject : ClaimsService ;
11
11
12
12
beforeEach ( ( ) => {
13
- settings = {
14
- authority : "op " ,
15
- client_id : "client " ,
16
- loadUserInfo : true ,
17
- } as OidcClientSettingsStore ;
13
+ settings = new OidcClientSettingsStore ( {
14
+ authority : "authority " ,
15
+ client_id : "client_id " ,
16
+ redirect_uri : "redirect_uri" ,
17
+ } ) ;
18
18
19
19
subject = new ClaimsService ( settings ) ;
20
20
} ) ;
@@ -218,7 +218,7 @@ describe("ClaimsService", () => {
218
218
expect ( result ) . toEqual ( { a : "apple" , c : "carrot" , b : "banana" } ) ;
219
219
} ) ;
220
220
221
- it ( "should not merge claims when claim types are objects" , ( ) => {
221
+ it ( "should merge claims when claim types are objects" , ( ) => {
222
222
// arrange
223
223
const c1 = {
224
224
custom : { apple : "foo" , pear : "bar" } ,
@@ -233,87 +233,86 @@ describe("ClaimsService", () => {
233
233
234
234
// assert
235
235
expect ( result ) . toEqual ( {
236
- custom : [
237
- { apple : "foo" , pear : "bar" } ,
238
- { apple : "foo" , orange : "peel" } ,
239
- ] ,
236
+ custom : { apple : "foo" , pear : "bar" , orange : "peel" } ,
240
237
b : "banana" ,
241
238
} ) ;
242
239
} ) ;
243
240
244
- it ( "should merge claims when claim types are objects when mergeClaims settings is true " , ( ) => {
241
+ it ( "should replace same claim types" , ( ) => {
245
242
// arrange
246
- Object . assign ( settings , { mergeClaims : true } ) ;
247
-
248
- const c1 = {
249
- custom : { apple : "foo" , pear : "bar" } ,
250
- } as unknown as UserProfile ;
251
- const c2 = {
252
- custom : { apple : "foo" , orange : "peel" } ,
253
- b : "banana" ,
254
- } ;
243
+ const c1 = { a : "apple" , b : "banana" } as unknown as UserProfile ;
244
+ const c2 = { a : "carrot" } ;
255
245
256
246
// act
257
247
const result = subject . mergeClaims ( c1 , c2 ) ;
258
248
259
249
// assert
260
- expect ( result ) . toEqual ( {
261
- custom : { apple : "foo" , pear : "bar" , orange : "peel" } ,
262
- b : "banana" ,
263
- } ) ;
250
+ expect ( result ) . toEqual ( { a : "carrot" , b : "banana" } ) ;
264
251
} ) ;
265
252
266
- it ( "should merge same claim types into array " , ( ) => {
253
+ it ( "should remove duplicates when producing arrays " , ( ) => {
267
254
// arrange
268
255
const c1 = { a : "apple" , b : "banana" } as unknown as UserProfile ;
269
- const c2 = { a : "carrot" } ;
256
+ const c2 = { a : [ "apple" , "durian" ] } ;
270
257
271
258
// act
272
259
const result = subject . mergeClaims ( c1 , c2 ) ;
273
260
274
261
// assert
275
- expect ( result ) . toEqual ( { a : [ "apple" , "carrot " ] , b : "banana" } ) ;
262
+ expect ( result ) . toEqual ( { a : [ "apple" , "durian " ] , b : "banana" } ) ;
276
263
} ) ;
277
264
278
- it ( "should merge arrays of same claim types into array" , ( ) => {
265
+ it ( "should merge arrays of same claim types into array (string vs. array) when mergeClaimsStrategy is 'merge' " , ( ) => {
279
266
// arrange
280
- const c1 = { a : "apple" , b : "banana" } as unknown as UserProfile ;
281
- const c2 = { a : [ "carrot" , "durian" ] } ;
267
+ Object . assign ( settings , { mergeClaimsStrategy : "merge" } ) ;
268
+ const c1 = {
269
+ a : "apple" ,
270
+ b : "banana" ,
271
+ } as unknown as UserProfile ;
272
+ const c2 = {
273
+ a : [ "carrot" , "durian" ] ,
274
+ } ;
282
275
283
276
// act
284
- let result = subject . mergeClaims ( c1 , c2 ) ;
277
+ const result = subject . mergeClaims ( c1 , c2 ) ;
285
278
286
279
// assert
287
280
expect ( result ) . toEqual ( {
288
281
a : [ "apple" , "carrot" , "durian" ] ,
289
282
b : "banana" ,
290
283
} ) ;
284
+ } ) ;
291
285
286
+ it ( "should merge arrays of same claim types into array (array vs. array) when mergeClaimsStrategy is 'merge'" , ( ) => {
292
287
// arrange
288
+ Object . assign ( settings , { mergeClaimsStrategy : "merge" } ) ;
293
289
const d1 = {
294
290
a : [ "apple" , "carrot" ] ,
295
291
b : "banana" ,
296
292
} as unknown as UserProfile ;
297
293
const d2 = { a : [ "durian" ] } ;
298
294
299
295
// act
300
- result = subject . mergeClaims ( d1 , d2 ) ;
296
+ const result = subject . mergeClaims ( d1 , d2 ) ;
301
297
302
298
// assert
303
299
expect ( result ) . toEqual ( {
304
300
a : [ "apple" , "carrot" , "durian" ] ,
305
301
b : "banana" ,
306
302
} ) ;
303
+ } ) ;
307
304
305
+ it ( "should merge arrays of same claim types into array (array vs. string) when mergeClaimsStrategy is 'merge'" , ( ) => {
308
306
// arrange
307
+ Object . assign ( settings , { mergeClaimsStrategy : "merge" } ) ;
309
308
const e1 = {
310
309
a : [ "apple" , "carrot" ] ,
311
310
b : "banana" ,
312
311
} as unknown as UserProfile ;
313
312
const e2 = { a : "durian" } ;
314
313
315
314
// act
316
- result = subject . mergeClaims ( e1 , e2 ) ;
315
+ const result = subject . mergeClaims ( e1 , e2 ) ;
317
316
318
317
// assert
319
318
expect ( result ) . toEqual ( {
@@ -322,31 +321,51 @@ describe("ClaimsService", () => {
322
321
} ) ;
323
322
} ) ;
324
323
325
- it ( "should remove duplicates when producing arrays" , ( ) => {
326
- // arrange
327
- const c1 = { a : "apple" , b : "banana" } as unknown as UserProfile ;
328
- const c2 = { a : [ "apple" , "durian" ] } ;
324
+ it ( "should replace if type is different (array vs. string)" , ( ) => {
325
+ // arrange
326
+ const c1 = {
327
+ a : [ "apple" , "durian" ] ,
328
+ b : "banana" ,
329
+ } as unknown as UserProfile ;
330
+ const c2 = { a : "apple" } ;
329
331
330
332
// act
331
333
const result = subject . mergeClaims ( c1 , c2 ) ;
332
334
333
335
// assert
334
- expect ( result ) . toEqual ( { a : [ "apple" , "durian" ] , b : "banana" } ) ;
336
+ expect ( result ) . toEqual ( { a : "apple" , b : "banana" } ) ;
335
337
} ) ;
336
338
337
- it ( "should not add if already present in array " , ( ) => {
338
- // arrange
339
+ it ( "should replace if type is different (object vs. string) " , ( ) => {
340
+ // arrange
339
341
const c1 = {
340
- a : [ "apple" , "durian" ] ,
342
+ custom : { a : "apple" } ,
341
343
b : "banana" ,
342
344
} as unknown as UserProfile ;
343
- const c2 = { a : "apple" } ;
345
+ const c2 = { custom : "apple" } ;
344
346
345
347
// act
346
348
const result = subject . mergeClaims ( c1 , c2 ) ;
347
349
348
350
// assert
349
- expect ( result ) . toEqual ( { a : [ "apple" , "durian" ] , b : "banana" } ) ;
351
+ expect ( result ) . toEqual ( { custom : "apple" , b : "banana" } ) ;
352
+ } ) ;
353
+
354
+ it ( "should replace if type is different (array vs. object)" , ( ) => {
355
+ // arrange
356
+ const c1 = {
357
+ custom : [ "apple" , "durian" ] ,
358
+ b : "banana" ,
359
+ } as unknown as UserProfile ;
360
+ const c2 = {
361
+ custom : { a : "apple" } ,
362
+ } as unknown as UserProfile ;
363
+
364
+ // act
365
+ const result = subject . mergeClaims ( c1 , c2 ) ;
366
+
367
+ // assert
368
+ expect ( result ) . toEqual ( { custom : { a : "apple" } , b : "banana" } ) ;
350
369
} ) ;
351
370
} ) ;
352
371
} ) ;
0 commit comments