Skip to content

Commit 21375c6

Browse files
wraithgarlukekarrys
authored andcommitted
chore: add fallback audit to tests
1 parent ced0acf commit 21375c6

File tree

2 files changed

+97
-20
lines changed

2 files changed

+97
-20
lines changed

tap-snapshots/test/lib/commands/audit.js.test.cjs

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
* Make sure to inspect the output below. Do not ignore changes!
66
*/
77
'use strict'
8-
exports[`test/lib/commands/audit.js TAP audit fix > lockfile has [email protected] 1`] = `
8+
exports[`test/lib/commands/audit.js TAP audit fix - bulk endpoint > lockfile has [email protected] 1`] = `
99
{
1010
"name": "test-dep",
1111
"version": "1.0.0",
@@ -34,13 +34,28 @@ exports[`test/lib/commands/audit.js TAP audit fix > lockfile has [email protected].
3434
3535
`
3636

37-
exports[`test/lib/commands/audit.js TAP audit fix > must match snapshot 1`] = `
37+
exports[`test/lib/commands/audit.js TAP audit fix - bulk endpoint > must match snapshot 1`] = `
3838
3939
added 1 package, and audited 2 packages in xxx
4040
4141
found 0 vulnerabilities
4242
`
4343

44+
exports[`test/lib/commands/audit.js TAP fallback audit > must match snapshot 1`] = `
45+
# npm audit report
46+
47+
test-dep-a 1.0.0
48+
Severity: high
49+
Test advisory 100 - https://github.com/advisories/GHSA-100
50+
fix available via \`npm audit fix\`
51+
node_modules/test-dep-a
52+
53+
1 high severity vulnerability
54+
55+
To address all issues, run:
56+
npm audit fix
57+
`
58+
4459
exports[`test/lib/commands/audit.js TAP json audit > must match snapshot 1`] = `
4560
{
4661
"auditReportVersion": 2,
@@ -98,14 +113,14 @@ exports[`test/lib/commands/audit.js TAP json audit > must match snapshot 1`] = `
98113
exports[`test/lib/commands/audit.js TAP normal audit > must match snapshot 1`] = `
99114
# npm audit report
100115
101-
test-dep-a *
116+
test-dep-a 1.0.0
102117
Severity: high
103118
Test advisory 100 - https://github.com/advisories/GHSA-100
104-
No fix available
119+
fix available via \`npm audit fix\`
105120
node_modules/test-dep-a
106121
107122
1 high severity vulnerability
108123
109-
Some issues need review, and may require choosing
110-
a different dependency.
124+
To address all issues, run:
125+
npm audit fix
111126
`

test/lib/commands/audit.js

Lines changed: 76 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ const t = require('tap')
22

33
const { load: loadMockNpm } = require('../../fixtures/mock-npm')
44
const MockRegistry = require('../../fixtures/mock-registry.js')
5-
const util = require('util')
65
const zlib = require('zlib')
7-
const gzip = util.promisify(zlib.gzip)
6+
const gzip = zlib.gzipSync
7+
const gunzip = zlib.gunzipSync
88
const path = require('path')
99
const fs = require('fs')
1010

@@ -43,7 +43,14 @@ const tree = {
4343
},
4444
},
4545
}),
46-
'test-dep-a': {
46+
'test-dep-a-vuln': {
47+
'package.json': JSON.stringify({
48+
name: 'test-dep-a',
49+
version: '1.0.0',
50+
}),
51+
'vulnerable.txt': 'vulnerable test-dep-a',
52+
},
53+
'test-dep-a-fixed': {
4754
'package.json': JSON.stringify({
4855
name: 'test-dep-a',
4956
version: '1.0.1',
@@ -66,8 +73,11 @@ t.test('normal audit', async t => {
6673
packuments: [{ version: '1.0.0' }, { version: '1.0.1' }],
6774
})
6875
await registry.package({ manifest })
69-
const advisory = registry.advisory({ id: 100 })
70-
const bulkBody = await gzip(JSON.stringify({ 'test-dep-a': ['1.0.0'] }))
76+
const advisory = registry.advisory({
77+
id: 100,
78+
vulnerable_versions: '<1.0.1',
79+
})
80+
const bulkBody = gzip(JSON.stringify({ 'test-dep-a': ['1.0.0'] }))
7181
registry.nock.post('/-/npm/v1/security/advisories/bulk', bulkBody)
7282
.reply(200, {
7383
'test-dep-a': [advisory],
@@ -79,6 +89,55 @@ t.test('normal audit', async t => {
7989
t.matchSnapshot(joinedOutput())
8090
})
8191

92+
t.test('fallback audit ', async t => {
93+
const { npm, joinedOutput } = await loadMockNpm(t, {
94+
prefixDir: tree,
95+
})
96+
const registry = new MockRegistry({
97+
tap: t,
98+
registry: npm.config.get('registry'),
99+
})
100+
const manifest = registry.manifest({
101+
name: 'test-dep-a',
102+
packuments: [{ version: '1.0.0' }, { version: '1.0.1' }],
103+
})
104+
await registry.package({ manifest })
105+
const advisory = registry.advisory({
106+
id: 100,
107+
module_name: 'test-dep-a',
108+
vulnerable_versions: '<1.0.1',
109+
findings: [{ version: '1.0.0', paths: ['test-dep-a'] }],
110+
})
111+
registry.nock
112+
.post('/-/npm/v1/security/advisories/bulk').reply(404)
113+
.post('/-/npm/v1/security/audits/quick', body => {
114+
const unzipped = JSON.parse(gunzip(Buffer.from(body, 'hex')))
115+
return t.match(unzipped, {
116+
name: 'test-dep',
117+
version: '1.0.0',
118+
requires: { 'test-dep-a': '*' },
119+
dependencies: { 'test-dep-a': { version: '1.0.0' } },
120+
})
121+
}).reply(200, {
122+
actions: [],
123+
muted: [],
124+
advisories: {
125+
100: advisory,
126+
},
127+
metadata: {
128+
vulnerabilities: { info: 0, low: 0, moderate: 0, high: 1, critical: 0 },
129+
dependencies: 1,
130+
devDependencies: 0,
131+
optionalDependencies: 0,
132+
totalDependencies: 1,
133+
},
134+
})
135+
await npm.exec('audit', [])
136+
t.ok(process.exitCode, 'would have exited uncleanly')
137+
process.exitCode = 0
138+
t.matchSnapshot(joinedOutput())
139+
})
140+
82141
t.test('json audit', async t => {
83142
const { npm, joinedOutput } = await loadMockNpm(t, {
84143
prefixDir: tree,
@@ -97,7 +156,7 @@ t.test('json audit', async t => {
97156
})
98157
await registry.package({ manifest })
99158
const advisory = registry.advisory({ id: 100 })
100-
const bulkBody = await gzip(JSON.stringify({ 'test-dep-a': ['1.0.0'] }))
159+
const bulkBody = gzip(JSON.stringify({ 'test-dep-a': ['1.0.0'] }))
101160
registry.nock.post('/-/npm/v1/security/advisories/bulk', bulkBody)
102161
.reply(200, {
103162
'test-dep-a': [advisory],
@@ -109,7 +168,7 @@ t.test('json audit', async t => {
109168
t.matchSnapshot(joinedOutput())
110169
})
111170

112-
t.test('audit fix', async t => {
171+
t.test('audit fix - bulk endpoint', async t => {
113172
const { npm, joinedOutput } = await loadMockNpm(t, {
114173
prefixDir: tree,
115174
})
@@ -124,20 +183,23 @@ t.test('audit fix', async t => {
124183
await registry.package({
125184
manifest,
126185
tarballs: {
127-
'1.0.1': path.join(npm.prefix, 'test-dep-a'),
186+
'1.0.1': path.join(npm.prefix, 'test-dep-a-fixed'),
128187
},
129188
})
130189
const advisory = registry.advisory({ id: 100, vulnerable_versions: '1.0.0' })
131-
// Can't validate this request body because it changes with each node
132-
// version/npm version and nock's body validation is not async, while
133-
// zlib.gunzip is
134-
registry.nock.post('/-/npm/v1/security/advisories/bulk')
190+
registry.nock.post('/-/npm/v1/security/advisories/bulk', body => {
191+
const unzipped = JSON.parse(gunzip(Buffer.from(body, 'hex')))
192+
return t.same(unzipped, { 'test-dep-a': ['1.0.0'] })
193+
})
135194
.reply(200, { // first audit
136195
'test-dep-a': [advisory],
137196
})
138-
.post('/-/npm/v1/security/advisories/bulk')
197+
.post('/-/npm/v1/security/advisories/bulk', body => {
198+
const unzipped = JSON.parse(gunzip(Buffer.from(body, 'hex')))
199+
return t.same(unzipped, { 'test-dep-a': ['1.0.1'] })
200+
})
139201
.reply(200, { // after fix
140-
'test-dep-a': [advisory],
202+
'test-dep-a': [],
141203
})
142204
await npm.exec('audit', ['fix'])
143205
t.matchSnapshot(joinedOutput())

0 commit comments

Comments
 (0)