|
1 |
| -var proxyquire = require('proxyquire'); |
| 1 | +var proxyquire = require('proxyquire').noPreserveCache(); |
2 | 2 | var sinon = require('sinon');
|
3 | 3 | var Code = require('code');
|
4 | 4 | var Lab = require('lab');
|
@@ -41,6 +41,7 @@ lab.experiment('index', function() {
|
41 | 41 | // Set up stubs/spies to verify correct flow
|
42 | 42 | var driverSpy = sinon.stub(driver, 'connect').yields(null, {});
|
43 | 43 | var tunnelStub = sinon.stub().callsArg(1);
|
| 44 | + |
44 | 45 | var index = proxyquire('../../lib/driver/index', {
|
45 | 46 | 'tunnel-ssh': tunnelStub,
|
46 | 47 | './mysql': driver
|
@@ -137,4 +138,80 @@ lab.experiment('index', function() {
|
137 | 138 | done();
|
138 | 139 | }
|
139 | 140 | });
|
| 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 | + }); |
140 | 217 | });
|
0 commit comments