Skip to content

Commit 65897ec

Browse files
committed
chore: add fallback audit to tests
1 parent 9f57404 commit 65897ec

File tree

2 files changed

+97
-19
lines changed

2 files changed

+97
-19
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 test-dep-a@1.0.1 1`] = `
8+
exports[`test/lib/commands/audit.js TAP audit fix - bulk endpoint > lockfile has test-dep-a@1.0.1 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 test-dep-a@1.0.
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 & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@ const { load: loadMockNpm } = require('../../fixtures/mock-npm')
44
const MockRegistry = require('../../fixtures/mock-registry.js')
55
const util = require('util')
66
const zlib = require('zlib')
7-
const gzip = util.promisify(zlib.gzip)
7+
const gzip = zlib.gzipSync
8+
const gunzip = zlib.gunzipSync
89
const path = require('path')
910
const fs = require('fs')
1011

@@ -43,7 +44,14 @@ const tree = {
4344
},
4445
},
4546
}),
46-
'test-dep-a': {
47+
'test-dep-a-vuln': {
48+
'package.json': JSON.stringify({
49+
name: 'test-dep-a',
50+
version: '1.0.0',
51+
}),
52+
'vulnerable.txt': 'vulnerable test-dep-a',
53+
},
54+
'test-dep-a-fixed': {
4755
'package.json': JSON.stringify({
4856
name: 'test-dep-a',
4957
version: '1.0.1',
@@ -66,8 +74,11 @@ t.test('normal audit', async t => {
6674
packuments: [{ version: '1.0.0' }, { version: '1.0.1' }],
6775
})
6876
await registry.package({ manifest })
69-
const advisory = registry.advisory({ id: 100 })
70-
const bulkBody = await gzip(JSON.stringify({ 'test-dep-a': ['1.0.0'] }))
77+
const advisory = registry.advisory({
78+
id: 100,
79+
vulnerable_versions: '<1.0.1',
80+
})
81+
const bulkBody = gzip(JSON.stringify({ 'test-dep-a': ['1.0.0'] }))
7182
registry.nock.post('/-/npm/v1/security/advisories/bulk', bulkBody)
7283
.reply(200, {
7384
'test-dep-a': [advisory],
@@ -79,6 +90,55 @@ t.test('normal audit', async t => {
7990
t.matchSnapshot(joinedOutput())
8091
})
8192

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

112-
t.test('audit fix', async t => {
172+
t.test('audit fix - bulk endpoint', async t => {
113173
const { npm, joinedOutput } = await loadMockNpm(t, {
114174
prefixDir: tree,
115175
})
@@ -124,20 +184,23 @@ t.test('audit fix', async t => {
124184
await registry.package({
125185
manifest,
126186
tarballs: {
127-
'1.0.1': path.join(npm.prefix, 'test-dep-a'),
187+
'1.0.1': path.join(npm.prefix, 'test-dep-a-fixed'),
128188
},
129189
})
130190
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')
191+
registry.nock.post('/-/npm/v1/security/advisories/bulk', body => {
192+
const unzipped = JSON.parse(gunzip(Buffer.from(body, 'hex')))
193+
return t.same(unzipped, { 'test-dep-a': [ '1.0.0' ] })
194+
})
135195
.reply(200, { // first audit
136196
'test-dep-a': [advisory],
137197
})
138-
.post('/-/npm/v1/security/advisories/bulk')
198+
.post('/-/npm/v1/security/advisories/bulk', body => {
199+
const unzipped = JSON.parse(gunzip(Buffer.from(body, 'hex')))
200+
return t.same(unzipped, { 'test-dep-a': [ '1.0.1' ] })
201+
})
139202
.reply(200, { // after fix
140-
'test-dep-a': [advisory],
203+
'test-dep-a': [],
141204
})
142205
await npm.exec('audit', ['fix'])
143206
t.matchSnapshot(joinedOutput())

0 commit comments

Comments
 (0)