Skip to content

Commit

Permalink
chore(tests): add test for scope specific registry
Browse files Browse the repository at this point in the history
refactor publish tests so we know it's checking creds for the right
registry
  • Loading branch information
wraithgar authored and ruyadorno committed Feb 4, 2021
1 parent 65327c1 commit 4750c3c
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 10 deletions.
1 change: 1 addition & 0 deletions lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const publish_ = async (arg, opts) => {
const resolved = npa.resolve(manifest.name, manifest.version)
const registry = npmFetch.pickRegistry(resolved, opts)

console.log(`picked registry ${registry}`)
if (!dryRun) {
const creds = npm.config.getCredentialsByURI(registry)
if (!creds.token && !creds.username) {
Expand Down
75 changes: 65 additions & 10 deletions test/lib/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,29 @@ const requireInject = require('require-inject')
// mock config
const {defaults} = require('../../lib/utils/config.js')
const credentials = {
token: 'asdfasdf',
alwaysAuth: false,
'https://unauthed.registry': {
email: 'me@example.com',
},
'https://scope.specific.registry': {
token: 'some.regsitry.token',
alwaysAuth: false,
},
'https://some.registry': {
token: 'some.regsitry.token',
alwaysAuth: false,
},
'https://registry.npmjs.org/': {
token: 'npmjs.registry.token',
alwaysAuth: false,
},
}
const config = {
list: [defaults],
getCredentialsByURI: () => credentials,
getCredentialsByURI: (uri) => {
return credentials[uri]
},
}

const fs = require('fs')

t.test('should publish with libnpmpublish, respecting publishConfig', (t) => {
Expand Down Expand Up @@ -329,14 +345,9 @@ t.test('throw if not logged in', async t => {
'../../lib/npm.js': {
flatOptions: {
json: false,
registry: 'https://registry.npmjs.org/',
},
config: {
...config,
getCredentialsByURI: () => ({
email: 'me@example.com',
}),
registry: 'https://unauthed.registry',
},
config,
},
})

Expand Down Expand Up @@ -388,3 +399,47 @@ t.test('read registry only from publishConfig', t => {
t.pass('got to callback')
})
})

t.test('should check auth for scope specific registry', t => {
const testDir = t.testdir({
'package.json': JSON.stringify({
name: '@npm/my-cool-pkg',
version: '1.0.0',
}, null, 2),
})

const registry = 'https://scope.specific.registry'
const publish = requireInject('../../lib/publish.js', {
'../../lib/npm.js': {
flatOptions: {
json: false,
'@npm:registry': registry,
},
config: {
...config,
getCredentialsByURI: (uri) => {
t.same(uri, registry, 'gets credentials for scope specific registry')
return credentials[uri]
},
},
},
'../../lib/utils/tar.js': {
getContents: () => ({
id: 'someid',
}),
logTar: () => {},
},
'../../lib/utils/output.js': () => {},
'../../lib/utils/otplease.js': (opts, fn) => {
return Promise.resolve().then(() => fn(opts))
},
libnpmpublish: {
publish: () => '',
},
})
return publish([testDir], (er) => {
if (er)
throw er
t.pass('got to callback')
})
})

0 comments on commit 4750c3c

Please sign in to comment.