Skip to content

Commit 540aef3

Browse files
committed
test: count provider calls
1 parent 58cd433 commit 540aef3

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/cmap/auth/mongodb_aws.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,12 @@ interface AWSSaslContinuePayload {
3535

3636
export class MongoDBAWS extends AuthProvider {
3737
private credentialFetcher: AWSTemporaryCredentialProvider;
38+
private credentialProvider?: AWSCredentialProvider;
39+
3840
constructor(credentialProvider?: AWSCredentialProvider) {
3941
super();
4042

43+
this.credentialProvider = credentialProvider;
4144
this.credentialFetcher = AWSTemporaryCredentialProvider.isAWSSDKInstalled
4245
? new AWSSDKCredentialProvider(credentialProvider)
4346
: new LegacyAWSTemporaryCredentialProvider();

test/integration/auth/mongodb_aws.test.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ describe('MONGODB-AWS', function () {
137137
});
138138

139139
context('when user supplies a credentials provider', function () {
140+
let providerCount = 0;
141+
140142
beforeEach(function () {
141143
if (!awsSdkPresent) {
142144
this.skipReason = 'only relevant to AssumeRoleWithWebIdentity with SDK installed';
@@ -147,9 +149,13 @@ describe('MONGODB-AWS', function () {
147149
it('authenticates with a user provided credentials provider', async function () {
148150
// @ts-expect-error We intentionally access a protected variable.
149151
const credentialProvider = AWSTemporaryCredentialProvider.awsSDK;
152+
const provider = async () => {
153+
providerCount++;
154+
return await credentialProvider.fromNodeProviderChain().apply();
155+
};
150156
client = this.configuration.newClient(process.env.MONGODB_URI, {
151157
authMechanismProperties: {
152-
AWS_CREDENTIAL_PROVIDER: credentialProvider.fromNodeProviderChain()
158+
AWS_CREDENTIAL_PROVIDER: provider
153159
}
154160
});
155161

@@ -161,6 +167,13 @@ describe('MONGODB-AWS', function () {
161167

162168
expect(result).to.not.be.instanceOf(MongoServerError);
163169
expect(result).to.be.a('number');
170+
// If we have a username the credentials have been set from the URI, options, or environment
171+
// variables per the auth spec stated order.
172+
if (client.options.credentials.username) {
173+
expect(providerCount).to.equal(0);
174+
} else {
175+
expect(providerCount).to.be.greaterThan(0);
176+
}
164177
});
165178
});
166179

0 commit comments

Comments
 (0)