@@ -3,7 +3,7 @@ import chai, { expect } from 'chai';
3
3
import { Collection , Db , MongoClient } from 'mongodb' ;
4
4
import sinonChai from 'sinon-chai' ;
5
5
import sinon , { StubbedInstance , stubInterface } from 'ts-sinon' ;
6
- import CliServiceProvider from './cli-service-provider' ;
6
+ import CliServiceProvider , { connectMongoClient } from './cli-service-provider' ;
7
7
8
8
chai . use ( sinonChai ) ;
9
9
@@ -34,6 +34,98 @@ describe('CliServiceProvider', () => {
34
34
let serviceProvider : CliServiceProvider ;
35
35
let collectionStub : StubbedInstance < Collection > ;
36
36
37
+ describe ( 'connectMongoClient' , ( ) => {
38
+ it ( 'connects once when no AutoEncryption set' , async ( ) => {
39
+ const uri = 'localhost:27017' ;
40
+ const mClientType = stubInterface < typeof MongoClient > ( ) ;
41
+ const mClient = stubInterface < MongoClient > ( ) ;
42
+ mClientType . connect . onFirstCall ( ) . resolves ( mClient ) ;
43
+ const result = await connectMongoClient ( uri , { } , mClientType ) ;
44
+ const calls = mClientType . connect . getCalls ( ) ;
45
+ expect ( calls . length ) . to . equal ( 1 ) ;
46
+ expect ( calls [ 0 ] . args ) . to . deep . equal ( [
47
+ uri , { }
48
+ ] ) ;
49
+ expect ( result ) . to . equal ( mClient ) ;
50
+ } ) ;
51
+ it ( 'connects once when bypassAutoEncryption is true' , async ( ) => {
52
+ const uri = 'localhost:27017' ;
53
+ const opts = { autoEncryption : { bypassAutoEncryption : true } } ;
54
+ const mClientType = stubInterface < typeof MongoClient > ( ) ;
55
+ const mClient = stubInterface < MongoClient > ( ) ;
56
+ mClientType . connect . onFirstCall ( ) . resolves ( mClient ) ;
57
+ const result = await connectMongoClient ( uri , opts , mClientType ) ;
58
+ const calls = mClientType . connect . getCalls ( ) ;
59
+ expect ( calls . length ) . to . equal ( 1 ) ;
60
+ expect ( calls [ 0 ] . args ) . to . deep . equal ( [
61
+ uri , opts
62
+ ] ) ;
63
+ expect ( result ) . to . equal ( mClient ) ;
64
+ } ) ;
65
+ it ( 'connects twice when bypassAutoEncryption is false and enterprise via modules' , async ( ) => {
66
+ const uri = 'localhost:27017' ;
67
+ const opts = { autoEncryption : { bypassAutoEncryption : false } } ;
68
+ const mClientType = stubInterface < typeof MongoClient > ( ) ;
69
+ const mClientFirst = stubInterface < MongoClient > ( ) ;
70
+ const commandSpy = sinon . spy ( ) ;
71
+ mClientFirst . db . returns ( { admin : ( ) => ( { command : ( ...args ) => {
72
+ commandSpy ( ...args ) ;
73
+ return { modules : [ 'enterprise' ] } ;
74
+ } } as any ) } as any ) ;
75
+ const mClientSecond = stubInterface < MongoClient > ( ) ;
76
+ mClientType . connect . onFirstCall ( ) . resolves ( mClientFirst ) ;
77
+ mClientType . connect . onSecondCall ( ) . resolves ( mClientSecond ) ;
78
+ const result = await connectMongoClient ( uri , opts , mClientType ) ;
79
+ const calls = mClientType . connect . getCalls ( ) ;
80
+ expect ( calls . length ) . to . equal ( 2 ) ;
81
+ expect ( calls [ 0 ] . args ) . to . deep . equal ( [
82
+ uri , { }
83
+ ] ) ;
84
+ expect ( commandSpy ) . to . have . been . calledOnceWithExactly ( { buildInfo : 1 } ) ;
85
+ expect ( result ) . to . equal ( mClientSecond ) ;
86
+ } ) ;
87
+ it ( 'errors when bypassAutoEncryption is falsy and not enterprise' , async ( ) => {
88
+ const uri = 'localhost:27017' ;
89
+ const opts = { autoEncryption : { } } ;
90
+ const mClientType = stubInterface < typeof MongoClient > ( ) ;
91
+ const mClientFirst = stubInterface < MongoClient > ( ) ;
92
+ const commandSpy = sinon . spy ( ) ;
93
+ mClientFirst . db . returns ( { admin : ( ) => ( { command : ( ...args ) => {
94
+ commandSpy ( ...args ) ;
95
+ return { modules : [ ] } ;
96
+ } } as any ) } as any ) ;
97
+ const mClientSecond = stubInterface < MongoClient > ( ) ;
98
+ mClientType . connect . onFirstCall ( ) . resolves ( mClientFirst ) ;
99
+ mClientType . connect . onSecondCall ( ) . resolves ( mClientSecond ) ;
100
+ try {
101
+ await connectMongoClient ( uri , opts , mClientType ) ;
102
+ } catch ( e ) {
103
+ return expect ( e . message . toLowerCase ( ) ) . to . include ( 'automatic encryption' ) ;
104
+ }
105
+ expect . fail ( 'Failed to throw expected error' ) ;
106
+ } ) ;
107
+ it ( 'errors when bypassAutoEncryption is falsy, missing modules' , async ( ) => {
108
+ const uri = 'localhost:27017' ;
109
+ const opts = { autoEncryption : { } } ;
110
+ const mClientType = stubInterface < typeof MongoClient > ( ) ;
111
+ const mClientFirst = stubInterface < MongoClient > ( ) ;
112
+ const commandSpy = sinon . spy ( ) ;
113
+ mClientFirst . db . returns ( { admin : ( ) => ( { command : ( ...args ) => {
114
+ commandSpy ( ...args ) ;
115
+ return { } ;
116
+ } } as any ) } as any ) ;
117
+ const mClientSecond = stubInterface < MongoClient > ( ) ;
118
+ mClientType . connect . onFirstCall ( ) . resolves ( mClientFirst ) ;
119
+ mClientType . connect . onSecondCall ( ) . resolves ( mClientSecond ) ;
120
+ try {
121
+ await connectMongoClient ( uri , opts , mClientType ) ;
122
+ } catch ( e ) {
123
+ return expect ( e . message . toLowerCase ( ) ) . to . include ( 'automatic encryption' ) ;
124
+ }
125
+ expect . fail ( 'Failed to throw expected error' ) ;
126
+ } ) ;
127
+ } ) ;
128
+
37
129
describe ( '#constructor' , ( ) => {
38
130
const mongoClient : any = sinon . spy ( ) ;
39
131
serviceProvider = new CliServiceProvider ( mongoClient ) ;
0 commit comments