11import  {  AssumeRoleCommand ,  STSClient  }  from  "@aws-sdk/nested-clients/sts" ; 
2- import  {  beforeEach ,  describe ,  expect ,  test  as  it ,  vi  }  from  "vitest" ; 
2+ import  {  LoadedConfigSelectors  }  from  "@smithy/node-config-provider" ; 
3+ import  type  {  ParsedIniData  }  from  "@smithy/types" ; 
4+ import  {  afterEach ,  beforeEach ,  describe ,  expect ,  test  as  it ,  vi  }  from  "vitest" ; 
35
46import  {  fromTemporaryCredentials  as  fromTemporaryCredentialsNode  }  from  "./fromTemporaryCredentials" ; 
57import  {  fromTemporaryCredentials  }  from  "./fromTemporaryCredentials.base" ; 
@@ -25,6 +27,20 @@ vi.mock("@aws-sdk/nested-clients/sts", () => ({
2527  } ) , 
2628} ) ) ; 
2729
30+ let  iniProfileData : ParsedIniData  =  null  as  any ; 
31+ vi . mock ( import ( "@smithy/node-config-provider" ) ,  async  ( importOriginal )  =>  { 
32+   return  { 
33+     ...( await  importOriginal ( ) ) , 
34+     loadConfig : ( ( 
35+       {  environmentVariableSelector,  configFileSelector,  default : defaultValue  } : LoadedConfigSelectors < any > , 
36+       {  profile =  process . env . AWS_PROFILE  ??  "default"  } : {  profile ?: string  } 
37+     )  =>  { 
38+       return  ( )  => 
39+         environmentVariableSelector ( process . env )  ??  configFileSelector ( iniProfileData [ profile ]  ??  { } )  ??  defaultValue ( ) ; 
40+     } )  as  any , 
41+   } ; 
42+ } ) ; 
43+ 
2844describe ( "fromTemporaryCredentials" ,  ( )  =>  { 
2945  const  RoleArn  =  "ROLE_ARN" ; 
3046  const  RoleSessionName  =  "ROLE_SESSION_NAME" ; 
@@ -33,6 +49,7 @@ describe("fromTemporaryCredentials", () => {
3349    secretAccessKey : "SECRET_ACCESS_KEY" , 
3450  } ; 
3551  const  region  =  "US_BAR_1" ; 
52+   let  processSnapshot : Record < string ,  any > ; 
3653
3754  beforeEach ( ( )  =>  { 
3855    vi . clearAllMocks ( ) ; 
@@ -43,6 +60,21 @@ describe("fromTemporaryCredentials", () => {
4360        SessionToken : "SESSION_TOKEN" , 
4461      } , 
4562    } ) ; 
63+ 
64+     processSnapshot  =  { 
65+       ...process . env , 
66+     } ; 
67+     process . env  =  { } ; 
68+ 
69+     iniProfileData  =  { 
70+       default : { 
71+         region : "us-west-2" , 
72+       } , 
73+     } ; 
74+   } ) ; 
75+ 
76+   afterEach ( ( )  =>  { 
77+     process . env  =  processSnapshot ; 
4678  } ) ; 
4779
4880  it ( "should call STS::AssumeRole API with master credentials" ,  async  ( )  =>  { 
@@ -91,7 +123,7 @@ describe("fromTemporaryCredentials", () => {
91123      credentials : masterCredentials , 
92124      logger : void  0 , 
93125      profile : void  0 , 
94-       region : "us-east-1"  , 
126+       region : "us-west-2"  ,   // profile default 
95127      requestHandler : void  0 , 
96128    } ) ; 
97129    expect ( mockUsePlugin ) . toHaveBeenCalledTimes ( 1 ) ; 
@@ -326,4 +358,79 @@ describe("fromTemporaryCredentials", () => {
326358      expect ( e . tryNextLink ) . toBe ( false ) ; 
327359    } 
328360  } ) ; 
361+ 
362+   describe ( "env configuration" ,  ( )  =>  { 
363+     beforeEach ( ( )  =>  { 
364+       iniProfileData  =  { 
365+         default : { 
366+           region : "us-west-2" , 
367+         } , 
368+         xyz : { 
369+           region : "us-west-1" , 
370+         } , 
371+       } ; 
372+     } ) ; 
373+ 
374+     it ( "should allow region configuration from config file" ,  async  ( )  =>  { 
375+       const  provider  =  fromTemporaryCredentialsNode ( { 
376+         params : { 
377+           RoleArn, 
378+           RoleSessionName, 
379+         } , 
380+         masterCredentials, 
381+       } ) ; 
382+       await  provider ( ) ; 
383+       expect ( vi . mocked ( STSClient  as  any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( { 
384+         region : "us-west-2" , 
385+       } ) ; 
386+     } ) ; 
387+ 
388+     it ( "should allow region configuration from config file non-default profile" ,  async  ( )  =>  { 
389+       // SDK does not use AWS_DEFAULT_PROFILE. 
390+       process . env . AWS_PROFILE  =  "xyz" ; 
391+       const  provider  =  fromTemporaryCredentialsNode ( { 
392+         params : { 
393+           RoleArn, 
394+           RoleSessionName, 
395+         } , 
396+         masterCredentials, 
397+       } ) ; 
398+       await  provider ( ) ; 
399+       expect ( vi . mocked ( STSClient  as  any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( { 
400+         region : "us-west-1" , 
401+       } ) ; 
402+     } ) ; 
403+ 
404+     it ( "should allow region configuration from env" ,  async  ( )  =>  { 
405+       // SDK does not use AWS_DEFAULT_REGION. 
406+       process . env . AWS_REGION  =  "ap-southeast-7" ; 
407+       const  provider  =  fromTemporaryCredentialsNode ( { 
408+         params : { 
409+           RoleArn, 
410+           RoleSessionName, 
411+         } , 
412+         masterCredentials, 
413+       } ) ; 
414+       await  provider ( ) ; 
415+       expect ( vi . mocked ( STSClient  as  any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( { 
416+         region : "ap-southeast-7" , 
417+       } ) ; 
418+     } ) ; 
419+ 
420+     it ( "should allow region configuration from env overriding region in profile" ,  async  ( )  =>  { 
421+       process . env . AWS_PROFILE  =  "xyz" ; 
422+       process . env . AWS_REGION  =  "eu-west-1" ; 
423+       const  provider  =  fromTemporaryCredentialsNode ( { 
424+         params : { 
425+           RoleArn, 
426+           RoleSessionName, 
427+         } , 
428+         masterCredentials, 
429+       } ) ; 
430+       await  provider ( ) ; 
431+       expect ( vi . mocked ( STSClient  as  any ) . mock . calls [ 0 ] [ 0 ] ) . toMatchObject ( { 
432+         region : "eu-west-1" , 
433+       } ) ; 
434+     } ) ; 
435+   } ) ; 
329436} ) ; 
0 commit comments