Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitHubCredentials.test.ts

151 lines5.9 KB

1import * as NodeCrypto from "@effect/platform-node/NodeCrypto";
2import { afterEach, beforeEach, describe, expect, it, vi } from "@effect/vitest";
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 { DEFAULT_SERVER_SETTINGS } from "@t3tools/contracts";
9import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
10import { applyServerSettingsPatch } from "@t3tools/shared/serverSettings";
11import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost";
12import * as GitHubCredentials from "./GitHubCredentials.ts";
13​
14const TOKEN_VARIABLES = [
15 "GH_TOKEN",
16 "GITHUB_TOKEN",
17 "GH_ENTERPRISE_TOKEN",
18 "GITHUB_ENTERPRISE_TOKEN",
19] as const;
20​
21const hostSettings = (
22 hosts: Record<string, { readonly account?: string; readonly enabled?: boolean }>,
23) =>
24 Object.fromEntries(
25 Object.entries(hosts).map(([host, choice]) => [host, { enabled: true, ...choice }]),
26 );
27​
28function harness(
29 hosts: Record<string, { readonly account?: string; readonly enabled?: boolean }> = {},
30 signedOut: ReadonlyArray<string> = [],
31 tokens: Record<string, string> = {},
32) {
33 const calls: Array<ReadonlyArray<string>> = [];
34 const run: SourceControlHost.SourceControlHost["Service"]["process"]["run"] = (input) =>
35 Effect.sync(() => {
36 calls.push(input.args);
37 const user = input.args[input.args.indexOf("--user") + 1];
38 if (input.args.includes("--user") && user !== undefined && signedOut.includes(user)) {
39 return {
40 exitCode: ChildProcessSpawner.ExitCode(0),
41 stdout: "",
42 stderr: "",
43 stdoutTruncated: false,
44 stderrTruncated: false,
45 };
46 }
47 return {
48 exitCode: ChildProcessSpawner.ExitCode(0),
49 stdout: input.args.includes("--user") ? `token-for-${user}\n` : "active-token\n",
50 stderr: "",
51 stdoutTruncated: false,
52 stderrTruncated: false,
53 };
54 });
55 const layer = GitHubCredentials.layer.pipe(
56 Layer.provideMerge(
57 TestSourceControlHost.layer({
58 settings: applyServerSettingsPatch(DEFAULT_SERVER_SETTINGS, {
59 sourceControlHosts: { github: { hosts: hostSettings(hosts), tokens } },
60 }),
61 process: { run },
62 }),
63 ),
64 Layer.provide(NodeCrypto.layer),
65 );
66 return { layer, calls };
68​
69describe("GitHubCredentials", () => {
70 beforeEach(() => {
71 for (const name of TOKEN_VARIABLES) vi.stubEnv(name, "");
72 });
73 afterEach(() => {
74 vi.unstubAllEnvs();
75 });
76​
77 it.effect("lets an environment token win over a pinned account, as gh does", () => {
78 vi.stubEnv("GH_TOKEN", "from-env");
79 const { layer, calls } = harness({ "github.com": { account: "work" } });
80 return Effect.gen(function* () {
81 const credentials = yield* GitHubCredentials.GitHubCredentials;
82 const credential = yield* credentials.get("github.com");
83 expect(Redacted.value(credential.token)).toBe("from-env");
84 expect(credential.source).toBe("env");
85 expect(calls).toEqual([]);
86 }).pipe(Effect.provide(layer));
87 });
88​
89 it.effect("picks up a changed account on the next request without a restart", () => {
90 const { layer, calls } = harness();
91 return Effect.gen(function* () {
92 const credentials = yield* GitHubCredentials.GitHubCredentials;
93 const settings = yield* TestSourceControlHost.TestSourceControlHostSettings;
94 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("active-token");
95​
96 yield* settings.update({
97 sourceControlHosts: {
98 github: { hosts: { "github.com": { account: "work", enabled: true } } },
99 },
100 });
101 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("token-for-work");
102​
103 yield* settings.update({
104 sourceControlHosts: { github: { hosts: { "github.com": { enabled: false } } } },
105 });
106 expect((yield* Effect.flip(credentials.get("github.com")))._tag).toBe(
107 "GitHubHostDisabledError",
108 );
109​
110 yield* settings.update({ sourceControlHosts: { github: { hosts: {} } } });
111 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("active-token");
112 expect(calls).toHaveLength(2);
113 }).pipe(Effect.provide(layer));
114 });
115​
116 it.effect("falls back to the active login when the pinned one is no longer signed in", () => {
117 const { layer, calls } = harness({ "github.com": { account: "gone" } }, ["gone"]);
118 return Effect.gen(function* () {
119 const credentials = yield* GitHubCredentials.GitHubCredentials;
120 expect(Redacted.value((yield* credentials.get("github.com")).token)).toBe("active-token");
121 expect(calls).toEqual([
122 ["auth", "token", "--hostname", "github.com", "--user", "gone"],
123 ["auth", "token", "--hostname", "github.com"],
124 ]);
125 }).pipe(Effect.provide(layer));
126 });
127​
128 it.effect("uses a token saved in Settings before GH_TOKEN and gh", () => {
129 vi.stubEnv("GH_TOKEN", "env-token");
130 const { layer, calls } = harness({}, [], { "github.com": "saved-token" });
131 return Effect.gen(function* () {
132 const credentials = yield* GitHubCredentials.GitHubCredentials;
133 const credential = yield* credentials.get("github.com");
134 expect(Redacted.value(credential.token)).toBe("saved-token");
135 expect(credential.source).toBe("settings");
136 expect(calls).toEqual([]);
137 }).pipe(Effect.provide(layer));
138 });
139​
140 it.effect("keeps a host turned off even with a token saved in Settings", () => {
141 const { layer } = harness({ "github.com": { enabled: false } }, [], {
142 "github.com": "saved-token",
143 });
144 return Effect.gen(function* () {
145 const credentials = yield* GitHubCredentials.GitHubCredentials;
146 expect((yield* Effect.flip(credentials.get("github.com")))._tag).toBe(
147 "GitHubHostDisabledError",
148 );
149 }).pipe(Effect.provide(layer));
150 });
151});