MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / withWorkOSStub

Function withWorkOSStub

apps/cloud/src/auth/workos.node.test.ts:121–175  ·  view source on GitHub ↗
(
  keypair: Keypair,
  use: (stub: WorkOSStub) => Promise<A>,
)

Source from the content-addressed store, hash-verified

119};
120
121const withWorkOSStub = async <A>(
122 keypair: Keypair,
123 use: (stub: WorkOSStub) => Promise<A>,
124): Promise<A> => {
125 let keys: ReadonlyArray<JWK> = [keypair.publicJwk];
126 const requests: RecordedRequest[] = [];
127 const server = createServer((request, response) => {
128 // oxlint-disable-next-line executor/no-promise-catch -- boundary: Node's request callback cannot await, so the fixture converts async failures into a stable HTTP 500
129 void (async () => {
130 const path = request.url ?? "/";
131 const body = request.method === "POST" ? await readJsonBody(request) : null;
132 requests.push({ method: request.method ?? "GET", path, body });
133
134 if (request.method === "GET" && path === `/sso/jwks/${CLIENT_ID}`) {
135 writeJson(response, 200, { keys: keys.map((key) => ({ ...key })) } satisfies JSONWebKeySet);
136 return;
137 }
138
139 if (request.method === "POST" && path === "/user_management/authenticate") {
140 const refreshed = await signAccessToken(keypair, {
141 sessionId: "session_refreshed",
142 });
143 writeJson(response, 200, {
144 user: workosUserResponse,
145 organization_id: "org_test",
146 access_token: refreshed,
147 refresh_token: "refresh_next",
148 authentication_method: "Password",
149 });
150 return;
151 }
152
153 writeJson(response, 404, { error: "unexpected request" });
154 })().catch((_error: unknown) => {
155 writeJson(response, 500, {
156 error: "fixture server failed",
157 });
158 });
159 });
160
161 await new Promise<void>((resolve) => server.listen(0, "127.0.0.1", resolve));
162 const address = server.address() as AddressInfo;
163 const stub: WorkOSStub = {
164 baseUrl: `http://127.0.0.1:${address.port}`,
165 requests: () => requests,
166 setKeys: (next) => {
167 keys = next;
168 },
169 };
170
171 return use(stub).finally(() => {
172 server.closeAllConnections();
173 server.close();
174 });
175};
176
177const runAuthenticate = (sessionData: string, baseUrl: string) => {
178 Object.assign(env, {

Callers 1

Calls 8

readJsonBodyFunction · 0.85
signAccessTokenFunction · 0.85
pushMethod · 0.80
listenMethod · 0.80
addressMethod · 0.80
writeJsonFunction · 0.70
useFunction · 0.70
closeMethod · 0.65

Tested by

no test coverage detected