Repository navigation
Expand file tree
/
Copy pathnpm-auth-refresh.test.ts
More file actions
263 lines (227 loc) · 9.67 KB
/
Copy pathnpm-auth-refresh.test.ts
File metadata and controls
263 lines (227 loc) · 9.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
import { spawnSync } from "node:child_process";
import { mkdtemp, mkdir, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import path from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
import { afterEach, describe, expect, it, vi } from "vitest";
import {
azureFeedLocalRegistry,
buildProjectNpmConfig,
cfsRegistry,
credentialProviderRegistry,
getAuthCommands,
getCommandInvocation,
getProjectNpmrcPaths,
main,
refreshNpmAuthentication,
runCommand,
writeProjectNpmConfigs,
} from "../../scripts/npm-auth-refresh.mjs";
vi.mock("node:child_process", async (importOriginal) => {
const original = await importOriginal<typeof import("node:child_process")>();
return { ...original, spawnSync: vi.fn(original.spawnSync) };
});
const scriptPath = fileURLToPath(new URL("../../scripts/npm-auth-refresh.mjs", import.meta.url));
const temporaryDirectories: string[] = [];
const successfulSpawn: ReturnType<typeof spawnSync> = {
pid: 1,
output: [],
stdout: Buffer.alloc(0),
stderr: Buffer.alloc(0),
status: 0,
signal: null,
};
async function createTemporaryNpmrcPaths(): Promise<string[]> {
const repositoryRoot = await mkdtemp(path.join(tmpdir(), "copilot-sdk-npm-auth-"));
temporaryDirectories.push(repositoryRoot);
const directories = [
path.join(repositoryRoot, "nodejs"),
path.join(repositoryRoot, "test", "harness"),
path.join(repositoryRoot, "java", "scripts", "codegen"),
];
await Promise.all(directories.map((directory) => mkdir(directory, { recursive: true })));
return directories.map((directory) => path.join(directory, ".npmrc"));
}
afterEach(async () => {
vi.mocked(spawnSync).mockReset();
vi.unstubAllEnvs();
await Promise.all(
temporaryDirectories.splice(0).map((directory) => rm(directory, { recursive: true }))
);
});
describe("local npm authentication refresh", () => {
it.each(["--help", "-h"])("prints help successfully for %s", (flag) => {
const result = spawnSync(process.execPath, [scriptPath, flag], {
encoding: "utf8",
});
expect(result.status).toBe(0);
expect(result.stdout).toContain("Usage: npm run auth:refresh");
});
it.each([[[]], [["--refresh"]], [["--run", "unexpected"]]])(
"requires the explicit --run argument for %j",
(args) => {
const result = spawnSync(process.execPath, [scriptPath, ...args], {
encoding: "utf8",
});
expect(result.status).toBe(1);
expect(result.stdout).toContain("Usage: npm run auth:refresh");
}
);
it("runs authentication only for --run", () => {
const refresh = vi.fn();
expect(main(["--run"], refresh)).toBe(0);
expect(refresh).toHaveBeenCalledOnce();
});
it("resolves all project configs from the script URL", () => {
const repositoryRoot = path.resolve(path.dirname(scriptPath), "..");
expect(getProjectNpmrcPaths(pathToFileURL(scriptPath).href)).toEqual([
path.join(repositoryRoot, "nodejs", ".npmrc"),
path.join(repositoryRoot, "test", "harness", ".npmrc"),
path.join(repositoryRoot, "java", "scripts", "codegen", ".npmrc"),
]);
});
it("writes only the scoped registry to all three project configs", async () => {
const npmrcPaths = await createTemporaryNpmrcPaths();
writeProjectNpmConfigs(npmrcPaths);
const expected = `@github:registry=${azureFeedLocalRegistry}\n`;
await Promise.all(
npmrcPaths.map(async (npmrcPath) => {
await expect(readFile(npmrcPath, "utf8")).resolves.toBe(expected);
})
);
expect(buildProjectNpmConfig()).not.toMatch(/^registry=/m);
expect(buildProjectNpmConfig()).not.toMatch(/(?:_auth|token|password)/i);
});
it.each(["\n", "\r\n"])(
"preserves unrelated config content using %j line endings",
async (newline) => {
const npmrcPaths = await createTemporaryNpmrcPaths();
const unrelatedLines = [
"; Local npm settings",
"registry=https://registry.npmjs.org/",
"@other:registry=https://example.com/npm/",
"# @github:registry=https://example.com/commented/",
"strict-ssl=true",
];
const existingConfig = [
...unrelatedLines.slice(0, 2),
"@github:registry=https://example.com/old/",
...unrelatedLines.slice(2),
"\t@github:registry = https://example.com/duplicate/",
].join(newline);
await Promise.all(npmrcPaths.map((npmrcPath) => writeFile(npmrcPath, existingConfig)));
const expected = [
...unrelatedLines,
`@github:registry=${azureFeedLocalRegistry}`,
"",
].join(newline);
for (let refresh = 0; refresh < 2; refresh++) {
writeProjectNpmConfigs(npmrcPaths);
await Promise.all(
npmrcPaths.map(async (npmrcPath) => {
await expect(readFile(npmrcPath, "utf8")).resolves.toBe(expected);
})
);
}
}
);
it.each(["strict-ssl=true", "strict-ssl=true\n", "strict-ssl=true\r\n"])(
"adds the scoped registry without joining existing settings for %j",
(existingConfig) => {
const newline = existingConfig.includes("\r\n") ? "\r\n" : "\n";
const expected = `strict-ssl=true${newline}@github:registry=${azureFeedLocalRegistry}${newline}`;
expect(buildProjectNpmConfig(existingConfig)).toBe(expected);
expect(buildProjectNpmConfig(expected)).toBe(expected);
}
);
it("surfaces errors reading existing configs", async () => {
const npmrcPaths = await createTemporaryNpmrcPaths();
await mkdir(npmrcPaths[0]);
expect(() => writeProjectNpmConfigs(npmrcPaths)).toThrow();
});
it("authenticates once using the nodejs config", () => {
const npmrcPaths = [
"C:\\repo\\nodejs\\.npmrc",
"C:\\repo\\test\\harness\\.npmrc",
"C:\\repo\\java\\scripts\\codegen\\.npmrc",
];
const writer = vi.fn();
const runner = vi.fn();
refreshNpmAuthentication("win32", npmrcPaths, writer, runner);
expect(writer).toHaveBeenCalledOnce();
expect(writer).toHaveBeenCalledWith(npmrcPaths);
expect(runner).toHaveBeenCalledTimes(2);
expect(runner).toHaveBeenLastCalledWith(
"vsts-npm-auth.cmd",
["-config", ".npmrc", "-Force", "-ReadOnly"],
"win32",
"C:\\repo\\nodejs"
);
});
it("uses vsts-npm-auth on Windows", () => {
expect(getAuthCommands("win32", "C:\\repo\\nodejs\\.npmrc")).toEqual([
{
command: "npm.cmd",
args: ["install", "--global", "vsts-npm-auth@0.43.0", `--registry=${cfsRegistry}`],
},
{
command: "vsts-npm-auth.cmd",
args: ["-config", ".npmrc", "-Force", "-ReadOnly"],
cwd: "C:\\repo\\nodejs",
},
]);
});
it("launches Windows command shims through the command interpreter", () => {
expect(getCommandInvocation("win32", "npm.cmd", ["--version"])).toEqual({
command: "cmd.exe",
args: ["/d", "/s", "/c", "npm.cmd", "--version"],
});
});
it.each([
"C:\\repo with spaces\\nodejs\\.npmrc",
"C:\\repo&other\\nodejs\\.npmrc",
"C:\\repo%TEMP%^!()\\nodejs\\.npmrc",
])("ignores ComSpec and keeps the config path out of shell arguments for %s", (npmrcPath) => {
vi.stubEnv("ComSpec", "C:\\untrusted\\not-cmd.exe");
vi.mocked(spawnSync).mockReturnValueOnce(successfulSpawn);
const { command, args, cwd } = getAuthCommands("win32", npmrcPath)[1];
runCommand(command, args, "win32", cwd);
expect(spawnSync).toHaveBeenCalledExactlyOnceWith(
"cmd.exe",
["/d", "/s", "/c", "vsts-npm-auth.cmd", "-config", ".npmrc", "-Force", "-ReadOnly"],
{ stdio: "inherit", cwd: path.win32.dirname(npmrcPath) }
);
});
it("surfaces command spawn errors", () => {
const error = new Error("Unable to spawn command");
vi.mocked(spawnSync).mockReturnValueOnce({ ...successfulSpawn, status: null, error });
expect(() => runCommand("npm", ["--version"], "linux")).toThrow(error);
});
it("surfaces nonzero command exit statuses", () => {
vi.mocked(spawnSync).mockReturnValueOnce({ ...successfulSpawn, status: 7 });
expect(() => runCommand("npm", ["--version"], "linux")).toThrow("exited with code 7");
});
it.each(["linux", "darwin"])("uses the Azure credential provider on %s", (platform) => {
expect(getAuthCommands(platform, "/repo/nodejs/.npmrc")).toEqual([
{
command: "npm",
args: [
"install",
"--global",
"@microsoft/artifacts-npm-credprovider@1.1.3",
`--registry=${credentialProviderRegistry}`,
`--@microsoft:registry=${credentialProviderRegistry}`,
],
},
{
command: "artifacts-npm-credprovider",
args: ["-f", "-c", ".npmrc"],
cwd: "/repo/nodejs",
},
]);
expect(getCommandInvocation(platform, "npm", ["--version"])).toEqual({
command: "npm",
args: ["--version"],
});
});
});