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

BootstrapCredentialService.test.ts

151 lines6.2 KB

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