|
| 1 | +// Selfhost-only (browser): a spec/probe-DETECTED auth method is immutable in |
| 2 | +// the add flow. The shared AuthMethodListEditor renders detected methods as a |
| 3 | +// disabled, read-only summary ("Pulled from spec. Remove to override.") with no |
| 4 | +// kind selector, so a user can't silently retype the spec's method into a kind |
| 5 | +// nothing backs. A method the user adds by hand stays fully editable. Both the |
| 6 | +// MCP and OpenAPI add flows compose the same editor, so one behavior, two |
| 7 | +// surfaces. Selfhost runs with EXECUTOR_ALLOW_LOCAL_NETWORK so the probe/analyze |
| 8 | +// can reach the loopback fixtures. Video is the artifact. |
| 9 | +import { randomBytes } from "node:crypto"; |
| 10 | +import { createServer } from "node:http"; |
| 11 | + |
| 12 | +import { expect } from "@effect/vitest"; |
| 13 | +import { Effect } from "effect"; |
| 14 | +import { makeGreetingMcpServer, serveMcpServerWithOAuth } from "@executor-js/plugin-mcp/testing"; |
| 15 | +import { OAuthTestServer } from "@executor-js/sdk/testing"; |
| 16 | + |
| 17 | +import { scenario } from "../src/scenario"; |
| 18 | +import { Browser, Target } from "../src/services"; |
| 19 | + |
| 20 | +const REMOVE_HINT = "Pulled from spec. Remove to override."; |
| 21 | + |
| 22 | +scenario( |
| 23 | + "Detected auth · an MCP probe's OAuth method is immutable in the add flow", |
| 24 | + {}, |
| 25 | + Effect.scoped( |
| 26 | + Effect.gen(function* () { |
| 27 | + const target = yield* Target; |
| 28 | + const browser = yield* Browser; |
| 29 | + // OAuth-protected server: the probe 401s with resource metadata, so the |
| 30 | + // method list seeds a single detected OAuth row (discovered metadata). |
| 31 | + const server = yield* serveMcpServerWithOAuth( |
| 32 | + () => makeGreetingMcpServer({ name: `oauth-mcp-${randomBytes(3).toString("hex")}` }), |
| 33 | + { path: "/mcp" }, |
| 34 | + ); |
| 35 | + const identity = yield* target.newIdentity(); |
| 36 | + |
| 37 | + yield* browser.session(identity, async ({ page, step }) => { |
| 38 | + await step("Open the add-MCP flow pointed at the OAuth server", async () => { |
| 39 | + await page.goto(`/integrations/add/mcp?url=${encodeURIComponent(server.endpoint)}`, { |
| 40 | + waitUntil: "networkidle", |
| 41 | + }); |
| 42 | + await page.getByText("How does this server authenticate?").waitFor(); |
| 43 | + await page.getByText("Method 1 · Detected").waitFor(); |
| 44 | + }); |
| 45 | + |
| 46 | + await step("The detected method is locked: read-only, named, no selector", async () => { |
| 47 | + // The kind is named explicitly ("OAuth"), the discovered-OAuth summary |
| 48 | + // and override hint sit inside a disabled block, and there is NO |
| 49 | + // editable kind selector (the FilterTabs render as buttons). |
| 50 | + await page.getByText("OAuth", { exact: true }).first().waitFor(); |
| 51 | + await page.getByText("OAuth metadata is discovered from this server").waitFor(); |
| 52 | + await page.getByText(REMOVE_HINT).waitFor(); |
| 53 | + expect( |
| 54 | + await page.locator("[aria-disabled]").count(), |
| 55 | + "the detected method renders a disabled (non-interactive) block", |
| 56 | + ).toBeGreaterThan(0); |
| 57 | + expect( |
| 58 | + await page.getByRole("button", { name: "API key", exact: true }).count(), |
| 59 | + "no editable kind selector is shown for the detected method", |
| 60 | + ).toBe(0); |
| 61 | + }); |
| 62 | + |
| 63 | + await step("A hand-added method keeps the full editable selector", async () => { |
| 64 | + await page.getByRole("button", { name: "Add method" }).click(); |
| 65 | + await page.getByText("Method 2").waitFor(); |
| 66 | + // The added row defaults to API key and exposes the None/API key/OAuth |
| 67 | + // kind tabs (buttons) — the detected row above still shows none. |
| 68 | + expect( |
| 69 | + await page.getByRole("button", { name: "API key", exact: true }).count(), |
| 70 | + "the added method exposes the kind selector", |
| 71 | + ).toBeGreaterThan(0); |
| 72 | + await page.getByText(REMOVE_HINT).waitFor(); |
| 73 | + }); |
| 74 | + }); |
| 75 | + }), |
| 76 | + ).pipe(Effect.provide(OAuthTestServer.layer())), |
| 77 | +); |
| 78 | + |
| 79 | +/** A real 127.0.0.1 server that serves a static OpenAPI spec for the add flow. */ |
| 80 | +const serveSpec = (body: string) => |
| 81 | + Effect.acquireRelease( |
| 82 | + Effect.callback<{ readonly url: string; readonly close: () => void }>((resume) => { |
| 83 | + const server = createServer((_request, response) => { |
| 84 | + response.writeHead(200, { "content-type": "application/json" }); |
| 85 | + response.end(body); |
| 86 | + }); |
| 87 | + server.listen(0, "127.0.0.1", () => { |
| 88 | + const address = server.address(); |
| 89 | + const port = typeof address === "object" && address ? address.port : 0; |
| 90 | + resume( |
| 91 | + Effect.succeed({ |
| 92 | + url: `http://127.0.0.1:${port}/spec.json`, |
| 93 | + close: () => { |
| 94 | + server.close(); |
| 95 | + server.closeAllConnections(); |
| 96 | + }, |
| 97 | + }), |
| 98 | + ); |
| 99 | + }); |
| 100 | + }), |
| 101 | + (server) => Effect.sync(server.close), |
| 102 | + ); |
| 103 | + |
| 104 | +const apiKeyAndOAuthSpec = (): string => |
| 105 | + JSON.stringify({ |
| 106 | + openapi: "3.0.3", |
| 107 | + info: { title: "Acme Immutable Auth Fixture", version: "1.0.0" }, |
| 108 | + servers: [{ url: "https://api.acme.test" }], |
| 109 | + security: [{ bearerAuth: [] }, { acmeOAuth: ["read"] }], |
| 110 | + components: { |
| 111 | + securitySchemes: { |
| 112 | + bearerAuth: { type: "http", scheme: "bearer" }, |
| 113 | + acmeOAuth: { |
| 114 | + type: "oauth2", |
| 115 | + flows: { |
| 116 | + authorizationCode: { |
| 117 | + authorizationUrl: "https://api.acme.test/oauth/authorize", |
| 118 | + tokenUrl: "https://api.acme.test/oauth/token", |
| 119 | + scopes: { read: "Read access" }, |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + paths: { |
| 126 | + "/widgets": { |
| 127 | + get: { |
| 128 | + operationId: "listWidgets", |
| 129 | + summary: "List widgets", |
| 130 | + responses: { "200": { description: "ok" } }, |
| 131 | + }, |
| 132 | + }, |
| 133 | + }, |
| 134 | + }); |
| 135 | + |
| 136 | +scenario( |
| 137 | + "Detected auth · OpenAPI spec-detected methods are immutable in the add flow", |
| 138 | + {}, |
| 139 | + Effect.scoped( |
| 140 | + Effect.gen(function* () { |
| 141 | + const target = yield* Target; |
| 142 | + const browser = yield* Browser; |
| 143 | + const spec = yield* serveSpec(apiKeyAndOAuthSpec()); |
| 144 | + const identity = yield* target.newIdentity(); |
| 145 | + |
| 146 | + yield* browser.session(identity, async ({ page, step }) => { |
| 147 | + await step("Analyze a spec that declares both API key and OAuth", async () => { |
| 148 | + await page.goto(`/integrations/add/openapi`, { waitUntil: "networkidle" }); |
| 149 | + await page |
| 150 | + .getByPlaceholder(/openapi\.json/i) |
| 151 | + .first() |
| 152 | + .fill(spec.url); |
| 153 | + await page.getByText("How does this API authenticate?").waitFor(); |
| 154 | + await page.getByText("Method 2").waitFor(); |
| 155 | + }); |
| 156 | + |
| 157 | + await step("Both detected methods are locked, named, read-only", async () => { |
| 158 | + // Two detected methods, each with the override hint and its kind named |
| 159 | + // ("API key" / "OAuth"); the OAuth one shows the spec's real endpoints |
| 160 | + // read-only. No editable kind selector (FilterTabs render as buttons). |
| 161 | + expect( |
| 162 | + await page.getByText(REMOVE_HINT).count(), |
| 163 | + "both detected methods show the remove-to-override hint", |
| 164 | + ).toBe(2); |
| 165 | + await page.getByText("https://api.acme.test/oauth/authorize").waitFor(); |
| 166 | + await page.getByText("API key", { exact: true }).first().waitFor(); |
| 167 | + await page.getByText("OAuth", { exact: true }).first().waitFor(); |
| 168 | + expect( |
| 169 | + await page.getByRole("button", { name: "OAuth", exact: true }).count(), |
| 170 | + "no editable kind selector is shown for the detected methods", |
| 171 | + ).toBe(0); |
| 172 | + }); |
| 173 | + |
| 174 | + await step("A hand-added method keeps the full editable selector", async () => { |
| 175 | + await page.getByRole("button", { name: "Add method" }).click(); |
| 176 | + await page.getByText("Method 3").waitFor(); |
| 177 | + expect( |
| 178 | + await page.getByRole("button", { name: "API key", exact: true }).count(), |
| 179 | + "the added method exposes the kind selector", |
| 180 | + ).toBeGreaterThan(0); |
| 181 | + }); |
| 182 | + }); |
| 183 | + }), |
| 184 | + ), |
| 185 | +); |
0 commit comments