Skip to content

Commit ecf2343

Browse files
committed
feat(test): edit tests for preReceive in order to handle auto approval
1 parent f672318 commit ecf2343

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

test/chain.test.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
const chai = require('chai');
22
const sinon = require('sinon');
33
const { PluginLoader } = require('../src/plugin');
4+
const db = require('../src/db');
45

56
chai.should();
67
const expect = chai.expect;
@@ -245,4 +246,52 @@ describe('proxy chain', function () {
245246
expect(mockPushProcessors.parsePush.called).to.be.false;
246247
expect(result).to.deep.equal(action);
247248
});
249+
250+
it('should approve push automatically and record in the database', async function () {
251+
const req = {};
252+
const action = {
253+
type: 'push',
254+
continue: () => true,
255+
allowPush: false,
256+
setAllowAutoApprover: sinon.stub(),
257+
repoName: 'test-repo',
258+
commitTo: 'newCommitHash',
259+
autoApproved: false,
260+
};
261+
262+
mockPreProcessors.parseAction.resolves(action);
263+
mockPushProcessors.parsePush.resolves(action);
264+
mockPushProcessors.checkRepoInAuthorisedList.resolves(action);
265+
mockPushProcessors.checkCommitMessages.resolves(action);
266+
mockPushProcessors.checkAuthorEmails.resolves(action);
267+
mockPushProcessors.checkUserPushPermission.resolves(action);
268+
mockPushProcessors.checkIfWaitingAuth.resolves(action);
269+
mockPushProcessors.pullRemote.resolves(action);
270+
mockPushProcessors.writePack.resolves(action);
271+
272+
mockPushProcessors.preReceive.resolves({
273+
...action,
274+
steps: [{ error: false, logs: ['Push automatically approved by pre-receive hook.'] }],
275+
allowPush: true,
276+
autoApproved: true,
277+
});
278+
279+
mockPushProcessors.getDiff.resolves(action);
280+
mockPushProcessors.clearBareClone.resolves(action);
281+
mockPushProcessors.scanDiff.resolves(action);
282+
mockPushProcessors.blockForAuth.resolves(action);
283+
284+
const dbStub = sinon.stub(db, 'authorise').resolves(true);
285+
286+
const result = await chain.executeChain(req);
287+
288+
expect(result.type).to.equal('push');
289+
expect(result.allowPush).to.be.true;
290+
expect(result.continue).to.be.a('function');
291+
292+
expect(dbStub.calledOnce).to.be.true;
293+
expect(dbStub.calledWith(action.id, sinon.match({ autoApproved: true }))).to.be.true;
294+
295+
dbStub.restore();
296+
});
248297
});
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/bin/bash
2+
while read oldrev newrev refname; do
3+
echo "Push need manual approve to $refname"
4+
done
5+
exit 1

test/preReceive/pre-receive-hooks/always-reject.sh

100644100755
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
while read oldrev newrev refname; do
33
echo "Push rejected to $refname"
44
done
5-
exit 1
5+
exit 2

test/preReceive/preReceive.test.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,24 @@ describe('Pre-Receive Hook Execution', function () {
1919
addStep: function (step) {
2020
this.steps.push(step);
2121
},
22+
setAllowAutoApprover: sinon.stub(),
2223
};
2324
});
2425

2526
afterEach(() => {
2627
sinon.restore();
2728
});
2829

29-
it('should execute hook successfully', async () => {
30-
const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/always-allow.sh');
30+
it('should execute hook successfully and require manual approval', async () => {
31+
const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/always-exit-1.sh');
3132

3233
const result = await exec(req, action, scriptPath);
3334

3435
expect(result.steps).to.have.lengthOf(1);
3536
expect(result.steps[0].error).to.be.false;
36-
expect(
37-
result.steps[0].logs.some((log) => log.includes('Pre-receive hook executed successfully')),
38-
).to.be.true;
37+
expect(result.steps[0].logs.some((log) => log.includes('Push requires manual approval.'))).to.be
38+
.true;
39+
expect(action.setAllowAutoApprover.called).to.be.false;
3940
});
4041

4142
it('should skip execution when hook file does not exist', async () => {
@@ -50,6 +51,7 @@ describe('Pre-Receive Hook Execution', function () {
5051
log.includes('Pre-receive hook not found, skipping execution.'),
5152
),
5253
).to.be.true;
54+
expect(action.setAllowAutoApprover.called).to.be.false;
5355
});
5456

5557
it('should skip execution when hook directory does not exist', async () => {
@@ -64,6 +66,7 @@ describe('Pre-Receive Hook Execution', function () {
6466
log.includes('Pre-receive hook not found, skipping execution.'),
6567
),
6668
).to.be.true;
69+
expect(action.setAllowAutoApprover.called).to.be.false;
6770
});
6871

6972
it('should fail when hook execution returns an error', async () => {
@@ -76,11 +79,13 @@ describe('Pre-Receive Hook Execution', function () {
7679
const step = result.steps[0];
7780

7881
expect(step.error).to.be.true;
82+
expect(step.logs.some((log) => log.includes('Push rejected by pre-receive hook.'))).to.be.true;
7983
expect(step.logs.some((log) => log.includes('Hook stderr:'))).to.be.true;
8084

8185
expect(step.errorMessage).to.exist;
8286

8387
expect(action.steps).to.deep.include(step);
88+
expect(action.setAllowAutoApprover.called).to.be.false;
8489
});
8590

8691
it('should catch and handle unexpected errors', async () => {
@@ -95,5 +100,21 @@ describe('Pre-Receive Hook Execution', function () {
95100
expect(
96101
result.steps[0].logs.some((log) => log.includes('Hook execution error: Unexpected FS error')),
97102
).to.be.true;
103+
expect(action.setAllowAutoApprover.called).to.be.false;
104+
});
105+
106+
it('should approve push automatically when hook returns status 0', async () => {
107+
const scriptPath = path.resolve(__dirname, 'pre-receive-hooks/always-allow.sh');
108+
109+
const result = await exec(req, action, scriptPath);
110+
111+
expect(result.steps).to.have.lengthOf(1);
112+
expect(result.steps[0].error).to.be.false;
113+
expect(
114+
result.steps[0].logs.some((log) =>
115+
log.includes('Push automatically approved by pre-receive hook.'),
116+
),
117+
).to.be.true;
118+
expect(action.setAllowAutoApprover.calledOnce).to.be.true;
98119
});
99120
});

0 commit comments

Comments
 (0)