From 940e4a907035e34824db02308a738b3e311ac553 Mon Sep 17 00:00:00 2001 From: Nathan LaFreniere Date: Thu, 17 Sep 2020 08:36:53 -0700 Subject: [PATCH 1/3] chore: add whoami command tests --- test/lib/whoami.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 test/lib/whoami.js diff --git a/test/lib/whoami.js b/test/lib/whoami.js new file mode 100644 index 0000000000000..dc9dbdfd570ff --- /dev/null +++ b/test/lib/whoami.js @@ -0,0 +1,34 @@ +const { test } = require('tap') +const requireInject = require('require-inject') + +test('whoami', (t) => { + t.plan(3) + const whoami = requireInject('../../lib/whoami.js', { + '../../lib/utils/get-identity.js': () => Promise.resolve('foo'), + '../../lib/npm.js': { flatOptions: {} }, + '../../lib/utils/output.js': (output) => { + t.equal(output, 'foo', 'should output the username') + } + }) + + whoami([], (err) => { + t.ifError(err, 'npm whoami') + t.ok('should successfully print username') + }) +}) + +test('whoami json', (t) => { + t.plan(3) + const whoami = requireInject('../../lib/whoami.js', { + '../../lib/utils/get-identity.js': () => Promise.resolve('foo'), + '../../lib/npm.js': { flatOptions: { json: true } }, + '../../lib/utils/output.js': (output) => { + t.equal(output, '"foo"', 'should output the username as json') + } + }) + + whoami([], (err) => { + t.ifError(err, 'npm whoami') + t.ok('should successfully print username as json') + }) +}) From 5e780a5f067476c1d207173fc9249faf9eaac0c2 Mon Sep 17 00:00:00 2001 From: Nathan LaFreniere Date: Thu, 17 Sep 2020 08:37:06 -0700 Subject: [PATCH 2/3] chore: remove unused spec parameter, assign error code --- lib/utils/get-identity.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/utils/get-identity.js b/lib/utils/get-identity.js index bf35f20305e01..d31b3ae28734f 100644 --- a/lib/utils/get-identity.js +++ b/lib/utils/get-identity.js @@ -4,10 +4,10 @@ const npm = require('../npm') const needsAuthError = (msg) => Object.assign(new Error(msg), { code: 'ENEEDAUTH' }) -module.exports = async (opts = {}, spec = null) => { +module.exports = async (opts = {}) => { const { registry } = opts if (!registry) { - throw new Error('no default registry set') + throw Object.assign(new Error('No registry specified.'), { code: 'ENOREGISTRY' }) } // First, check if we have a user/pass-based auth @@ -20,8 +20,7 @@ module.exports = async (opts = {}, spec = null) => { } else if (token) { // No username, but we have a token; fetch the username from registry const registryData = await npmFetch.json('/-/whoami', { - ...opts, - spec + ...opts }) const { username: usernameFromRegistry } = registryData // Retrieved username from registry; return it From 5831a54f12f70f8fc78a3d9a86df7864a3919b20 Mon Sep 17 00:00:00 2001 From: Nathan LaFreniere Date: Thu, 17 Sep 2020 08:40:06 -0700 Subject: [PATCH 3/3] chore: add get-identity tests --- test/lib/utils/get-identity.js | 107 +++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 test/lib/utils/get-identity.js diff --git a/test/lib/utils/get-identity.js b/test/lib/utils/get-identity.js new file mode 100644 index 0000000000000..c72f48b2e8f62 --- /dev/null +++ b/test/lib/utils/get-identity.js @@ -0,0 +1,107 @@ +const { test } = require('tap') +const requireInject = require('require-inject') + +test('throws ENOREGISTRY when no registry option is provided', async (t) => { + t.plan(2) + const getIdentity = requireInject('../../../lib/utils/get-identity.js', { + '../../../lib/npm.js': {} + }) + + try { + await getIdentity() + } catch (err) { + t.equal(err.code, 'ENOREGISTRY', 'assigns the appropriate error code') + t.equal(err.message, 'No registry specified.', 'returns the correct error message') + } +}) + +test('returns username from uri when provided', async (t) => { + t.plan(1) + + const getIdentity = requireInject('../../../lib/utils/get-identity.js', { + '../../../lib/npm.js': { + config: { + getCredentialsByURI: () => { + return { username: 'foo' } + } + } + } + }) + + const identity = await getIdentity({ registry: 'https://registry.npmjs.org' }) + t.equal(identity, 'foo', 'returns username from uri') +}) + +test('calls registry whoami when token is provided', async (t) => { + t.plan(3) + + const options = { + registry: 'https://registry.npmjs.org', + token: 'thisisnotreallyatoken' + } + + const getIdentity = requireInject('../../../lib/utils/get-identity.js', { + '../../../lib/npm.js': { + config: { + getCredentialsByURI: () => options + } + }, + 'npm-registry-fetch': { + json: (path, opts) => { + t.equal(path, '/-/whoami', 'calls whoami') + t.same(opts, options, 'passes through provided options') + return { username: 'foo' } + } + } + }) + + const identity = await getIdentity(options) + t.equal(identity, 'foo', 'fetched username from registry') +}) + +test('throws ENEEDAUTH when response does not include a username', async (t) => { + t.plan(3) + + const options = { + registry: 'https://registry.npmjs.org', + token: 'thisisnotreallyatoken' + } + + const getIdentity = requireInject('../../../lib/utils/get-identity.js', { + '../../../lib/npm.js': { + config: { + getCredentialsByURI: () => options + } + }, + 'npm-registry-fetch': { + json: (path, opts) => { + t.equal(path, '/-/whoami', 'calls whoami') + t.same(opts, options, 'passes through provided options') + return {} + } + } + }) + + try { + await getIdentity(options) + } catch (err) { + t.equal(err.code, 'ENEEDAUTH', 'throws correct error code') + } +}) + +test('throws ENEEDAUTH when neither username nor token is configured', async (t) => { + t.plan(1) + const getIdentity = requireInject('../../../lib/utils/get-identity.js', { + '../../../lib/npm.js': { + config: { + getCredentialsByURI: () => ({}) + } + } + }) + + try { + await getIdentity({ registry: 'https://registry.npmjs.org' }) + } catch (err) { + t.equal(err.code, 'ENEEDAUTH', 'throws correct error code') + } +})