Skip to content

Commit a6aa30a

Browse files
committed
test: add tests using credentials shim
1 parent 5c6f68a commit a6aa30a

File tree

1 file changed

+74
-0
lines changed

1 file changed

+74
-0
lines changed

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

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import { Client } from '@aws-sdk/smithy-client'
1414
import { extensionVersion } from '../../shared'
1515
import { assertTelemetry } from '../testUtil'
1616
import { telemetry } from '../../shared/telemetry'
17+
import { CredentialsShim } from '../../auth/deprecated/loginManager'
18+
import { Credentials } from '@aws-sdk/types'
1719

1820
describe('AwsClientBuilderV3', function () {
1921
let builder: AWSClientBuilderV3
@@ -57,6 +59,52 @@ describe('AwsClientBuilderV3', function () {
5759
assert.strictEqual(service.config.userAgent[0][0], 'CUSTOM USER AGENT')
5860
})
5961
})
62+
63+
describe('clientCredentials', function () {
64+
let fakeContext: FakeAwsContext
65+
let mockCredsShim: MockCredentialsShim
66+
let oldCreds: Credentials
67+
let newCreds: Credentials
68+
69+
beforeEach(function () {
70+
fakeContext = new FakeAwsContext()
71+
oldCreds = {
72+
accessKeyId: 'old',
73+
secretAccessKey: 'old',
74+
sessionToken: 'old',
75+
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24),
76+
}
77+
newCreds = {
78+
accessKeyId: 'new',
79+
secretAccessKey: 'new',
80+
sessionToken: 'new',
81+
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24 * 2),
82+
}
83+
mockCredsShim = new MockCredentialsShim(oldCreds, newCreds)
84+
fakeContext.credentialsShim = mockCredsShim
85+
builder = new AWSClientBuilderV3(fakeContext)
86+
})
87+
88+
it('refreshes credentials when they expire', async function () {
89+
const service = await builder.createAwsService(Client)
90+
assert.strictEqual(await service.config.credentials(), oldCreds)
91+
mockCredsShim.expire()
92+
assert.strictEqual(await service.config.credentials(), newCreds)
93+
})
94+
95+
it('does not cache stale credentials', async function () {
96+
const service = await builder.createAwsService(Client)
97+
assert.strictEqual(await service.config.credentials(), oldCreds)
98+
const newerCreds = {
99+
accessKeyId: 'old2',
100+
secretAccessKey: 'old2',
101+
sessionToken: 'old2',
102+
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24),
103+
}
104+
mockCredsShim.update(newerCreds)
105+
assert.strictEqual(await service.config.credentials(), newerCreds)
106+
})
107+
})
60108
})
61109

62110
describe('getServiceId', function () {
@@ -77,3 +125,29 @@ describe('recordErrorTelemetry', function () {
77125
assertTelemetry('vscode_executeCommand', { requestServiceType: 'aws-service' })
78126
})
79127
})
128+
129+
class MockCredentialsShim implements CredentialsShim {
130+
public constructor(
131+
public credentials: Credentials,
132+
public readonly refreshedCredentials: Credentials
133+
) {}
134+
135+
public expire(): void {
136+
this.credentials = {
137+
...this.credentials,
138+
expiration: new Date(Date.now() - 1000 * 60 * 60 * 24),
139+
}
140+
}
141+
142+
public update(newCreds: Credentials): void {
143+
this.credentials = newCreds
144+
}
145+
146+
public async get(): Promise<Credentials> {
147+
return this.credentials
148+
}
149+
150+
public async refresh(): Promise<Credentials> {
151+
return this.refreshedCredentials
152+
}
153+
}

0 commit comments

Comments
 (0)