Skip to content

Commit

Permalink
test: fix aws auth tests (#3325)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbbeeken authored Jul 19, 2022
1 parent a24132d commit 7613d4b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
"check:ts": "node ./node_modules/typescript/bin/tsc -v && node ./node_modules/typescript/bin/tsc --noEmit",
"check:atlas": "mocha --config test/manual/mocharc.json test/manual/atlas_connectivity.test.js",
"check:adl": "mocha --config test/mocha_mongodb.json test/manual/atlas-data-lake-testing",
"check:aws": "mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.js",
"check:aws": "mocha --config test/mocha_mongodb.json test/integration/auth/mongodb_aws.test.ts",
"check:ocsp": "mocha --config test/manual/mocharc.json test/manual/ocsp_support.test.js",
"check:kerberos": "mocha --config test/manual/mocharc.json test/manual/kerberos.test.js",
"check:tls": "mocha --config test/manual/mocharc.json test/manual/tls_support.test.js",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
'use strict';
const { expect } = require('chai');
const { removeAuthFromConnectionString } = require('../../tools/utils');
const sinon = require('sinon');
const http = require('http');
const { performance } = require('perf_hooks');
const { MongoAWSError } = require('../../../src');
import { expect } from 'chai';
import * as http from 'http';
import { performance } from 'perf_hooks';
import * as sinon from 'sinon';

import { MongoAWSError, MongoClient, MongoServerError } from '../../../src';
import { removeAuthFromConnectionString } from '../../tools/utils';

describe('MONGODB-AWS', function () {
let client: MongoClient;
beforeEach(function () {
const MONGODB_URI = process.env.MONGODB_URI;
if (!MONGODB_URI || MONGODB_URI.indexOf('MONGODB-AWS') === -1) {
Expand All @@ -15,32 +16,39 @@ describe('MONGODB-AWS', function () {
}
});

afterEach(async () => {
await client?.close();
});

it('should not authorize when not authenticated', async function () {
const url = removeAuthFromConnectionString(this.configuration.url());
const client = this.configuration.newClient(url); // strip provided URI of credentials
client = this.configuration.newClient(url); // strip provided URI of credentials

const error = await client
.db('aws')
.command({ ping: 1 })
.collection('aws_test')
.estimatedDocumentCount()
.catch(error => error);

expect(error).to.be.instanceOf(MongoAWSError);
expect(error).to.be.instanceOf(MongoServerError);
expect(error).to.have.property('code', 13);
});

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

const result = await client
.db('aws')
.command({ ping: 1 })
.collection('aws_test')
.estimatedDocumentCount()
.catch(error => error);

expect(result).to.not.be.instanceOf(MongoAWSError);
expect(result).to.have.property('ok', 1);
expect(result).to.not.be.instanceOf(MongoServerError);
expect(result).to.be.a('number');
});

it('should allow empty string in authMechanismProperties.AWS_SESSION_TOKEN to override AWS_SESSION_TOKEN environment variable', function () {
const client = this.configuration.newClient(this.configuration.url(), {
client = this.configuration.newClient(this.configuration.url(), {
authMechanismProperties: { AWS_SESSION_TOKEN: '' }
});
expect(client)
Expand All @@ -56,18 +64,21 @@ describe('MONGODB-AWS', function () {
this.currentTest.skipReason = 'requires an AWS EC2 environment';
this.skip();
}
sinon.stub(http, 'request').callsFake(function () {
arguments[0].hostname = 'www.example.com';
arguments[0].port = 81;
return http.request.wrappedMethod.apply(this, arguments);
sinon.stub(http, 'request').callsFake(function (...args) {
// We pass in a legacy object that has the same properties as a URL
// but it is not an instanceof URL.
expect(args[0]).to.be.an('object');
if (typeof args[0] === 'object') {
args[0].hostname = 'www.example.com';
args[0].port = '81';
}
return http.request.wrappedMethod.apply(this, args);
});
});

afterEach(async () => {
sinon.restore();
if (client) {
await client.close();
}
await client?.close();
});

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

0 comments on commit 7613d4b

Please sign in to comment.