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

storageDocument.test.ts

232 lines7.3 KB

1import { EnvironmentId } from "@t3tools/contracts";
2import { describe, expect, it } from "@effect/vitest";
3import * as Schema from "effect/Schema";
4​
5import * as TokenStore from "../authorization/tokenStore.ts";
6import {
7 BearerConnectionCredential,
8 BearerConnectionProfile,
9 BearerConnectionRegistration,
10 RelayConnectionRegistration,
11 SshConnectionProfile,
12 SshConnectionRegistration,
13} from "../connection/catalog.ts";
14import {
15 BearerConnectionTarget,
16 RelayConnectionTarget,
17 SshConnectionTarget,
18} from "../connection/model.ts";
19import {
20 ConnectionCatalogDocument,
21 EMPTY_CONNECTION_CATALOG_DOCUMENT,
22 putRemoteDpopTokenInCatalog,
23 registerConnectionInCatalog,
24 removeConnectionFromCatalog,
25} from "./storageDocument.ts";
26​
27const ENVIRONMENT_ID = EnvironmentId.make("environment-1");
28​
29const RELAY_TARGET = new RelayConnectionTarget({
30 environmentId: ENVIRONMENT_ID,
31 label: "Remote",
32});
33const BEARER_TARGET = new BearerConnectionTarget({
34 environmentId: ENVIRONMENT_ID,
35 label: "Remote",
36 connectionId: "bearer-1",
37});
38const BEARER_PROFILE = new BearerConnectionProfile({
39 connectionId: BEARER_TARGET.connectionId,
40 environmentId: ENVIRONMENT_ID,
41 label: BEARER_TARGET.label,
42 httpBaseUrl: "https://remote.example.test",
43 wsBaseUrl: "wss://remote.example.test",
44});
45const BEARER_CREDENTIAL = new BearerConnectionCredential({
46 token: "bearer-token",
47});
48const REMOTE_TOKEN = new TokenStore.RemoteDpopAccessToken({
49 environmentId: ENVIRONMENT_ID,
50 label: "Remote",
51 endpoint: {
52 httpBaseUrl: "https://remote.example.test",
53 wsBaseUrl: "wss://remote.example.test",
54 providerKind: "cloudflare_tunnel",
55 },
56 accessToken: "dpop-token",
57 expiresAtEpochMs: 1_000_000,
58 dpopThumbprint: "thumbprint",
59});
60​
61describe("ConnectionCatalogDocument", () => {
62 it.each([
63 { name: "legacy", accountId: undefined },
64 { name: "account-bound", accountId: "account-1" },
65 ])("round-trips a catalog containing a $name DPoP token", ({ accountId }) => {
66 const token = new TokenStore.RemoteDpopAccessToken({
67 ...REMOTE_TOKEN,
68 ...(accountId === undefined ? {} : { accountId }),
69 });
70 const document = {
71 ...EMPTY_CONNECTION_CATALOG_DOCUMENT,
72 targets: [RELAY_TARGET],
73 remoteDpopTokens: [token],
74 };
75 const schema = Schema.fromJsonString(ConnectionCatalogDocument);
76 const restored = Schema.decodeUnknownSync(schema)(Schema.encodeSync(schema)(document));
77​
78 expect(restored).toEqual(document);
79 expect(restored.remoteDpopTokens[0]?.accountId).toBe(accountId);
80 });
81​
82 it("registers a bearer connection as one catalog mutation", () => {
83 const document = registerConnectionInCatalog(
84 EMPTY_CONNECTION_CATALOG_DOCUMENT,
85 new BearerConnectionRegistration({
86 target: BEARER_TARGET,
87 profile: BEARER_PROFILE,
88 credential: BEARER_CREDENTIAL,
89 }),
90 );
91​
92 expect(document.targets).toEqual([BEARER_TARGET]);
93 expect(document.profiles).toEqual([BEARER_PROFILE]);
94 expect(document.credentials).toEqual([
95 {
96 connectionId: BEARER_TARGET.connectionId,
97 credential: BEARER_CREDENTIAL,
98 },
99 ]);
100 });
101​
102 it("replaces obsolete connection metadata without discarding a reusable DPoP token", () => {
103 const bearer = registerConnectionInCatalog(
104 {
105 ...EMPTY_CONNECTION_CATALOG_DOCUMENT,
106 remoteDpopTokens: [REMOTE_TOKEN],
107 },
108 new BearerConnectionRegistration({
109 target: BEARER_TARGET,
110 profile: BEARER_PROFILE,
111 credential: BEARER_CREDENTIAL,
112 }),
113 );
114 const relayTarget = new RelayConnectionTarget({
115 environmentId: ENVIRONMENT_ID,
116 label: "Remote",
117 });
118 const relay = registerConnectionInCatalog(
119 bearer,
120 new RelayConnectionRegistration({ target: relayTarget }),
121 );
122​
123 expect(relay.targets).toEqual([relayTarget]);
124 expect(relay.profiles).toEqual([]);
125 expect(relay.credentials).toEqual([]);
126 expect(relay.remoteDpopTokens).toEqual([REMOTE_TOKEN]);
127 });
128​
129 it("removes every catalog record owned by an explicit disconnect", () => {
130 const registered = registerConnectionInCatalog(
131 {
132 ...EMPTY_CONNECTION_CATALOG_DOCUMENT,
133 remoteDpopTokens: [REMOTE_TOKEN],
134 },
135 new BearerConnectionRegistration({
136 target: BEARER_TARGET,
137 profile: BEARER_PROFILE,
138 credential: BEARER_CREDENTIAL,
139 }),
140 );
141​
142 expect(removeConnectionFromCatalog(registered, BEARER_TARGET)).toEqual(
143 EMPTY_CONNECTION_CATALOG_DOCUMENT,
144 );
145 });
146​
147 it("stores a DPoP token for a registered relay environment", () => {
148 const registered = registerConnectionInCatalog(
149 EMPTY_CONNECTION_CATALOG_DOCUMENT,
150 new RelayConnectionRegistration({ target: RELAY_TARGET }),
151 );
152​
153 expect(putRemoteDpopTokenInCatalog(registered, REMOTE_TOKEN)).toEqual({
154 ...registered,
155 remoteDpopTokens: [REMOTE_TOKEN],
156 });
157 });
158​
159 it("ignores a late DPoP token after the environment is removed", () => {
160 const registered = registerConnectionInCatalog(
161 EMPTY_CONNECTION_CATALOG_DOCUMENT,
162 new RelayConnectionRegistration({ target: RELAY_TARGET }),
163 );
164 const removed = removeConnectionFromCatalog(registered, RELAY_TARGET);
165​
166 expect(putRemoteDpopTokenInCatalog(removed, REMOTE_TOKEN)).toEqual(
167 EMPTY_CONNECTION_CATALOG_DOCUMENT,
168 );
169 });
170​
171 it("removes a DPoP token stored before the environment is removed", () => {
172 const registered = registerConnectionInCatalog(
173 EMPTY_CONNECTION_CATALOG_DOCUMENT,
174 new RelayConnectionRegistration({ target: RELAY_TARGET }),
175 );
176 const refreshed = putRemoteDpopTokenInCatalog(registered, REMOTE_TOKEN);
177​
178 expect(removeConnectionFromCatalog(refreshed, RELAY_TARGET)).toEqual(
179 EMPTY_CONNECTION_CATALOG_DOCUMENT,
180 );
181 });
182​
183 it("does not store DPoP tokens for a different environment or connection kind", () => {
184 const bearer = registerConnectionInCatalog(
185 EMPTY_CONNECTION_CATALOG_DOCUMENT,
186 new BearerConnectionRegistration({
187 target: BEARER_TARGET,
188 profile: BEARER_PROFILE,
189 credential: BEARER_CREDENTIAL,
190 }),
191 );
192 const otherRelay = registerConnectionInCatalog(
193 EMPTY_CONNECTION_CATALOG_DOCUMENT,
194 new RelayConnectionRegistration({
195 target: new RelayConnectionTarget({
196 environmentId: EnvironmentId.make("environment-2"),
197 label: "Other relay",
198 }),
199 }),
200 );
201​
202 expect(putRemoteDpopTokenInCatalog(bearer, REMOTE_TOKEN)).toBe(bearer);
203 expect(putRemoteDpopTokenInCatalog(otherRelay, REMOTE_TOKEN)).toBe(otherRelay);
204 });
205​
206 it("persists the normalized SSH profile beside its target", () => {
207 const target = new SshConnectionTarget({
208 environmentId: ENVIRONMENT_ID,
209 label: "SSH",
210 connectionId: "ssh-1",
211 });
212 const profile = new SshConnectionProfile({
213 connectionId: target.connectionId,
214 environmentId: target.environmentId,
215 label: target.label,
216 target: {
217 alias: "devbox",
218 hostname: "devbox.example.test",
219 username: "developer",
220 port: 22,
221 },
222 });
223 const document = registerConnectionInCatalog(
224 EMPTY_CONNECTION_CATALOG_DOCUMENT,
225 new SshConnectionRegistration({ target, profile }),
226 );
227​
228 expect(document.targets).toEqual([target]);
229 expect(document.profiles).toEqual([profile]);
230 expect(document.credentials).toEqual([]);
231 });
232});