Skip to content

Commit 827b9c3

Browse files
committed
added singleton and factory design patterns to CxWrapper
1 parent e4663cb commit 827b9c3

11 files changed

+137
-162
lines changed

src/main/wrapper/CxWrapper.ts

Lines changed: 78 additions & 89 deletions
Large diffs are not rendered by default.

src/main/wrapper/CxWrapperFactory.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
1-
// ICxWrapperFactory.ts
2-
import { CxWrapper } from "./CxWrapper";
3-
import { CxConfig } from "./CxConfig";
1+
import {CxWrapper} from "./CxWrapper";
2+
import {CxConfig} from "./CxConfig";
43

5-
export interface ICxWrapperFactory {
6-
createWrapper(cxScanConfig: CxConfig, logFilePath?: string): Promise<CxWrapper>;
7-
}
4+
class CxWrapperFactory {
5+
static async createWrapper(cxScanConfig: CxConfig, type?: string, logFilePath?: string) {
6+
let wrapper: CxWrapper;
87

9-
class CxWrapperFactory implements ICxWrapperFactory {
10-
async createWrapper(cxScanConfig: CxConfig, logFilePath?: string): Promise<CxWrapper> {
11-
const wrapper = await CxWrapper.getInstance(cxScanConfig, logFilePath);
8+
if (type === 'mock') {
9+
wrapper = new CxWrapper(cxScanConfig, logFilePath);
10+
}
11+
else {
12+
wrapper = await CxWrapper.getInstance(cxScanConfig, logFilePath);
13+
}
1214
await wrapper.init();
1315
return wrapper;
1416
}
1517
}
1618

17-
export default CxWrapperFactory;
19+
export default CxWrapperFactory;

src/tests/AuthTest.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import {CxConfig} from "../main/wrapper/CxConfig";
33
import {BaseTest} from "./BaseTest";
44
import CxWrapperFactory from "../main/wrapper/CxWrapperFactory";
55

6-
const cxWrapperFactory = new CxWrapperFactory();
7-
8-
describe("Authentication validation", () => {
6+
describe("Authentication validation",() => {
97
const cxScanConfig = new BaseTest();
108
it('Result authentication successful case', async () => {
11-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
9+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
1210
const cxCommandOutput: CxCommandOutput = await auth.authValidate();
1311
expect(cxCommandOutput.exitCode).toBe(0);
1412
});
@@ -20,7 +18,7 @@ describe("Authentication validation", () => {
2018
cxScanConfig_fail.clientSecret = "error";
2119
cxScanConfig_fail.tenant = process.env["CX_TENANT"];
2220
cxScanConfig_fail.apiKey = "error";
23-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig_fail);
21+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig_fail,'mock');
2422
const cxCommandOutput: CxCommandOutput = await auth.authValidate();
2523
expect(cxCommandOutput.exitCode).toBe(1);
2624
});

src/tests/ChatTest.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,6 @@ function createOutput(exitCode:number,payload:CxChat):CxCommandOutput {
1313
return output;
1414
}
1515

16-
const cxWrapperFactory = new CxWrapperFactory();
17-
1816
describe("Gpt Chat Cases", () => {
1917
// tests preparation
2018
const cxScanConfig = new BaseTest();
@@ -41,7 +39,7 @@ describe("Gpt Chat Cases", () => {
4139
});
4240

4341
it('KICS Gpt Chat Failed case', async () => {
44-
const originalWrapper: CxWrapper = await cxWrapperFactory.createWrapper(cxScanConfig);
42+
const originalWrapper: CxWrapper = await CxWrapperFactory.createWrapper(cxScanConfig);
4543
const cxCommandOutput = await originalWrapper.kicsChat(
4644
"APIKEY",
4745
"FILE",
@@ -57,7 +55,7 @@ describe("Gpt Chat Cases", () => {
5755
});
5856

5957
it('Sast Gpt Chat Failed case', async () => {
60-
const originalWrapper: CxWrapper = await cxWrapperFactory.createWrapper(cxScanConfig);
58+
const originalWrapper: CxWrapper = await CxWrapperFactory.createWrapper(cxScanConfig);
6159
const cxCommandOutput = await originalWrapper.sastChat(
6260
"APIKEY",
6361
"SOURCE_FILE",

src/tests/LearnMoreDescriptions.test.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,18 @@ import {BaseTest} from "./BaseTest";
22
import {CxCommandOutput} from "../main/wrapper/CxCommandOutput";
33
import CxWrapperFactory from "../main/wrapper/CxWrapperFactory";
44

5-
const cxWrapperFactory = new CxWrapperFactory();
6-
75
describe("LearnMoreDescriptions cases",() => {
86
const cxScanConfig = new BaseTest();
97
it('LearnMoreDescriptions Successful case', async () => {
10-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
8+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
119
const queryId = process.env.CX_TEST_QUERY_ID;
1210
const data = await auth.learnMore(queryId !== undefined? queryId : "16772998409937314312")
1311
const cxCommandOutput: CxCommandOutput = data;
1412
expect(cxCommandOutput.payload.length).toBeGreaterThan(0);
1513
})
1614

1715
it('LearnMoreDescriptions Failure case', async () => {
18-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
16+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
1917
const data = await auth.learnMore("")
2018
const cxCommandOutput: CxCommandOutput = data;
2119
expect(cxCommandOutput.status).toBe("Value of query-id is invalid\n");

src/tests/MaskTest.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@ import {CxCommandOutput} from "../main/wrapper/CxCommandOutput";
22
import {BaseTest} from "./BaseTest";
33
import CxWrapperFactory from "../main/wrapper/CxWrapperFactory";
44

5-
const cxWrapperFactory = new CxWrapperFactory();
6-
75
describe("Mask cases",() => {
86
const cxScanConfig = new BaseTest();
97
it('Mask Successful case', async () => {
10-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
8+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
119
const data = await auth.maskSecrets("dist/tests/data/package.json")
1210
const cxCommandOutput: CxCommandOutput = data;
1311
expect(cxCommandOutput.payload.length).toEqual(1);

src/tests/PredicateTest.test.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@ import CxResult from '../main/results/CxResult';
44
import {CxConstants} from '../main/wrapper/CxConstants';
55
import CxWrapperFactory from "../main/wrapper/CxWrapperFactory";
66

7-
const cxWrapperFactory = new CxWrapperFactory();
8-
97
describe("Triage cases", () => {
108
const cxScanConfig = new BaseTest();
119

1210
it('Triage Successful case', async () => {
13-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
11+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
1412

1513
const scanList: CxCommandOutput = await auth.scanList("statuses=Completed,limit=100");
1614
let result: CxResult;

src/tests/ProjectTest.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ import {CxParamType} from "../main/wrapper/CxParamType";
44
import CxScan from "../main/scan/CxScan";
55
import CxWrapperFactory from "../main/wrapper/CxWrapperFactory";
66

7-
const cxWrapperFactory = new CxWrapperFactory();
8-
97
describe("ProjectList cases",() => {
108
const cxScanConfig = new BaseTest();
119
it('ProjectList Successful case', async () => {
12-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
10+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
1311
const data = await auth.projectList("");
1412
const cxCommandOutput: CxCommandOutput = data;
1513
expect(cxCommandOutput.payload.length).toBeGreaterThan(0);
@@ -21,7 +19,7 @@ describe("ProjectList cases",() => {
2119
params.set(CxParamType.S, "./src");
2220
params.set(CxParamType.FILTER, "*.ts,!**/node_modules/**/*");
2321
params.set(CxParamType.BRANCH, "master");
24-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
22+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
2523
const cxCommandOutput: CxCommandOutput = await auth.scanCreate(params);
2624
const scan: CxScan = cxCommandOutput.payload.pop();
2725

@@ -36,7 +34,7 @@ describe("ProjectList cases",() => {
3634
params.set(CxParamType.S, "./src");
3735
params.set(CxParamType.FILTER, "*.ts,!**/node_modules/**/*");
3836
params.set(CxParamType.BRANCH, "master");
39-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
37+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
4038
const cxCommandOutput: CxCommandOutput = await auth.scanCreate(params);
4139
const scan: CxScan = cxCommandOutput.payload.pop();
4240

src/tests/RemediationTest.test.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import {CxCommandOutput} from "../main/wrapper/CxCommandOutput";
33
import CxKicsRemediation from "../main/remediation/CxKicsRemediation";
44
import CxWrapperFactory from "../main/wrapper/CxWrapperFactory";
55

6-
const cxWrapperFactory = new CxWrapperFactory();
7-
86
describe("SCA Remediation cases",() => {
97
const cxScanConfig = new BaseTest();
108
it('SCA Remediation Successful case ', async () => {
11-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
9+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
1210
const e:CxCommandOutput = await auth.scaRemediation("dist/tests/data/package.json","copyfiles","1.2")
1311
expect(e.exitCode).toBe(0);
1412
});
@@ -17,7 +15,7 @@ describe("SCA Remediation cases",() => {
1715
describe("Kics Remediation cases",() => {
1816
const cxScanConfig = new BaseTest();
1917
it('Kics Remediation Successful case', async () => {
20-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
18+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
2119
const e:[Promise<CxCommandOutput>,any] = await auth.kicsRemediation("dist/tests/data/results.json",__dirname+"/data","docker")
2220
const output = await e[0];
2321
const remediation: CxKicsRemediation = output.payload[0];
@@ -26,7 +24,7 @@ describe("Kics Remediation cases",() => {
2624
});
2725

2826
it('Kics Remediation Successful case with filter', async () => {
29-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
27+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
3028
const e:[Promise<CxCommandOutput>,any] = await auth.kicsRemediation("dist/tests/data/results.json",__dirname+"/data/","","9574288c118e8c87eea31b6f0b011295a39ec5e70d83fb70e839b8db4a99eba8")
3129
const output = await e[0];
3230
const remediation: CxKicsRemediation = output.payload[0];

src/tests/ResultTest.test.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,10 @@ import {BaseTest} from "./BaseTest";
33
import * as fs from "fs";
44
import CxWrapperFactory from "../main/wrapper/CxWrapperFactory";
55

6-
const cxWrapperFactory = new CxWrapperFactory();
7-
86
describe("Results cases",() => {
97
const cxScanConfig = new BaseTest();
108
it('Result Test Successful case', async () => {
11-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
9+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
1210
const cxCommandOutput: CxCommandOutput = await auth.scanList("statuses=Completed");
1311
const sampleId = cxCommandOutput.payload.pop().id;
1412

@@ -18,7 +16,7 @@ describe("Results cases",() => {
1816
});
1917

2018
it('Result Test With Agent Flug Successful case', async () => {
21-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
19+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
2220
const cxCommandOutput: CxCommandOutput = await auth.scanList("statuses=Completed");
2321
const sampleId = cxCommandOutput.payload.pop().id;
2422

@@ -28,7 +26,7 @@ describe("Results cases",() => {
2826
});
2927

3028
it('Result List Successful case', async () => {
31-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
29+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
3230
const scanList: CxCommandOutput = await auth.scanList("statuses=Completed");
3331
let output;
3432
while (!output && scanList && scanList.payload && scanList.payload.length > 0) {
@@ -44,7 +42,7 @@ describe("Results cases",() => {
4442
});
4543

4644
it('Result summary html file generation successful case', async () => {
47-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
45+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
4846
const cxCommandOutput: CxCommandOutput = await auth.scanList("statuses=Completed");
4947
const sampleId = cxCommandOutput.payload.pop().id;
5048
await auth.getResults(sampleId,"summaryHTML","test", ".");
@@ -53,15 +51,15 @@ describe("Results cases",() => {
5351
});
5452

5553
it('Result summary html string successful case', async () => {
56-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
54+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
5755
const cxCommandOutput: CxCommandOutput = await auth.scanList("statuses=Completed");
5856
const sampleId = cxCommandOutput.payload.pop().id;
5957
const written = await auth.getResultsSummary(sampleId);
6058
expect(written.payload.length).toBeGreaterThan(0);
6159
});
6260

6361
it('Result codebashing successful case', async () => {
64-
const auth = await cxWrapperFactory.createWrapper(cxScanConfig);
62+
const auth = await CxWrapperFactory.createWrapper(cxScanConfig);
6563
const cxCommandOutput: CxCommandOutput = await auth.codeBashingList("79","PHP","Reflected XSS All Clients");
6664
expect(cxCommandOutput.payload.length).toBeGreaterThan(0);
6765
});

0 commit comments

Comments
 (0)