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

BootstrapCredentialService.test.ts

153 lines6.3 KB

1import * as NodeServices from "@effect/platform-node/NodeServices";
2import { expect, it } from "@effect/vitest";
3import * as Duration from "effect/Duration";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as TestClock from "effect/testing/TestClock";
7​
8import type { ServerConfigShape } from "../../config.ts";
9import { ServerConfig } from "../../config.ts";
10import { SqlitePersistenceMemory } from "../../persistence/Layers/Sqlite.ts";
11import { BootstrapCredentialService } from "../Services/BootstrapCredentialService.ts";
12import { BootstrapCredentialServiceLive } from "./BootstrapCredentialService.ts";
13​
14const makeServerConfigLayer = (
15 overrides?: Partial<Pick<ServerConfigShape, "desktopBootstrapToken">>,
16) =>
17 Layer.effect(
18 ServerConfig,
19 Effect.gen(function* () {
20 const config = yield* ServerConfig;
21 return {
22 ...config,
23 ...overrides,
24 } satisfies ServerConfigShape;
25 }),
26 ).pipe(
27 Layer.provide(ServerConfig.layerTest(process.cwd(), { prefix: "t3-auth-bootstrap-test-" })),
28 );
29​
30const makeBootstrapCredentialLayer = (
31 overrides?: Partial<Pick<ServerConfigShape, "desktopBootstrapToken">>,
32) =>
33 BootstrapCredentialServiceLive.pipe(
34 Layer.provide(SqlitePersistenceMemory),
35 Layer.provide(makeServerConfigLayer(overrides)),
36 );
37​
38it.layer(NodeServices.layer)("BootstrapCredentialServiceLive", (it) => {
39 it.effect("issues pairing tokens in a short manual-entry format", () =>
40 Effect.gen(function* () {
41 const bootstrapCredentials = yield* BootstrapCredentialService;
42 const issued = yield* bootstrapCredentials.issueOneTimeToken();
43​
44 expect(issued.credential).toMatch(/^[23456789ABCDEFGHJKLMNPQRSTUVWXYZ]{12}$/);
45 }).pipe(Effect.provide(makeBootstrapCredentialLayer())),
46 );
47​
48 it.effect("issues one-time bootstrap tokens that can only be consumed once", () =>
49 Effect.gen(function* () {
50 const bootstrapCredentials = yield* BootstrapCredentialService;
51 const issued = yield* bootstrapCredentials.issueOneTimeToken({ label: "Julius iPhone" });
52 const first = yield* bootstrapCredentials.consume(issued.credential);
53 const second = yield* Effect.flip(bootstrapCredentials.consume(issued.credential));
54​
55 expect(first.method).toBe("one-time-token");
56 expect(first.role).toBe("client");
57 expect(first.subject).toBe("one-time-token");
58 expect(first.label).toBe("Julius iPhone");
59 expect(issued.label).toBe("Julius iPhone");
60 expect(second._tag).toBe("BootstrapCredentialError");
61 expect(second.message).toContain("Unknown bootstrap credential");
62 }).pipe(Effect.provide(makeBootstrapCredentialLayer())),
63 );
64​
65 it.effect("atomically consumes a one-time token when multiple requests race", () =>
66 Effect.gen(function* () {
67 const bootstrapCredentials = yield* BootstrapCredentialService;
68 const token = yield* bootstrapCredentials.issueOneTimeToken();
69 const results = yield* Effect.all(
70 Array.from({ length: 8 }, () =>
71 Effect.result(bootstrapCredentials.consume(token.credential)),
72 ),
73 {
74 concurrency: "unbounded",
75 },
76 );
77​
78 const successes = results.filter((result) => result._tag === "Success");
79 const failures = results.filter((result) => result._tag === "Failure");
80​
81 expect(successes).toHaveLength(1);
82 expect(failures).toHaveLength(7);
83 for (const failure of failures) {
84 expect(failure.failure._tag).toBe("BootstrapCredentialError");
85 expect(failure.failure.message).toContain("Unknown bootstrap credential");
86 }
87 }).pipe(Effect.provide(makeBootstrapCredentialLayer())),
88 );
89​
90 it.effect("seeds the desktop bootstrap credential as a one-time grant", () =>
91 Effect.gen(function* () {
92 const bootstrapCredentials = yield* BootstrapCredentialService;
93 const first = yield* bootstrapCredentials.consume("desktop-bootstrap-token");
94 const second = yield* Effect.flip(bootstrapCredentials.consume("desktop-bootstrap-token"));
95​
96 expect(first.method).toBe("desktop-bootstrap");
97 expect(first.role).toBe("owner");
98 expect(first.subject).toBe("desktop-bootstrap");
99 expect(second._tag).toBe("BootstrapCredentialError");
100 expect(second.status).toBe(401);
101 }).pipe(
102 Effect.provide(
103 makeBootstrapCredentialLayer({
104 desktopBootstrapToken: "desktop-bootstrap-token",
105 }),
106 ),
107 ),
108 );
109​
110 it.effect("reports seeded desktop bootstrap credentials as expired after their ttl", () =>
111 Effect.gen(function* () {
112 const bootstrapCredentials = yield* BootstrapCredentialService;
113​
114 yield* TestClock.adjust(Duration.minutes(6));
115 const expired = yield* Effect.flip(bootstrapCredentials.consume("desktop-bootstrap-token"));
116​
117 expect(expired._tag).toBe("BootstrapCredentialError");
118 expect(expired.status).toBe(401);
119 expect(expired.message).toContain("Bootstrap credential expired");
120 }).pipe(
121 Effect.provide(
122 Layer.merge(
123 makeBootstrapCredentialLayer({
124 desktopBootstrapToken: "desktop-bootstrap-token",
125 }),
126 TestClock.layer(),
127 ),
128 ),
129 ),
130 );
131​
132 it.effect("lists and revokes active pairing links", () =>
133 Effect.gen(function* () {
134 const bootstrapCredentials = yield* BootstrapCredentialService;
135 const first = yield* bootstrapCredentials.issueOneTimeToken();
136 const second = yield* bootstrapCredentials.issueOneTimeToken({ role: "owner" });
137​
138 const activeBeforeRevoke = yield* bootstrapCredentials.listActive();
139 expect(activeBeforeRevoke.map((entry) => entry.id)).toContain(first.id);
140 expect(activeBeforeRevoke.map((entry) => entry.id)).toContain(second.id);
141​
142 const revoked = yield* bootstrapCredentials.revoke(first.id);
143 const activeAfterRevoke = yield* bootstrapCredentials.listActive();
144 const revokedConsume = yield* Effect.flip(bootstrapCredentials.consume(first.credential));
145​
146 expect(revoked).toBe(true);
147 expect(activeAfterRevoke.map((entry) => entry.id)).not.toContain(first.id);
148 expect(activeAfterRevoke.map((entry) => entry.id)).toContain(second.id);
149 expect(revokedConsume.message).toContain("no longer available");
150 expect(revokedConsume.status).toBe(401);
151 }).pipe(Effect.provide(makeBootstrapCredentialLayer())),
152 );
153});