|
| 1 | +import { describe, expect, it } from "@effect/vitest"; |
| 2 | +import * as Effect from "effect/Effect"; |
| 3 | +import * as Layer from "effect/Layer"; |
| 4 | +import * as Option from "effect/Option"; |
| 5 | +import * as Redacted from "effect/Redacted"; |
| 6 | + |
| 7 | +import type * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; |
| 8 | +import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; |
| 9 | +import * as GitLabCredentials from "./GitLabCredentials.ts"; |
| 10 | + |
| 11 | +/** A scripted glab: `config get <key> --host <host>` reads `config`, `api` refreshes. */ |
| 12 | +function harness(config: Record<string, Record<string, string>>, refreshed?: string) { |
| 13 | + const runs: Array<SourceControlHost.SourceControlProcessInput> = []; |
| 14 | + const run: SourceControlHost.SourceControlHost["Service"]["process"]["run"] = (input) => |
| 15 | + Effect.sync(() => { |
| 16 | + runs.push(input); |
| 17 | + const host = input.args[input.args.indexOf("--host") + 1] ?? ""; |
| 18 | + if (input.args[0] === "api") { |
| 19 | + if (refreshed !== undefined) config[input.args[2]!]!.token = refreshed; |
| 20 | + return TestSourceControlHost.processOutput("{}"); |
| 21 | + } |
| 22 | + return TestSourceControlHost.processOutput(`${config[host]?.[input.args[2]!] ?? ""}\n`); |
| 23 | + }); |
| 24 | + const layer = GitLabCredentials.layer.pipe( |
| 25 | + Layer.provide(TestSourceControlHost.layer({ process: { run } })), |
| 26 | + ); |
| 27 | + return { layer, runs }; |
| 28 | +} |
| 29 | + |
| 30 | +const token = (host: string) => |
| 31 | + GitLabCredentials.GitLabCredentials.pipe( |
| 32 | + Effect.flatMap((credentials) => credentials.get(host)), |
| 33 | + Effect.map(Option.map(Redacted.value)), |
| 34 | + ); |
| 35 | + |
| 36 | +describe("GitLabCredentials", () => { |
| 37 | + it.effect("reads only the token glab stores for the host", () => { |
| 38 | + const { layer, runs } = harness({ "gitlab.example.com": { token: "stored" } }); |
| 39 | + return Effect.gen(function* () { |
| 40 | + expect(yield* token("GitLab.Example.com")).toEqual(Option.some("stored")); |
| 41 | + expect(runs.map((input) => input.args)).toEqual([ |
| 42 | + ["config", "get", "token", "--host", "gitlab.example.com"], |
| 43 | + ["config", "get", "is_oauth2", "--host", "gitlab.example.com"], |
| 44 | + ["config", "get", "token", "--host", "gitlab.example.com"], |
| 45 | + ]); |
| 46 | + // glab would otherwise answer every host with an ambient token. |
| 47 | + for (const input of runs) { |
| 48 | + expect(input.env).toMatchObject({ GITLAB_TOKEN: "", OAUTH_TOKEN: "", CI_JOB_TOKEN: "" }); |
| 49 | + } |
| 50 | + // Cached: a second read asks glab nothing. |
| 51 | + yield* token("gitlab.example.com"); |
| 52 | + expect(runs).toHaveLength(3); |
| 53 | + }).pipe(Effect.provide(layer)); |
| 54 | + }); |
| 55 | + |
| 56 | + it.effect("has nothing for a host glab holds no login for", () => { |
| 57 | + const { layer, runs } = harness({}); |
| 58 | + return Effect.gen(function* () { |
| 59 | + expect(yield* token("attacker.example")).toEqual(Option.none()); |
| 60 | + expect(runs).toHaveLength(1); |
| 61 | + }).pipe(Effect.provide(layer)); |
| 62 | + }); |
| 63 | + |
| 64 | + it.effect("refreshes an OAuth login through an API call before reading it", () => { |
| 65 | + const { layer, runs } = harness( |
| 66 | + { "gitlab.com": { token: "expired", is_oauth2: "true" } }, |
| 67 | + "refreshed", |
| 68 | + ); |
| 69 | + return Effect.gen(function* () { |
| 70 | + expect(yield* token("gitlab.com")).toEqual(Option.some("refreshed")); |
| 71 | + expect(runs.map((input) => input.args)).toContainEqual([ |
| 72 | + "api", |
| 73 | + "--hostname", |
| 74 | + "gitlab.com", |
| 75 | + "user", |
| 76 | + ]); |
| 77 | + }).pipe(Effect.provide(layer)); |
| 78 | + }); |
| 79 | + |
| 80 | + it.effect("asks glab again after a refused token is invalidated", () => { |
| 81 | + const { layer, runs } = harness({ "gitlab.com": { token: "stored" } }); |
| 82 | + return Effect.gen(function* () { |
| 83 | + const credentials = yield* GitLabCredentials.GitLabCredentials; |
| 84 | + yield* credentials.get("gitlab.com"); |
| 85 | + yield* credentials.invalidate("gitlab.com"); |
| 86 | + yield* credentials.get("gitlab.com"); |
| 87 | + expect(runs).toHaveLength(6); |
| 88 | + }).pipe(Effect.provide(layer)); |
| 89 | + }); |
| 90 | +}); |
0 commit comments