-
Notifications
You must be signed in to change notification settings - Fork 3.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add tests for test/lib/utils/replace-info.js
- Loading branch information
Showing
5 changed files
with
134 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 }) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' | ||
) |