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 _auth tests and update _auth docs #4782

Merged
merged 2 commits into from
Apr 21, 2022
Merged
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
2 changes: 2 additions & 0 deletions docs/content/using-npm/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ npm ls --global --parseable --long --loglevel info
* Type: null or String

A basic-auth string to use when authenticating against the npm registry.
This will ONLY be used to authenticate against the npm registry. For other
registries you will need to scope it like "//other-registry.tld/:_auth"

Warning: This should generally not be set via a command-line option. It is
safer to use a registry-provided authentication bearer token stored in the
Expand Down
2 changes: 2 additions & 0 deletions lib/utils/config/definitions.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ define('_auth', {
type: [null, String],
description: `
A basic-auth string to use when authenticating against the npm registry.
This will ONLY be used to authenticate against the npm registry. For other
registries you will need to scope it like "//other-registry.tld/:_auth"

Warning: This should generally not be set via a command-line option. It
is safer to use a registry-provided authentication bearer token stored in
Expand Down
8 changes: 8 additions & 0 deletions tap-snapshots/test/lib/commands/publish.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
* Make sure to inspect the output below. Do not ignore changes!
*/
'use strict'
exports[`test/lib/commands/publish.js TAP _auth config default registry > new package version 1`] = `
+ test-package@1.0.0
`

exports[`test/lib/commands/publish.js TAP dry-run > must match snapshot 1`] = `
Array [
Array [
Expand Down Expand Up @@ -108,6 +112,10 @@ exports[`test/lib/commands/publish.js TAP respects publishConfig.registry, runs

`

exports[`test/lib/commands/publish.js TAP scoped _auth config scoped registry > new package version 1`] = `
+ @npm/test-package@1.0.0
`

exports[`test/lib/commands/publish.js TAP tarball > must match snapshot 1`] = `
Array [
Array [
Expand Down
2 changes: 2 additions & 0 deletions tap-snapshots/test/lib/utils/config/definitions.js.test.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ exports[`test/lib/utils/config/definitions.js TAP > config description for _auth
* Type: null or String

A basic-auth string to use when authenticating against the npm registry.
This will ONLY be used to authenticate against the npm registry. For other
registries you will need to scope it like "//other-registry.tld/:_auth"

Warning: This should generally not be set via a command-line option. It is
safer to use a registry-provided authentication bearer token stored in the
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ exports[`test/lib/utils/config/describe-all.js TAP > must match snapshot 1`] = `
* Type: null or String

A basic-auth string to use when authenticating against the npm registry.
This will ONLY be used to authenticate against the npm registry. For other
registries you will need to scope it like "//other-registry.tld/:_auth"

Warning: This should generally not be set via a command-line option. It is
safer to use a registry-provided authentication bearer token stored in the
Expand Down
5 changes: 5 additions & 0 deletions test/fixtures/mock-registry.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ class MockRegistry {
#nock
#registry
#authorization
#basic

constructor (opts) {
if (!opts.registry) {
throw new Error('mock registry requires a registry value')
}
this.#registry = (new URL(opts.registry)).origin
this.#authorization = opts.authorization
this.#basic = opts.basic
// Required for this.package
this.#tap = opts.tap
}
Expand All @@ -32,6 +34,9 @@ class MockRegistry {
if (this.#authorization) {
reqheaders.authorization = `Bearer ${this.#authorization}`
}
if (this.#basic) {
reqheaders.authorization = `Basic ${this.#basic}`
}
this.#nock = tnock(this.#tap, this.#registry, { reqheaders })
}
return this.#nock
Expand Down
72 changes: 72 additions & 0 deletions test/lib/commands/publish.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const pkg = 'test-package'
const token = 'test-auth-token'
const auth = { '//registry.npmjs.org/:_authToken': token }
const alternateRegistry = 'https://other.registry.npmjs.org'
const basic = Buffer.from('test-user:test-password').toString('base64')

const pkgJson = {
name: pkg,
Expand Down Expand Up @@ -602,3 +603,74 @@ t.test('ignore-scripts', async t => {
'did not run postpublish'
)
})

t.test('_auth config default registry', async t => {
const { npm, joinedOutput } = await loadMockNpm(t, {
config: {
_auth: basic,
},
prefixDir: {
'package.json': JSON.stringify(pkgJson),
},
globals: ({ prefix }) => ({
'process.cwd': () => prefix,
}),
})
const registry = new MockRegistry({
tap: t,
registry: npm.config.get('registry'),
basic,
})
registry.nock.put(`/${pkg}`).reply(200, {})
await npm.exec('publish', [])
t.matchSnapshot(joinedOutput(), 'new package version')
})

t.test('bare _auth config scoped registry', async t => {
const { npm } = await loadMockNpm(t, {
config: {
'@npm:registry': alternateRegistry,
_auth: basic,
},
prefixDir: {
'package.json': JSON.stringify({
name: '@npm/test-package',
version: '1.0.0',
}, null, 2),
},
globals: ({ prefix }) => ({
'process.cwd': () => prefix,
}),
})
await t.rejects(
npm.exec('publish', []),
{ message: `This command requires you to be logged in to ${alternateRegistry}` }
)
})

t.test('scoped _auth config scoped registry', async t => {
const spec = npa('@npm/test-package')
const { npm, joinedOutput } = await loadMockNpm(t, {
config: {
'@npm:registry': alternateRegistry,
[`${alternateRegistry.slice(6)}/:_auth`]: basic,
},
prefixDir: {
'package.json': JSON.stringify({
name: '@npm/test-package',
version: '1.0.0',
}, null, 2),
},
globals: ({ prefix }) => ({
'process.cwd': () => prefix,
}),
})
const registry = new MockRegistry({
tap: t,
registry: alternateRegistry,
basic,
})
registry.nock.put(`/${spec.escapedName}`).reply(200, {})
await npm.exec('publish', [])
t.matchSnapshot(joinedOutput(), 'new package version')
})