forked from G-Research/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchain.test.js
More file actions
480 lines (414 loc) · 19.3 KB
/
chain.test.js
File metadata and controls
480 lines (414 loc) · 19.3 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
const chai = require('chai');
const sinon = require('sinon');
const { PluginLoader } = require('../src/plugin');
const db = require('../src/db');
chai.should();
const expect = chai.expect;
const mockLoader = {
pushPlugins: [
{ exec: Object.assign(async () => console.log('foo'), { displayName: 'foo.exec' }) },
],
pullPlugins: [
{ exec: Object.assign(async () => console.log('foo'), { displayName: 'bar.exec' }) },
],
};
const initMockPushProcessors = (sinon) => {
const mockPushProcessors = {
parsePush: sinon.stub(),
checkEmptyBranch: sinon.stub(),
audit: sinon.stub(),
checkRepoInAuthorisedList: sinon.stub(),
checkCommitMessages: sinon.stub(),
checkAuthorEmails: sinon.stub(),
checkUserPushPermission: sinon.stub(),
checkIfWaitingAuth: sinon.stub(),
checkHiddenCommits: sinon.stub(),
pullRemote: sinon.stub(),
writePack: sinon.stub(),
preReceive: sinon.stub(),
getDiff: sinon.stub(),
gitleaks: sinon.stub(),
clearBareClone: sinon.stub(),
scanDiff: sinon.stub(),
blockForAuth: sinon.stub(),
};
mockPushProcessors.parsePush.displayName = 'parsePush';
mockPushProcessors.checkEmptyBranch.displayName = 'checkEmptyBranch';
mockPushProcessors.audit.displayName = 'audit';
mockPushProcessors.checkRepoInAuthorisedList.displayName = 'checkRepoInAuthorisedList';
mockPushProcessors.checkCommitMessages.displayName = 'checkCommitMessages';
mockPushProcessors.checkAuthorEmails.displayName = 'checkAuthorEmails';
mockPushProcessors.checkUserPushPermission.displayName = 'checkUserPushPermission';
mockPushProcessors.checkIfWaitingAuth.displayName = 'checkIfWaitingAuth';
mockPushProcessors.checkHiddenCommits.displayName = 'checkHiddenCommits';
mockPushProcessors.pullRemote.displayName = 'pullRemote';
mockPushProcessors.writePack.displayName = 'writePack';
mockPushProcessors.preReceive.displayName = 'preReceive';
mockPushProcessors.getDiff.displayName = 'getDiff';
mockPushProcessors.gitleaks.displayName = 'gitleaks';
mockPushProcessors.clearBareClone.displayName = 'clearBareClone';
mockPushProcessors.scanDiff.displayName = 'scanDiff';
mockPushProcessors.blockForAuth.displayName = 'blockForAuth';
return mockPushProcessors;
};
const mockPreProcessors = {
parseAction: sinon.stub(),
};
const clearCache = (sandbox) => {
delete require.cache[require.resolve('../src/proxy/processors')];
delete require.cache[require.resolve('../src/proxy/chain')];
sandbox.restore();
};
describe('proxy chain', function () {
let processors;
let chain;
let mockPushProcessors;
let sandboxSinon;
beforeEach(async () => {
// Create a new sandbox for each test
sandboxSinon = sinon.createSandbox();
// Initialize the mock push processors
mockPushProcessors = initMockPushProcessors(sandboxSinon);
// Re-import the processors module after clearing the cache
processors = await import('../src/proxy/processors');
// Mock the processors module
sandboxSinon.stub(processors, 'pre').value(mockPreProcessors);
sandboxSinon.stub(processors, 'push').value(mockPushProcessors);
// Re-import the chain module after stubbing processors
chain = require('../src/proxy/chain').default;
chain.chainPluginLoader = new PluginLoader([]);
});
afterEach(() => {
// Clear the module from the cache after each test
clearCache(sandboxSinon);
});
it('getChain should set pluginLoaded if loader is undefined', async function () {
chain.chainPluginLoader = undefined;
const actual = await chain.getChain({ type: 'push' });
expect(actual).to.deep.equal(chain.pushActionChain);
expect(chain.chainPluginLoader).to.be.undefined;
expect(chain.pluginsInserted).to.be.true;
});
it('getChain should load plugins from an initialized PluginLoader', async function () {
chain.chainPluginLoader = mockLoader;
const initialChain = [...chain.pushActionChain];
const actual = await chain.getChain({ type: 'push' });
expect(actual.length).to.be.greaterThan(initialChain.length);
expect(chain.pluginsInserted).to.be.true;
});
it('getChain should load pull plugins from an initialized PluginLoader', async function () {
chain.chainPluginLoader = mockLoader;
const initialChain = [...chain.pullActionChain];
const actual = await chain.getChain({ type: 'pull' });
expect(actual.length).to.be.greaterThan(initialChain.length);
expect(chain.pluginsInserted).to.be.true;
});
it('executeChain should stop executing if action has continue returns false', async function () {
const req = {};
const continuingAction = { type: 'push', continue: () => true, allowPush: false };
mockPreProcessors.parseAction.resolves({ type: 'push' });
mockPushProcessors.parsePush.resolves(continuingAction);
mockPushProcessors.checkEmptyBranch.resolves(continuingAction);
mockPushProcessors.checkRepoInAuthorisedList.resolves(continuingAction);
mockPushProcessors.checkCommitMessages.resolves(continuingAction);
mockPushProcessors.checkAuthorEmails.resolves(continuingAction);
mockPushProcessors.checkUserPushPermission.resolves(continuingAction);
mockPushProcessors.checkHiddenCommits.resolves(continuingAction);
mockPushProcessors.pullRemote.resolves(continuingAction);
mockPushProcessors.writePack.resolves(continuingAction);
// this stops the chain from further execution
mockPushProcessors.checkIfWaitingAuth.resolves({
type: 'push',
continue: () => false,
allowPush: false,
});
const result = await chain.executeChain(req);
expect(mockPreProcessors.parseAction.called).to.be.true;
expect(mockPushProcessors.parsePush.called).to.be.true;
expect(mockPushProcessors.checkRepoInAuthorisedList.called).to.be.true;
expect(mockPushProcessors.checkCommitMessages.called).to.be.true;
expect(mockPushProcessors.checkAuthorEmails.called).to.be.true;
expect(mockPushProcessors.checkUserPushPermission.called).to.be.true;
expect(mockPushProcessors.checkIfWaitingAuth.called).to.be.true;
expect(mockPushProcessors.pullRemote.called).to.be.true;
expect(mockPushProcessors.checkHiddenCommits.called).to.be.true;
expect(mockPushProcessors.writePack.called).to.be.true;
expect(mockPushProcessors.checkEmptyBranch.called).to.be.true;
expect(mockPushProcessors.audit.called).to.be.true;
expect(result.type).to.equal('push');
expect(result.allowPush).to.be.false;
expect(result.continue).to.be.a('function');
});
it('executeChain should stop executing if action has allowPush is set to true', async function () {
const req = {};
const continuingAction = { type: 'push', continue: () => true, allowPush: false };
mockPreProcessors.parseAction.resolves({ type: 'push' });
mockPushProcessors.parsePush.resolves(continuingAction);
mockPushProcessors.checkEmptyBranch.resolves(continuingAction);
mockPushProcessors.checkRepoInAuthorisedList.resolves(continuingAction);
mockPushProcessors.checkCommitMessages.resolves(continuingAction);
mockPushProcessors.checkAuthorEmails.resolves(continuingAction);
mockPushProcessors.checkUserPushPermission.resolves(continuingAction);
mockPushProcessors.checkHiddenCommits.resolves(continuingAction);
mockPushProcessors.pullRemote.resolves(continuingAction);
mockPushProcessors.writePack.resolves(continuingAction);
// this stops the chain from further execution
mockPushProcessors.checkIfWaitingAuth.resolves({
type: 'push',
continue: () => true,
allowPush: true,
});
const result = await chain.executeChain(req);
expect(mockPreProcessors.parseAction.called).to.be.true;
expect(mockPushProcessors.parsePush.called).to.be.true;
expect(mockPushProcessors.checkEmptyBranch.called).to.be.true;
expect(mockPushProcessors.checkRepoInAuthorisedList.called).to.be.true;
expect(mockPushProcessors.checkCommitMessages.called).to.be.true;
expect(mockPushProcessors.checkAuthorEmails.called).to.be.true;
expect(mockPushProcessors.checkUserPushPermission.called).to.be.true;
expect(mockPushProcessors.checkIfWaitingAuth.called).to.be.true;
expect(mockPushProcessors.pullRemote.called).to.be.true;
expect(mockPushProcessors.checkHiddenCommits.called).to.be.true;
expect(mockPushProcessors.writePack.called).to.be.true;
expect(mockPushProcessors.audit.called).to.be.true;
expect(result.type).to.equal('push');
expect(result.allowPush).to.be.true;
expect(result.continue).to.be.a('function');
});
it('executeChain should execute all steps if all actions succeed', async function () {
const req = {};
const continuingAction = { type: 'push', continue: () => true, allowPush: false };
mockPreProcessors.parseAction.resolves({ type: 'push' });
mockPushProcessors.parsePush.resolves(continuingAction);
mockPushProcessors.checkEmptyBranch.resolves(continuingAction);
mockPushProcessors.checkRepoInAuthorisedList.resolves(continuingAction);
mockPushProcessors.checkCommitMessages.resolves(continuingAction);
mockPushProcessors.checkAuthorEmails.resolves(continuingAction);
mockPushProcessors.checkUserPushPermission.resolves(continuingAction);
mockPushProcessors.checkIfWaitingAuth.resolves(continuingAction);
mockPushProcessors.pullRemote.resolves(continuingAction);
mockPushProcessors.writePack.resolves(continuingAction);
mockPushProcessors.checkHiddenCommits.resolves(continuingAction);
mockPushProcessors.preReceive.resolves(continuingAction);
mockPushProcessors.getDiff.resolves(continuingAction);
mockPushProcessors.gitleaks.resolves(continuingAction);
mockPushProcessors.clearBareClone.resolves(continuingAction);
mockPushProcessors.scanDiff.resolves(continuingAction);
mockPushProcessors.blockForAuth.resolves(continuingAction);
const result = await chain.executeChain(req);
expect(mockPreProcessors.parseAction.called).to.be.true;
expect(mockPushProcessors.parsePush.called).to.be.true;
expect(mockPushProcessors.checkEmptyBranch.called).to.be.true;
expect(mockPushProcessors.checkRepoInAuthorisedList.called).to.be.true;
expect(mockPushProcessors.checkCommitMessages.called).to.be.true;
expect(mockPushProcessors.checkAuthorEmails.called).to.be.true;
expect(mockPushProcessors.checkUserPushPermission.called).to.be.true;
expect(mockPushProcessors.checkIfWaitingAuth.called).to.be.true;
expect(mockPushProcessors.pullRemote.called).to.be.true;
expect(mockPushProcessors.checkHiddenCommits.called).to.be.true;
expect(mockPushProcessors.writePack.called).to.be.true;
expect(mockPushProcessors.preReceive.called).to.be.true;
expect(mockPushProcessors.getDiff.called).to.be.true;
expect(mockPushProcessors.gitleaks.called).to.be.true;
expect(mockPushProcessors.clearBareClone.called).to.be.true;
expect(mockPushProcessors.scanDiff.called).to.be.true;
expect(mockPushProcessors.blockForAuth.called).to.be.true;
expect(mockPushProcessors.audit.called).to.be.true;
expect(result.type).to.equal('push');
expect(result.allowPush).to.be.false;
expect(result.continue).to.be.a('function');
});
it('executeChain should run the expected steps for pulls', async function () {
const req = {};
const continuingAction = { type: 'pull', continue: () => true, allowPush: false };
mockPreProcessors.parseAction.resolves({ type: 'pull' });
mockPushProcessors.checkRepoInAuthorisedList.resolves(continuingAction);
const result = await chain.executeChain(req);
expect(mockPushProcessors.checkRepoInAuthorisedList.called).to.be.true;
expect(mockPushProcessors.parsePush.called).to.be.false;
expect(result.type).to.equal('pull');
});
it('executeChain should handle errors and still call audit', async function () {
const req = {};
const action = { type: 'push', continue: () => true, allowPush: true };
processors.pre.parseAction.resolves(action);
mockPushProcessors.parsePush.rejects(new Error('Audit error'));
try {
await chain.executeChain(req);
} catch {
// Ignore the error
}
expect(mockPushProcessors.audit.called).to.be.true;
});
it('executeChain should always run at least checkRepoInAuthList', async function () {
const req = {};
const action = { type: 'foo', continue: () => true, allowPush: true };
mockPreProcessors.parseAction.resolves(action);
mockPushProcessors.checkRepoInAuthorisedList.resolves(action);
await chain.executeChain(req);
expect(mockPushProcessors.checkRepoInAuthorisedList.called).to.be.true;
});
it('should approve push automatically and record in the database', async function () {
const req = {};
const action = {
type: 'push',
continue: () => true,
allowPush: false,
setAutoApproval: sinon.stub(),
repoName: 'test-repo',
commitTo: 'newCommitHash',
};
mockPreProcessors.parseAction.resolves(action);
mockPushProcessors.parsePush.resolves(action);
mockPushProcessors.checkEmptyBranch.resolves(action);
mockPushProcessors.checkRepoInAuthorisedList.resolves(action);
mockPushProcessors.checkCommitMessages.resolves(action);
mockPushProcessors.checkAuthorEmails.resolves(action);
mockPushProcessors.checkUserPushPermission.resolves(action);
mockPushProcessors.checkIfWaitingAuth.resolves(action);
mockPushProcessors.pullRemote.resolves(action);
mockPushProcessors.writePack.resolves(action);
mockPushProcessors.checkHiddenCommits.resolves(action);
mockPushProcessors.preReceive.resolves({
...action,
steps: [{ error: false, logs: ['Push automatically approved by pre-receive hook.'] }],
allowPush: true,
autoApproved: true,
});
mockPushProcessors.getDiff.resolves(action);
mockPushProcessors.gitleaks.resolves(action);
mockPushProcessors.clearBareClone.resolves(action);
mockPushProcessors.scanDiff.resolves(action);
mockPushProcessors.blockForAuth.resolves(action);
const dbStub = sinon.stub(db, 'authorise').resolves(true);
const result = await chain.executeChain(req);
expect(result.type).to.equal('push');
expect(result.allowPush).to.be.true;
expect(result.continue).to.be.a('function');
expect(dbStub.calledOnce).to.be.true;
dbStub.restore();
});
it('should reject push automatically and record in the database', async function () {
const req = {};
const action = {
type: 'push',
continue: () => true,
allowPush: false,
setAutoRejection: sinon.stub(),
repoName: 'test-repo',
commitTo: 'newCommitHash',
};
mockPreProcessors.parseAction.resolves(action);
mockPushProcessors.parsePush.resolves(action);
mockPushProcessors.checkEmptyBranch.resolves(action);
mockPushProcessors.checkRepoInAuthorisedList.resolves(action);
mockPushProcessors.checkCommitMessages.resolves(action);
mockPushProcessors.checkAuthorEmails.resolves(action);
mockPushProcessors.checkUserPushPermission.resolves(action);
mockPushProcessors.checkIfWaitingAuth.resolves(action);
mockPushProcessors.pullRemote.resolves(action);
mockPushProcessors.writePack.resolves(action);
mockPushProcessors.checkHiddenCommits.resolves(action);
mockPushProcessors.preReceive.resolves({
...action,
steps: [{ error: false, logs: ['Push automatically rejected by pre-receive hook.'] }],
allowPush: true,
autoRejected: true,
});
mockPushProcessors.getDiff.resolves(action);
mockPushProcessors.gitleaks.resolves(action);
mockPushProcessors.clearBareClone.resolves(action);
mockPushProcessors.scanDiff.resolves(action);
mockPushProcessors.blockForAuth.resolves(action);
const dbStub = sinon.stub(db, 'reject').resolves(true);
const result = await chain.executeChain(req);
expect(result.type).to.equal('push');
expect(result.allowPush).to.be.true;
expect(result.continue).to.be.a('function');
expect(dbStub.calledOnce).to.be.true;
dbStub.restore();
});
it('executeChain should handle exceptions in attemptAutoApproval', async function () {
const req = {};
const action = {
type: 'push',
continue: () => true,
allowPush: false,
setAutoApproval: sinon.stub(),
repoName: 'test-repo',
commitTo: 'newCommitHash',
};
mockPreProcessors.parseAction.resolves(action);
mockPushProcessors.parsePush.resolves(action);
mockPushProcessors.checkEmptyBranch.resolves(action);
mockPushProcessors.checkRepoInAuthorisedList.resolves(action);
mockPushProcessors.checkCommitMessages.resolves(action);
mockPushProcessors.checkAuthorEmails.resolves(action);
mockPushProcessors.checkUserPushPermission.resolves(action);
mockPushProcessors.checkIfWaitingAuth.resolves(action);
mockPushProcessors.pullRemote.resolves(action);
mockPushProcessors.writePack.resolves(action);
mockPushProcessors.checkHiddenCommits.resolves(action);
mockPushProcessors.preReceive.resolves({
...action,
steps: [{ error: false, logs: ['Push automatically approved by pre-receive hook.'] }],
allowPush: true,
autoApproved: true,
});
mockPushProcessors.getDiff.resolves(action);
mockPushProcessors.gitleaks.resolves(action);
mockPushProcessors.clearBareClone.resolves(action);
mockPushProcessors.scanDiff.resolves(action);
mockPushProcessors.blockForAuth.resolves(action);
const error = new Error('Database error');
const consoleErrorStub = sinon.stub(console, 'error');
sinon.stub(db, 'authorise').rejects(error);
await chain.executeChain(req);
expect(consoleErrorStub.calledOnceWith('Error during auto-approval:', error.message)).to.be
.true;
db.authorise.restore();
consoleErrorStub.restore();
});
it('executeChain should handle exceptions in attemptAutoRejection', async function () {
const req = {};
const action = {
type: 'push',
continue: () => true,
allowPush: false,
setAutoRejection: sinon.stub(),
repoName: 'test-repo',
commitTo: 'newCommitHash',
autoRejected: true,
};
mockPreProcessors.parseAction.resolves(action);
mockPushProcessors.parsePush.resolves(action);
mockPushProcessors.checkEmptyBranch.resolves(action);
mockPushProcessors.checkRepoInAuthorisedList.resolves(action);
mockPushProcessors.checkCommitMessages.resolves(action);
mockPushProcessors.checkAuthorEmails.resolves(action);
mockPushProcessors.checkUserPushPermission.resolves(action);
mockPushProcessors.checkIfWaitingAuth.resolves(action);
mockPushProcessors.pullRemote.resolves(action);
mockPushProcessors.writePack.resolves(action);
mockPushProcessors.checkHiddenCommits.resolves(action);
mockPushProcessors.preReceive.resolves({
...action,
steps: [{ error: false, logs: ['Push automatically rejected by pre-receive hook.'] }],
allowPush: false,
autoRejected: true,
});
mockPushProcessors.getDiff.resolves(action);
mockPushProcessors.gitleaks.resolves(action);
mockPushProcessors.clearBareClone.resolves(action);
mockPushProcessors.scanDiff.resolves(action);
mockPushProcessors.blockForAuth.resolves(action);
const error = new Error('Database error');
const consoleErrorStub = sinon.stub(console, 'error');
sinon.stub(db, 'reject').rejects(error);
await chain.executeChain(req);
expect(consoleErrorStub.calledOnceWith('Error during auto-rejection:', error.message)).to.be
.true;
db.reject.restore();
consoleErrorStub.restore();
});
});