Skip to content

Commit

Permalink
raise an error early if publishing without login, registry
Browse files Browse the repository at this point in the history
PR-URL: #2360
Credit: @isaacs
Close: #2360
Reviewed-by: @nlf
  • Loading branch information
isaacs committed Dec 18, 2020
1 parent 628a554 commit be4a090
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 9 deletions.
16 changes: 15 additions & 1 deletion lib/publish.js
Expand Up @@ -35,7 +35,21 @@ const publish = async args => {
log.verbose('publish', args)

const opts = { ...npm.flatOptions }
const { json, defaultTag } = opts
const { json, defaultTag, registry } = opts

if (!registry) {
throw Object.assign(new Error('No registry specified.'), {
code: 'ENOREGISTRY',
})
}

const creds = npm.config.getCredentialsByURI(registry)
if (!creds.token && !creds.username) {
throw Object.assign(new Error('This command requires you to be logged in.'), {
code: 'ENEEDAUTH',
})
}

if (semver.validRange(defaultTag))
throw new Error('Tag name must not be a valid SemVer range: ' + defaultTag.trim())

Expand Down
77 changes: 69 additions & 8 deletions test/lib/publish.js
Expand Up @@ -3,7 +3,14 @@ const requireInject = require('require-inject')

// mock config
const {defaults} = require('../../lib/utils/config.js')
const config = { list: [defaults] }
const credentials = {
token: 'asdfasdf',
alwaysAuth: false,
}
const config = {
list: [defaults],
getCredentialsByURI: () => credentials,
}
const fs = require('fs')

t.test('should publish with libnpmpublish, respecting publishConfig', (t) => {
Expand All @@ -23,6 +30,7 @@ t.test('should publish with libnpmpublish, respecting publishConfig', (t) => {
flatOptions: {
json: true,
defaultTag: 'latest',
registry: 'https://registry.npmjs.org',
},
config,
},
Expand Down Expand Up @@ -55,7 +63,7 @@ t.test('should publish with libnpmpublish, respecting publishConfig', (t) => {
},
})

publish([testDir], (er) => {
return publish([testDir], (er) => {
if (er)
throw er
t.pass('got to callback')
Expand All @@ -77,6 +85,7 @@ t.test('re-loads publishConfig if added during script process', (t) => {
flatOptions: {
json: true,
defaultTag: 'latest',
registry: 'https://registry.npmjs.org/',
},
config,
},
Expand Down Expand Up @@ -108,7 +117,7 @@ t.test('re-loads publishConfig if added during script process', (t) => {
},
})

publish([testDir], (er) => {
return publish([testDir], (er) => {
if (er)
throw er
t.pass('got to callback')
Expand All @@ -131,6 +140,7 @@ t.test('should not log if silent', (t) => {
json: false,
defaultTag: 'latest',
dryRun: true,
registry: 'https://registry.npmjs.org/',
},
config,
},
Expand Down Expand Up @@ -159,7 +169,7 @@ t.test('should not log if silent', (t) => {
},
})

publish([testDir], (er) => {
return publish([testDir], (er) => {
if (er)
throw er
t.pass('got to callback')
Expand All @@ -181,6 +191,7 @@ t.test('should log tarball contents', (t) => {
json: false,
defaultTag: 'latest',
dryRun: true,
registry: 'https://registry.npmjs.org/',
},
config,
},
Expand All @@ -206,7 +217,7 @@ t.test('should log tarball contents', (t) => {
},
})

publish([testDir], (er) => {
return publish([testDir], (er) => {
if (er)
throw er
t.pass('got to callback')
Expand All @@ -220,12 +231,13 @@ t.test('shows usage with wrong set of arguments', (t) => {
flatOptions: {
json: false,
defaultTag: '0.0.13',
registry: 'https://registry.npmjs.org/',
},
config,
},
})

publish(['a', 'b', 'c'], (er) => t.matchSnapshot(er, 'should print usage'))
return publish(['a', 'b', 'c'], (er) => t.matchSnapshot(er, 'should print usage'))
})

t.test('throws when invalid tag', (t) => {
Expand All @@ -235,12 +247,13 @@ t.test('throws when invalid tag', (t) => {
flatOptions: {
json: false,
defaultTag: '0.0.13',
registry: 'https://registry.npmjs.org/',
},
config,
},
})

publish([], (err) => {
return publish([], (err) => {
t.match(err, {
message: /Tag name must not be a valid SemVer range: /,
}, 'throws when tag name is a valid SemVer range')
Expand Down Expand Up @@ -274,6 +287,7 @@ t.test('can publish a tarball', t => {
flatOptions: {
json: true,
defaultTag: 'latest',
registry: 'https://registry.npmjs.org/',
},
config,
},
Expand All @@ -298,9 +312,56 @@ t.test('can publish a tarball', t => {
},
})

publish([`${testDir}/package.tgz`], (er) => {
return publish([`${testDir}/package.tgz`], (er) => {
if (er)
throw er
t.pass('got to callback')
})
})

t.test('throw if no registry', async t => {
t.plan(1)
const publish = requireInject('../../lib/publish.js', {
'../../lib/npm.js': {
flatOptions: {
json: false,
defaultTag: '0.0.13',
registry: null,
},
config,
},
})

return publish([], (err) => {
t.match(err, {
message: 'No registry specified.',
code: 'ENOREGISTRY',
}, 'throws when registry unset')
})
})

t.test('throw if not logged in', async t => {
t.plan(1)
const publish = requireInject('../../lib/publish.js', {
'../../lib/npm.js': {
flatOptions: {
json: false,
defaultTag: '0.0.13',
registry: 'https://registry.npmjs.org/',
},
config: {
...config,
getCredentialsByURI: () => ({
email: 'me@example.com',
}),
},
},
})

return publish([], (err) => {
t.match(err, {
message: 'This command requires you to be logged in.',
code: 'ENEEDAUTH',
}, 'throws when not logged in')
})
})

0 comments on commit be4a090

Please sign in to comment.