Skip to content

Commit 0cc8988

Browse files
committed
add tests to ensure credentials are refreshed on expiration
1 parent 7d3cdf6 commit 0cc8988

File tree

1 file changed

+50
-1
lines changed

1 file changed

+50
-1
lines changed

packages/core/src/test/shared/defaultAwsClientBuilderV3.test.ts

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,54 @@ import { Client } from '@aws-sdk/smithy-client'
1919
import { extensionVersion } from '../../shared'
2020
import { assertTelemetry } from '../testUtil'
2121
import { 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

2346
describe('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

Comments
 (0)