Skip to content
Myr

credentials.test.ts

This file

Path

apps/cli/test/credentials.test.ts

229 lines · 7.0 KiB · TypeScript

Last changed by

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

History of this file

Take it

The walk stopped after 200 commits: lines older than 678c8b8 point at it.

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 1–35why, one step back
1import { NodeServices } from "@effect/platform-node";
2import { describe, expect, it } from "@effect/vitest";3import { Brand } from "@repo/brand";4import { Handle } from "@repo/protocol/ids";5import {6 ConfigProvider,7 Effect,8 FileSystem,9 Layer,10 Option,11 Path,12 Redacted,13} from "effect";1415import {16 Credentials,17 CredentialsFile,18 CredentialsUnwritable,19 gitHelperConfig,20} from "../src/credentials.ts";21import type { Profile } from "../src/credentials.ts";2223const SLUG = Brand.slug.toUpperCase();2425const token = "tok-default-0123456789abcdefghijklmnopqrstuvwxyz";2627const defaultProfile: Profile = {28 handle: Handle.make("oscar"),29 token: Redacted.make(token),30 url: "https://platform-dev.example.workers.dev",31};3233const aliceProfile: Profile = {34 handle: Handle.make("alice"),35 token: Redacted.make("tok-alice-0123456789abcdefghijklmnopqrstuvwxyz"),

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

line 36why, one step back
36 url: "https://platform-dev-oscabriel.example.workers.dev",

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 37–136why, one step back
37};
3839const configHome = Effect.map(40 Effect.flatMap(FileSystem.FileSystem, (fs) => fs.makeTempDirectoryScoped()),41 (dir) => ({ dir, env: { HOME: "/nonexistent", XDG_CONFIG_HOME: dir } })42);4344const credentialsIn = (env: Record<string, string>) =>45 Effect.provide(46 Effect.service(Credentials),47 CredentialsFile.pipe(48 Layer.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env })))49 )50 );5152const seen = (profile: Option.Option<Profile>) =>53 Option.map(profile, ({ handle, token: secret, url }) => ({54 handle,55 token: Redacted.value(secret),56 url,57 }));5859describe("stored credentials", () => {60 it.live(61 "are written to <config>/<slug>/credentials.json, readable by the owner alone, and read back",62 () =>63 Effect.gen(function* () {64 const fs = yield* FileSystem.FileSystem;65 const path = yield* Path.Path;66 const { dir, env } = yield* configHome;67 const credentials = yield* credentialsIn(env);6869 yield* credentials.put("default", defaultProfile);7071 const file = path.join(dir, Brand.slug, "credentials.json");7273 expect(yield* fs.exists(file)).toBeTruthy();7475 const info = yield* fs.stat(file);7677 expect(info.mode % 0o1000).toBe(0o600);78 expect(seen(yield* credentials.current)).toStrictEqual(79 seen(Option.some(defaultProfile))80 );81 expect(seen(yield* (yield* credentialsIn(env)).current)).toStrictEqual(82 seen(Option.some(defaultProfile))83 );84 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))85 );8687 it.live("give way to a token and url in the env", () =>88 Effect.gen(function* () {89 const { env } = yield* configHome;9091 yield* (yield* credentialsIn(env)).put("default", defaultProfile);9293 const fromEnv = yield* credentialsIn({94 ...env,95 [`${SLUG}_TOKEN`]: "tok-env-0123456789abcdefghijklmnopqrstuvwxyz",96 [`${SLUG}_URL`]: "https://platform-ci.example.workers.dev",97 });9899 expect(seen(yield* fromEnv.current)).toStrictEqual(100 Option.some({101 handle: undefined,102 token: "tok-env-0123456789abcdefghijklmnopqrstuvwxyz",103 url: "https://platform-ci.example.workers.dev",104 })105 );106 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))107 );108109 it.live("are chosen by the profile the env names", () =>110 Effect.gen(function* () {111 const { env } = yield* configHome;112 const credentials = yield* credentialsIn(env);113114 yield* credentials.put("default", defaultProfile);115 yield* credentials.put("alice", aliceProfile);116117 const alice = yield* credentialsIn({118 ...env,119 [`${SLUG}_PROFILE`]: "alice",120 });121122 expect(seen(yield* alice.current)).toStrictEqual(123 seen(Option.some(aliceProfile))124 );125 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))126 );127128 it.live("are found by the platform's host", () =>129 Effect.gen(function* () {130 const { env } = yield* configHome;131 const credentials = yield* credentialsIn(env);132133 yield* credentials.put("default", defaultProfile);134 yield* credentials.put("alice", aliceProfile);135136 expect(

Merge infra/drop-demo into mainoscabrielbefore adoptionc49b6bf

lines 137–141why, one step back
137 seen(
138 yield* credentials.forHost(139 "platform-dev-oscabriel.example.workers.dev"140 )141 )

Merge build/614-env-service: deciders set write-only values for @sensitive items, the public sees the env shape, and checks see only placeholdersoscabrielbefore adoption678c8b8

lines 142–229why, one step back
142 ).toStrictEqual(seen(Option.some(aliceProfile)));
143 expect(yield* credentials.forHost("elsewhere.example.com")).toStrictEqual(144 Option.none()145 );146 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))147 );148149 it.live("are removed by name, once", () =>150 Effect.gen(function* () {151 const { env } = yield* configHome;152 const credentials = yield* credentialsIn(env);153154 yield* credentials.put("default", defaultProfile);155156 expect(yield* credentials.remove("default")).toBeTruthy();157 expect(yield* credentials.remove("default")).toBeFalsy();158 expect(yield* credentials.current).toStrictEqual(Option.none());159 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))160 );161162 it.live("never show the token when printed or encoded", () =>163 Effect.gen(function* () {164 const { env } = yield* configHome;165 const credentials = yield* credentialsIn(env);166167 yield* credentials.put("default", defaultProfile);168169 const profile = yield* credentials.current;170171 expect(Option.isSome(profile)).toBeTruthy();172 expect(String(profile)).not.toContain(token);173 expect(JSON.stringify(profile)).not.toContain(token);174 expect(JSON.stringify(Option.getOrUndefined(profile))).not.toContain(175 token176 );177 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))178 );179180 it.live(181 "are never written over a file that can't be read, and read as none",182 () =>183 Effect.gen(function* () {184 const fs = yield* FileSystem.FileSystem;185 const path = yield* Path.Path;186 const { dir, env } = yield* configHome;187 const file = path.join(dir, Brand.slug, "credentials.json");188189 yield* fs.makeDirectory(path.dirname(file), { recursive: true });190 yield* fs.writeFileString(file, "not json");191192 const credentials = yield* credentialsIn(env);193194 const put = yield* Effect.flip(195 credentials.put("default", defaultProfile)196 );197198 expect(put).toStrictEqual(new CredentialsUnwritable({ path: file }));199 expect(yield* fs.readFileString(file)).toBe("not json");200 expect(yield* credentials.current).toStrictEqual(Option.none());201 }).pipe(Effect.scoped, Effect.provide(NodeServices.layer))202 );203});204205describe("git's credential helper config", () => {206 it("names this CLI's helper for the platform's url alone, after clearing any other, with the repo path", () => {207 const url = "https://platform-dev.example.workers.dev";208209 expect(gitHelperConfig(url)).toStrictEqual([210 [211 "git",212 "config",213 "--global",214 "--replace-all",215 `credential.${url}.helper`,216 "",217 ],218 [219 "git",220 "config",221 "--global",222 "--add",223 `credential.${url}.helper`,224 Brand.slug,225 ],226 ["git", "config", "--global", `credential.${url}.useHttpPath`, "true"],227 ]);228 });229});