Skip to content

Commit 7613d4b

Browse files
authored
test: fix aws auth tests (#3325)
1 parent a24132d commit 7613d4b

File tree

2 files changed

+34
-23
lines changed

2 files changed

+34
-23
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@
107107
"check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
108108
"check:atlas": "mocha --config test/manual/mocharc.json test/manual/atlas_connectivity.test.js",
109109
"check:adl": "mocha --config test/mocha_mongodb.json test/manual/atlas-data-lake-testing",
110-
"check:aws": "mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.js",
110+
"check:aws": "mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.ts",
111111
"check:ocsp": "mocha --config test/manual/mocharc.json test/manual/ocsp_support.test.js",
112112
"check:kerberos": "mocha --config test/manual/mocharc.json test/manual/kerberos.test.js",
113113
"check:tls": "mocha --config test/manual/mocharc.json test/manual/tls_support.test.js",

test/integration/auth/mongodb_aws.test.js renamed to test/integration/auth/mongodb_aws.test.ts

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
1-
'use strict';
2-
const { expect } = require('chai');
3-
const { removeAuthFromConnectionString } = require('../../tools/utils');
4-
const sinon = require('sinon');
5-
const http = require('http');
6-
const { performance } = require('perf_hooks');
7-
const { MongoAWSError } = require('../../../src');
1+
import { expect } from 'chai';
2+
import * as http from 'http';
3+
import { performance } from 'perf_hooks';
4+
import * as sinon from 'sinon';
5+
6+
import { MongoAWSError, MongoClient, MongoServerError } from '../../../src';
7+
import { removeAuthFromConnectionString } from '../../tools/utils';
88

99
describe('MONGODB-AWS', function () {
10+
let client: MongoClient;
1011
beforeEach(function () {
1112
const MONGODB_URI = process.env.MONGODB_URI;
1213
if (!MONGODB_URI || MONGODB_URI.indexOf('MONGODB-AWS') === -1) {
@@ -15,32 +16,39 @@ describe('MONGODB-AWS', function () {
1516
}
1617
});
1718

19+
afterEach(async () => {
20+
await client?.close();
21+
});
22+
1823
it('should not authorize when not authenticated', async function () {
1924
const url = removeAuthFromConnectionString(this.configuration.url());
20-
const client = this.configuration.newClient(url); // strip provided URI of credentials
25+
client = this.configuration.newClient(url); // strip provided URI of credentials
2126

2227
const error = await client
2328
.db('aws')
24-
.command({ ping: 1 })
29+
.collection('aws_test')
30+
.estimatedDocumentCount()
2531
.catch(error => error);
2632

27-
expect(error).to.be.instanceOf(MongoAWSError);
33+
expect(error).to.be.instanceOf(MongoServerError);
34+
expect(error).to.have.property('code', 13);
2835
});
2936

3037
it('should authorize when successfully authenticated', async function () {
31-
const client = this.configuration.newClient(process.env.MONGODB_URI); // use the URI built by the test environment
38+
client = this.configuration.newClient(process.env.MONGODB_URI); // use the URI built by the test environment
3239

3340
const result = await client
3441
.db('aws')
35-
.command({ ping: 1 })
42+
.collection('aws_test')
43+
.estimatedDocumentCount()
3644
.catch(error => error);
3745

38-
expect(result).to.not.be.instanceOf(MongoAWSError);
39-
expect(result).to.have.property('ok', 1);
46+
expect(result).to.not.be.instanceOf(MongoServerError);
47+
expect(result).to.be.a('number');
4048
});
4149

4250
it('should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable', function () {
43-
const client = this.configuration.newClient(this.configuration.url(), {
51+
client = this.configuration.newClient(this.configuration.url(), {
4452
authMechanismProperties: { AWS_SESSION_TOKEN: '' }
4553
});
4654
expect(client)
@@ -56,18 +64,21 @@ describe('MONGODB-AWS', function () {
5664
this.currentTest.skipReason = 'requires an AWS EC2 environment';
5765
this.skip();
5866
}
59-
sinon.stub(http, 'request').callsFake(function () {
60-
arguments[0].hostname = 'www.example.com';
61-
arguments[0].port = 81;
62-
return http.request.wrappedMethod.apply(this, arguments);
67+
sinon.stub(http, 'request').callsFake(function (...args) {
68+
// We pass in a legacy object that has the same properties as a URL
69+
// but it is not an instanceof URL.
70+
expect(args[0]).to.be.an('object');
71+
if (typeof args[0] === 'object') {
72+
args[0].hostname = 'www.example.com';
73+
args[0].port = '81';
74+
}
75+
return http.request.wrappedMethod.apply(this, args);
6376
});
6477
});
6578

6679
afterEach(async () => {
6780
sinon.restore();
68-
if (client) {
69-
await client.close();
70-
}
81+
await client?.close();
7182
});
7283

7384
it('should respect the default timeout of 10000ms', async function () {

0 commit comments

Comments
 (0)