forked from npm/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: convert tests to use mock-npm options (npm#6484)
This converts some tests to use the available mock-npm options in favor of manually calling mockGlobals and npm.config.set.
- Loading branch information
1 parent
f3cfe12
commit 00e6217
Showing
10 changed files
with
141 additions
and
201 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
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 |
---|---|---|
|
@@ -8,6 +8,34 @@ const mockGlobals = require('@npmcli/mock-globals') | |
const MockRegistry = require('@npmcli/mock-registry') | ||
const stream = require('stream') | ||
|
||
const mockAddUser = async (t, { stdin: stdinLines, registry: registryUrl, ...options } = {}) => { | ||
let stdin | ||
if (stdinLines) { | ||
stdin = new stream.PassThrough() | ||
for (const l of stdinLines) { | ||
stdin.write(l + '\n') | ||
} | ||
mockGlobals(t, { | ||
'process.stdin': stdin, | ||
'process.stdout': new stream.PassThrough(), // to quiet readline | ||
}, { replace: true }) | ||
} | ||
const mock = await loadMockNpm(t, { | ||
...options, | ||
command: 'adduser', | ||
}) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: registryUrl ?? mock.npm.config.get('registry'), | ||
}) | ||
return { | ||
registry, | ||
stdin, | ||
rc: () => ini.parse(fs.readFileSync(path.join(mock.home, '.npmrc'), 'utf8')), | ||
...mock, | ||
} | ||
} | ||
|
||
t.test('usage', async t => { | ||
const { npm } = await loadMockNpm(t) | ||
const adduser = await npm.cmd('adduser') | ||
|
@@ -16,15 +44,8 @@ t.test('usage', async t => { | |
|
||
t.test('legacy', async t => { | ||
t.test('simple adduser', async t => { | ||
const stdin = new stream.PassThrough() | ||
stdin.write('test-user\n') | ||
stdin.write('test-password\n') | ||
stdin.write('[email protected]\n') | ||
mockGlobals(t, { | ||
'process.stdin': stdin, | ||
'process.stdout': new stream.PassThrough(), // to quiet readline | ||
}, { replace: true }) | ||
const { npm, home } = await loadMockNpm(t, { | ||
const { npm, rc, registry, adduser } = await mockAddUser(t, { | ||
stdin: ['test-user', 'test-password', '[email protected]'], | ||
config: { 'auth-type': 'legacy' }, | ||
homeDir: { | ||
'.npmrc': [ | ||
|
@@ -34,71 +55,48 @@ t.test('legacy', async t => { | |
].join('\n'), | ||
}, | ||
}) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
registry.couchadduser({ | ||
username: 'test-user', | ||
password: 'test-password', | ||
email: '[email protected]', | ||
token: 'npm_test-token', | ||
}) | ||
await npm.exec('adduser', []) | ||
await adduser.exec([]) | ||
t.same(npm.config.get('email'), '[email protected]') | ||
t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') | ||
const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) | ||
t.same(rc, { | ||
t.same(rc(), { | ||
'//registry.npmjs.org/:_authToken': 'npm_test-token', | ||
email: '[email protected]', | ||
}, 'should only have token and un-nerfed old email') | ||
}) | ||
|
||
t.test('scoped adduser', async t => { | ||
const stdin = new stream.PassThrough() | ||
stdin.write('test-user\n') | ||
stdin.write('test-password\n') | ||
stdin.write('[email protected]\n') | ||
mockGlobals(t, { | ||
'process.stdin': stdin, | ||
'process.stdout': new stream.PassThrough(), // to quiet readline | ||
}, { replace: true }) | ||
const { npm, home } = await loadMockNpm(t, { | ||
const { npm, rc, registry, adduser } = await mockAddUser(t, { | ||
stdin: ['test-user', 'test-password', '[email protected]'], | ||
config: { | ||
'auth-type': 'legacy', | ||
scope: '@myscope', | ||
}, | ||
}) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
registry.couchadduser({ | ||
username: 'test-user', | ||
password: 'test-password', | ||
email: '[email protected]', | ||
token: 'npm_test-token', | ||
}) | ||
await npm.exec('adduser', []) | ||
await adduser.exec([]) | ||
t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') | ||
t.same(npm.config.get('@myscope:registry'), 'https://registry.npmjs.org/') | ||
const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) | ||
t.same(rc, { | ||
t.same(rc(), { | ||
'//registry.npmjs.org/:_authToken': 'npm_test-token', | ||
'@myscope:registry': 'https://registry.npmjs.org/', | ||
}, 'should only have token and scope:registry') | ||
}) | ||
|
||
t.test('scoped adduser with valid scoped registry config', async t => { | ||
const stdin = new stream.PassThrough() | ||
stdin.write('test-user\n') | ||
stdin.write('test-password\n') | ||
stdin.write('[email protected]\n') | ||
mockGlobals(t, { | ||
'process.stdin': stdin, | ||
'process.stdout': new stream.PassThrough(), // to quiet readline | ||
}, { replace: true }) | ||
const { npm, home } = await loadMockNpm(t, { | ||
const { npm, rc, registry, adduser } = await mockAddUser(t, { | ||
stdin: ['test-user', 'test-password', '[email protected]'], | ||
registry: 'https://diff-registry.npmjs.org', | ||
homeDir: { | ||
'.npmrc': '@myscope:registry=https://diff-registry.npmjs.org', | ||
}, | ||
|
@@ -107,106 +105,70 @@ t.test('legacy', async t => { | |
scope: '@myscope', | ||
}, | ||
}) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: 'https://diff-registry.npmjs.org', | ||
}) | ||
registry.couchadduser({ | ||
username: 'test-user', | ||
password: 'test-password', | ||
email: '[email protected]', | ||
token: 'npm_test-token', | ||
}) | ||
await npm.exec('adduser', []) | ||
await adduser.exec([]) | ||
t.same(npm.config.get('//diff-registry.npmjs.org/:_authToken'), 'npm_test-token') | ||
t.same(npm.config.get('@myscope:registry'), 'https://diff-registry.npmjs.org') | ||
const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) | ||
t.same(rc, { | ||
t.same(rc(), { | ||
'@myscope:registry': 'https://diff-registry.npmjs.org', | ||
'//diff-registry.npmjs.org/:_authToken': 'npm_test-token', | ||
}, 'should only have token and scope:registry') | ||
}) | ||
|
||
t.test('save config failure', async t => { | ||
const stdin = new stream.PassThrough() | ||
stdin.write('test-user\n') | ||
stdin.write('test-password\n') | ||
stdin.write('[email protected]\n') | ||
mockGlobals(t, { | ||
'process.stdin': stdin, | ||
'process.stdout': new stream.PassThrough(), // to quiet readline | ||
}, { replace: true }) | ||
const { npm } = await loadMockNpm(t, { | ||
const { registry, adduser } = await mockAddUser(t, { | ||
stdin: ['test-user', 'test-password', '[email protected]'], | ||
config: { 'auth-type': 'legacy' }, | ||
homeDir: { | ||
'.npmrc': {}, | ||
}, | ||
}) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
registry.couchadduser({ | ||
username: 'test-user', | ||
password: 'test-password', | ||
email: '[email protected]', | ||
token: 'npm_test-token', | ||
}) | ||
await t.rejects(npm.exec('adduser', [])) | ||
await t.rejects(adduser.exec([])) | ||
}) | ||
t.end() | ||
}) | ||
|
||
t.test('web', t => { | ||
t.test('basic adduser', async t => { | ||
const { npm, home } = await loadMockNpm(t, { | ||
const { npm, rc, registry, adduser } = await mockAddUser(t, { | ||
config: { 'auth-type': 'web' }, | ||
}) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
registry.webadduser({ token: 'npm_test-token' }) | ||
await npm.exec('adduser', []) | ||
await adduser.exec([]) | ||
t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') | ||
const rc = ini.parse(fs.readFileSync(path.join(home, '.npmrc'), 'utf8')) | ||
t.same(rc, { | ||
t.same(rc(), { | ||
'//registry.npmjs.org/:_authToken': 'npm_test-token', | ||
}) | ||
}) | ||
|
||
t.test('server error', async t => { | ||
const { npm } = await loadMockNpm(t, { | ||
const { adduser, registry } = await mockAddUser(t, { | ||
config: { 'auth-type': 'web' }, | ||
}) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
registry.nock.post(registry.fullPath('/-/v1/login')) | ||
.reply(503, {}) | ||
await t.rejects( | ||
npm.exec('adduser', []), | ||
adduser.exec([]), | ||
{ message: /503/ } | ||
) | ||
}) | ||
|
||
t.test('fallback', async t => { | ||
const stdin = new stream.PassThrough() | ||
stdin.write('test-user\n') | ||
stdin.write('test-password\n') | ||
stdin.write('[email protected]\n') | ||
mockGlobals(t, { | ||
'process.stdin': stdin, | ||
'process.stdout': new stream.PassThrough(), // to quiet readline | ||
}, { replace: true }) | ||
const { npm } = await loadMockNpm(t, { | ||
const { npm, registry, adduser } = await mockAddUser(t, { | ||
stdin: ['test-user', 'test-password', '[email protected]'], | ||
config: { 'auth-type': 'web' }, | ||
}) | ||
const registry = new MockRegistry({ | ||
tap: t, | ||
registry: npm.config.get('registry'), | ||
}) | ||
registry.nock.post(registry.fullPath('/-/v1/login')) | ||
.reply(404, {}) | ||
registry.couchadduser({ | ||
|
@@ -215,7 +177,7 @@ t.test('web', t => { | |
email: '[email protected]', | ||
token: 'npm_test-token', | ||
}) | ||
await npm.exec('adduser', []) | ||
await adduser.exec([]) | ||
t.same(npm.config.get('//registry.npmjs.org/:_authToken'), 'npm_test-token') | ||
}) | ||
t.end() | ||
|
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
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
Oops, something went wrong.