Skip to content

Commit

Permalink
test: add tests for test/lib/utils/replace-info.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ruyadorno committed Oct 30, 2020
1 parent e34f975 commit 3109d36
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 13 deletions.
6 changes: 4 additions & 2 deletions lib/utils/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ module.exports = (er) => {
const short = []
const detail = []

er.message = replaceInfo(er.message)
er.stack = replaceInfo(er.stack)
if (er.message)
er.message = replaceInfo(er.message)
if (er.stack)
er.stack = replaceInfo(er.stack)

switch (er.code) {
case 'ERESOLVE':
Expand Down
33 changes: 22 additions & 11 deletions lib/utils/replace-info.js
Original file line number Diff line number Diff line change
@@ -1,22 +1,33 @@
const URL = require('url')
'use strict'

// replaces auth info in an array
// of arguments or in a strings
const URL = require('url').URL

// replaces auth info in an array of arguments or in a strings
function replaceInfo (arg) {
const isArray = Array.isArray(arg)
const isString = typeof arg === 'string'
const isString = str => typeof str === 'string'

if (!isArray && !isString) return arg
if (!isArray && !isString(arg))
return arg

const args = isString ? arg.split(' ') : arg
const info = args.map(arg => {
const testUrlAndReplace = str => {
try {
const url = new URL(arg)
return url.password === '' ? arg : arg.replace(url.password, '***')
} catch (e) { return arg }
const url = new URL(str)
return url.password === '' ? str : str.replace(url.password, '***')
} catch (e) {
return str
}
}

const args = isString(arg) ? arg.split(' ') : arg
const info = args.map(a => {
if (isString(a) && a.indexOf(' ') > -1)
return a.split(' ').map(testUrlAndReplace).join(' ')

return testUrlAndReplace(a)
})

return isString ? info.join(' ') : info
return isString(arg) ? info.join(' ') : info
}

module.exports = replaceInfo
12 changes: 12 additions & 0 deletions tap-snapshots/test-lib-utils-error-message.js-TAP.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1209,6 +1209,18 @@ Object {
}
`

exports[`test/lib/utils/error-message.js TAP replace message/stack sensistive info > must match snapshot 1`] = `
Object {
"detail": Array [],
"summary": Array [
Array [
"audit",
"Error at registry: https://user:***@registry.npmjs.org/",
],
],
}
`

exports[`v TAP json parse json somewhere else > must match snapshot 1`] = `
Object {
"detail": Array [
Expand Down
18 changes: 18 additions & 0 deletions test/lib/utils/error-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,24 @@ t.test('just simple messages', t => {
})
})

t.test('replace message/stack sensistive info', t => {
npm.command = 'audit'
const path = '/some/path'
const pkgid = 'some@package'
const file = '/some/file'
const stack = 'dummy stack trace at https://user:[email protected]/'
const message = 'Error at registry: https://user:[email protected]/'
const er = Object.assign(new Error(message), {
code: 'ENOAUDIT',
path,
pkgid,
file,
stack,
})
t.matchSnapshot(errorMessage(er))
t.end()
})

t.test('bad engine with config loaded', t => {
npm.config.loaded = true
t.teardown(() => { npm.config.loaded = false })
Expand Down
78 changes: 78 additions & 0 deletions test/lib/utils/replace-info.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const t = require('tap')
const replaceInfo = require('../../../lib/utils/replace-info.js')

t.equal(
replaceInfo(),
undefined,
'should return undefined item'
)

t.equal(
replaceInfo(null),
null,
'should return null'
)

t.equal(
replaceInfo(1234),
1234,
'should return numbers'
)

t.equal(
replaceInfo('https://user:[email protected]/'),
'https://user:***@registry.npmjs.org/',
'should replace single item'
)

t.equal(
replaceInfo('https://example.npmjs.org'),
'https://example.npmjs.org',
'should not replace single item with no password'
)

t.equal(
replaceInfo('foo bar https://example.npmjs.org lorem ipsum'),
'foo bar https://example.npmjs.org lorem ipsum',
'should not replace single item with no password with multiple items'
)

t.equal(
replaceInfo('https://user:[email protected]/ http://a:[email protected]'),
'https://user:***@registry.npmjs.org/ http://a:***@reg.github.com',
'should replace multiple items on a string'
)

t.equal(
replaceInfo('Something https://user:[email protected]/ foo bar'),
'Something https://user:***@registry.npmjs.org/ foo bar',
'should replace single item withing a phrase'
)

t.deepEqual(
replaceInfo([
'Something https://user:[email protected]/ foo bar',
'http://foo:[email protected]',
'http://example.npmjs.org',
]),
[
'Something https://user:***@registry.npmjs.org/ foo bar',
'http://foo:***@registry.npmjs.org',
'http://example.npmjs.org',
],
'should replace single item within a phrase'
)

t.deepEqual(
replaceInfo([
'Something https://user:[email protected]/ foo bar',
null,
[],
]),
[
'Something https://user:***@registry.npmjs.org/ foo bar',
null,
[],
],
'should ignore invalid items of array'
)

0 comments on commit 3109d36

Please sign in to comment.