Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions packages/core/src/test/shared/awsClientBuilderV3.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { Client } from '@aws-sdk/smithy-client'
import { extensionVersion } from '../../shared'
import { assertTelemetry } from '../testUtil'
import { telemetry } from '../../shared/telemetry'
import { CredentialsShim } from '../../auth/deprecated/loginManager'
import { Credentials } from '@aws-sdk/types'

describe('AwsClientBuilderV3', function () {
let builder: AWSClientBuilderV3
Expand Down Expand Up @@ -57,6 +59,52 @@ describe('AwsClientBuilderV3', function () {
assert.strictEqual(service.config.userAgent[0][0], 'CUSTOM USER AGENT')
})
})

describe('clientCredentials', function () {
let fakeContext: FakeAwsContext
let mockCredsShim: MockCredentialsShim
let oldCreds: Credentials
let newCreds: Credentials

beforeEach(function () {
fakeContext = new FakeAwsContext()
oldCreds = {
accessKeyId: 'old',
secretAccessKey: 'old',
sessionToken: 'old',
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use these

// constants for working with milliseconds

}
newCreds = {
accessKeyId: 'new',
secretAccessKey: 'new',
sessionToken: 'new',
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24 * 2),
}
mockCredsShim = new MockCredentialsShim(oldCreds, newCreds)
fakeContext.credentialsShim = mockCredsShim
builder = new AWSClientBuilderV3(fakeContext)
})

it('refreshes credentials when they expire', async function () {
const service = await builder.createAwsService(Client)
assert.strictEqual(await service.config.credentials(), oldCreds)
mockCredsShim.expire()
assert.strictEqual(await service.config.credentials(), newCreds)
})

it('does not cache stale credentials', async function () {
const service = await builder.createAwsService(Client)
assert.strictEqual(await service.config.credentials(), oldCreds)
const newerCreds = {
accessKeyId: 'old2',
secretAccessKey: 'old2',
sessionToken: 'old2',
expiration: new Date(Date.now() + 1000 * 60 * 60 * 24),
}
mockCredsShim.update(newerCreds)
assert.strictEqual(await service.config.credentials(), newerCreds)
})
})
})

describe('getServiceId', function () {
Expand All @@ -77,3 +125,29 @@ describe('recordErrorTelemetry', function () {
assertTelemetry('vscode_executeCommand', { requestServiceType: 'aws-service' })
})
})

class MockCredentialsShim implements CredentialsShim {
public constructor(
public credentials: Credentials,
public readonly refreshedCredentials: Credentials
) {}

public expire(): void {
this.credentials = {
...this.credentials,
expiration: new Date(Date.now() - 1000 * 60 * 60 * 24),
}
}

public update(newCreds: Credentials): void {
this.credentials = newCreds
}

public async get(): Promise<Credentials> {
return this.credentials
}

public async refresh(): Promise<Credentials> {
return this.refreshedCredentials
}
}
Loading