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

fix(publish): follow all configs for registry auth check #2602

Merged
merged 1 commit into from Feb 4, 2021
Merged
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
70 changes: 46 additions & 24 deletions test/lib/publish.js
Expand Up @@ -8,11 +8,11 @@ const credentials = {
email: 'me@example.com',
},
'https://scope.specific.registry': {
token: 'some.regsitry.token',
token: 'some.registry.token',
alwaysAuth: false,
},
'https://some.registry': {
token: 'some.regsitry.token',
token: 'some.registry.token',
alwaysAuth: false,
},
'https://registry.npmjs.org/': {
Expand All @@ -22,9 +22,6 @@ const credentials = {
}
const config = {
list: [defaults],
getCredentialsByURI: (uri) => {
return credentials[uri]
},
}

const registryCredentials = (t, registry) => {
Expand Down Expand Up @@ -155,9 +152,10 @@ t.test('re-loads publishConfig if added during script process', (t) => {
})
})

t.test('should not log if silent', (t) => {
t.test('should not log if silent (dry run)', (t) => {
t.plan(2)

const registry = 'https://registry.npmjs.org'
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
Expand All @@ -171,9 +169,14 @@ t.test('should not log if silent', (t) => {
json: false,
defaultTag: 'latest',
dryRun: true,
registry: 'https://registry.npmjs.org/',
registry,
},
config: {
...config,
getCredentialsByURI: () => {
throw new Error('should not call getCredentialsByURI in dry run')
},
},
config,
},
'../../lib/utils/tar.js': {
getContents: () => ({}),
Expand All @@ -195,7 +198,7 @@ t.test('should not log if silent', (t) => {
libnpmpack: async () => '',
libnpmpublish: {
publish: (manifest, tarData, opts) => {
throw new Error('should not call libnpmpublish!')
throw new Error('should not call libnpmpublish in dry run')
},
},
})
Expand All @@ -207,8 +210,10 @@ t.test('should not log if silent', (t) => {
})
})

t.test('should log tarball contents', (t) => {
t.test('should log tarball contents (dry run)', (t) => {
t.plan(3)

const registry = 'https://registry.npmjs.org'
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
Expand All @@ -222,12 +227,12 @@ t.test('should log tarball contents', (t) => {
json: false,
defaultTag: 'latest',
dryRun: true,
registry: 'https://registry.npmjs.org/',
registry,
},
config: {
...config,
getCredentialsByURI: () => {
throw new Error('should not call getCredentialsByURI!')
throw new Error('should not call getCredentialsByURI in dry run')
}},
},
'../../lib/utils/tar.js': {
Expand All @@ -247,7 +252,7 @@ t.test('should log tarball contents', (t) => {
libnpmpack: async () => '',
libnpmpublish: {
publish: () => {
throw new Error('should not call libnpmpublish!')
throw new Error('should not call libnpmpublish in dry run')
},
},
})
Expand Down Expand Up @@ -277,12 +282,15 @@ t.test('shows usage with wrong set of arguments', (t) => {

t.test('throws when invalid tag', (t) => {
t.plan(1)

const registry = 'https://registry.npmjs.org'

const publish = requireInject('../../lib/publish.js', {
'../../lib/npm.js': {
flatOptions: {
json: false,
defaultTag: '0.0.13',
registry: 'https://registry.npmjs.org/',
registry,
},
config,
},
Expand All @@ -296,7 +304,9 @@ t.test('throws when invalid tag', (t) => {
})

t.test('can publish a tarball', t => {
t.plan(3)
t.plan(4)

const registry = 'https://registry.npmjs.org/'
const testDir = t.testdir({
package: {
'package.json': JSON.stringify({
Expand All @@ -322,9 +332,12 @@ t.test('can publish a tarball', t => {
flatOptions: {
json: true,
defaultTag: 'latest',
registry: 'https://registry.npmjs.org/',
registry,
},
config: {
...config,
getCredentialsByURI: registryCredentials(t, registry),
},
config,
},
'../../lib/utils/tar.js': {
getContents: () => ({
Expand Down Expand Up @@ -355,14 +368,19 @@ t.test('can publish a tarball', t => {
})

t.test('throw if not logged in', async t => {
t.plan(1)
t.plan(2)
const registry = 'https://unauthed.registry'

const publish = requireInject('../../lib/publish.js', {
'../../lib/npm.js': {
flatOptions: {
json: false,
registry: 'https://unauthed.registry',
registry,
},
config: {
...config,
getCredentialsByURI: registryCredentials(t, registry),
},
config,
},
})

Expand All @@ -375,9 +393,10 @@ t.test('throw if not logged in', async t => {
})

t.test('read registry only from publishConfig', t => {
t.plan(3)
t.plan(4)

const publishConfig = { registry: 'https://some.registry' }
const registry = 'https://some.registry'
const publishConfig = { registry }
const testDir = t.testdir({
'package.json': JSON.stringify({
name: 'my-cool-pkg',
Expand All @@ -391,7 +410,10 @@ t.test('read registry only from publishConfig', t => {
flatOptions: {
json: false,
},
config,
config: {
...config,
getCredentialsByURI: registryCredentials(t, registry),
},
},
'../../lib/utils/tar.js': {
getContents: () => ({
Expand All @@ -403,7 +425,7 @@ t.test('read registry only from publishConfig', t => {
libnpmpublish: {
publish: (manifest, tarData, opts) => {
t.match(manifest, { name: 'my-cool-pkg', version: '1.0.0' }, 'gets manifest')
t.same(opts.registry, publishConfig.registry, 'publishConfig is passed through')
t.same(opts.registry, registry, 'publishConfig is passed through')
},
},
})
Expand Down