|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +var Promise = require('ember-cli/lib/ext/promise'); |
| 4 | +var assert = require('ember-cli/tests/helpers/assert'); |
| 5 | +var fs = require('fs'); |
| 6 | +var path = require('path'); |
| 7 | + |
| 8 | +describe('ssh-tunnel plugin', function() { |
| 9 | + var subject, mockUi; |
| 10 | + |
| 11 | + beforeEach(function() { |
| 12 | + subject = require('../../index'); |
| 13 | + mockUi = { |
| 14 | + messages: [], |
| 15 | + write: function() { }, |
| 16 | + writeLine: function(message) { |
| 17 | + this.messages.push(message); |
| 18 | + } |
| 19 | + }; |
| 20 | + }); |
| 21 | + |
| 22 | + it('has a name', function() { |
| 23 | + var result = subject.createDeployPlugin({ |
| 24 | + name: 'test-plugin' |
| 25 | + }); |
| 26 | + |
| 27 | + assert.equal(result.name, 'test-plugin'); |
| 28 | + }); |
| 29 | + |
| 30 | + it('implements the correct hooks', function() { |
| 31 | + var result = subject.createDeployPlugin({ |
| 32 | + name: 'test-plugin' |
| 33 | + }); |
| 34 | + |
| 35 | + assert.equal(typeof result.defaultConfig, 'object'); |
| 36 | + assert.equal(typeof result.requiredConfig, 'object'); |
| 37 | + assert.equal(typeof result.willDeploy, 'function'); |
| 38 | + assert.equal(typeof result.didDeploy, 'function'); |
| 39 | + }); |
| 40 | + |
| 41 | + describe('configure hook', function() { |
| 42 | + |
| 43 | + it('resolves if config is ok', function() { |
| 44 | + var plugin = subject.createDeployPlugin({ |
| 45 | + name: 'ssh-tunnel' |
| 46 | + }); |
| 47 | + |
| 48 | + var context = { |
| 49 | + ui: mockUi, |
| 50 | + config: { |
| 51 | + 'ssh-tunnel': { |
| 52 | + host: 'example.com', |
| 53 | + username: 'ghedamat' |
| 54 | + } |
| 55 | + } |
| 56 | + }; |
| 57 | + |
| 58 | + plugin.beforeHook(context); |
| 59 | + plugin.configure(context); |
| 60 | + assert.ok(true); // it didn't throw |
| 61 | + }); |
| 62 | + |
| 63 | + describe('without providing config', function () { |
| 64 | + var plugin, context, config; |
| 65 | + |
| 66 | + beforeEach(function() { |
| 67 | + plugin = subject.createDeployPlugin({ |
| 68 | + name: 'ssh-tunnel' |
| 69 | + }); |
| 70 | + }); |
| 71 | + |
| 72 | + it('raises about missing required config', function() { |
| 73 | + config = { }; |
| 74 | + context = { |
| 75 | + ui: mockUi, |
| 76 | + config: config |
| 77 | + }; |
| 78 | + plugin.beforeHook(context); |
| 79 | + assert.throws(function(error){ |
| 80 | + plugin.configure(context); |
| 81 | + }); |
| 82 | + var messages = mockUi.messages.reduce(function(previous, current) { |
| 83 | + if (/- Missing required config:\s.*/.test(current)) { |
| 84 | +previous.push(current); |
| 85 | + } |
| 86 | + |
| 87 | + return previous; |
| 88 | + }, []); |
| 89 | + assert.equal(messages.length, 1); |
| 90 | + }); |
| 91 | + |
| 92 | + it('warns about missing optional config', function() { |
| 93 | + context = { |
| 94 | + ui: mockUi, |
| 95 | + config: { |
| 96 | + 'ssh-tunnel': { |
| 97 | + host: 'example.com', |
| 98 | + username: 'ghedamat' |
| 99 | + } |
| 100 | + } |
| 101 | + }; |
| 102 | + plugin.beforeHook(context); |
| 103 | + plugin.configure(context); |
| 104 | + var messages = mockUi.messages.reduce(function(previous, current) { |
| 105 | + if (/- Missing config:\s.*, using default:\s/.test(current)) { |
| 106 | + previous.push(current); |
| 107 | + } |
| 108 | + |
| 109 | + return previous; |
| 110 | + }, []); |
| 111 | + assert.equal(messages.length, 4); |
| 112 | + }); |
| 113 | + |
| 114 | + it('adds default config to the config object', function() { |
| 115 | + context = { |
| 116 | + ui: mockUi, |
| 117 | + config: { |
| 118 | + 'ssh-tunnel': { |
| 119 | + host: 'example.com', |
| 120 | + username: 'ghedamat' |
| 121 | + } |
| 122 | + } |
| 123 | + }; |
| 124 | + plugin.beforeHook(context); |
| 125 | + plugin.configure(context); |
| 126 | + assert.isDefined(config['ssh-tunnel'].dstPort); |
| 127 | + assert.isDefined(config['ssh-tunnel'].srcPort); |
| 128 | + assert.isDefined(config['ssh-tunnel'].tunnelClient); |
| 129 | + assert.isDefined(config['ssh-tunnel'].privateKeyPath); |
| 130 | + }); |
| 131 | + }); |
| 132 | + }); |
| 133 | + |
| 134 | + describe('willDeploy hook', function() { |
| 135 | + var plugin; |
| 136 | + var context; |
| 137 | + |
| 138 | + beforeEach(function() { |
| 139 | + plugin = subject.createDeployPlugin({ |
| 140 | + name: 'ssh-tunnel' |
| 141 | + }); |
| 142 | + |
| 143 | + context = { |
| 144 | + ui: mockUi, |
| 145 | + config: { |
| 146 | + 'ssh-tunnel': { |
| 147 | + host: 'example.com', |
| 148 | + username: 'ghedamat', |
| 149 | + srcPort: 50000, |
| 150 | + dstPort: 6379, |
| 151 | + tunnelClient: function() { |
| 152 | + return function(config, cbk) { |
| 153 | + process.nextTick(cbk, false, true); |
| 154 | + return { |
| 155 | + close: function() { } |
| 156 | + }; |
| 157 | + }; |
| 158 | + } |
| 159 | + } |
| 160 | + }, |
| 161 | + }; |
| 162 | + plugin.beforeHook(context); |
| 163 | + }); |
| 164 | + |
| 165 | + it('returns the updated context if tunnel is successful', function(done) { |
| 166 | + assert.isFulfilled(plugin.willDeploy(context)).then(function(result) { |
| 167 | + assert.isDefined(result.tunnel.handler); |
| 168 | + assert.isDefined(result.tunnel.srcPort); |
| 169 | + done(); |
| 170 | + }).catch(function(reason) { |
| 171 | + done(reason.actual.stack); |
| 172 | + }); |
| 173 | + }); |
| 174 | + }); |
| 175 | + |
| 176 | + describe('didDeploy hook', function() { |
| 177 | + var plugin; |
| 178 | + var context; |
| 179 | + |
| 180 | + beforeEach(function() { |
| 181 | + plugin = subject.createDeployPlugin({ |
| 182 | + name: 'ssh-tunnel' |
| 183 | + }); |
| 184 | + |
| 185 | + context = { |
| 186 | + ui: mockUi, |
| 187 | + config: { |
| 188 | + 'ssh-tunnel': { |
| 189 | + } |
| 190 | + } |
| 191 | + }; |
| 192 | + plugin.beforeHook(context); |
| 193 | + }); |
| 194 | + |
| 195 | + it('closes the tunnel', function(done) { |
| 196 | + context.tunnel = { |
| 197 | + handler: { |
| 198 | + close: function() { |
| 199 | + assert.ok(true); |
| 200 | + done(); |
| 201 | + } |
| 202 | + } |
| 203 | + }; |
| 204 | + |
| 205 | + var result = plugin.didDeploy(context); |
| 206 | + }); |
| 207 | + }); |
| 208 | +}); |
0 commit comments