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 token tests #1853

Closed
wants to merge 5 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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions lib/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,21 @@ token.usage = usageUtil('token',
'npm token revoke <tokenKey>\n' +
'npm token create [--read-only] [--cidr=list]')

const UsageError = (msg) =>
Object.assign(new Error(`\nUsage: ${msg}\n\n` + token.usage), {
code: 'EUSAGE'
})

const InvalidCIDRError = (msg) =>
Object.assign(new Error(msg), { code: 'EINVALIDCIDR' })

token.subcommands = ['list', 'revoke', 'create']

token.completion = function (opts, cb) {
var argv = opts.conf.argv.remain
if (argv.length === 2) {
return cb(null, token.subcommands)
}

switch (argv[2]) {
case 'list':
Expand Down Expand Up @@ -59,7 +70,7 @@ function token (args, cb) {
withCb(create(args.slice(1)), cb)
break
default:
cb(new Error('Unknown profile command: ' + args[0]))
cb(UsageError(`${args[0]} is not a recognized subcommand.`))
}
}

Expand Down Expand Up @@ -146,7 +157,7 @@ function list (args) {

function rm (args) {
if (args.length === 0) {
throw new Error('npm token revoke <tokenKey>')
return Promise.reject(UsageError('`<tokenKey>` argument is required.'))
}
const conf = config()
const toRemove = []
Expand All @@ -161,7 +172,7 @@ function rm (args) {
throw new Error(`Token ID "${id}" was ambiguous, a new token may have been created since you last ran \`npm token list\`.`)
} else {
const tokenMatches = tokens.filter((token) => id.indexOf(token.token) === 0)
if (tokenMatches === 0) {
if (tokenMatches.length === 0) {
throw new Error(`Unknown token id or value "${id}".`)
}
toRemove.push(id)
Expand All @@ -188,8 +199,8 @@ function create (args) {
const cidr = conf.cidr
const readonly = conf.readOnly

const validCIDR = validateCIDRList(cidr)
return readUserInfo.password().then((password) => {
const validCIDR = validateCIDRList(cidr)
log.info('token', 'creating')
return pulseTillDone.withPromise(otplease(conf, conf => {
return profile.createToken(password, readonly, validCIDR, conf)
Expand All @@ -211,10 +222,10 @@ function create (args) {

function validateCIDR (cidr) {
if (isCidrV6(cidr)) {
throw new Error('CIDR whitelist can only contain IPv4 addresses, ' + cidr + ' is IPv6')
throw InvalidCIDRError('CIDR whitelist can only contain IPv4 addresses, ' + cidr + ' is IPv6')
}
if (!isCidrV4(cidr)) {
throw new Error('CIDR whitelist contains invalid CIDR entry: ' + cidr)
throw InvalidCIDRError('CIDR whitelist contains invalid CIDR entry: ' + cidr)
}
}

Expand Down
Loading