forked from G-Research-Forks/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckCommitMessages.test.js
More file actions
155 lines (126 loc) · 5.03 KB
/
checkCommitMessages.test.js
File metadata and controls
155 lines (126 loc) · 5.03 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
const chai = require('chai');
const sinon = require('sinon');
const proxyquire = require('proxyquire');
const { Action, Step } = require('../../src/proxy/actions');
chai.should();
const expect = chai.expect;
describe('checkCommitMessages', () => {
let commitConfig;
let exec;
let getCommitConfigStub;
let logStub;
beforeEach(() => {
logStub = sinon.stub(console, 'log');
commitConfig = {
message: {
block: {
literals: ['secret', 'password'],
patterns: ['\\b\\d{4}-\\d{4}-\\d{4}-\\d{4}\\b'] // Credit card pattern
}
}
};
getCommitConfigStub = sinon.stub().returns(commitConfig);
const checkCommitMessages = proxyquire('../../src/proxy/processors/push-action/checkCommitMessages', {
'../../../config': { getCommitConfig: getCommitConfigStub }
});
exec = checkCommitMessages.exec;
});
afterEach(() => {
sinon.restore();
});
describe('exec', () => {
let action;
let req;
let stepSpy;
beforeEach(() => {
req = {};
action = new Action(
'1234567890',
'push',
'POST',
1234567890,
'test/repo'
);
action.commitData = [
{ message: 'Fix bug', author: 'test@example.com' },
{ message: 'Update docs', author: 'test@example.com' }
];
stepSpy = sinon.spy(Step.prototype, 'log');
});
it('should allow commit with valid messages', async () => {
const result = await exec(req, action);
expect(result.steps).to.have.lengthOf(1);
expect(result.steps[0].error).to.be.false;
expect(logStub.calledWith('The following commit messages are legal: Fix bug,Update docs')).to.be.true;
});
it('should block commit with illegal messages', async () => {
action.commitData?.push({ message: 'secret password here', author: 'test@example.com' });
const result = await exec(req, action);
expect(result.steps).to.have.lengthOf(1);
expect(result.steps[0].error).to.be.true;
expect(stepSpy.calledWith(
'The following commit messages are illegal: secret password here'
)).to.be.true;
expect(result.steps[0].errorMessage).to.include('Your push has been blocked');
expect(logStub.calledWith('The following commit messages are illegal: secret password here')).to.be.true;
});
it('should handle duplicate messages only once', async () => {
action.commitData = [
{ message: 'secret', author: 'test@example.com' },
{ message: 'secret', author: 'test@example.com' },
{ message: 'password', author: 'test@example.com' }
];
const result = await exec(req, action);
expect(result.steps[0].error).to.be.true;
expect(stepSpy.calledWith(
'The following commit messages are illegal: secret,password'
)).to.be.true;
expect(logStub.calledWith('The following commit messages are illegal: secret,password')).to.be.true;
});
it('should not error when commit data is empty', async () => {
// Empty commit data happens when making a branch from an unapproved commit
// or when pushing an empty branch or deleting a branch
// This is handled in the checkEmptyBranch.exec action
action.commitData = [];
const result = await exec(req, action);
expect(result.steps).to.have.lengthOf(1);
expect(result.steps[0].error).to.be.false;
expect(logStub.calledWith('The following commit messages are legal: ')).to.be.true;
});
it('should handle commit data with null values', async () => {
action.commitData = [
{ message: null, author: 'test@example.com' },
{ message: undefined, author: 'test@example.com' }
];
const result = await exec(req, action);
expect(result.steps).to.have.lengthOf(1);
expect(result.steps[0].error).to.be.true;
});
it('should handle commit messages of incorrect type', async () => {
action.commitData = [
{ message: 123, author: 'test@example.com' },
{ message: {}, author: 'test@example.com' }
];
const result = await exec(req, action);
expect(result.steps).to.have.lengthOf(1);
expect(result.steps[0].error).to.be.true;
expect(stepSpy.calledWith(
'The following commit messages are illegal: 123,[object Object]'
)).to.be.true;
expect(logStub.calledWith('The following commit messages are illegal: 123,[object Object]')).to.be.true;
});
it('should handle a mix of valid and invalid messages', async () => {
action.commitData = [
{ message: 'Fix bug', author: 'test@example.com' },
{ message: 'secret password here', author: 'test@example.com' }
];
const result = await exec(req, action);
expect(result.steps).to.have.lengthOf(1);
expect(result.steps[0].error).to.be.true;
expect(stepSpy.calledWith(
'The following commit messages are illegal: secret password here'
)).to.be.true;
expect(logStub.calledWith('The following commit messages are illegal: secret password here')).to.be.true;
});
});
});