|
| 1 | +import test from "ava"; |
| 2 | +import * as td from "testdouble"; |
| 3 | + |
| 4 | +let execa, verifyAuth, getRegistry, setNpmrcAuth; |
| 5 | +const DEFAULT_NPM_REGISTRY = "https://registry.npmjs.org/"; |
| 6 | +const npmrc = "npmrc contents"; |
| 7 | +const pkg = {}; |
| 8 | +const otherEnvVars = { foo: "bar" }; |
| 9 | +const env = { DEFAULT_NPM_REGISTRY, ...otherEnvVars }; |
| 10 | +const cwd = "./path/to/current/working/directory"; |
| 11 | +const context = { env, cwd }; |
| 12 | + |
| 13 | +test.beforeEach(async (t) => { |
| 14 | + ({ execa } = await td.replaceEsm("execa")); |
| 15 | + ({ default: getRegistry } = await td.replaceEsm("../lib/get-registry.js")); |
| 16 | + ({ default: setNpmrcAuth } = await td.replaceEsm("../lib/set-npmrc-auth.js")); |
| 17 | + |
| 18 | + ({ default: verifyAuth } = await import("../lib/verify-auth.js")); |
| 19 | +}); |
| 20 | + |
| 21 | +test.afterEach.always((t) => { |
| 22 | + td.reset(); |
| 23 | +}); |
| 24 | + |
| 25 | +test.serial( |
| 26 | + "that the provided token is verified with `npm whoami` when trusted publishing is not established for the official registry", |
| 27 | + async (t) => { |
| 28 | + td.when(getRegistry(pkg, context)).thenReturn(DEFAULT_NPM_REGISTRY); |
| 29 | + td.when( |
| 30 | + execa("npm", ["whoami", "--userconfig", npmrc, "--registry", DEFAULT_NPM_REGISTRY], { |
| 31 | + cwd, |
| 32 | + env: otherEnvVars, |
| 33 | + preferLocal: true, |
| 34 | + }) |
| 35 | + ).thenReturn({ |
| 36 | + stdout: { pipe: () => undefined }, |
| 37 | + stderr: { pipe: () => undefined }, |
| 38 | + }); |
| 39 | + |
| 40 | + await t.notThrowsAsync(verifyAuth(npmrc, pkg, context)); |
| 41 | + } |
| 42 | +); |
| 43 | + |
| 44 | +test.serial( |
| 45 | + "that the auth context for the official registry is considered invalid when no token is provided and trusted publishing is not established", |
| 46 | + async (t) => { |
| 47 | + td.when(getRegistry(pkg, context)).thenReturn(DEFAULT_NPM_REGISTRY); |
| 48 | + td.when( |
| 49 | + execa("npm", ["whoami", "--userconfig", npmrc, "--registry", DEFAULT_NPM_REGISTRY], { |
| 50 | + cwd, |
| 51 | + env: otherEnvVars, |
| 52 | + preferLocal: true, |
| 53 | + }) |
| 54 | + ).thenThrow(new Error()); |
| 55 | + |
| 56 | + const { |
| 57 | + errors: [error], |
| 58 | + } = await t.throwsAsync(verifyAuth(npmrc, pkg, context)); |
| 59 | + |
| 60 | + t.is(error.name, "SemanticReleaseError"); |
| 61 | + t.is(error.code, "EINVALIDNPMTOKEN"); |
| 62 | + t.is(error.message, "Invalid npm token."); |
| 63 | + } |
| 64 | +); |
| 65 | + |
| 66 | +// since alternative registries are not consistent in implementing `npm whoami`, |
| 67 | +// we do not attempt to verify the provided token when publishing to them |
| 68 | +test.serial("that `npm whoami` is not invoked when publishing to a custom registry", async (t) => { |
| 69 | + td.when(getRegistry(pkg, context)).thenReturn("https://other.registry.org"); |
| 70 | + |
| 71 | + await t.notThrowsAsync(verifyAuth(npmrc, pkg, context)); |
| 72 | +}); |
0 commit comments