-
Notifications
You must be signed in to change notification settings - Fork 3
/
simple-methods.js
155 lines (126 loc) · 3.88 KB
/
simple-methods.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import sinon from 'sinon';
import { expect } from 'chai';
import Adit from '../index';
describe('Adit simple methods', () => {
let to;
let adit;
const oldSock = process.env.SSH_AUTH_SOCK;
beforeEach(() => {
to = {
username: 'me',
password: 'pass',
host: 'to-host',
port: [1, 5]
};
sinon.stub(Adit.prototype, 'connect');
sinon.stub(Adit.prototype, 'addEvents');
sinon.stub(Adit, 'Connection', () => {
return {
connect: sinon.stub(),
on: sinon.stub(),
end: sinon.stub()
};
});
adit = new Adit(to);
});
afterEach(() => {
if (Adit.prototype.connect.restore) {
Adit.prototype.connect.restore();
}
if (Adit.prototype.addEvents.restore) {
Adit.prototype.addEvents.restore();
}
Adit.Connection.restore();
process.env.SSH_AUTH_SOCK = oldSock;
});
describe('Adit#close', () => {
beforeEach(() => {
adit.connect();
adit.close();
});
it('should close connection', () => {
expect(adit.connection.end.calledOnce).to.equal(true);
});
it('should clean-up all streams', () => {
expect(adit.streams).to.be.empty;
});
});
describe('Adit#(forward | reverse)', () => {
let adit;
beforeEach(() => {
adit = new Adit(
'9999:localhost:3306 [email protected]',
'password'
);
sinon.stub(Adit.prototype, 'open').returns(new Promise(resolve => resolve()));
sinon.stub(Adit.prototype, 'in');
sinon.stub(Adit.prototype, 'out');
});
afterEach(() => {
Adit.prototype.open.restore();
Adit.prototype.in.restore();
Adit.prototype.out.restore();
});
describe('Adit#forward', () => {
beforeEach(() => adit.forward());
it('should call Adit#open method', () => {
expect(adit.open).to.be.called;
});
it('should call Adit#out method', () => {
expect(adit.out).to.be.called;
});
it('should pass args to Adit#out method', () => {
const args = adit.out.firstCall.args;
expect(args[0]).to.be.an('object');
expect(args[1]).to.be.an('object');
});
it('should pass args in correct order to Adit#out method', () => {
const args = adit.out.firstCall.args;
expect(args[0].port).to.equal('9999');
expect(args[1].port).to.equal('3306');
});
});
describe('Adit#reverse', () => {
beforeEach(() => adit.reverse());
it('should call Adit#open method', () => {
expect(adit.open).to.be.called;
});
it('should call Adit#in method', () => {
expect(adit.in).to.be.called;
});
it('should pass args to Adit#in method', () => {
const args = adit.in.firstCall.args;
expect(args[0]).to.be.an('object');
expect(args[1]).to.be.an('object');
});
it('should pass args in correct order to Adit#in method', () => {
const args = adit.in.firstCall.args;
expect(args[0].port).to.equal('3306');
expect(args[1].port).to.equal('9999');
});
});
});
describe('Adit#reTry', () => {
it('should not try to reconnect when there is no more attemps', () => {
adit.connect();
adit.reTry('test');
return adit.promise.fail((error) => {
expect(error).to.equal('test');
expect(adit.addEvents.callCount).to.equal(0);
expect(adit.connection.end.callCount).to.equal(0);
});
});
it('should not try to reconnect when there is one more attempt', () => {
Adit.prototype.connect.restore();
adit.connect(1);
adit.reTry('test');
adit.reTry('test');
return adit.promise.fail((error) => {
expect(error).to.equal('test');
expect(adit.addEvents.callCount).to.equal(1);
expect(adit.connection.connect.callCount).to.equal(1);
expect(adit.connection.end.callCount).to.equal(0);
});
});
});
});