22// Licensed under the Apache License, Version 2.0. See LICENSE in the project root for license information.
33
44import { ClaimsService } from "./ClaimsService" ;
5- import type { OidcClientSettingsStore } from "./OidcClientSettings" ;
5+ import { OidcClientSettingsStore } from "./OidcClientSettings" ;
66import type { UserProfile } from "./User" ;
77
88describe ( "ClaimsService" , ( ) => {
99 let settings : OidcClientSettingsStore ;
1010 let subject : ClaimsService ;
1111
1212 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+ } ) ;
1818
1919 subject = new ClaimsService ( settings ) ;
2020 } ) ;
@@ -218,7 +218,7 @@ describe("ClaimsService", () => {
218218 expect ( result ) . toEqual ( { a : "apple" , c : "carrot" , b : "banana" } ) ;
219219 } ) ;
220220
221- it ( "should not merge claims when claim types are objects" , ( ) => {
221+ it ( "should merge claims when claim types are objects" , ( ) => {
222222 // arrange
223223 const c1 = {
224224 custom : { apple : "foo" , pear : "bar" } ,
@@ -233,87 +233,86 @@ describe("ClaimsService", () => {
233233
234234 // assert
235235 expect ( result ) . toEqual ( {
236- custom : [
237- { apple : "foo" , pear : "bar" } ,
238- { apple : "foo" , orange : "peel" } ,
239- ] ,
236+ custom : { apple : "foo" , pear : "bar" , orange : "peel" } ,
240237 b : "banana" ,
241238 } ) ;
242239 } ) ;
243240
244- it ( "should merge claims when claim types are objects when mergeClaims settings is true " , ( ) => {
241+ it ( "should replace same claim types" , ( ) => {
245242 // 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" } ;
255245
256246 // act
257247 const result = subject . mergeClaims ( c1 , c2 ) ;
258248
259249 // 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" } ) ;
264251 } ) ;
265252
266- it ( "should merge same claim types into array " , ( ) => {
253+ it ( "should remove duplicates when producing arrays " , ( ) => {
267254 // arrange
268255 const c1 = { a : "apple" , b : "banana" } as unknown as UserProfile ;
269- const c2 = { a : "carrot" } ;
256+ const c2 = { a : [ "apple" , "durian" ] } ;
270257
271258 // act
272259 const result = subject . mergeClaims ( c1 , c2 ) ;
273260
274261 // assert
275- expect ( result ) . toEqual ( { a : [ "apple" , "carrot " ] , b : "banana" } ) ;
262+ expect ( result ) . toEqual ( { a : [ "apple" , "durian " ] , b : "banana" } ) ;
276263 } ) ;
277264
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' " , ( ) => {
279266 // 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+ } ;
282275
283276 // act
284- let result = subject . mergeClaims ( c1 , c2 ) ;
277+ const result = subject . mergeClaims ( c1 , c2 ) ;
285278
286279 // assert
287280 expect ( result ) . toEqual ( {
288281 a : [ "apple" , "carrot" , "durian" ] ,
289282 b : "banana" ,
290283 } ) ;
284+ } ) ;
291285
286+ it ( "should merge arrays of same claim types into array (array vs. array) when mergeClaimsStrategy is 'merge'" , ( ) => {
292287 // arrange
288+ Object . assign ( settings , { mergeClaimsStrategy : "merge" } ) ;
293289 const d1 = {
294290 a : [ "apple" , "carrot" ] ,
295291 b : "banana" ,
296292 } as unknown as UserProfile ;
297293 const d2 = { a : [ "durian" ] } ;
298294
299295 // act
300- result = subject . mergeClaims ( d1 , d2 ) ;
296+ const result = subject . mergeClaims ( d1 , d2 ) ;
301297
302298 // assert
303299 expect ( result ) . toEqual ( {
304300 a : [ "apple" , "carrot" , "durian" ] ,
305301 b : "banana" ,
306302 } ) ;
303+ } ) ;
307304
305+ it ( "should merge arrays of same claim types into array (array vs. string) when mergeClaimsStrategy is 'merge'" , ( ) => {
308306 // arrange
307+ Object . assign ( settings , { mergeClaimsStrategy : "merge" } ) ;
309308 const e1 = {
310309 a : [ "apple" , "carrot" ] ,
311310 b : "banana" ,
312311 } as unknown as UserProfile ;
313312 const e2 = { a : "durian" } ;
314313
315314 // act
316- result = subject . mergeClaims ( e1 , e2 ) ;
315+ const result = subject . mergeClaims ( e1 , e2 ) ;
317316
318317 // assert
319318 expect ( result ) . toEqual ( {
@@ -322,31 +321,51 @@ describe("ClaimsService", () => {
322321 } ) ;
323322 } ) ;
324323
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" } ;
329331
330332 // act
331333 const result = subject . mergeClaims ( c1 , c2 ) ;
332334
333335 // assert
334- expect ( result ) . toEqual ( { a : [ "apple" , "durian" ] , b : "banana" } ) ;
336+ expect ( result ) . toEqual ( { a : "apple" , b : "banana" } ) ;
335337 } ) ;
336338
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
339341 const c1 = {
340- a : [ "apple" , "durian" ] ,
342+ custom : { a : "apple" } ,
341343 b : "banana" ,
342344 } as unknown as UserProfile ;
343- const c2 = { a : "apple" } ;
345+ const c2 = { custom : "apple" } ;
344346
345347 // act
346348 const result = subject . mergeClaims ( c1 , c2 ) ;
347349
348350 // 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" } ) ;
350369 } ) ;
351370 } ) ;
352371} ) ;
0 commit comments