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' ;
8
8
9
9
describe ( 'MONGODB-AWS' , function ( ) {
10
+ let client : MongoClient ;
10
11
beforeEach ( function ( ) {
11
12
const MONGODB_URI = process . env . MONGODB_URI ;
12
13
if ( ! MONGODB_URI || MONGODB_URI . indexOf ( 'MONGODB-AWS' ) === - 1 ) {
@@ -15,32 +16,39 @@ describe('MONGODB-AWS', function () {
15
16
}
16
17
} ) ;
17
18
19
+ afterEach ( async ( ) => {
20
+ await client ?. close ( ) ;
21
+ } ) ;
22
+
18
23
it ( 'should not authorize when not authenticated' , async function ( ) {
19
24
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
21
26
22
27
const error = await client
23
28
. db ( 'aws' )
24
- . command ( { ping : 1 } )
29
+ . collection ( 'aws_test' )
30
+ . estimatedDocumentCount ( )
25
31
. catch ( error => error ) ;
26
32
27
- expect ( error ) . to . be . instanceOf ( MongoAWSError ) ;
33
+ expect ( error ) . to . be . instanceOf ( MongoServerError ) ;
34
+ expect ( error ) . to . have . property ( 'code' , 13 ) ;
28
35
} ) ;
29
36
30
37
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
32
39
33
40
const result = await client
34
41
. db ( 'aws' )
35
- . command ( { ping : 1 } )
42
+ . collection ( 'aws_test' )
43
+ . estimatedDocumentCount ( )
36
44
. catch ( error => error ) ;
37
45
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' ) ;
40
48
} ) ;
41
49
42
50
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 ( ) , {
44
52
authMechanismProperties : { AWS_SESSION_TOKEN : '' }
45
53
} ) ;
46
54
expect ( client )
@@ -56,18 +64,21 @@ describe('MONGODB-AWS', function () {
56
64
this . currentTest . skipReason = 'requires an AWS EC2 environment' ;
57
65
this . skip ( ) ;
58
66
}
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 ) ;
63
76
} ) ;
64
77
} ) ;
65
78
66
79
afterEach ( async ( ) => {
67
80
sinon . restore ( ) ;
68
- if ( client ) {
69
- await client . close ( ) ;
70
- }
81
+ await client ?. close ( ) ;
71
82
} ) ;
72
83
73
84
it ( 'should respect the default timeout of 10000ms' , async function ( ) {
0 commit comments