diff options
author | Gar <gar+gh@danger.computer> | 2022-04-20 19:21:54 +0300 |
---|---|---|
committer | Luke Karrys <luke@lukekarrys.com> | 2022-04-21 06:16:14 +0300 |
commit | 392882b2413f77c8a10e14dd2d23c0b524595f25 (patch) | |
tree | 9c888c67beb3cac068ab53b0d33af56d10e0836d /test | |
parent | 52fd23bf05d5017b05ba67e1c1a94e2244e91093 (diff) |
chore(publish): add _auth tests
Diffstat (limited to 'test')
-rw-r--r-- | test/fixtures/mock-registry.js | 5 | ||||
-rw-r--r-- | test/lib/commands/publish.js | 72 |
2 files changed, 77 insertions, 0 deletions
diff --git a/test/fixtures/mock-registry.js b/test/fixtures/mock-registry.js index 5e39dcf48..5890fa7ee 100644 --- a/test/fixtures/mock-registry.js +++ b/test/fixtures/mock-registry.js @@ -11,6 +11,7 @@ class MockRegistry { #nock #registry #authorization + #basic constructor (opts) { if (!opts.registry) { @@ -18,6 +19,7 @@ class MockRegistry { } this.#registry = (new URL(opts.registry)).origin this.#authorization = opts.authorization + this.#basic = opts.basic // Required for this.package this.#tap = opts.tap } @@ -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 diff --git a/test/lib/commands/publish.js b/test/lib/commands/publish.js index b17424b08..885e82042 100644 --- a/test/lib/commands/publish.js +++ b/test/lib/commands/publish.js @@ -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, @@ -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') +}) |