|
| 1 | +import { describe, it, expect, beforeEach, afterEach, vi } from "vite-plus/test"; |
| 2 | +import { join } from "node:path"; |
| 3 | +import { existsSync, readFileSync, writeFileSync } from "node:fs"; |
| 4 | +import { configAuthentication } from "./auth.js"; |
| 5 | +import { exportVariable } from "@actions/core"; |
| 6 | + |
| 7 | +vi.mock("@actions/core", () => ({ |
| 8 | + debug: vi.fn(), |
| 9 | + exportVariable: vi.fn(), |
| 10 | +})); |
| 11 | + |
| 12 | +vi.mock("node:fs", async () => { |
| 13 | + const actual = await vi.importActual<typeof import("node:fs")>("node:fs"); |
| 14 | + return { |
| 15 | + ...actual, |
| 16 | + existsSync: vi.fn(), |
| 17 | + readFileSync: vi.fn(), |
| 18 | + writeFileSync: vi.fn(), |
| 19 | + }; |
| 20 | +}); |
| 21 | + |
| 22 | +describe("configAuthentication", () => { |
| 23 | + const runnerTemp = "/tmp/runner"; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + vi.stubEnv("RUNNER_TEMP", runnerTemp); |
| 27 | + vi.mocked(existsSync).mockReturnValue(false); |
| 28 | + }); |
| 29 | + |
| 30 | + afterEach(() => { |
| 31 | + vi.unstubAllEnvs(); |
| 32 | + vi.resetAllMocks(); |
| 33 | + }); |
| 34 | + |
| 35 | + it("should write .npmrc with registry and auth token", () => { |
| 36 | + configAuthentication("https://registry.npmjs.org/"); |
| 37 | + |
| 38 | + const expectedPath = join(runnerTemp, ".npmrc"); |
| 39 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 40 | + expectedPath, |
| 41 | + expect.stringContaining("//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}"), |
| 42 | + ); |
| 43 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 44 | + expectedPath, |
| 45 | + expect.stringContaining("registry=https://registry.npmjs.org/"), |
| 46 | + ); |
| 47 | + }); |
| 48 | + |
| 49 | + it("should append trailing slash if missing", () => { |
| 50 | + configAuthentication("https://registry.npmjs.org"); |
| 51 | + |
| 52 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 53 | + expect.any(String), |
| 54 | + expect.stringContaining("registry=https://registry.npmjs.org/"), |
| 55 | + ); |
| 56 | + }); |
| 57 | + |
| 58 | + it("should auto-detect scope for GitHub Packages registry", () => { |
| 59 | + vi.stubEnv("GITHUB_REPOSITORY_OWNER", "voidzero-dev"); |
| 60 | + |
| 61 | + configAuthentication("https://npm.pkg.github.com"); |
| 62 | + |
| 63 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 64 | + expect.any(String), |
| 65 | + expect.stringContaining("@voidzero-dev:registry=https://npm.pkg.github.com/"), |
| 66 | + ); |
| 67 | + }); |
| 68 | + |
| 69 | + it("should use explicit scope", () => { |
| 70 | + configAuthentication("https://npm.pkg.github.com", "@myorg"); |
| 71 | + |
| 72 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 73 | + expect.any(String), |
| 74 | + expect.stringContaining("@myorg:registry=https://npm.pkg.github.com/"), |
| 75 | + ); |
| 76 | + }); |
| 77 | + |
| 78 | + it("should prepend @ to scope if missing", () => { |
| 79 | + configAuthentication("https://npm.pkg.github.com", "myorg"); |
| 80 | + |
| 81 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 82 | + expect.any(String), |
| 83 | + expect.stringContaining("@myorg:registry=https://npm.pkg.github.com/"), |
| 84 | + ); |
| 85 | + }); |
| 86 | + |
| 87 | + it("should lowercase scope", () => { |
| 88 | + configAuthentication("https://npm.pkg.github.com", "@MyOrg"); |
| 89 | + |
| 90 | + expect(writeFileSync).toHaveBeenCalledWith( |
| 91 | + expect.any(String), |
| 92 | + expect.stringContaining("@myorg:registry=https://npm.pkg.github.com/"), |
| 93 | + ); |
| 94 | + }); |
| 95 | + |
| 96 | + it("should preserve existing .npmrc content except registry and auth lines", () => { |
| 97 | + vi.mocked(existsSync).mockReturnValue(true); |
| 98 | + vi.mocked(readFileSync).mockReturnValue( |
| 99 | + [ |
| 100 | + "always-auth=true", |
| 101 | + "registry=https://old.reg/", |
| 102 | + "//old.reg/:_authToken=${NODE_AUTH_TOKEN}", |
| 103 | + ].join("\n"), |
| 104 | + ); |
| 105 | + |
| 106 | + configAuthentication("https://registry.npmjs.org/"); |
| 107 | + |
| 108 | + const written = vi.mocked(writeFileSync).mock.calls[0]![1] as string; |
| 109 | + expect(written).toContain("always-auth=true"); |
| 110 | + expect(written).not.toContain("https://old.reg/"); |
| 111 | + expect(written).toContain("registry=https://registry.npmjs.org/"); |
| 112 | + }); |
| 113 | + |
| 114 | + it("should remove existing auth token lines for the same registry", () => { |
| 115 | + vi.mocked(existsSync).mockReturnValue(true); |
| 116 | + vi.mocked(readFileSync).mockReturnValue( |
| 117 | + [ |
| 118 | + "//registry.npmjs.org/:_authToken=old-token", |
| 119 | + "registry=https://registry.npmjs.org/", |
| 120 | + "other-config=true", |
| 121 | + ].join("\n"), |
| 122 | + ); |
| 123 | + |
| 124 | + configAuthentication("https://registry.npmjs.org/"); |
| 125 | + |
| 126 | + const written = vi.mocked(writeFileSync).mock.calls[0]![1] as string; |
| 127 | + expect(written).not.toContain("old-token"); |
| 128 | + expect(written).toContain("other-config=true"); |
| 129 | + expect(written).toContain("//registry.npmjs.org/:_authToken=${NODE_AUTH_TOKEN}"); |
| 130 | + }); |
| 131 | + |
| 132 | + it("should handle Windows-style line endings in existing .npmrc", () => { |
| 133 | + vi.mocked(existsSync).mockReturnValue(true); |
| 134 | + vi.mocked(readFileSync).mockReturnValue("always-auth=true\r\nregistry=https://old.reg/\r\n"); |
| 135 | + |
| 136 | + configAuthentication("https://registry.npmjs.org/"); |
| 137 | + |
| 138 | + const written = vi.mocked(writeFileSync).mock.calls[0]![1] as string; |
| 139 | + expect(written).toContain("always-auth=true"); |
| 140 | + expect(written).not.toContain("https://old.reg/"); |
| 141 | + }); |
| 142 | + |
| 143 | + it("should not auto-detect scope for lookalike GitHub Packages URLs", () => { |
| 144 | + vi.stubEnv("GITHUB_REPOSITORY_OWNER", "voidzero-dev"); |
| 145 | + |
| 146 | + configAuthentication("https://npm.pkg.github.com.evil.example"); |
| 147 | + |
| 148 | + const written = vi.mocked(writeFileSync).mock.calls[0]![1] as string; |
| 149 | + // Should NOT have scoped registry — the host doesn't match exactly |
| 150 | + expect(written).not.toContain("@voidzero-dev:"); |
| 151 | + }); |
| 152 | + |
| 153 | + it("should throw on invalid URL", () => { |
| 154 | + expect(() => configAuthentication("not-a-url")).toThrow("Invalid registry-url"); |
| 155 | + }); |
| 156 | + |
| 157 | + it("should export NPM_CONFIG_USERCONFIG", () => { |
| 158 | + configAuthentication("https://registry.npmjs.org/"); |
| 159 | + |
| 160 | + expect(exportVariable).toHaveBeenCalledWith( |
| 161 | + "NPM_CONFIG_USERCONFIG", |
| 162 | + join(runnerTemp, ".npmrc"), |
| 163 | + ); |
| 164 | + }); |
| 165 | + |
| 166 | + it("should export NODE_AUTH_TOKEN placeholder when not set", () => { |
| 167 | + configAuthentication("https://registry.npmjs.org/"); |
| 168 | + |
| 169 | + expect(exportVariable).toHaveBeenCalledWith("NODE_AUTH_TOKEN", "XXXXX-XXXXX-XXXXX-XXXXX"); |
| 170 | + }); |
| 171 | + |
| 172 | + it("should preserve existing NODE_AUTH_TOKEN", () => { |
| 173 | + vi.stubEnv("NODE_AUTH_TOKEN", "my-real-token"); |
| 174 | + |
| 175 | + configAuthentication("https://registry.npmjs.org/"); |
| 176 | + |
| 177 | + expect(exportVariable).toHaveBeenCalledWith("NODE_AUTH_TOKEN", "my-real-token"); |
| 178 | + }); |
| 179 | +}); |
0 commit comments