New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add whoami and get-identity tests #1818
Closed
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 |
---|---|---|
@@ -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') | ||
} | ||
}) |
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,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') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just a heads up since this is a very simplistic module it doesn't matter that much but in other tests we have been trying to only have a single
requireInject()
import at the top and mutate the mocked values within each test since the requireInject calls can be really slow and add precious seconds to our unit tests, e.g: https://github.com/npm/cli/pull/1786/files#diff-4b5283ecd4a834e470103ca4fc13fbd7R41There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ah! ok, i wasn't sure since i saw it both ways. tracking global state in a test felt weird so i went the direction that didn't require that, but it makes sense