|
1 | 1 | 'use strict';
|
2 | 2 | const { expect } = require('chai');
|
3 | 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'); |
4 | 8 |
|
5 | 9 | describe('MONGODB-AWS', function () {
|
6 | 10 | beforeEach(function () {
|
@@ -52,4 +56,46 @@ describe('MONGODB-AWS', function () {
|
52 | 56 | .to.have.nested.property('options.credentials.mechanismProperties.AWS_SESSION_TOKEN')
|
53 | 57 | .that.equals('');
|
54 | 58 | });
|
| 59 | + |
| 60 | + describe('EC2 with missing credentials', () => { |
| 61 | + let client; |
| 62 | + |
| 63 | + beforeEach(function () { |
| 64 | + if (!process.env.IS_EC2) { |
| 65 | + this.currentTest.skipReason = 'requires an AWS EC2 environment'; |
| 66 | + this.skip(); |
| 67 | + } |
| 68 | + sinon.stub(http, 'request').callsFake(function () { |
| 69 | + arguments[0].hostname = 'www.example.com'; |
| 70 | + arguments[0].port = 81; |
| 71 | + return http.request.wrappedMethod.apply(this, arguments); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + afterEach(async () => { |
| 76 | + sinon.restore(); |
| 77 | + if (client) { |
| 78 | + await client.close(); |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + it('should respect the default timeout of 10000ms', async function () { |
| 83 | + const config = this.configuration; |
| 84 | + client = config.newClient(process.env.MONGODB_URI, { authMechanism: 'MONGODB-AWS' }); // use the URI built by the test environment |
| 85 | + const startTime = performance.now(); |
| 86 | + |
| 87 | + let caughtError = null; |
| 88 | + await client.connect().catch(err => { |
| 89 | + caughtError = err; |
| 90 | + }); |
| 91 | + |
| 92 | + const endTime = performance.now(); |
| 93 | + const timeTaken = endTime - startTime; |
| 94 | + expect(caughtError).to.be.instanceOf(MongoAWSError); |
| 95 | + expect(caughtError) |
| 96 | + .property('message') |
| 97 | + .match(/timed out after/); |
| 98 | + expect(timeTaken).to.be.within(10000, 12000); |
| 99 | + }); |
| 100 | + }); |
55 | 101 | });
|
0 commit comments