Skip to content

Commit 7c1961f

Browse files
fix: fle kms credential fetching tests
1 parent fb6a4bc commit 7c1961f

File tree

1 file changed

+49
-24
lines changed

1 file changed

+49
-24
lines changed

test/unit/client-side-encryption/providers/credentialsProvider.test.ts

Lines changed: 49 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@ import {
1010
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
1111
import {
1212
isEmptyCredentials,
13-
type KMSProviders,
14-
refreshKMSCredentials
13+
KMSCredentialProvider,
14+
type KMSProviders
1515
} from '../../../../src/client-side-encryption/providers';
1616
// eslint-disable-next-line @typescript-eslint/no-restricted-imports
1717
import {
@@ -85,7 +85,7 @@ describe('#refreshKMSCredentials', function () {
8585
});
8686

8787
it('refreshes the aws credentials', async function () {
88-
const providers = await refreshKMSCredentials(kmsProviders);
88+
const providers = await new KMSCredentialProvider(kmsProviders).refreshCredentials();
8989
expect(providers).to.deep.equal({
9090
aws: {
9191
accessKeyId: accessKey,
@@ -114,7 +114,7 @@ describe('#refreshKMSCredentials', function () {
114114
});
115115

116116
it('refreshes only the aws credentials', async function () {
117-
const providers = await refreshKMSCredentials(kmsProviders);
117+
const providers = await new KMSCredentialProvider(kmsProviders).refreshCredentials();
118118
expect(providers).to.deep.equal({
119119
local: {
120120
key: Buffer.alloc(96)
@@ -147,7 +147,7 @@ describe('#refreshKMSCredentials', function () {
147147
});
148148

149149
it('does not refresh credentials', async function () {
150-
const providers = await refreshKMSCredentials(kmsProviders);
150+
const providers = await new KMSCredentialProvider(kmsProviders).refreshCredentials();
151151
expect(providers).to.deep.equal(kmsProviders);
152152
});
153153
});
@@ -171,7 +171,7 @@ describe('#refreshKMSCredentials', function () {
171171
});
172172

173173
it('does not refresh credentials', async function () {
174-
const providers = await refreshKMSCredentials(kmsProviders);
174+
const providers = await new KMSCredentialProvider(kmsProviders).refreshCredentials();
175175
expect(providers).to.deep.equal(kmsProviders);
176176
});
177177
});
@@ -222,7 +222,7 @@ describe('#refreshKMSCredentials', function () {
222222
const kmsProviders = { gcp: {} };
223223

224224
it('refreshes the gcp credentials', async function () {
225-
const providers = await refreshKMSCredentials(kmsProviders);
225+
const providers = await new KMSCredentialProvider(kmsProviders).refreshCredentials();
226226
expect(providers).to.deep.equal({
227227
gcp: {
228228
accessToken: 'abc'
@@ -238,7 +238,9 @@ describe('#refreshKMSCredentials', function () {
238238
const kmsProviders = { gcp: {} };
239239

240240
it('surfaces error from server', async function () {
241-
const error = await refreshKMSCredentials(kmsProviders).catch(error => error);
241+
const error = await new KMSCredentialProvider(kmsProviders)
242+
.refreshCredentials()
243+
.catch(error => error);
242244
expect(error).to.be.instanceOf(Error);
243245
});
244246
});
@@ -258,7 +260,7 @@ describe('#refreshKMSCredentials', function () {
258260
const kmsProviders = { gcp: {} };
259261

260262
it('does not modify the gcp credentials', async function () {
261-
const providers = await refreshKMSCredentials(kmsProviders);
263+
const providers = await new KMSCredentialProvider(kmsProviders).refreshCredentials();
262264
expect(providers).to.deep.equal({ gcp: {} });
263265
});
264266
});
@@ -358,7 +360,7 @@ describe('#refreshKMSCredentials', function () {
358360
httpSpy = sinon.stub(utils, 'get');
359361
httpSpy.resolves(mockResponse);
360362

361-
await refreshKMSCredentials({ azure: {} });
363+
await new KMSCredentialProvider({ azure: {} }).refreshCredentials();
362364
});
363365

364366
it('sets the `api-version` param to 2012-02-01', () => {
@@ -441,7 +443,9 @@ describe('#refreshKMSCredentials', function () {
441443
});
442444

443445
it('throws a MongoCryptKMSRequestError', async () => {
444-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
446+
const error = await new KMSCredentialProvider({ azure: {} })
447+
.refreshCredentials()
448+
.catch(e => e);
445449
expect(error).to.be.instanceOf(MongoCryptAzureKMSRequestError);
446450
});
447451
});
@@ -453,7 +457,9 @@ describe('#refreshKMSCredentials', function () {
453457
});
454458

455459
it('throws a MongoCryptKMSRequestError', async () => {
456-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
460+
const error = await new KMSCredentialProvider({ azure: {} })
461+
.refreshCredentials()
462+
.catch(e => e);
457463
expect(error).to.be.instanceOf(MongoCryptAzureKMSRequestError);
458464
expect(error).to.match(/Malformed JSON body in GET request/);
459465
});
@@ -465,7 +471,9 @@ describe('#refreshKMSCredentials', function () {
465471
});
466472

467473
it('throws a MongoCryptKMSRequestError', async () => {
468-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
474+
const error = await new KMSCredentialProvider({ azure: {} })
475+
.refreshCredentials()
476+
.catch(e => e);
469477
expect(error).to.be.instanceOf(MongoCryptAzureKMSRequestError);
470478
expect(error).to.match(/Malformed JSON body in GET request/);
471479
});
@@ -479,12 +487,16 @@ describe('#refreshKMSCredentials', function () {
479487
});
480488

481489
it('throws a MongoCryptKMSRequestError', async () => {
482-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
490+
const error = await new KMSCredentialProvider({ azure: {} })
491+
.refreshCredentials()
492+
.catch(e => e);
483493
expect(error).to.be.instanceOf(MongoCryptAzureKMSRequestError);
484494
});
485495

486496
it('attaches the body to the error', async () => {
487-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
497+
const error = await new KMSCredentialProvider({ azure: {} })
498+
.refreshCredentials()
499+
.catch(e => e);
488500
expect(error).to.have.property('body').to.deep.equal({ error: 'something went wrong' });
489501
});
490502
});
@@ -497,7 +509,9 @@ describe('#refreshKMSCredentials', function () {
497509
});
498510

499511
it('throws a MongoCryptKMSRequestError', async () => {
500-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
512+
const error = await new KMSCredentialProvider({ azure: {} })
513+
.refreshCredentials()
514+
.catch(e => e);
501515
expect(error).to.be.instanceOf(MongoCryptAzureKMSRequestError);
502516
expect(error).to.match(/Malformed JSON body in GET request/);
503517
});
@@ -509,7 +523,9 @@ describe('#refreshKMSCredentials', function () {
509523
});
510524

511525
it('throws a MongoCryptKMSRequestError', async () => {
512-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
526+
const error = await new KMSCredentialProvider({ azure: {} })
527+
.refreshCredentials()
528+
.catch(e => e);
513529
expect(error).to.be.instanceOf(MongoCryptAzureKMSRequestError);
514530
expect(error).to.match(/Malformed JSON body in GET request/);
515531
});
@@ -521,7 +537,9 @@ describe('#refreshKMSCredentials', function () {
521537
});
522538

523539
it('throws a MongoCryptKMSRequestError', async () => {
524-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
540+
const error = await new KMSCredentialProvider({ azure: {} })
541+
.refreshCredentials()
542+
.catch(e => e);
525543
expect(error).to.be.instanceOf(MongoCryptAzureKMSRequestError);
526544
expect(error).to.match(/missing field `access_token/);
527545
});
@@ -533,7 +551,9 @@ describe('#refreshKMSCredentials', function () {
533551
});
534552

535553
it('throws a MongoCryptKMSRequestError', async () => {
536-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
554+
const error = await new KMSCredentialProvider({ azure: {} })
555+
.refreshCredentials()
556+
.catch(e => e);
537557
expect(error).to.be.instanceOf(MongoCryptAzureKMSRequestError);
538558
expect(error).to.match(/missing field `expires_in/);
539559
});
@@ -548,7 +568,9 @@ describe('#refreshKMSCredentials', function () {
548568
});
549569

550570
it('throws a MongoCryptKMSRequestError', async () => {
551-
const error = await refreshKMSCredentials({ azure: {} }).catch(e => e);
571+
const error = await new KMSCredentialProvider({ azure: {} })
572+
.refreshCredentials()
573+
.catch(e => e);
552574
expect(error).to.be.instanceOf(MongoCryptAzureKMSRequestError);
553575
expect(error).to.match(/unable to parse int from `expires_in` field/);
554576
});
@@ -557,13 +579,16 @@ describe('#refreshKMSCredentials', function () {
557579

558580
context('when a valid token was returned', () => {
559581
beforeEach(() => {
560-
sinon
561-
.stub(utils, 'get')
562-
.resolves({ status: 200, body: '{ "access_token": "token", "expires_in": "10000" }' });
582+
sinon.stub(utils, 'get').resolves({
583+
status: 200,
584+
body: '{ "access_token": "token", "expires_in": "10000" }'
585+
});
563586
});
564587

565588
it('returns the token in the `azure` field of the kms providers', async () => {
566-
const kmsProviders = await refreshKMSCredentials({ azure: {} });
589+
const kmsProviders = await new KMSCredentialProvider({
590+
azure: {}
591+
}).refreshCredentials();
567592
const azure = kmsProviders.azure;
568593
expect(azure).to.have.property('accessToken', 'token');
569594
});

0 commit comments

Comments
 (0)