@@ -4,6 +4,7 @@ const Connection = require('../');
4
4
const connect = Connection . connect ;
5
5
const mock = require ( 'mock-require' ) ;
6
6
const sinon = require ( 'sinon' ) ;
7
+ const SSHTunnel = require ( '@mongodb-js/ssh-tunnel' ) . default ;
7
8
8
9
const setupListeners = ( ) => { } ;
9
10
@@ -74,13 +75,15 @@ describe('connection model connector', () => {
74
75
} ) ;
75
76
76
77
describe ( 'ssh tunnel failures' , ( ) => {
77
- const spy = sinon . spy ( ) ;
78
+ let closeSpy ;
78
79
79
- mock ( '../lib/ssh-tunnel' , ( model , cb ) => {
80
- // simulate successful tunnel creation
81
- cb ( ) ;
82
- // then return a mocked tunnel object with a spy close() function
83
- return { close : spy } ;
80
+ mock ( '@mongodb-js/ssh-tunnel' , {
81
+ default : class MockTunnel extends SSHTunnel {
82
+ constructor ( ...args ) {
83
+ super ( ...args ) ;
84
+ this . close = closeSpy = sinon . spy ( this . close . bind ( this ) ) ;
85
+ }
86
+ }
84
87
} ) ;
85
88
86
89
const MockConnection = mock . reRequire ( '../lib/extended-model' ) ;
@@ -99,12 +102,46 @@ describe('connection model connector', () => {
99
102
100
103
assert ( model . isValid ( ) ) ;
101
104
mockConnect ( model , setupListeners , ( err ) => {
102
- // must throw error here, because the connection details are invalid
103
- assert . ok ( err ) ;
104
- assert . ok ( / E C O N N R E F U S E D / . test ( err . message ) ) ;
105
- // assert that tunnel.close() was called once
106
- assert . ok ( spy . calledOnce ) ;
107
- done ( ) ;
105
+ try {
106
+ // must throw error here, because the connection details are invalid
107
+ assert . ok ( err ) ;
108
+ // assert that tunnel.close() was called once
109
+ assert . ok (
110
+ closeSpy . calledOnce ,
111
+ 'Expected tunnel.close to be called exactly once'
112
+ ) ;
113
+ done ( ) ;
114
+ } catch ( e ) {
115
+ done ( e ) ;
116
+ }
117
+ } ) ;
118
+ } ) ;
119
+
120
+ it ( 'should propagate tunnel error if tunnel fails to connect' , ( done ) => {
121
+ const model = new MockConnection ( {
122
+ hostname : 'localhost' ,
123
+ port : 27020 ,
124
+ sshTunnel : 'USER_PASSWORD' ,
125
+ sshTunnelHostname : 'my.ssh-server.com' ,
126
+ sshTunnelPassword : 'password' ,
127
+ sshTunnelUsername : 'my-user' ,
128
+ extraOptions : { serverSelectionTimeoutMS : 100 }
129
+ } ) ;
130
+
131
+ assert ( model . isValid ( ) ) ;
132
+ mockConnect ( model , setupListeners , ( err ) => {
133
+ try {
134
+ const regex = / E N O T F O U N D m y .s s h - s e r v e r .c o m / ;
135
+
136
+ assert . ok ( err ) ;
137
+ assert . ok (
138
+ regex . test ( err . message ) ,
139
+ `Expected "${ err . message } " to match ${ regex } `
140
+ ) ;
141
+ done ( ) ;
142
+ } catch ( e ) {
143
+ done ( e ) ;
144
+ }
108
145
} ) ;
109
146
} ) ;
110
147
} ) ;
0 commit comments