|
| 1 | +import { randomBytes } from "node:crypto"; |
| 2 | +import { expect } from "@effect/vitest"; |
| 3 | +import { Effect } from "effect"; |
| 4 | +import { composePluginApi } from "@executor-js/api/server"; |
| 5 | +import { openApiHttpPlugin } from "@executor-js/plugin-openapi/api"; |
| 6 | +import { IntegrationSlug } from "@executor-js/sdk/shared"; |
| 7 | + |
| 8 | +import { Api, Browser } from "./services"; |
| 9 | +import type { Identity } from "./target"; |
| 10 | +import { visit } from "./surfaces/browser"; |
| 11 | + |
| 12 | +const api = composePluginApi([openApiHttpPlugin()] as const); |
| 13 | + |
| 14 | +/** Exercise integration creation and member restrictions through the shared console. */ |
| 15 | +export const integrationCreationPermissions = (admin: Identity, member: Identity) => |
| 16 | + Effect.gen(function* () { |
| 17 | + const browser = yield* Browser; |
| 18 | + const { client } = yield* Api; |
| 19 | + const adminClient = yield* client(api, admin); |
| 20 | + const title = `Permissions API ${randomBytes(4).toString("hex")}`; |
| 21 | + const slug = IntegrationSlug.make(title.toLowerCase().replaceAll(" ", "_")); |
| 22 | + const spec = JSON.stringify({ |
| 23 | + openapi: "3.0.3", |
| 24 | + info: { title, version: "1.0.0" }, |
| 25 | + servers: [{ url: "https://api.example.com" }], |
| 26 | + paths: {}, |
| 27 | + components: { |
| 28 | + securitySchemes: { apiKey: { type: "apiKey", in: "header", name: "X-API-Key" } }, |
| 29 | + }, |
| 30 | + security: [{ apiKey: [] }], |
| 31 | + }); |
| 32 | + |
| 33 | + yield* Effect.ensuring( |
| 34 | + Effect.gen(function* () { |
| 35 | + yield* browser.session(admin, async ({ page, step }) => { |
| 36 | + await step("Admin opens the integration catalog", async () => { |
| 37 | + await visit(page, "/"); |
| 38 | + await page.getByRole("button", { name: "Browse integrations", exact: true }).waitFor(); |
| 39 | + await page.keyboard.press("ControlOrMeta+k"); |
| 40 | + await page.getByRole("option", { name: /^Add OpenAPI/ }).waitFor(); |
| 41 | + await page.keyboard.press("Escape"); |
| 42 | + await page.getByRole("link", { name: "Add integration", exact: true }).click(); |
| 43 | + await page.getByRole("heading", { name: "Add an integration", exact: true }).waitFor(); |
| 44 | + await page |
| 45 | + .getByRole("textbox", { name: "Search integrations, or paste a URL" }) |
| 46 | + .waitFor(); |
| 47 | + }); |
| 48 | + await step("Admin creates an integration from the setup form", async () => { |
| 49 | + await visit(page, "/integrations/add/openapi"); |
| 50 | + await page.getByPlaceholder("https://api.example.com/openapi.json").fill(spec); |
| 51 | + await page.getByRole("button", { name: "Add integration", exact: true }).click(); |
| 52 | + await page.waitForURL((url) => url.pathname.endsWith(`/integrations/${slug}`), { |
| 53 | + timeout: 30_000, |
| 54 | + }); |
| 55 | + await page.getByRole("button", { name: "Edit", exact: true }).waitFor(); |
| 56 | + await page.getByRole("button", { name: "Delete", exact: true }).waitFor(); |
| 57 | + }); |
| 58 | + }); |
| 59 | + expect(yield* adminClient.integrations.get({ params: { slug } })).toMatchObject({ |
| 60 | + name: title, |
| 61 | + }); |
| 62 | + |
| 63 | + yield* browser.session(member, async ({ page, step }) => { |
| 64 | + await step( |
| 65 | + "Member sees disabled creation controls with an admin explanation", |
| 66 | + async () => { |
| 67 | + await visit(page, "/"); |
| 68 | + await page.getByRole("heading", { name: "Integrations", exact: true }).waitFor(); |
| 69 | + await page.getByTestId(`integration-entry-${slug}`).waitFor(); |
| 70 | + const add = page.getByRole("button", { name: "Add integration", exact: true }); |
| 71 | + await add.waitFor(); |
| 72 | + expect(await add.isDisabled()).toBe(true); |
| 73 | + expect( |
| 74 | + await page |
| 75 | + .getByRole("button", { name: "Browse integrations", exact: true }) |
| 76 | + .isDisabled(), |
| 77 | + ).toBe(true); |
| 78 | + const hint = page |
| 79 | + .getByRole("group", { name: "Requires a workspace admin" }) |
| 80 | + .filter({ has: add }); |
| 81 | + await hint.hover(); |
| 82 | + await page.getByRole("tooltip", { name: "Requires a workspace admin" }).waitFor(); |
| 83 | + await hint.focus(); |
| 84 | + const before = page.url(); |
| 85 | + await page.keyboard.press("Enter"); |
| 86 | + expect(page.url()).toBe(before); |
| 87 | + }, |
| 88 | + ); |
| 89 | + await step( |
| 90 | + "Member sees disabled add commands and can still find existing integrations", |
| 91 | + async () => { |
| 92 | + await page.keyboard.press("ControlOrMeta+k"); |
| 93 | + const palette = page.getByRole("dialog"); |
| 94 | + await palette.getByRole("option", { name: new RegExp(title) }).waitFor(); |
| 95 | + const addCommand = palette.getByRole("option", { name: /^Add OpenAPI/ }); |
| 96 | + await addCommand.waitFor(); |
| 97 | + expect(await addCommand.getAttribute("aria-disabled")).toBe("true"); |
| 98 | + expect(await addCommand.textContent()).toContain("Admin only"); |
| 99 | + await page.keyboard.press("Escape"); |
| 100 | + }, |
| 101 | + ); |
| 102 | + await step("Member sees disabled Edit and Delete actions", async () => { |
| 103 | + await page.getByTestId(`integration-entry-${slug}`).click(); |
| 104 | + await page.getByRole("button", { name: "Add connection", exact: true }).waitFor(); |
| 105 | + for (const name of ["Edit", "Delete"]) { |
| 106 | + const action = page.getByRole("button", { name, exact: true }); |
| 107 | + await action.waitFor(); |
| 108 | + expect(await action.isDisabled()).toBe(true); |
| 109 | + } |
| 110 | + }); |
| 111 | + await step("Member can still add a personal connection", async () => { |
| 112 | + await page.getByRole("button", { name: "Add connection", exact: true }).click(); |
| 113 | + const dialog = page.getByRole("dialog"); |
| 114 | + await dialog.waitFor(); |
| 115 | + expect(await dialog.getByText("Workspace", { exact: true }).count()).toBe(0); |
| 116 | + }); |
| 117 | + await step("Member browses the catalog with disabled Add buttons", async () => { |
| 118 | + await visit(page, "/integrations/browse"); |
| 119 | + await page.getByRole("heading", { name: "Add an integration", exact: true }).waitFor(); |
| 120 | + await page |
| 121 | + .getByText("Requires a workspace admin to add integrations.", { exact: true }) |
| 122 | + .waitFor(); |
| 123 | + const addButtons = page.getByRole("button", { name: /^Add / }); |
| 124 | + await addButtons.first().waitFor(); |
| 125 | + for (const button of await addButtons.all()) |
| 126 | + expect(await button.isDisabled()).toBe(true); |
| 127 | + const scratch = page.getByRole("button", { |
| 128 | + name: "New OpenAPI integration from scratch", |
| 129 | + exact: true, |
| 130 | + }); |
| 131 | + expect(await scratch.isDisabled()).toBe(true); |
| 132 | + const view = page.getByRole("link", { name: `View ${title}`, exact: true }); |
| 133 | + await view.waitFor(); |
| 134 | + expect(await view.isEnabled()).toBe(true); |
| 135 | + }); |
| 136 | + await step("Member cannot add a URL with the button or Enter key", async () => { |
| 137 | + const input = page.getByRole("textbox", { |
| 138 | + name: "Search integrations, or paste a URL", |
| 139 | + }); |
| 140 | + await input.fill("https://api.example.com/openapi.json"); |
| 141 | + expect( |
| 142 | + await page.getByRole("button", { name: "Add this URL", exact: true }).isDisabled(), |
| 143 | + ).toBe(true); |
| 144 | + const before = page.url(); |
| 145 | + await input.press("Enter"); |
| 146 | + expect(page.url()).toBe(before); |
| 147 | + }); |
| 148 | + for (const path of ["/integrations/add/openapi", "/integrations/add/mcp"]) { |
| 149 | + await step(`Member follows ${path} and sees the admin explanation`, async () => { |
| 150 | + await visit(page, path); |
| 151 | + await page.getByRole("heading", { name: "An admin must add integrations" }).waitFor(); |
| 152 | + expect(await page.getByRole("textbox").count()).toBe(0); |
| 153 | + expect(await page.getByRole("button", { name: /^Add/ }).count()).toBe(0); |
| 154 | + }); |
| 155 | + } |
| 156 | + await step("Member returns to their existing integrations", async () => { |
| 157 | + await page.getByRole("link", { name: "Back to integrations" }).click(); |
| 158 | + await page.getByRole("heading", { name: "Integrations", exact: true }).waitFor(); |
| 159 | + }); |
| 160 | + }); |
| 161 | + }), |
| 162 | + adminClient.openapi.removeSpec({ params: { slug } }).pipe(Effect.ignore), |
| 163 | + ); |
| 164 | + }); |
0 commit comments