Skip to content
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
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/utils/get-identity.js
Expand Up @@ -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
Expand All @@ -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
Expand Down
107 changes: 107 additions & 0 deletions 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')
}
})
34 changes: 34 additions & 0 deletions 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')
}
})
Copy link
Collaborator

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-4b5283ecd4a834e470103ca4fc13fbd7R41

Copy link
Contributor Author

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


whoami([], (err) => {
t.ifError(err, 'npm whoami')
t.ok('should successfully print username as json')
})
})