Skip to content

Commit b06d61e

Browse files
committed
test: simplify scanDiff tests with generateDiffStep helper
1 parent e7ffacb commit b06d61e

File tree

2 files changed

+57
-88
lines changed

2 files changed

+57
-88
lines changed

test/processors/scanDiff.emptyDiff.test.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
import { describe, it, expect } from 'vitest';
22
import { Action, Step } from '../../src/proxy/actions';
33
import { exec } from '../../src/proxy/processors/push-action/scanDiff';
4+
import { generateDiffStep } from './scanDiff.test';
45

56
describe('scanDiff - Empty Diff Handling', () => {
67
describe('Empty diff scenarios', () => {
78
it('should allow empty diff (legitimate empty push)', async () => {
89
const action = new Action('empty-diff-test', 'push', 'POST', Date.now(), 'test/repo.git');
910

1011
// Simulate getDiff step with empty content
11-
const diffStep = { stepName: 'diff', content: '', error: false };
12+
const diffStep = generateDiffStep('');
1213
action.steps = [diffStep as Step];
1314

1415
const result = await exec({}, action);
@@ -22,7 +23,7 @@ describe('scanDiff - Empty Diff Handling', () => {
2223
const action = new Action('null-diff-test', 'push', 'POST', Date.now(), 'test/repo.git');
2324

2425
// Simulate getDiff step with null content
25-
const diffStep = { stepName: 'diff', content: null, error: false };
26+
const diffStep = generateDiffStep(null);
2627
action.steps = [diffStep as Step];
2728

2829
const result = await exec({}, action);
@@ -36,7 +37,7 @@ describe('scanDiff - Empty Diff Handling', () => {
3637
const action = new Action('undefined-diff-test', 'push', 'POST', Date.now(), 'test/repo.git');
3738

3839
// Simulate getDiff step with undefined content
39-
const diffStep = { stepName: 'diff', content: undefined, error: false };
40+
const diffStep = generateDiffStep(undefined);
4041
action.steps = [diffStep as Step];
4142

4243
const result = await exec({}, action);
@@ -63,7 +64,7 @@ index 1234567..abcdefg 100644
6364
database: "production"
6465
};`;
6566

66-
const diffStep = { stepName: 'diff', content: normalDiff, error: false };
67+
const diffStep = generateDiffStep(normalDiff);
6768
action.steps = [diffStep as Step];
6869

6970
const result = await exec({}, action);
@@ -76,7 +77,7 @@ index 1234567..abcdefg 100644
7677
describe('Error conditions', () => {
7778
it('should handle non-string diff content', async () => {
7879
const action = new Action('non-string-test', 'push', 'POST', Date.now(), 'test/repo.git');
79-
const diffStep = { stepName: 'diff', content: 12345 as any, error: false };
80+
const diffStep = generateDiffStep(12345 as any);
8081
action.steps = [diffStep as Step];
8182

8283
const result = await exec({}, action);

test/processors/scanDiff.test.ts

Lines changed: 51 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { describe, it, expect, beforeAll, afterAll } from 'vitest';
1+
import { describe, it, expect, beforeAll, afterAll, vi } from 'vitest';
22
import crypto from 'crypto';
33
import * as processor from '../../src/proxy/processors/push-action/scanDiff';
44
import { Action, Step } from '../../src/proxy/actions';
@@ -56,6 +56,23 @@ index 8b97e49..de18d43 100644
5656
`;
5757
};
5858

59+
export const generateDiffStep = (content?: string | null): Step => {
60+
return {
61+
stepName: 'diff',
62+
content: content,
63+
error: false,
64+
errorMessage: null,
65+
blocked: false,
66+
blockedMessage: null,
67+
logs: [],
68+
id: '1',
69+
setError: vi.fn(),
70+
setContent: vi.fn(),
71+
setAsyncBlock: vi.fn(),
72+
log: vi.fn(),
73+
};
74+
};
75+
5976
const TEST_REPO = {
6077
project: 'private-org-test',
6178
name: 'repo.git',
@@ -94,12 +111,8 @@ describe('Scan commit diff', () => {
94111

95112
it('should block push when diff includes AWS Access Key ID', async () => {
96113
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
97-
action.steps = [
98-
{
99-
stepName: 'diff',
100-
content: generateDiff('AKIAIOSFODNN7EXAMPLE'),
101-
} as Step,
102-
];
114+
const diffStep = generateDiffStep(generateDiff('AKIAIOSFODNN7EXAMPLE'));
115+
action.steps = [diffStep];
103116
action.setCommit('38cdc3e', '8a9c321');
104117
action.setBranch('b');
105118
action.setMessage('Message');
@@ -113,12 +126,8 @@ describe('Scan commit diff', () => {
113126
// Formatting tests
114127
it('should block push when diff includes multiple AWS Access Keys', async () => {
115128
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
116-
action.steps = [
117-
{
118-
stepName: 'diff',
119-
content: generateMultiLineDiff(),
120-
} as Step,
121-
];
129+
const diffStep = generateDiffStep(generateMultiLineDiff());
130+
action.steps = [diffStep];
122131
action.setCommit('8b97e49', 'de18d43');
123132

124133
const { error, errorMessage } = await processor.exec(null, action);
@@ -132,12 +141,8 @@ describe('Scan commit diff', () => {
132141

133142
it('should block push when diff includes multiple AWS Access Keys and blocked literal with appropriate message', async () => {
134143
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
135-
action.steps = [
136-
{
137-
stepName: 'diff',
138-
content: generateMultiLineDiffWithLiteral(),
139-
} as Step,
140-
];
144+
const diffStep = generateDiffStep(generateMultiLineDiffWithLiteral());
145+
action.steps = [diffStep];
141146
action.setCommit('8b97e49', 'de18d43');
142147

143148
const { error, errorMessage } = await processor.exec(null, action);
@@ -154,12 +159,8 @@ describe('Scan commit diff', () => {
154159

155160
it('should block push when diff includes Google Cloud Platform API Key', async () => {
156161
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
157-
action.steps = [
158-
{
159-
stepName: 'diff',
160-
content: generateDiff('AIza0aB7Z4Rfs23MnPqars81yzu19KbH72zaFda'),
161-
} as Step,
162-
];
162+
const diffStep = generateDiffStep(generateDiff('AIza0aB7Z4Rfs23MnPqars81yzu19KbH72zaFda'));
163+
action.steps = [diffStep];
163164
action.commitFrom = '38cdc3e';
164165
action.commitTo = '8a9c321';
165166

@@ -171,12 +172,10 @@ describe('Scan commit diff', () => {
171172

172173
it('should block push when diff includes GitHub Personal Access Token', async () => {
173174
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
174-
action.steps = [
175-
{
176-
stepName: 'diff',
177-
content: generateDiff(`ghp_${crypto.randomBytes(36).toString('hex')}`),
178-
} as Step,
179-
];
175+
const diffStep = generateDiffStep(
176+
generateDiff(`ghp_${crypto.randomBytes(36).toString('hex')}`),
177+
);
178+
action.steps = [diffStep];
180179

181180
const { error, errorMessage } = await processor.exec(null, action);
182181

@@ -186,14 +185,10 @@ describe('Scan commit diff', () => {
186185

187186
it('should block push when diff includes GitHub Fine Grained Personal Access Token', async () => {
188187
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
189-
action.steps = [
190-
{
191-
stepName: 'diff',
192-
content: generateDiff(
193-
`github_pat_1SMAGDFOYZZK3P9ndFemen_${crypto.randomBytes(59).toString('hex')}`,
194-
),
195-
} as Step,
196-
];
188+
const diffStep = generateDiffStep(
189+
generateDiff(`github_pat_1SMAGDFOYZZK3P9ndFemen_${crypto.randomBytes(59).toString('hex')}`),
190+
);
191+
action.steps = [diffStep];
197192
action.commitFrom = '38cdc3e';
198193
action.commitTo = '8a9c321';
199194

@@ -205,12 +200,10 @@ describe('Scan commit diff', () => {
205200

206201
it('should block push when diff includes GitHub Actions Token', async () => {
207202
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
208-
action.steps = [
209-
{
210-
stepName: 'diff',
211-
content: generateDiff(`ghs_${crypto.randomBytes(20).toString('hex')}`),
212-
} as Step,
213-
];
203+
const diffStep = generateDiffStep(
204+
generateDiff(`ghs_${crypto.randomBytes(20).toString('hex')}`),
205+
);
206+
action.steps = [diffStep];
214207
action.commitFrom = '38cdc3e';
215208
action.commitTo = '8a9c321';
216209

@@ -222,14 +215,12 @@ describe('Scan commit diff', () => {
222215

223216
it('should block push when diff includes JSON Web Token (JWT)', async () => {
224217
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
225-
action.steps = [
226-
{
227-
stepName: 'diff',
228-
content: generateDiff(
229-
`eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1cm46Z21haWwuY29tOmNsaWVudElkOjEyMyIsInN1YiI6IkphbmUgRG9lIiwiaWF0IjoxNTIzOTAxMjM0LCJleHAiOjE1MjM5ODc2MzR9.s5_hA8hyIT5jXfU9PlXJ-R74m5F_aPcVEFJSV-g-_kX`,
230-
),
231-
} as Step,
232-
];
218+
const diffStep = generateDiffStep(
219+
generateDiff(
220+
`eyJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJ1cm46Z21haWwuY29tOmNsaWVudElkOjEyMyIsInN1YiI6IkphbmUgRG9lIiwiaWF0IjoxNTIzOTAxMjM0LCJleHAiOjE1MjM5ODc2MzR9.s5_hA8hyIT5jXfU9PlXJ-R74m5F_aPcVEFJSV-g-_kX`,
221+
),
222+
);
223+
action.steps = [diffStep];
233224
action.commitFrom = '38cdc3e';
234225
action.commitTo = '8a9c321';
235226

@@ -242,12 +233,8 @@ describe('Scan commit diff', () => {
242233
it('should block push when diff includes blocked literal', async () => {
243234
for (const literal of blockedLiterals) {
244235
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
245-
action.steps = [
246-
{
247-
stepName: 'diff',
248-
content: generateDiff(literal),
249-
} as Step,
250-
];
236+
const diffStep = generateDiffStep(generateDiff(literal));
237+
action.steps = [diffStep];
251238
action.commitFrom = '38cdc3e';
252239
action.commitTo = '8a9c321';
253240

@@ -260,12 +247,7 @@ describe('Scan commit diff', () => {
260247

261248
it('should allow push when no diff is present (legitimate empty diff)', async () => {
262249
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
263-
action.steps = [
264-
{
265-
stepName: 'diff',
266-
content: null,
267-
} as Step,
268-
];
250+
action.steps = [generateDiffStep(null)];
269251

270252
const result = await processor.exec(null, action);
271253
const scanDiffStep = result.steps.find((s) => s.stepName === 'scanDiff');
@@ -275,12 +257,7 @@ describe('Scan commit diff', () => {
275257

276258
it('should block push when diff is not a string', async () => {
277259
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
278-
action.steps = [
279-
{
280-
stepName: 'diff',
281-
content: 1337 as any,
282-
} as Step,
283-
];
260+
action.steps = [generateDiffStep(1337 as any)];
284261

285262
const { error, errorMessage } = await processor.exec(null, action);
286263

@@ -290,12 +267,7 @@ describe('Scan commit diff', () => {
290267

291268
it('should allow push when diff has no secrets or sensitive information', async () => {
292269
const action = new Action('1', 'type', 'method', 1, 'test/repo.git');
293-
action.steps = [
294-
{
295-
stepName: 'diff',
296-
content: generateDiff(''),
297-
} as Step,
298-
];
270+
action.steps = [generateDiffStep(generateDiff(''))];
299271
action.commitFrom = '38cdc3e';
300272
action.commitTo = '8a9c321';
301273

@@ -312,12 +284,8 @@ describe('Scan commit diff', () => {
312284
1,
313285
'https://github.com/private-org-test/repo.git', // URL needs to be parseable AND exist in DB
314286
);
315-
action.steps = [
316-
{
317-
stepName: 'diff',
318-
content: generateDiff('AKIAIOSFODNN7EXAMPLE'),
319-
} as Step,
320-
];
287+
const diffStep = generateDiffStep(generateDiff('AKIAIOSFODNN7EXAMPLE'));
288+
action.steps = [diffStep];
321289

322290
const { error } = await processor.exec(null, action);
323291

0 commit comments

Comments
 (0)