|
| 1 | +// Run with: node --test scripts/check-npm-auth.test.mjs |
| 2 | +// |
| 3 | +// Covers the pure resolveNpmAuth detection (offline, no network) and the CLI |
| 4 | +// fail-closed guarantee: the no-token path must exit non-zero WITHOUT touching |
| 5 | +// the network (no `npm whoami`), so it can never hang on a publish web-auth wall. |
| 6 | + |
| 7 | +import test from "node:test"; |
| 8 | +import assert from "node:assert/strict"; |
| 9 | +import { spawnSync } from "node:child_process"; |
| 10 | +import fs from "node:fs"; |
| 11 | +import os from "node:os"; |
| 12 | +import path from "node:path"; |
| 13 | +import { fileURLToPath } from "node:url"; |
| 14 | + |
| 15 | +import { resolveNpmAuth } from "./check-npm-auth.mjs"; |
| 16 | + |
| 17 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 18 | +const SCRIPT = path.join(__dirname, "check-npm-auth.mjs"); |
| 19 | + |
| 20 | +test("resolveNpmAuth: NPM_TOKEN present → ok, method NPM_TOKEN", () => { |
| 21 | + const r = resolveNpmAuth({ env: { NPM_TOKEN: "npm_abc123" }, npmrc: "" }); |
| 22 | + assert.equal(r.ok, true); |
| 23 | + assert.equal(r.method, "NPM_TOKEN"); |
| 24 | +}); |
| 25 | + |
| 26 | +test("resolveNpmAuth: NODE_AUTH_TOKEN present → ok, method NPM_TOKEN", () => { |
| 27 | + const r = resolveNpmAuth({ env: { NODE_AUTH_TOKEN: "npm_ci_token" }, npmrc: "" }); |
| 28 | + assert.equal(r.ok, true); |
| 29 | + assert.equal(r.method, "NPM_TOKEN"); |
| 30 | +}); |
| 31 | + |
| 32 | +test("resolveNpmAuth: npmrc with _authToken → ok, method npmrc-token", () => { |
| 33 | + const npmrc = "//registry.npmjs.org/:_authToken=npm_fromfile\n"; |
| 34 | + const r = resolveNpmAuth({ env: {}, npmrc }); |
| 35 | + assert.equal(r.ok, true); |
| 36 | + assert.equal(r.method, "npmrc-token"); |
| 37 | +}); |
| 38 | + |
| 39 | +test("resolveNpmAuth: neither → not ok, method null", () => { |
| 40 | + const r = resolveNpmAuth({ env: {}, npmrc: "" }); |
| 41 | + assert.equal(r.ok, false); |
| 42 | + assert.equal(r.method, null); |
| 43 | +}); |
| 44 | + |
| 45 | +test("resolveNpmAuth: empty NPM_TOKEN string is not a token", () => { |
| 46 | + const r = resolveNpmAuth({ env: { NPM_TOKEN: " " }, npmrc: "" }); |
| 47 | + assert.equal(r.ok, false); |
| 48 | + assert.equal(r.method, null); |
| 49 | +}); |
| 50 | + |
| 51 | +test("resolveNpmAuth: commented-out _authToken line does not count", () => { |
| 52 | + const npmrc = "# //registry.npmjs.org/:_authToken=npm_disabled\n"; |
| 53 | + const r = resolveNpmAuth({ env: {}, npmrc }); |
| 54 | + assert.equal(r.ok, false); |
| 55 | +}); |
| 56 | + |
| 57 | +test("resolveNpmAuth: _authToken line with empty value does not count", () => { |
| 58 | + const npmrc = "//registry.npmjs.org/:_authToken=\n"; |
| 59 | + const r = resolveNpmAuth({ env: {}, npmrc }); |
| 60 | + assert.equal(r.ok, false); |
| 61 | +}); |
| 62 | + |
| 63 | +test("resolveNpmAuth: env token wins even when npmrc has none", () => { |
| 64 | + const r = resolveNpmAuth({ env: { NPM_TOKEN: "npm_env" }, npmrc: "registry=https://x\n" }); |
| 65 | + assert.equal(r.ok, true); |
| 66 | + assert.equal(r.method, "NPM_TOKEN"); |
| 67 | +}); |
| 68 | + |
| 69 | +test("CLI: no-token path exits non-zero with a FAIL message and does not hang", () => { |
| 70 | + // Temp HOME with an EMPTY .npmrc, env scrubbed of any npm token. |
| 71 | + const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "npmauth-test-")); |
| 72 | + fs.writeFileSync(path.join(tmpHome, ".npmrc"), "", "utf8"); |
| 73 | + |
| 74 | + const env = { ...process.env }; |
| 75 | + delete env.NPM_TOKEN; |
| 76 | + delete env.NODE_AUTH_TOKEN; |
| 77 | + env.HOME = tmpHome; |
| 78 | + env.USERPROFILE = tmpHome; // Windows-safe, harmless on POSIX |
| 79 | + |
| 80 | + const res = spawnSync(process.execPath, [SCRIPT], { |
| 81 | + env, |
| 82 | + encoding: "utf8", |
| 83 | + timeout: 20000, // if it ever hangs on network, the test fails loudly instead |
| 84 | + }); |
| 85 | + |
| 86 | + fs.rmSync(tmpHome, { recursive: true, force: true }); |
| 87 | + |
| 88 | + assert.equal(res.error, undefined, `spawn must not error/timeout: ${res.error}`); |
| 89 | + assert.equal(res.status, 1, "no-token CLI must exit 1 (fail closed)"); |
| 90 | + const out = `${res.stdout}\n${res.stderr}`; |
| 91 | + assert.match(out, /FAIL/, "must print a FAIL message"); |
| 92 | + // Prove it exited BEFORE any whoami network probe. |
| 93 | + assert.doesNotMatch(out, /authenticated to npm registry/, "must not have reached whoami"); |
| 94 | +}); |
| 95 | + |
| 96 | +test("CLI: still runs main() (fail-closed) when the script path contains spaces (I1 regression)", () => { |
| 97 | + // A naive `file://${argv[1]}` entry guard silently SKIPS main() when the path |
| 98 | + // has spaces (percent-encoding mismatch) — which would let a publish proceed |
| 99 | + // with NO auth check. pathToFileURL() fixes it. Prove main() still fires here. |
| 100 | + const spacesDir = fs.mkdtempSync(path.join(os.tmpdir(), "npm auth spaces-")); |
| 101 | + const copied = path.join(spacesDir, "check-npm-auth.mjs"); |
| 102 | + fs.copyFileSync(SCRIPT, copied); |
| 103 | + const tmpHome = fs.mkdtempSync(path.join(os.tmpdir(), "npmauth-home-")); |
| 104 | + fs.writeFileSync(path.join(tmpHome, ".npmrc"), "", "utf8"); |
| 105 | + |
| 106 | + const env = { ...process.env }; |
| 107 | + delete env.NPM_TOKEN; |
| 108 | + delete env.NODE_AUTH_TOKEN; |
| 109 | + env.HOME = tmpHome; |
| 110 | + env.USERPROFILE = tmpHome; |
| 111 | + |
| 112 | + const res = spawnSync(process.execPath, [copied], { env, encoding: "utf8", timeout: 20000 }); |
| 113 | + |
| 114 | + fs.rmSync(spacesDir, { recursive: true, force: true }); |
| 115 | + fs.rmSync(tmpHome, { recursive: true, force: true }); |
| 116 | + |
| 117 | + assert.equal(res.status, 1, "main() must run (exit 1) even from a path containing spaces"); |
| 118 | + assert.match(`${res.stdout}\n${res.stderr}`, /FAIL/); |
| 119 | +}); |
0 commit comments