@@ -19,12 +19,54 @@ import { Client } from '@aws-sdk/smithy-client'
1919import { extensionVersion } from '../../shared'
2020import { assertTelemetry } from '../testUtil'
2121import { telemetry } from '../../shared/telemetry'
22+ import { CredentialsShim } from '../../auth/deprecated/loginManager'
23+ import { Credentials } from '@aws-sdk/types'
24+
25+ class MockCredentialsShim implements CredentialsShim {
26+ public constructor (
27+ public credentials : Credentials ,
28+ public readonly refreshedCredentials : Credentials
29+ ) { }
30+
31+ public expire ( ) : void {
32+ this . credentials = {
33+ ...this . credentials ,
34+ expiration : new Date ( Date . now ( ) - 1000 * 60 * 60 * 24 ) ,
35+ }
36+ }
37+ public async get ( ) : Promise < Credentials > {
38+ return this . credentials
39+ }
40+
41+ public async refresh ( ) : Promise < Credentials > {
42+ return this . refreshedCredentials
43+ }
44+ }
2245
2346describe ( 'DefaultAwsClientBuilderV3' , function ( ) {
2447 let builder : AWSClientBuilderV3
48+ let fakeContext : FakeAwsContext
49+ let mockCredsShim : MockCredentialsShim
50+ let oldCreds : Credentials
51+ let newCreds : Credentials
2552
2653 beforeEach ( function ( ) {
27- builder = new DefaultAWSClientBuilderV3 ( new FakeAwsContext ( ) )
54+ fakeContext = new FakeAwsContext ( )
55+ oldCreds = {
56+ accessKeyId : 'old' ,
57+ secretAccessKey : 'old' ,
58+ sessionToken : 'old' ,
59+ expiration : new Date ( Date . now ( ) + 1000 * 60 * 60 * 24 ) ,
60+ }
61+ newCreds = {
62+ accessKeyId : 'new' ,
63+ secretAccessKey : 'new' ,
64+ sessionToken : 'new' ,
65+ expiration : new Date ( Date . now ( ) + 1000 * 60 * 60 * 24 * 2 ) ,
66+ }
67+ mockCredsShim = new MockCredentialsShim ( oldCreds , newCreds )
68+ fakeContext . credentialsShim = mockCredsShim
69+ builder = new DefaultAWSClientBuilderV3 ( fakeContext )
2870 } )
2971
3072 describe ( 'createAndConfigureSdkClient' , function ( ) {
@@ -61,6 +103,13 @@ describe('DefaultAwsClientBuilderV3', function () {
61103
62104 assert . strictEqual ( service . config . customUserAgent [ 0 ] [ 0 ] , 'CUSTOM USER AGENT' )
63105 } )
106+
107+ it ( 'refreshes credentials when they expire' , async function ( ) {
108+ const service = await builder . createAwsService ( Client as any )
109+ assert . strictEqual ( await service . config . credentials ( ) , oldCreds )
110+ mockCredsShim . expire ( )
111+ assert . strictEqual ( await service . config . credentials ( ) , newCreds )
112+ } )
64113 } )
65114} )
66115
0 commit comments