forked from G-Research/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestProxy.test.js
More file actions
308 lines (259 loc) · 9.01 KB
/
testProxy.test.js
File metadata and controls
308 lines (259 loc) · 9.01 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
const chai = require('chai');
const sinon = require('sinon');
const http = require('http');
const https = require('https');
const proxyquire = require('proxyquire');
const expect = chai.expect;
describe('Proxy', () => {
let sandbox;
let Proxy;
let mockHttpServer;
let mockHttpsServer;
beforeEach(() => {
sandbox = sinon.createSandbox();
mockHttpServer = {
listen: sandbox.stub().callsFake((port, callback) => {
if (callback) setImmediate(callback);
return mockHttpServer;
}),
close: sandbox.stub().callsFake((callback) => {
if (callback) setImmediate(callback);
return mockHttpServer;
}),
};
mockHttpsServer = {
listen: sandbox.stub().callsFake((port, callback) => {
if (callback) setImmediate(callback);
return mockHttpsServer;
}),
close: sandbox.stub().callsFake((callback) => {
if (callback) setImmediate(callback);
return mockHttpsServer;
}),
};
sandbox.stub(http, 'createServer').returns(mockHttpServer);
sandbox.stub(https, 'createServer').returns(mockHttpsServer);
// deep mocking for express router
const mockRouter = sandbox.stub();
mockRouter.use = sandbox.stub();
mockRouter.get = sandbox.stub();
mockRouter.post = sandbox.stub();
mockRouter.stack = [];
Proxy = proxyquire('../src/proxy/index', {
'./routes': {
getRouter: sandbox.stub().resolves(mockRouter),
},
'../config': {
getTLSEnabled: sandbox.stub().returns(false),
getTLSKeyPemPath: sandbox.stub().returns('/tmp/key.pem'),
getTLSCertPemPath: sandbox.stub().returns('/tmp/cert.pem'),
getPlugins: sandbox.stub().returns(['mock-plugin']),
getAuthorisedList: sandbox.stub().returns([{ project: 'test-proj', name: 'test-repo' }]),
},
'../db': {
getRepos: sandbox.stub().resolves([]),
createRepo: sandbox.stub().resolves({ _id: 'mock-repo-id' }),
addUserCanPush: sandbox.stub().resolves(),
addUserCanAuthorise: sandbox.stub().resolves(),
},
'../plugin': {
PluginLoader: sandbox.stub().returns({
load: sandbox.stub().resolves(),
}),
},
'./chain': {
default: {},
},
'../config/env': {
serverConfig: {
GIT_PROXY_SERVER_PORT: 3000,
GIT_PROXY_HTTPS_SERVER_PORT: 3001,
},
},
fs: {
readFileSync: sandbox.stub().returns(Buffer.from('mock-cert')),
},
}).default;
});
afterEach(() => {
sandbox.restore();
});
describe('start()', () => {
it('should start HTTP server when TLS is disabled', async () => {
const proxy = new Proxy();
await proxy.start();
expect(http.createServer.calledOnce).to.be.true;
expect(https.createServer.called).to.be.false;
expect(mockHttpServer.listen.calledWith(3000)).to.be.true;
await proxy.stop();
});
it('should start both HTTP and HTTPS servers when TLS is enabled', async () => {
const mockRouterTLS = sandbox.stub();
mockRouterTLS.use = sandbox.stub();
mockRouterTLS.get = sandbox.stub();
mockRouterTLS.post = sandbox.stub();
mockRouterTLS.stack = [];
const ProxyWithTLS = proxyquire('../src/proxy/index', {
'./routes': {
getRouter: sandbox.stub().resolves(mockRouterTLS),
},
'../config': {
getTLSEnabled: sandbox.stub().returns(true), // TLS enabled
getTLSKeyPemPath: sandbox.stub().returns('/tmp/key.pem'),
getTLSCertPemPath: sandbox.stub().returns('/tmp/cert.pem'),
getPlugins: sandbox.stub().returns(['mock-plugin']),
getAuthorisedList: sandbox.stub().returns([]),
},
'../db': {
getRepos: sandbox.stub().resolves([]),
createRepo: sandbox.stub().resolves({ _id: 'mock-repo-id' }),
addUserCanPush: sandbox.stub().resolves(),
addUserCanAuthorise: sandbox.stub().resolves(),
},
'../plugin': {
PluginLoader: sandbox.stub().returns({
load: sandbox.stub().resolves(),
}),
},
'./chain': {
default: {},
},
'../config/env': {
serverConfig: {
GIT_PROXY_SERVER_PORT: 3000,
GIT_PROXY_HTTPS_SERVER_PORT: 3001,
},
},
fs: {
readFileSync: sandbox.stub().returns(Buffer.from('mock-cert')),
},
}).default;
const proxy = new ProxyWithTLS();
await proxy.start();
expect(http.createServer.calledOnce).to.be.true;
expect(https.createServer.calledOnce).to.be.true;
expect(mockHttpServer.listen.calledWith(3000)).to.be.true;
expect(mockHttpsServer.listen.calledWith(3001)).to.be.true;
await proxy.stop();
});
it('should set up express app after starting', async () => {
const proxy = new Proxy();
expect(proxy.getExpressApp()).to.be.null;
await proxy.start();
expect(proxy.getExpressApp()).to.not.be.null;
expect(proxy.getExpressApp()).to.be.a('function');
await proxy.stop();
});
});
describe('getExpressApp()', () => {
it('should return null before start() is called', () => {
const proxy = new Proxy();
expect(proxy.getExpressApp()).to.be.null;
});
it('should return express app after start() is called', async () => {
const proxy = new Proxy();
await proxy.start();
const app = proxy.getExpressApp();
expect(app).to.not.be.null;
expect(app).to.be.a('function');
expect(app.use).to.be.a('function');
await proxy.stop();
});
});
describe('stop()', () => {
it('should close HTTP server when running', async () => {
const proxy = new Proxy();
await proxy.start();
await proxy.stop();
expect(mockHttpServer.close.calledOnce).to.be.true;
});
it('should close both HTTP and HTTPS servers when both are running', async () => {
const mockRouterStop = sandbox.stub();
mockRouterStop.use = sandbox.stub();
mockRouterStop.get = sandbox.stub();
mockRouterStop.post = sandbox.stub();
mockRouterStop.stack = [];
const ProxyWithTLS = proxyquire('../src/proxy/index', {
'./routes': {
getRouter: sandbox.stub().resolves(mockRouterStop),
},
'../config': {
getTLSEnabled: sandbox.stub().returns(true),
getTLSKeyPemPath: sandbox.stub().returns('/tmp/key.pem'),
getTLSCertPemPath: sandbox.stub().returns('/tmp/cert.pem'),
getPlugins: sandbox.stub().returns([]),
getAuthorisedList: sandbox.stub().returns([]),
},
'../db': {
getRepos: sandbox.stub().resolves([]),
createRepo: sandbox.stub().resolves({ _id: 'mock-repo-id' }),
addUserCanPush: sandbox.stub().resolves(),
addUserCanAuthorise: sandbox.stub().resolves(),
},
'../plugin': {
PluginLoader: sandbox.stub().returns({
load: sandbox.stub().resolves(),
}),
},
'./chain': {
default: {},
},
'../config/env': {
serverConfig: {
GIT_PROXY_SERVER_PORT: 3000,
GIT_PROXY_HTTPS_SERVER_PORT: 3001,
},
},
fs: {
readFileSync: sandbox.stub().returns(Buffer.from('mock-cert')),
},
}).default;
const proxy = new ProxyWithTLS();
await proxy.start();
await proxy.stop();
expect(mockHttpServer.close.calledOnce).to.be.true;
expect(mockHttpsServer.close.calledOnce).to.be.true;
});
it('should resolve successfully when no servers are running', async () => {
const proxy = new Proxy();
await proxy.stop();
expect(mockHttpServer.close.called).to.be.false;
expect(mockHttpsServer.close.called).to.be.false;
});
it('should handle errors gracefully', async () => {
const proxy = new Proxy();
await proxy.start();
// simulate error in server close
mockHttpServer.close.callsFake(() => {
throw new Error('Server close error');
});
try {
await proxy.stop();
expect.fail('Expected stop() to reject');
} catch (error) {
expect(error.message).to.equal('Server close error');
}
});
});
describe('full lifecycle', () => {
it('should start and stop successfully', async () => {
const proxy = new Proxy();
await proxy.start();
expect(proxy.getExpressApp()).to.not.be.null;
expect(mockHttpServer.listen.calledOnce).to.be.true;
await proxy.stop();
expect(mockHttpServer.close.calledOnce).to.be.true;
});
it('should handle multiple start/stop cycles', async () => {
const proxy = new Proxy();
await proxy.start();
await proxy.stop();
mockHttpServer.listen.resetHistory();
mockHttpServer.close.resetHistory();
await proxy.start();
await proxy.stop();
expect(mockHttpServer.listen.calledOnce).to.be.true;
expect(mockHttpServer.close.calledOnce).to.be.true;
});
});
});