Open History to see the last commit
1import { afterEach, beforeEach, describe, expect, it, vi } from "@effect/vitest";
2import * as NodeServices from "@effect/platform-node/NodeServices";
3import * as Effect from "effect/Effect";
4import * as Layer from "effect/Layer";
5import * as Redacted from "effect/Redacted";
6import { ChildProcessSpawner } from "effect/process";
7
8import * as ServerSettings from "../serverSettings.ts";
9import * as VcsProcess from "../vcs/VcsProcess.ts";
10import * as GitHubCredentials from "./GitHubCredentials.ts";
11
12const TOKEN_VARIABLES = [
13 "GH_TOKEN",
14 "GITHUB_TOKEN",
15 "GH_ENTERPRISE_TOKEN",
16 "GITHUB_ENTERPRISE_TOKEN",
17] as const;
18
19function harness(
20 hosts: Record<string, { readonly account?: string; readonly enabled?: boolean }> = {},
21 signedOut: ReadonlyArray<string> = [],
22 tokens: Record<string, string> = {},
23) {
24 const calls: Array<ReadonlyArray<string>> = [];
25 const process = Layer.mock(VcsProcess.VcsProcess)({
26 run: (input) =>
27 Effect.sync(() => {
28 calls.push(input.args);
29 const user = input.args[input.args.indexOf("--user") + 1];
30 if (input.args.includes("--user") && user !== undefined && signedOut.includes(user)) {
31 return {
32 exitCode: ChildProcessSpawner.ExitCode(0),
33 stdout: "",
34 stderr: "",
35 stdoutTruncated: false,
36 stderrTruncated: false,
37 };
38 }
39 return {
40 exitCode: ChildProcessSpawner.ExitCode(0),
41 stdout: input.args.includes("--user") ? `token-for-${user}\n` : "active-token\n",
42 stderr: "",
43 stdoutTruncated: false,
44 stderrTruncated: false,
45 };
46 }),
47 });
48 const layer = GitHubCredentials.layer.pipe(
49 Layer.provideMerge(
50 ServerSettings.ServerSettingsService.layerTest({ github: { hosts, tokens } }),
51 ),
52 Layer.provide(process),
53 Layer.provide(NodeServices.layer),
54 );
55 return { layer, calls };
56}
57
58describe("GitHubCredentials", () => {
59 beforeEach(() => {
60 for (const name of TOKEN_VARIABLES) vi.stubEnv(name, "");
61 });
62 afterEach(() => {
63 vi.unstubAllEnvs();
64 });
65
66 it.effect("asks gh for the active login when Settings pin nothing", () => {
67 const { layer, calls } = harness();
68 return Effect.gen(function* () {
69 const credentials = yield* GitHubCredentials.GitHubCredentials;
70 const credential = yield* credentials.get("GitHub.com");
71 expect(Redacted.value(credential.token)).toBe("active-token");
72 expect(calls).toEqual([["auth", "token", "--hostname", "github.com"]]);
73 }).pipe(Effect.provide(layer));
74 });
75
76 it.effect("passes --user for an account pinned in Settings", () => {
77 const { layer, calls } = harness({ "github.com": { account: "work" } });
78 return Effect.gen(function* () {
79 const credentials = yield* GitHubCredentials.GitHubCredentials;
80 const credential = yield* credentials.get("github.com");
81 expect(Redacted.value(credential.token)).toBe("token-for-work");
82 expect(calls).toEqual([["auth", "token", "--hostname", "github.com", "--user", "work"]]);
83 }).pipe(Effect.provide(layer));
84 });
85
86 it.effect("fails a host turned off in Settings without asking gh", () => {
87 const { layer, calls } = harness({ "github.com": { enabled: false } });
88 return Effect.gen(function* () {
89 const credentials = yield* GitHubCredentials.GitHubCredentials;
90 const error = yield* Effect.flip(credentials.get("github.com"));
91 expect(error._tag).toBe("GitHubHostDisabledError");
92 expect(error.message).toBe(
93 "GitHub host github.com is turned off in Settings → Source Control.",
94 );
95 expect(calls).toEqual([]);
96 }).pipe(Effect.provide(layer));
97 });
98
99 it.effect("lets an environment token win over a pinned account, as gh does", () => {
100 vi.stubEnv("GH_TOKEN", "from-env");
101 const { layer, calls } = harness({ "github.com": { account: "work" } });
102 return Effect.gen(function* () {
103 const credentials = yield* GitHubCredentials.GitHubCredentials;
104 const credential = yield* credentials.get("github.com");
105 expect(Redacted.value(credential.token)).toBe("from-env");
106 expect(credential.source).toBe("env");
107 expect(calls).toEqual([]);
108 }).pipe(Effect.provide(layer));
109 });
110
111 it.effect("picks up a changed account on the next request without a restart", () => {
112 const { layer, calls } = harness();
113 return Effect.gen(function* () {
114 const credentials = yield* GitHubCredentials.GitHubCredentials;
115 const settings = yield* ServerSettings.ServerSettingsService;
116 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("active-token");
117
118 yield* settings.updateSettings({
119 github: { hosts: { "github.com": { account: "work", enabled: true } } },
120 });
121 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("token-for-work");
122
123 yield* settings.updateSettings({ github: { hosts: { "github.com": { enabled: false } } } });
124 expect((yield* Effect.flip(credentials.get("github.com")))._tag).toBe(
125 "GitHubHostDisabledError",
126 );
127
128 yield* settings.updateSettings({ github: { hosts: {} } });
129 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("active-token");
130 // The unpinned token stayed cached; only the newly pinned account cost a gh call.
131 expect(calls).toHaveLength(2);
132 }).pipe(Effect.provide(layer));
133 });
134
135 it.effect("falls back to the active login when the pinned one is no longer signed in", () => {
136 const { layer, calls } = harness({ "github.com": { account: "gone" } }, ["gone"]);
137 return Effect.gen(function* () {
138 const credentials = yield* GitHubCredentials.GitHubCredentials;
139 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("active-token");
140 expect(calls).toEqual([
141 ["auth", "token", "--hostname", "github.com", "--user", "gone"],
142 ["auth", "token", "--hostname", "github.com"],
143 ]);
144 }).pipe(Effect.provide(layer));
145 });
146
147 it.effect("reads a repository owner through its own login, not gh's active one", () => {
148 const { layer, calls } = harness();
149 return Effect.gen(function* () {
150 const credentials = yield* GitHubCredentials.GitHubCredentials;
151 const credential = yield* credentials.get("github.com", { account: "noext" });
152 expect(Redacted.value(credential.token)).toBe("token-for-noext");
153 expect(calls).toEqual([["auth", "token", "--hostname", "github.com", "--user", "noext"]]);
154 }).pipe(Effect.provide(layer));
155 });
156
157 it.effect("falls back to the active login for an owner that is not a login", () => {
158 // An organization owns its repositories but `gh` holds no login for it.
159 const { layer, calls } = harness({}, ["pharmania"]);
160 return Effect.gen(function* () {
161 const credentials = yield* GitHubCredentials.GitHubCredentials;
162 const credential = yield* credentials.get("github.com", { account: "pharmania" });
163 expect(Redacted.value(credential.token)).toBe("active-token");
164 expect(calls).toEqual([
165 ["auth", "token", "--hostname", "github.com", "--user", "pharmania"],
166 ["auth", "token", "--hostname", "github.com"],
167 ]);
168 }).pipe(Effect.provide(layer));
169 });
170
171 it.effect("ranks the owner before the login Settings pins", () => {
172 const { layer, calls } = harness({ "github.com": { account: "work" } });
173 return Effect.gen(function* () {
174 const credentials = yield* GitHubCredentials.GitHubCredentials;
175 const credential = yield* credentials.get("github.com", { account: "noext" });
176 expect(Redacted.value(credential.token)).toBe("token-for-noext");
177 expect(calls).toEqual([["auth", "token", "--hostname", "github.com", "--user", "noext"]]);
178 }).pipe(Effect.provide(layer));
179 });
180
181 it.effect("keeps the pinned login behind an owner gh holds no login for", () => {
182 const { layer, calls } = harness({ "github.com": { account: "work" } }, ["pharmania"]);
183 return Effect.gen(function* () {
184 const credentials = yield* GitHubCredentials.GitHubCredentials;
185 const credential = yield* credentials.get("github.com", { account: "pharmania" });
186 expect(Redacted.value(credential.token)).toBe("token-for-work");
187 expect(calls).toEqual([
188 ["auth", "token", "--hostname", "github.com", "--user", "pharmania"],
189 ["auth", "token", "--hostname", "github.com", "--user", "work"],
190 ]);
191 }).pipe(Effect.provide(layer));
192 });
193
194 it.effect("keeps two owners of one host apart, and drops one on demand", () => {
195 const { layer, calls } = harness();
196 return Effect.gen(function* () {
197 const credentials = yield* GitHubCredentials.GitHubCredentials;
198 expect(Redacted.value((yield* credentials.get("github.com", { account: "one" })).token)).toBe(
199 "token-for-one",
200 );
201 expect(Redacted.value((yield* credentials.get("github.com", { account: "two" })).token)).toBe(
202 "token-for-two",
203 );
204 // Both are cached, so neither is asked twice until it is invalidated.
205 yield* credentials.get("github.com", { account: "one" });
206 expect(calls).toHaveLength(2);
207
208 yield* credentials.invalidate("github.com", { account: "one" });
209 yield* credentials.get("github.com", { account: "one" });
210 expect(calls).toHaveLength(3);
211 expect(calls[2]).toEqual([
212 "auth",
213 "token",
214 "--hostname",
215 "github.com",
216 "--user",
217 "one",
218 ]);
219 }).pipe(Effect.provide(layer));
220 });
221
222 it.effect("lets Settings and the environment speak before an owner, as they do for a host", () => {
223 vi.stubEnv("GH_TOKEN", "from-env");
224 const { layer, calls } = harness({}, [], { "github.com": "saved-token" });
225 return Effect.gen(function* () {
226 const credentials = yield* GitHubCredentials.GitHubCredentials;
227 expect(
228 Redacted.value((yield* credentials.get("github.com", { account: "noext" })).token),
229 ).toBe("saved-token");
230 expect(calls).toEqual([]);
231 }).pipe(Effect.provide(layer));
232 });
233
234 it.effect("refuses to fall back when the caller asks for the owner's own login", () => {
235 // Git asks this way: its helper already answers as the active account, so a fallback would
236 // replace the machine's own configuration instead of adding an account to it.
237 const { layer, calls } = harness({ "github.com": { account: "work" } }, ["pharmania"]);
238 return Effect.gen(function* () {
239 const credentials = yield* GitHubCredentials.GitHubCredentials;
240 const error = yield* Effect.flip(
241 credentials.get("github.com", { account: "pharmania", ownerOnly: true }),
242 );
243 expect(error._tag).toBe("GitHubNotSignedInError");
244 expect(calls).toEqual([
245 ["auth", "token", "--hostname", "github.com", "--user", "pharmania"],
246 ]);
247 }).pipe(Effect.provide(layer));
248 });
249
250 it.effect("still answers an owner-only request from Settings and the environment", () => {
251 vi.stubEnv("GH_TOKEN", "from-env");
252 const { layer, calls } = harness({}, [], { "github.com": "saved-token" });
253 return Effect.gen(function* () {
254 const credentials = yield* GitHubCredentials.GitHubCredentials;
255 const request = { account: "pharmania", ownerOnly: true } as const;
256 expect(Redacted.value((yield* credentials.get("github.com", request)).token)).toBe(
257 "saved-token",
258 );
259 expect(calls).toEqual([]);
260 }).pipe(Effect.provide(layer));
261 });
262
263 it.effect("keeps an owner-only answer apart from a loose one on the same host", () => {
264 const { layer, calls } = harness({}, ["pharmania"]);
265 return Effect.gen(function* () {
266 const credentials = yield* GitHubCredentials.GitHubCredentials;
267 // The loose request accepts the active login; the strict one must not read that answer back.
268 expect(Redacted.value((yield* credentials.get("github.com", { account: "pharmania" })).token)).toBe(
269 "active-token",
270 );
271 expect(
272 (yield* Effect.flip(
273 credentials.get("github.com", { account: "pharmania", ownerOnly: true }),
274 ))._tag,
275 ).toBe("GitHubNotSignedInError");
276 expect(calls).toEqual([
277 ["auth", "token", "--hostname", "github.com", "--user", "pharmania"],
278 ["auth", "token", "--hostname", "github.com"],
279 ["auth", "token", "--hostname", "github.com", "--user", "pharmania"],
280 ]);
281 }).pipe(Effect.provide(layer));
282 });
283
284 it.effect("uses a token saved in Settings before GH_TOKEN and gh", () => {
285 vi.stubEnv("GH_TOKEN", "env-token");
286 const { layer, calls } = harness({}, [], { "github.com": "saved-token" });
287 return Effect.gen(function* () {
288 const credentials = yield* GitHubCredentials.GitHubCredentials;
289 const credential = yield* credentials.get("github.com");
290 expect(Redacted.value(credential.token)).toBe("saved-token");
291 expect(credential.source).toBe("settings");
292 expect(calls).toEqual([]);
293 }).pipe(Effect.provide(layer));
294 });
295
296 it.effect("keeps a host turned off even with a token saved in Settings", () => {
297 const { layer } = harness({ "github.com": { enabled: false } }, [], {
298 "github.com": "saved-token",
299 });
300 return Effect.gen(function* () {
301 const credentials = yield* GitHubCredentials.GitHubCredentials;
302 expect((yield* Effect.flip(credentials.get("github.com")))._tag).toBe(
303 "GitHubHostDisabledError",
304 );
305 }).pipe(Effect.provide(layer));
306 });
307});