Skip to content

Commit 27ccfb9

Browse files
committed
test: checkAuthorEmails action and fix bugs
1 parent 8423fd0 commit 27ccfb9

File tree

2 files changed

+175
-5
lines changed

2 files changed

+175
-5
lines changed

src/proxy/processors/push-action/checkAuthorEmails.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,27 @@ import { Commit } from '../../actions/Action';
55
const commitConfig = getCommitConfig();
66

77
const isEmailAllowed = (email: string): boolean => {
8+
if (!email) {
9+
return false;
10+
}
11+
812
const [emailLocal, emailDomain] = email.split('@');
9-
console.log({ emailLocal, emailDomain });
1013

11-
// E-mail address is not a permissible domain name
14+
if (!emailLocal || !emailDomain) {
15+
return false;
16+
}
17+
1218
if (
1319
commitConfig.author.email.domain.allow &&
1420
!emailDomain.match(new RegExp(commitConfig.author.email.domain.allow, 'g'))
1521
) {
16-
console.log('Bad e-mail address domain...');
1722
return false;
1823
}
1924

20-
// E-mail username is not a permissible form
2125
if (
2226
commitConfig.author.email.local.block &&
2327
emailLocal.match(new RegExp(commitConfig.author.email.local.block, 'g'))
2428
) {
25-
console.log('Bad e-mail address username...');
2629
return false;
2730
}
2831

Lines changed: 167 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
const sinon = require('sinon');
2+
const proxyquire = require('proxyquire').noCallThru();
3+
const { expect } = require('chai');
4+
5+
describe('checkAuthorEmails', () => {
6+
let exec;
7+
let Step;
8+
let getCommitConfig;
9+
let stepSpy;
10+
let action;
11+
let config;
12+
13+
beforeEach(() => {
14+
Step = class {
15+
constructor() {
16+
this.error = undefined;
17+
}
18+
log() {}
19+
setError() {}
20+
};
21+
stepSpy = sinon.spy(Step.prototype, 'log');
22+
sinon.spy(Step.prototype, 'setError');
23+
24+
config = {
25+
author: {
26+
email: {
27+
domain: { allow: null },
28+
local: { block: null }
29+
}
30+
}
31+
};
32+
getCommitConfig = sinon.stub().returns(config);
33+
34+
action = {
35+
commitData: [],
36+
addStep: sinon.stub().callsFake(function(step) {
37+
this.step = new Step();
38+
Object.assign(this.step, step);
39+
return this.step;
40+
})
41+
};
42+
43+
exec = proxyquire('../../src/proxy/processors/push-action/checkAuthorEmails', {
44+
'../../../config': { getCommitConfig },
45+
'../../actions': { Step }
46+
}).exec;
47+
});
48+
49+
afterEach(() => {
50+
sinon.restore();
51+
});
52+
53+
it('should allow valid emails when no restrictions', async () => {
54+
action.commitData = [
55+
{ authorEmail: '[email protected]' },
56+
{ authorEmail: '[email protected]' }
57+
];
58+
59+
await exec({}, action);
60+
61+
expect(action.step.error).to.be.undefined;
62+
});
63+
64+
it('should block emails from forbidden domains', async () => {
65+
config.author.email.domain.allow = 'example\\.com$';
66+
action.commitData = [
67+
{ authorEmail: '[email protected]' },
68+
{ authorEmail: '[email protected]' }
69+
];
70+
71+
await exec({}, action);
72+
73+
expect(action.step.error).to.be.true;
74+
expect(stepSpy.calledWith(
75+
'The following commit author e-mails are illegal: [email protected]'
76+
)).to.be.true;
77+
expect(Step.prototype.setError.calledWith(
78+
'Your push has been blocked. Please verify your Git configured e-mail address is valid (e.g. [email protected])'
79+
)).to.be.true;
80+
});
81+
82+
it('should block emails with forbidden usernames', async () => {
83+
config.author.email.local.block = 'blocked';
84+
action.commitData = [
85+
{ authorEmail: '[email protected]' },
86+
{ authorEmail: '[email protected]' }
87+
];
88+
89+
await exec({}, action);
90+
91+
expect(action.step.error).to.be.true;
92+
expect(stepSpy.calledWith(
93+
'The following commit author e-mails are illegal: [email protected]'
94+
)).to.be.true;
95+
});
96+
97+
it('should handle empty email strings', async () => {
98+
action.commitData = [
99+
{ authorEmail: '' },
100+
{ authorEmail: '[email protected]' }
101+
];
102+
103+
await exec({}, action);
104+
105+
expect(action.step.error).to.be.true;
106+
expect(stepSpy.calledWith(
107+
'The following commit author e-mails are illegal: '
108+
)).to.be.true;
109+
});
110+
111+
it('should allow emails when both checks pass', async () => {
112+
config.author.email.domain.allow = 'example\\.com$';
113+
config.author.email.local.block = 'forbidden';
114+
action.commitData = [
115+
{ authorEmail: '[email protected]' },
116+
{ authorEmail: '[email protected]' }
117+
];
118+
119+
await exec({}, action);
120+
121+
expect(action.step.error).to.be.undefined;
122+
});
123+
124+
it('should block emails that fail both checks', async () => {
125+
config.author.email.domain.allow = 'example\\.com$';
126+
config.author.email.local.block = 'forbidden';
127+
action.commitData = [
128+
{ authorEmail: '[email protected]' }
129+
];
130+
131+
await exec({}, action);
132+
133+
expect(action.step.error).to.be.true;
134+
expect(stepSpy.calledWith(
135+
'The following commit author e-mails are illegal: [email protected]'
136+
)).to.be.true;
137+
});
138+
139+
it('should handle emails without domain', async () => {
140+
action.commitData = [
141+
{ authorEmail: 'nodomain@' }
142+
];
143+
144+
await exec({}, action);
145+
146+
expect(action.step.error).to.be.true;
147+
expect(stepSpy.calledWith(
148+
'The following commit author e-mails are illegal: nodomain@'
149+
)).to.be.true;
150+
});
151+
152+
it('should handle multiple illegal emails', async () => {
153+
config.author.email.domain.allow = 'example\\.com$';
154+
action.commitData = [
155+
{ authorEmail: '[email protected]' },
156+
{ authorEmail: '[email protected]' },
157+
{ authorEmail: '[email protected]' }
158+
];
159+
160+
await exec({}, action);
161+
162+
expect(action.step.error).to.be.true;
163+
expect(stepSpy.calledWith(
164+
'The following commit author e-mails are illegal: [email protected],[email protected]'
165+
)).to.be.true;
166+
});
167+
});

0 commit comments

Comments
 (0)