Skip to content

Commit df82548

Browse files
committed
refactor: modified the test to provide exact action and diff content
1 parent 027a459 commit df82548

File tree

2 files changed

+49
-47
lines changed

2 files changed

+49
-47
lines changed

src/proxy/processors/push-action/checkSensitiveData.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const fs = require('fs');
22
const csv = require('csv-parser');
33
const XLSX = require('xlsx');
44
const path = require('path');
5+
const Step = require('../../actions').Step;
56
// const { exec: getDiffExec } = require('./getDiff');
67
// Function to check for sensitive data patterns
78
const checkForSensitiveData = (cell) => {
@@ -125,6 +126,7 @@ const extractFilePathsFromDiff = (diffContent) => {
125126

126127
const exec = async (req, action) => {
127128
const diffStep = action.steps.find((s) => s.stepName === 'diff');
129+
const step = new Step('checksensitiveData');
128130

129131
if (diffStep && diffStep.content) {
130132
console.log('Diff content:', diffStep.content);
@@ -138,18 +140,18 @@ const exec = async (req, action) => {
138140
const anySensitiveDataDetected = sensitiveDataFound.some(found => found);
139141

140142
if (anySensitiveDataDetected) {
141-
action.pushBlocked = true;
142-
action.error = true;
143-
action.errorMessage = 'Your push has been blocked due to sensitive data detection.';
144-
console.log(action.errorMessage);
143+
step.blocked= true;
144+
step.error = true;
145+
step.errorMessage = 'Your push has been blocked due to sensitive data detection.';
146+
console.log(step.errorMessage);
145147
}
146148
} else {
147149
console.log('No file paths provided in the diff step.');
148150
}
149151
} else {
150152
console.log('No diff content available.');
151153
}
152-
154+
action.addStep(step);
153155
return action; // Returning action for testing purposes
154156
};
155157

test/CheckSensitive.test.js

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// const path = require('path');
22
const { exec } = require('../src/proxy/processors/push-action/checkSensitiveData.js'); // Adjust path as necessary
33
const sinon = require('sinon');
4+
const {Action}=require('../src/proxy/actions/Action.js')
5+
const {Step}=require('../src/proxy/actions/Step.js')
46

57
describe('Sensitive Data Detection', () => {
68
let logStub;
@@ -19,78 +21,76 @@ describe('Sensitive Data Detection', () => {
1921
};
2022

2123
it('should detect sensitive data in CSV file and block execution', async () => {
22-
const action = {
23-
steps: [{
24-
stepName: 'diff',
25-
content: createDiffContent(['test/test_data/sensitive_data.csv']) // Ensure this path is correct
26-
}]
27-
};
24+
const action = new Action('action_id', 'push', 'create', Date.now(), 'owner/repo');
25+
const step = new Step('diff');
26+
27+
// Create diff content simulating sensitive data in CSV
28+
step.setContent(createDiffContent(['test/test_data/sensitive_data.csv']));
29+
action.addStep(step)
30+
2831
await exec(null, action);
2932
sinon.assert.calledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
3033
});
3134

3235
it('should detect sensitive data in XLSX file and block execution', async () => {
33-
const action = {
34-
steps: [{
35-
stepName: 'diff',
36-
content: createDiffContent(['test/test_data/sensitive_data2.xlsx']) // Ensure this path is correct
37-
}]
38-
};
36+
const action = new Action('action_id', 'push', 'create', Date.now(), 'owner/repo');
37+
const step = new Step('diff');
38+
step.setContent(createDiffContent(['test/test_data/sensitive_data2.xlsx']));
39+
action.addStep(step);
40+
3941
await exec(null, action);
4042
sinon.assert.calledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
4143
});
4244

4345
it('should detect sensitive data in a log file and block execution', async () => {
44-
const action = {
45-
steps: [{
46-
stepName: 'diff',
47-
content: createDiffContent(['test/test_data/sensitive_data3.log']) // Ensure this path is correct
48-
}]
49-
};
46+
47+
const action = new Action('action_id', 'push', 'create', Date.now(), 'owner/repo');
48+
const step = new Step('diff');
49+
step.setContent(createDiffContent(['test/test_data/sensitive_data3.log']));
50+
action.addStep(step);
5051
await exec(null, action);
5152
sinon.assert.calledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
5253
});
5354

5455
it('should detect sensitive data in a JSON file and block execution', async () => {
55-
const action = {
56-
steps: [{
57-
stepName: 'diff',
58-
content: createDiffContent(['test/test_data/sensitive_data4.json']) // Ensure this path is correct
59-
}]
60-
};
56+
57+
58+
const action = new Action('action_id', 'push', 'create', Date.now(), 'owner/repo');
59+
const step = new Step('diff');
60+
step.setContent(createDiffContent(['test/test_data/sensitive_data4.json']));
61+
action.addStep(step);
6162
await exec(null, action);
6263
sinon.assert.calledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
6364
});
6465

6566
it('should allow execution if no sensitive data is found', async () => {
66-
const action = {
67-
steps: [{
68-
stepName: 'diff',
69-
content: createDiffContent(['test_data/no_sensitive_data.txt']) // Ensure this path is correct
70-
}]
71-
};
67+
68+
69+
const action = new Action('action_id', 'push', 'create', Date.now(), 'owner/repo');
70+
const step = new Step('diff');
71+
step.setContent(createDiffContent(['test_data/no_sensitive_data.txt']));
72+
action.addStep(step);
7273
await exec(null, action);
7374
sinon.assert.neverCalledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
7475
});
7576

7677
it('should allow execution for an empty file', async () => {
77-
const action = {
78-
steps: [{
79-
stepName: 'diff',
80-
content: createDiffContent(['test_data/empty_file.txt']) // Ensure this path is correct
81-
}]
82-
};
78+
79+
80+
const action = new Action('action_id', 'push', 'create', Date.now(), 'owner/repo');
81+
const step = new Step('diff');
82+
step.setContent(createDiffContent(['test_data/empty_file.txt']));
83+
action.addStep(step);
8384
await exec(null, action);
8485
sinon.assert.neverCalledWith(logStub, sinon.match(/Your push has been blocked due to sensitive data detection/));
8586
});
8687

8788
it('should handle file-not-found scenario gracefully', async () => {
88-
const action = {
89-
steps: [{
90-
stepName: 'diff',
91-
content: createDiffContent(['test_data/non_existent_file.txt']) // Ensure this path is correct
92-
}]
93-
};
89+
90+
const action = new Action('action_id', 'push', 'create', Date.now(), 'owner/repo');
91+
const step = new Step('diff');
92+
step.setContent(createDiffContent(['test_data/non_existent_file.txt']));
93+
action.addStep(step);
9494
try {
9595
await exec(null, action);
9696
} catch (error) {

0 commit comments

Comments
 (0)