Skip to content

Commit 68de844

Browse files
committed
chore(test): Add Test for privateKey param on shh tunnel
1 parent 7fc8e5a commit 68de844

File tree

1 file changed

+78
-1
lines changed

1 file changed

+78
-1
lines changed

test/driver/index_test.js

+78-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var proxyquire = require('proxyquire');
1+
var proxyquire = require('proxyquire').noPreserveCache();
22
var sinon = require('sinon');
33
var Code = require('code');
44
var Lab = require('lab');
@@ -41,6 +41,7 @@ lab.experiment('index', function() {
4141
// Set up stubs/spies to verify correct flow
4242
var driverSpy = sinon.stub(driver, 'connect').yields(null, {});
4343
var tunnelStub = sinon.stub().callsArg(1);
44+
4445
var index = proxyquire('../../lib/driver/index', {
4546
'tunnel-ssh': tunnelStub,
4647
'./mysql': driver
@@ -137,4 +138,80 @@ lab.experiment('index', function() {
137138
done();
138139
}
139140
});
141+
142+
lab.test('privateKey gets set as expected',
143+
function(done, cleanup) {
144+
145+
// Ensure that require gets a new copy of the module for each test
146+
delete require.cache[require.resolve('db-migrate-mysql')];
147+
var driver = require('db-migrate-mysql');
148+
var fs = { readFileSync: sinon.stub().returns('sshkey') };
149+
150+
// Set up stubs/spies to verify correct flow
151+
var driverSpy = sinon.stub(driver, 'connect').yields(null, {});
152+
var tunnelStub = sinon.stub().callsArg(1);
153+
154+
var index = proxyquire('../../lib/driver/index', {
155+
'tunnel-ssh': tunnelStub,
156+
'fs': fs,
157+
'./mysql': driver,
158+
});
159+
160+
//register clean up
161+
cleanup(function(next) {
162+
163+
driverSpy.restore();
164+
165+
delete require.cache[require.resolve('tunnel-ssh')];
166+
delete require.cache[require.resolve('db-migrate-mysql')];
167+
next();
168+
});
169+
170+
validDbConfigWithTunnel.tunnel.privateKeyPath = '/test/key';
171+
172+
index.connect(
173+
validDbConfigWithTunnel,
174+
{},
175+
indexConnectCallback(tunnelStub, driverSpy, validate)
176+
);
177+
178+
function validate(err, db, tunnelStub, driverSpy) {
179+
var expectedTunnelConfig = {
180+
localPort: 'localPort',
181+
host: 'sshHost',
182+
port: 'sshPort',
183+
privateKeyPath: '/test/key',
184+
dstHost: '127.0.0.1',
185+
dstPort: 'localPort',
186+
privateKey: 'sshkey'
187+
};
188+
var expectedDbConfig = {
189+
driver: 'mysql',
190+
host: '127.0.0.1',
191+
port: 'localPort',
192+
tunnel: {
193+
localPort: 'localPort',
194+
host: 'sshHost',
195+
port: 'sshPort',
196+
privateKeyPath: '/test/key'
197+
}
198+
};
199+
200+
201+
Code.expect(err).to.be.null();
202+
Code.expect(db).to.not.be.null();
203+
Code.expect(
204+
fs.readFileSync.withArgs(validDbConfigWithTunnel.tunnel.privateKeyPath)
205+
.calledOnce
206+
).to.be.true();
207+
Code.expect(
208+
tunnelStub.withArgs(expectedTunnelConfig).calledOnce
209+
).to.be.true();
210+
Code.expect(
211+
driverSpy.withArgs(expectedDbConfig).calledOnce
212+
).to.be.true();
213+
214+
done();
215+
}
216+
});
140217
});

0 commit comments

Comments
 (0)