History

Merge remote-tracking branch 'origin/codex/batch10-openapi-oauth-tests' into codex/effect-lint-integration

Rhys Sullivan committed 6 months ago

Commit 86d576a2 parents a1200b1 4194ac62 files · +51 −19Browse files

Unified · Split

2 files
  1. packages/plugins/openapi/src/sdk/client-credentials-oauth.test.ts +19 −5
  2. packages/plugins/openapi/src/sdk/oauth-refresh.test.ts +32 −14
packages/plugins/openapi/src/sdk/client-credentials-oauth.test.ts+19−5
@@ -31,6 +31,13 @@ import { OAuth2Auth } from "./types"; · 30 unmodified lines
3131
3232 const autoApprove: InvokeOptions = { onElicitation: "accept-all" };
3333
34+class OpenApiClientCredentialsTestSetupError extends Schema.TaggedErrorClass<OpenApiClientCredentialsTestSetupError>()(
35+ "OpenApiClientCredentialsTestSetupError",
36+ {
37+ message: Schema.String,
38+ },
39+) {}
40+
3441 // ---------------------------------------------------------------------------
3542 // Test API — single endpoint that echoes the Authorization header.
3643 // ---------------------------------------------------------------------------
@@ -147,7 +154,11 @@ layer(TestLayer)("OpenAPI client_credentials OAuth", (it) => { · 110 unmodified lines
147154 const clientLayer = FetchHttpClient.layer;
148155 const server = yield* HttpServer.HttpServer;
149156 const address = server.address;
150− if (address._tag !== "TcpAddress") return yield* Effect.die("test server must bind to TCP");
157+ if (!("port" in address)) {
158+ return yield* new OpenApiClientCredentialsTestSetupError({
159+ message: "Test server must bind to TCP",
160+ });
161+ }
151162 const baseUrl = `http://127.0.0.1:${address.port}`;
152163 const plugins = [
153164 openApiPlugin({ httpClientLayer: clientLayer }),
@@ -231,12 +242,15 @@ layer(TestLayer)("OpenAPI client_credentials OAuth", (it) => { · 77 unmodified lines
231242 },
232243 });
233244
234− if (!started.completedConnection) {
235− throw new Error("expected completed clientCredentials connection");
245+ const completedConnection = started.completedConnection;
246+ if (!completedConnection) {
247+ return yield* new OpenApiClientCredentialsTestSetupError({
248+ message: "Expected completed clientCredentials connection",
249+ });
236250 }
237251 const auth = new OAuth2Auth({
238252 kind: "oauth2",
239− connectionId: started.completedConnection.connectionId,
253+ connectionId: completedConnection.connectionId,
240254 securitySchemeName: "oauth2",
241255 flow: "clientCredentials",
242256 tokenUrl: "https://token.example.com/token",
@@ -257,7 +271,7 @@ layer(TestLayer)("OpenAPI client_credentials OAuth", (it) => { · 14 unmodified lines
257271 // Add the source with OAuth2Auth pointing at the completed connection.
258272 yield* userExec.openapi.addSpec({
259273 spec: specJson,
260− scope: userScope.id as string,
274+ scope: userScope.id,
261275 namespace: "petstore",
262276 baseUrl,
263277 oauth2: auth,
packages/plugins/openapi/src/sdk/oauth-refresh.test.ts+32−14
@@ -20,7 +20,6 @@ import * as NodeHttpServer from "@effect/platform-node/NodeHttpServer"; · 19 unmodified lines
2020
2121 import {
2222 ConnectionId,
23− ConnectionReauthRequiredError,
2423 CreateConnectionInput,
2524 ScopeId,
2625 SecretId,
@@ -41,6 +40,13 @@ import { OAuth2Auth } from "./types"; · 14 unmodified lines
4140
4241 const autoApprove: InvokeOptions = { onElicitation: "accept-all" };
4342
43+class OpenApiOauthTestSetupError extends Schema.TaggedErrorClass<OpenApiOauthTestSetupError>()(
44+ "OpenApiOauthTestSetupError",
45+ {
46+ message: Schema.String,
47+ },
48+) {}
49+
4450 // ---------------------------------------------------------------------------
4551 // Test API — one endpoint that echoes the Authorization header so we can
4652 // prove which access token was in flight at invoke time.
@@ -141,11 +147,15 @@ const makeExecutor = () => · 94 unmodified lines
141147 storage: () => ({}),
142148 secretProviders: [memoryProvider],
143149 }));
144− const clientLayer = FetchHttpClient.layer;
145− const server = yield* HttpServer.HttpServer;
146− const address = server.address;
147− if (address._tag !== "TcpAddress") return yield* Effect.die("test server must bind to TCP");
148− const baseUrl = `http://127.0.0.1:${address.port}`;
150+ const clientLayer = FetchHttpClient.layer;
151+ const server = yield* HttpServer.HttpServer;
152+ const address = server.address;
153+ if (!("port" in address)) {
154+ return yield* new OpenApiOauthTestSetupError({
155+ message: "Test server must bind to TCP",
156+ });
157+ }
158+ const baseUrl = `http://127.0.0.1:${address.port}`;
149159 const plugins = [
150160 openApiPlugin({ httpClientLayer: clientLayer }),
151161 memorySecretsPlugin(),
@@ -277,7 +287,7 @@ layer(TestLayer)("OpenAPI oauth refresh", (it) => { · 125 unmodified lines
277287
278288 yield* executor.openapi.addSpec({
279289 spec: specJson,
280− scope: String(scopeId),
290+ scope: scopeId,
281291 namespace: "petstore",
282292 baseUrl,
283293 oauth2: auth,
@@ -334,7 +344,7 @@ layer(TestLayer)("OpenAPI oauth refresh", (it) => { · 50 unmodified lines
334344
335345 yield* executor.openapi.addSpec({
336346 spec: specJson,
337− scope: String(scopeId),
347+ scope: scopeId,
338348 namespace: "petstore",
339349 baseUrl,
340350 oauth2: auth,
@@ -392,7 +402,7 @@ layer(TestLayer)("OpenAPI oauth refresh", (it) => { · 51 unmodified lines
392402
393403 yield* executor.openapi.addSpec({
394404 spec: specJson,
395− scope: String(scopeId),
405+ scope: scopeId,
396406 namespace: "petstore",
397407 baseUrl,
398408 oauth2: auth,
@@ -402,15 +412,23 @@ layer(TestLayer)("OpenAPI oauth refresh", (it) => { · 3 unmodified lines
402412 // generic Error (see openapi invokeTool), so we assert against
403413 // the `accessToken` call directly too — that's the surface
404414 // the UI bridges use to trigger re-auth.
405− const flipped = yield* executor.connections
415+ const reauthRequired = yield* executor.connections
406416 .accessToken("conn-refresh-dead")
407− .pipe(Effect.flip);
408− expect(flipped._tag).toBe("ConnectionReauthRequiredError");
409− expect((flipped as ConnectionReauthRequiredError).provider).toBe(
417+ .pipe(
418+ Effect.flatMap(() =>
419+ Effect.fail(
420+ new OpenApiOauthTestSetupError({
421+ message: "Expected refresh to require re-auth",
422+ }),
423+ ),
424+ ),
425+ Effect.catchTag("ConnectionReauthRequiredError", Effect.succeed),
426+ );
427+ expect(reauthRequired.provider).toBe(
410428 "openapi:oauth2",
411429 );
412430 expect(
413− (flipped as ConnectionReauthRequiredError).message,
431+ reauthRequired.message,
414432 ).toMatch(/invalid_grant|revoked/i);
415433 }),
416434 );