forked from finos/git-proxy
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheckAuthorEmails.test.ts
More file actions
540 lines (437 loc) · 16.5 KB
/
checkAuthorEmails.test.ts
File metadata and controls
540 lines (437 loc) · 16.5 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
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { exec } from '../../src/proxy/processors/push-action/checkAuthorEmails';
import { Action, Step } from '../../src/proxy/actions';
import * as configModule from '../../src/config';
import * as validator from 'validator';
import { CommitData } from '../../src/proxy/processors/types';
// mock dependencies
vi.mock('../../src/config', async (importOriginal) => {
const actual: any = await importOriginal();
return {
...actual,
getCommitConfig: vi.fn(() => ({})),
};
});
vi.mock('validator', async (importOriginal) => {
const actual: any = await importOriginal();
return {
...actual,
isEmail: vi.fn(),
};
});
describe('checkAuthorEmails', () => {
let mockAction: Action;
let mockReq: any;
let consoleLogSpy: any;
beforeEach(async () => {
// setup default mocks
vi.mocked(validator.isEmail).mockImplementation((email: string) => {
// email validation mock
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(email);
});
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '',
},
local: {
block: '',
},
},
},
});
// mock console.log to suppress output and verify calls
consoleLogSpy = vi.spyOn(Step.prototype, 'log').mockImplementation(() => {});
// setup mock action
mockAction = {
commitData: [],
addStep: vi.fn(),
} as unknown as Action;
mockReq = {};
});
afterEach(() => {
vi.clearAllMocks();
});
describe('isEmailAllowed logic (via exec)', () => {
describe('basic email validation', () => {
it('should allow valid email addresses', async () => {
mockAction.commitData = [
{ authorEmail: 'john.doe@example.com' } as CommitData,
{ authorEmail: 'jane.smith@company.org' } as CommitData,
];
const result = await exec(mockReq, mockAction);
expect(result.addStep).toHaveBeenCalledTimes(1);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
it('should reject empty email', async () => {
mockAction.commitData = [{ authorEmail: '' } as CommitData];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
it('should reject null/undefined email', async () => {
vi.mocked(validator.isEmail).mockReturnValue(false);
mockAction.commitData = [{ authorEmail: null as any } as CommitData];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
it('should reject invalid email format', async () => {
vi.mocked(validator.isEmail).mockReturnValue(false);
mockAction.commitData = [
{ authorEmail: 'not-an-email' } as CommitData,
{ authorEmail: 'missing@domain' } as CommitData,
{ authorEmail: '@nodomain.com' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
});
describe('domain allow list', () => {
it('should allow emails from permitted domains', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '^(example\\.com|company\\.org)$',
},
local: {
block: '',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'user@example.com' } as CommitData,
{ authorEmail: 'admin@company.org' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
it('should reject emails from non-permitted domains when allow list is set', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '^example\\.com$',
},
local: {
block: '',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'user@notallowed.com' } as CommitData,
{ authorEmail: 'admin@different.org' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
it('should handle partial domain matches correctly', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: 'example\\.com',
},
local: {
block: '',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'user@subdomain.example.com' } as CommitData,
{ authorEmail: 'user@example.com.fake.org' } as CommitData,
];
const result = await exec(mockReq, mockAction);
// both should match because regex pattern 'example.com' appears in both
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
it('should allow all domains when allow list is empty', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '',
},
local: {
block: '',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'user@anydomain.com' } as CommitData,
{ authorEmail: 'admin@otherdomain.org' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
});
describe('local part block list', () => {
it('should reject emails with blocked local parts', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '',
},
local: {
block: '^(noreply|donotreply|bounce)$',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'noreply@example.com' } as CommitData,
{ authorEmail: 'donotreply@company.org' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
it('should allow emails with non-blocked local parts', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '',
},
local: {
block: '^noreply$',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'john.doe@example.com' } as CommitData,
{ authorEmail: 'valid.user@company.org' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
it('should handle regex patterns in local block correctly', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '',
},
local: {
block: '^(test|temp|fake)',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'test@example.com' } as CommitData,
{ authorEmail: 'temporary@example.com' } as CommitData,
{ authorEmail: 'fakeuser@example.com' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
it('should allow all local parts when block list is empty', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '',
},
local: {
block: '',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'noreply@example.com' } as CommitData,
{ authorEmail: 'anything@example.com' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
});
describe('combined domain and local rules', () => {
it('should enforce both domain allow and local block rules', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '^example\\.com$',
},
local: {
block: '^noreply$',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'valid@example.com' } as CommitData, // valid
{ authorEmail: 'noreply@example.com' } as CommitData, // invalid: blocked local
{ authorEmail: 'valid@otherdomain.com' } as CommitData, // invalid: wrong domain
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
});
});
describe('exec function behavior', () => {
it('should create a step with name "checkAuthorEmails"', async () => {
mockAction.commitData = [{ authorEmail: 'user@example.com' } as CommitData];
await exec(mockReq, mockAction);
expect(mockAction.addStep).toHaveBeenCalledWith(
expect.objectContaining({
stepName: 'checkAuthorEmails',
}),
);
});
it('should handle unique author emails correctly', async () => {
mockAction.commitData = [
{ authorEmail: 'user1@example.com' } as CommitData,
{ authorEmail: 'user2@example.com' } as CommitData,
{ authorEmail: 'user1@example.com' } as CommitData, // Duplicate
{ authorEmail: 'user3@example.com' } as CommitData,
{ authorEmail: 'user2@example.com' } as CommitData, // Duplicate
];
await exec(mockReq, mockAction);
expect(consoleLogSpy).toHaveBeenCalledWith(
'The following commit author e-mails are legal: user1@example.com,user2@example.com,user3@example.com',
);
});
it('should handle empty commitData', async () => {
mockAction.commitData = [];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
it('should handle undefined commitData', async () => {
mockAction.commitData = undefined;
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
it('should log error message when illegal emails found', async () => {
vi.mocked(validator.isEmail).mockReturnValue(false);
mockAction.commitData = [{ authorEmail: 'invalid-email' } as CommitData];
await exec(mockReq, mockAction);
});
it('should log success message when all emails are legal', async () => {
mockAction.commitData = [
{ authorEmail: 'user1@example.com' } as CommitData,
{ authorEmail: 'user2@example.com' } as CommitData,
];
await exec(mockReq, mockAction);
expect(consoleLogSpy).toHaveBeenCalledWith(
'The following commit author e-mails are legal: user1@example.com,user2@example.com',
);
});
it('should set error on step when illegal emails found', async () => {
vi.mocked(validator.isEmail).mockReturnValue(false);
mockAction.commitData = [{ authorEmail: 'bad@email' } as CommitData];
await exec(mockReq, mockAction);
const step = vi.mocked(mockAction.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
it('should call step.setError with user-friendly message', async () => {
vi.mocked(validator.isEmail).mockReturnValue(false);
mockAction.commitData = [{ authorEmail: 'bad' } as CommitData];
await exec(mockReq, mockAction);
const step = vi.mocked(mockAction.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
expect(step.errorMessage).toBe(
'Your push has been blocked. Please verify your Git configured e-mail address is valid (e.g. john.smith@example.com)',
);
});
it('should return the action object', async () => {
mockAction.commitData = [{ authorEmail: 'user@example.com' } as CommitData];
const result = await exec(mockReq, mockAction);
expect(result).toBe(mockAction);
});
it('should handle mixed valid and invalid emails', async () => {
mockAction.commitData = [
{ authorEmail: 'valid@example.com' } as CommitData,
{ authorEmail: 'invalid' } as CommitData,
{ authorEmail: 'also.valid@example.com' } as CommitData,
];
vi.mocked(validator.isEmail).mockImplementation((email: string) => {
return email.includes('@') && email.includes('.');
});
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
});
describe('displayName', () => {
it('should have correct displayName', () => {
expect(exec.displayName).toBe('checkAuthorEmails.exec');
});
});
describe('edge cases', () => {
it('should handle email with multiple @ symbols', async () => {
vi.mocked(validator.isEmail).mockReturnValue(false);
mockAction.commitData = [{ authorEmail: 'user@@example.com' } as CommitData];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
it('should handle email without domain', async () => {
vi.mocked(validator.isEmail).mockReturnValue(false);
mockAction.commitData = [{ authorEmail: 'user@' } as CommitData];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(true);
});
it('should handle very long email addresses', async () => {
const longLocal = 'a'.repeat(64);
const longEmail = `${longLocal}@example.com`;
mockAction.commitData = [{ authorEmail: longEmail } as CommitData];
const result = await exec(mockReq, mockAction);
expect(result.addStep).toHaveBeenCalled();
});
it('should handle special characters in local part', async () => {
mockAction.commitData = [
{ authorEmail: 'user+tag@example.com' } as CommitData,
{ authorEmail: 'user.name@example.com' } as CommitData,
{ authorEmail: 'user_name@example.com' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
it('should handle case sensitivity in domain checking', async () => {
vi.mocked(configModule.getCommitConfig).mockReturnValue({
author: {
email: {
domain: {
allow: '^example\\.com$',
},
local: {
block: '',
},
},
},
} as any);
mockAction.commitData = [
{ authorEmail: 'user@EXAMPLE.COM' } as CommitData,
{ authorEmail: 'user@Example.Com' } as CommitData,
];
const result = await exec(mockReq, mockAction);
const step = vi.mocked(result.addStep).mock.calls[0][0];
expect(step.error).toBe(false);
});
});
});