diff options
author | Nathan LaFreniere <quitlahok@gmail.com> | 2020-09-17 18:40:06 +0300 |
---|---|---|
committer | isaacs <i@izs.me> | 2020-09-18 01:21:41 +0300 |
commit | 5ccf22a4a4fdc365595ac9e8ea1b1829e61235eb (patch) | |
tree | 4842a0cf4c86da2f9018cb8c6055182a84dbf0c4 /test/lib/utils | |
parent | 5e780a5f067476c1d207173fc9249faf9eaac0c2 (diff) |
chore: add get-identity tests
PR-URL: https://github.com/npm/cli/pull/1818
Credit: @nlf
Close: #1818
Reviewed-by: @ruyadorno
Diffstat (limited to 'test/lib/utils')
-rw-r--r-- | test/lib/utils/get-identity.js | 107 |
1 files changed, 107 insertions, 0 deletions
diff --git a/test/lib/utils/get-identity.js b/test/lib/utils/get-identity.js new file mode 100644 index 000000000..c72f48b2e --- /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') + } +}) |