Skip to content

Commit 1bfae41

Browse files
committed
Update tests
1 parent d38ce6b commit 1bfae41

File tree

3 files changed

+87
-71
lines changed

3 files changed

+87
-71
lines changed

test/common.js

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
const path = require("path");
2+
const Allowlist = require("../lib/allowlist");
3+
14
function summaryWithDefault(additions = {}) {
25
const summary = {
36
allowlistedModulesFound: [],
@@ -12,4 +15,31 @@ function summaryWithDefault(additions = {}) {
1215
return { ...summary, ...additions };
1316
}
1417

15-
module.exports = { summaryWithDefault };
18+
function config(additions) {
19+
const defaultConfig = {
20+
levels: {
21+
low: false,
22+
moderate: false,
23+
high: false,
24+
critical: false,
25+
},
26+
"report-type": "important",
27+
allowlist: new Allowlist(),
28+
"show-not-found": false,
29+
"retry-count": 5,
30+
directory: "./",
31+
registry: undefined,
32+
"pass-enoaudit": false,
33+
};
34+
return { ...defaultConfig, ...additions };
35+
}
36+
37+
function testDir(s) {
38+
return path.resolve(__dirname, s);
39+
}
40+
41+
module.exports = {
42+
summaryWithDefault,
43+
config,
44+
testDir,
45+
};

test/npm-auditer.js

+1-25
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
const { expect } = require("chai");
2-
const path = require("path");
32
const { audit, report } = require("../lib/npm-auditer");
43
const Allowlist = require("../lib/allowlist");
5-
const { summaryWithDefault } = require("./common");
4+
const { summaryWithDefault, config, testDir } = require("./common");
65

76
const reportNpmCritical = require("./npm-critical/npm-output.json");
87
const reportNpmHighSeverity = require("./npm-high/npm-output.json");
@@ -11,29 +10,6 @@ const reportNpmAllowlistedPath = require("./npm-allowlisted-path/npm-output.json
1110
const reportNpmLow = require("./npm-low/npm-output.json");
1211
const reportNpmNone = require("./npm-none/npm-output.json");
1312

14-
function config(additions) {
15-
const defaultConfig = {
16-
levels: {
17-
low: false,
18-
moderate: false,
19-
high: false,
20-
critical: false,
21-
},
22-
"report-type": "important",
23-
allowlist: new Allowlist(),
24-
"show-not-found": false,
25-
"retry-count": 5,
26-
directory: "./",
27-
registry: undefined,
28-
"pass-enoaudit": false,
29-
};
30-
return { ...defaultConfig, ...additions };
31-
}
32-
33-
function testDir(s) {
34-
return path.resolve(__dirname, s);
35-
}
36-
3713
// To modify what slow times are, need to use
3814
// function() {} instead of () => {}
3915
describe("npm-auditer", function testNpmAuditer() {

test/npm7-auditer.js

+55-45
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,14 @@
11
const { expect } = require("chai");
2-
const path = require("path");
32
const { audit, report } = require("../lib/npm-auditer");
43
const Allowlist = require("../lib/allowlist");
5-
const { summaryWithDefault } = require("./common");
4+
const { summaryWithDefault, config, testDir } = require("./common");
65

76
const reportNpmCritical = require("./npm-critical/npm7-output.json");
8-
const reportNpmHigh = require("./npm-high/npm7-output.json");
9-
const reportNpmModerate = require("./npm-moderate/npm7-output.json");
10-
const reportNpmAllowlisted = require("./npm-allowlisted-path/npm7-output.json");
11-
const reportNpmNone = require("./npm-none/npm7-output.json");
7+
const reportNpmHighSeverity = require("./npm-high/npm7-output.json");
8+
const reportNpmModerateSeverity = require("./npm-moderate/npm7-output.json");
9+
const reportNpmAllowlistedPath = require("./npm-allowlisted-path/npm7-output.json");
1210
const reportNpmLow = require("./npm-low/npm7-output.json");
13-
14-
function config(additions) {
15-
const defaultConfig = {
16-
levels: {
17-
low: false,
18-
moderate: false,
19-
high: false,
20-
critical: false,
21-
},
22-
"report-type": "important",
23-
allowlist: new Allowlist(),
24-
"show-not-found": false,
25-
"retry-count": 5,
26-
directory: "./",
27-
registry: undefined,
28-
"pass-enoaudit": false,
29-
};
30-
return { ...defaultConfig, ...additions };
31-
}
32-
33-
function testDir(s) {
34-
return path.resolve(__dirname, s);
35-
}
11+
const reportNpmNone = require("./npm-none/npm7-output.json");
3612

3713
describe("npm7-auditer", function testNpm7Auditer() {
3814
it("prints full report with critical severity", () => {
@@ -65,7 +41,7 @@ describe("npm7-auditer", function testNpm7Auditer() {
6541
});
6642
it("reports summary with high severity", () => {
6743
const summary = report(
68-
reportNpmHigh,
44+
reportNpmHighSeverity,
6945
config({
7046
directory: testDir("npm-high"),
7147
levels: { high: true },
@@ -82,7 +58,7 @@ describe("npm7-auditer", function testNpm7Auditer() {
8258
});
8359
it("reports important info with moderate severity", () => {
8460
const summary = report(
85-
reportNpmModerate,
61+
reportNpmModerateSeverity,
8662
config({
8763
directory: testDir("npm-moderate"),
8864
levels: { moderate: true },
@@ -99,7 +75,7 @@ describe("npm7-auditer", function testNpm7Auditer() {
9975
});
10076
it("does not report moderate severity if it set to false", () => {
10177
const summary = report(
102-
reportNpmModerate,
78+
reportNpmModerateSeverity,
10379
config({
10480
directory: testDir("npm-moderate"),
10581
levels: { moderate: false },
@@ -110,7 +86,7 @@ describe("npm7-auditer", function testNpm7Auditer() {
11086
});
11187
it("[DEPRECATED - advisories] ignores an advisory if it is whitelisted", () => {
11288
const summary = report(
113-
reportNpmModerate,
89+
reportNpmModerateSeverity,
11490
config({
11591
directory: testDir("npm-moderate"),
11692
levels: { moderate: true },
@@ -126,7 +102,7 @@ describe("npm7-auditer", function testNpm7Auditer() {
126102
});
127103
it("ignores an advisory if it is allowlisted", () => {
128104
const summary = report(
129-
reportNpmModerate,
105+
reportNpmModerateSeverity,
130106
config({
131107
directory: testDir("npm-moderate"),
132108
levels: { moderate: true },
@@ -142,7 +118,7 @@ describe("npm7-auditer", function testNpm7Auditer() {
142118
});
143119
it("[DEPRECATED - advisories] does not ignore an advisory that is not whitelisted", () => {
144120
const summary = report(
145-
reportNpmModerate,
121+
reportNpmModerateSeverity,
146122
config({
147123
directory: testDir("npm-moderate"),
148124
levels: { moderate: true },
@@ -160,7 +136,7 @@ describe("npm7-auditer", function testNpm7Auditer() {
160136
});
161137
it("does not ignore an advisory that is not allowlisted", () => {
162138
const summary = report(
163-
reportNpmModerate,
139+
reportNpmModerateSeverity,
164140
config({
165141
directory: testDir("npm-moderate"),
166142
levels: { moderate: true },
@@ -178,39 +154,73 @@ describe("npm7-auditer", function testNpm7Auditer() {
178154
});
179155
it("[DEPRECATED - path-whitelist] reports only vulnerabilities with a not whitelisted path", () => {
180156
const summary = report(
181-
reportNpmAllowlisted,
157+
reportNpmAllowlistedPath,
182158
config({
183159
directory: testDir("npm-allowlisted-path"),
184160
levels: { moderate: true },
185161
allowlist: Allowlist.mapConfigToAllowlist({
186-
"path-whitelist": ["axios|github-build"],
162+
"path-whitelist": ["880|github-build>axios"],
187163
}),
188164
}),
189165
(_summary) => _summary
190166
);
191167
expect(summary).to.eql(
192168
summaryWithDefault({
193-
allowlistedPathsFound: ["axios|github-build"],
169+
allowlistedPathsFound: ["880|github-build>axios"],
194170
failedLevelsFound: ["moderate"],
195-
advisoriesFound: ["axios"],
171+
advisoriesFound: [880],
196172
})
197173
);
198174
});
199175
it("reports only vulnerabilities with a not allowlisted path", () => {
200176
const summary = report(
201-
reportNpmAllowlisted,
177+
reportNpmAllowlistedPath,
202178
config({
203179
directory: testDir("npm-allowlisted-path"),
204180
levels: { moderate: true },
205-
allowlist: new Allowlist(["axios|github-build"]),
181+
allowlist: new Allowlist(["880|github-build>axios"]),
206182
}),
207183
(_summary) => _summary
208184
);
209185
expect(summary).to.eql(
210186
summaryWithDefault({
211-
allowlistedPathsFound: ["axios|github-build"],
187+
allowlistedPathsFound: ["880|github-build>axios"],
212188
failedLevelsFound: ["moderate"],
213-
advisoriesFound: ["axios"],
189+
advisoriesFound: [880],
190+
})
191+
);
192+
});
193+
it("[DEPRECATED - path-whitelist] whitelist all vulnerabilities with a whitelisted path", () => {
194+
const summary = report(
195+
reportNpmAllowlistedPath,
196+
config({
197+
directory: testDir("npm-allowlisted-path"),
198+
levels: { moderate: true },
199+
allowlist: Allowlist.mapConfigToAllowlist({
200+
"path-whitelist": ["880|axios", "880|github-build>axios"],
201+
}),
202+
}),
203+
(_summary) => _summary
204+
);
205+
expect(summary).to.eql(
206+
summaryWithDefault({
207+
allowlistedPathsFound: ["880|axios", "880|github-build>axios"],
208+
})
209+
);
210+
});
211+
it("allowlist all vulnerabilities with a allowlisted path", () => {
212+
const summary = report(
213+
reportNpmAllowlistedPath,
214+
config({
215+
directory: testDir("npm-allowlisted-path"),
216+
levels: { moderate: true },
217+
allowlist: new Allowlist(["880|axios", "880|github-build>axios"]),
218+
}),
219+
(_summary) => _summary
220+
);
221+
expect(summary).to.eql(
222+
summaryWithDefault({
223+
allowlistedPathsFound: ["880|axios", "880|github-build>axios"],
214224
})
215225
);
216226
});
@@ -226,7 +236,7 @@ describe("npm7-auditer", function testNpm7Auditer() {
226236
expect(summary).to.eql(
227237
summaryWithDefault({
228238
failedLevelsFound: ["low"],
229-
advisoriesFound: [786, "braces"],
239+
advisoriesFound: [786],
230240
})
231241
);
232242
});

0 commit comments

Comments
 (0)