Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.
Ruby b2b-commercee-commerceecommerceecommerce-apiecommerce-frameworkecommerce-platformheadlessheadless-commerceheadless-ecommercemarketplacemulti-tenantmulti-vendormulti-vendor-ecommerceopen-sourcespree-commerce
Split the agent and OAuth client tests, and covered a connection
Damian Legawiec639398722fb0
diff --git a/packages/dashboard/e2e/ai-agents.spec.ts b/packages/dashboard/e2e/ai-agents.spec.tsnew file mode 100644index 0000000000..69cf6491d2--- /dev/null+++ b/packages/dashboard/e2e/ai-agents.spec.ts@@ -0,0 +1,151 @@+import { expect, test } from '@playwright/test'+import { gotoIndex, login } from './helpers'+import {+ AGENTS_PATH,+ CLIENTS_PATH,+ CONNECT_CTA,+ clearRegistrations,+ connectKnownClient,+ REGISTER_CTA,+ seedConnection,+} from './oauth-helpers'++// Nothing is seeded, so a store that has never connected an agent carries no+// registrations at all — the first thing a merchant sees.+test.describe('before any agent has been connected', () => {+ test('both screens say so rather than showing an empty table', async ({ page }) => {+ const creds = await login(page)+ await clearRegistrations(page, creds.store_id, creds.accessToken)++ await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)+ await expect(page.getByText(/no agents have been connected/i)).toBeVisible({ timeout: 15_000 })++ await gotoIndex(page, CLIENTS_PATH(creds.store_id), REGISTER_CTA)+ await expect(page.getByText(/no clients registered/i)).toBeVisible({ timeout: 15_000 })+ })++ // The whole point of dropping the seeds: connecting works from nothing.+ test('connecting registers the first client', async ({ page }) => {+ const creds = await login(page)+ await clearRegistrations(page, creds.store_id, creds.accessToken)+ await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)++ const sheet = await connectKnownClient(page)+ await expect(sheet.getByText(/ready to connect/i)).toBeVisible({ timeout: 15_000 })+ await sheet.getByRole('button', { name: /^done$/i }).click()++ await expect(page.getByText('Claude').first()).toBeVisible({ timeout: 15_000 })+ })+})++test.describe('connecting an agent', () => {+ test('registers the client and shows what to paste into it', async ({ page }) => {+ const creds = await login(page)+ await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)++ const sheet = await connectKnownClient(page)++ await expect(sheet.getByText(/ready to connect/i)).toBeVisible({ timeout: 15_000 })+ await expect(sheet.getByText(/mcp address/i)).toBeVisible()+ await expect(sheet.getByText(/client id/i)).toBeVisible()++ // Claude Code connects by command rather than by pasting an address, so+ // the two surfaces are tabbed rather than listed together.+ await sheet.getByRole('tab', { name: /claude code/i }).click()+ await expect(sheet.getByText(/claude mcp add/i)).toBeVisible()+ })++ // Revoking keeps the registration so the same client can be connected+ // again — which is exactly why the card has to say who holds access now.+ test('says who is connected through a registration', async ({ page }) => {+ const creds = await login(page)+ await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)++ const sheet = await connectKnownClient(page)+ await expect(sheet.getByText(/ready to connect/i)).toBeVisible({ timeout: 15_000 })+ await sheet.getByRole('button', { name: /^done$/i }).click()++ // Registered, but nobody has signed in through it yet.+ await expect(page.getByText(/nobody is connected through this agent/i).first()).toBeVisible({+ timeout: 15_000,+ })+ })+})++test.describe('connecting a client we do not know', () => {+ // A merchant picking Claude never types a callback. Choosing "Something+ // else" is the developer's path, and it is the only one where the field+ // that must be right is filled in by hand.+ test('asks for a callback only once Something else is chosen', async ({ page }) => {+ const creds = await login(page)+ await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)++ await page.getByRole('button', { name: CONNECT_CTA }).click()+ const sheet = page.getByRole('dialog')++ await expect(sheet.getByText(/callback url/i)).toHaveCount(0)++ await sheet.getByText(/any other client that speaks mcp/i).click()+ await expect(sheet.getByText(/callback url/i)).toBeVisible()++ const name = `CI custom ${Date.now()}`+ await sheet.locator('#mcp-other-name').fill(name)+ await sheet.locator('#mcp-other-uri').fill('https://custom.example.com/oauth/callback')+ await sheet.getByRole('button', { name: /^continue$/i }).click()++ // Its own client id, and no CLI tab — we publish no command for a client+ // we know nothing about.+ await expect(sheet.getByText(/ready to connect/i)).toBeVisible({ timeout: 15_000 })+ await expect(sheet.getByText(/client id/i)).toBeVisible()+ await expect(sheet.getByRole('tab')).toHaveCount(0)++ await sheet.getByRole('button', { name: /^done$/i }).click()+ await expect(page.getByText(name).first()).toBeVisible({ timeout: 15_000 })+ })+})++// A store registers an agent once and every staff member connects to that+// same registration, so the card lists the people rather than the agent —+// and revoking has to cut off one of them without touching the rest.+test.describe('a connection someone holds', () => {+ test('names the person, what they approved, and when', async ({ page }) => {+ const creds = await login(page)+ await clearRegistrations(page, creds.store_id, creds.accessToken)+ await seedConnection(page, creds.store_id, creds.accessToken, { name: 'CI Connected' })++ await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)++ const card = page.locator('[data-slot="card"]').filter({ hasText: 'CI Connected' })+ await expect(card.getByText(creds.admin_email)).toBeVisible({ timeout: 15_000 })+ await expect(card.getByText(/view products/i).first()).toBeVisible()+ })++ // The bug this screen was rebuilt for: one row stood for the whole team,+ // so the only revoke available cut everybody off.+ test('revokes that one person and keeps the registration', async ({ page }) => {+ const creds = await login(page)+ await clearRegistrations(page, creds.store_id, creds.accessToken)+ await seedConnection(page, creds.store_id, creds.accessToken, { name: 'CI Revocable' })++ await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)++ const card = page.locator('[data-slot="card"]').filter({ hasText: 'CI Revocable' })+ await expect(card.getByText(creds.admin_email)).toBeVisible({ timeout: 15_000 })++ await card+ .getByRole('button', { name: /open menu|actions/i })+ .first()+ .click()+ await page.getByRole('menuitem', { name: /revoke/i }).click()+ await page+ .getByRole('dialog')+ .getByRole('button', { name: /revoke/i })+ .click()++ // The person goes; the agent stays, ready to be connected again.+ await expect(card.getByText(/nobody is connected through this agent/i)).toBeVisible({+ timeout: 15_000,+ })+ await expect(page.getByText('CI Revocable').first()).toBeVisible()+ })+})diff --git a/packages/dashboard/e2e/oauth-clients.spec.ts b/packages/dashboard/e2e/oauth-clients.spec.tsindex 66ce4cc229..93fcd6c737 100644--- a/packages/dashboard/e2e/oauth-clients.spec.ts+++ b/packages/dashboard/e2e/oauth-clients.spec.ts@@ -1,146 +1,10 @@-import { expect, type Page, test } from '@playwright/test'+import { expect, test } from '@playwright/test'import { gotoIndex, login } from './helpers'+import { CLIENTS_PATH, REGISTER_CTA } from './oauth-helpers'-const AGENTS_PATH = (storeId: string) => `/${storeId}/settings/ai-agents`-const CLIENTS_PATH = (storeId: string) => `/${storeId}/settings/oauth-clients`-const CONNECT_CTA = /connect an agent/i-const REGISTER_CTA = /register a client/i--/**- * Registers a client through the connect sheet, the way a merchant does:- * pick the assistant, continue, and the sheet hands back its address and id.- */-async function connectKnownClient(page: Page) {- await page.getByRole('button', { name: CONNECT_CTA }).click()- const sheet = page.getByRole('dialog')- await expect(sheet.getByText(/choose the assistant/i)).toBeVisible()-- // The callback is filled in for a client we know, so a merchant never sees- // a URL field — picking a name is the whole choice.- await expect(sheet.getByText(/callback url/i)).toHaveCount(0)-- await sheet.getByText(/claude\.ai, claude desktop/i).click()- await sheet.getByRole('button', { name: /^continue$/i }).click()-- return sheet-}--/**- * Removes every registration, so the screens show the state a store is in- * before anyone has connected anything. The suite runs serially and earlier- * specs register clients, so this cannot be left to ordering.- */-async function clearRegistrations(page: Page, storeId: string, accessToken: string) {- const headers = { 'X-Spree-Store-Id': storeId, Authorization: `Bearer ${accessToken}` }- const res = await page.request.get('/api/v3/admin/oauth/applications?limit=100', { headers })- if (!res.ok()) throw new Error(`Listing clients failed: ${res.status()} ${await res.text()}`)-- for (const client of (await res.json()).data ?? []) {- const deleted = await page.request.delete(`/api/v3/admin/oauth/applications/${client.id}`, {- headers,- })- if (!deleted.ok()) {- throw new Error(`Deleting ${client.id} failed: ${deleted.status()} ${await deleted.text()}`)- }- }-}--// Nothing is seeded, so a store that has never connected an agent carries no-// registrations at all — the first thing a merchant sees.-test.describe('before any agent has been connected', () => {- test('both screens say so rather than showing an empty table', async ({ page }) => {- const creds = await login(page)- await clearRegistrations(page, creds.store_id, creds.accessToken)-- await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)- await expect(page.getByText(/no agents have been connected/i)).toBeVisible({ timeout: 15_000 })-- await gotoIndex(page, CLIENTS_PATH(creds.store_id), REGISTER_CTA)- await expect(page.getByText(/no clients registered/i)).toBeVisible({ timeout: 15_000 })- })-- // The whole point of dropping the seeds: connecting works from nothing.- test('connecting registers the first client', async ({ page }) => {- const creds = await login(page)- await clearRegistrations(page, creds.store_id, creds.accessToken)- await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)-- const sheet = await connectKnownClient(page)- await expect(sheet.getByText(/ready to connect/i)).toBeVisible({ timeout: 15_000 })- await sheet.getByRole('button', { name: /^done$/i }).click()-- await expect(page.getByText('Claude').first()).toBeVisible({ timeout: 15_000 })- })-})--test.describe('connecting an agent', () => {- test('registers the client and shows what to paste into it', async ({ page }) => {- const creds = await login(page)- await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)-- const sheet = await connectKnownClient(page)-- await expect(sheet.getByText(/ready to connect/i)).toBeVisible({ timeout: 15_000 })- await expect(sheet.getByText(/mcp address/i)).toBeVisible()- await expect(sheet.getByText(/client id/i)).toBeVisible()-- // Claude Code connects by command rather than by pasting an address, so- // the two surfaces are tabbed rather than listed together.- await sheet.getByRole('tab', { name: /claude code/i }).click()- await expect(sheet.getByText(/claude mcp add/i)).toBeVisible()-- await sheet.getByRole('button', { name: /^done$/i }).click()- })-- // Revoking keeps the registration so the same client can be connected- // again — which is exactly why the card has to say who holds access now.- test('says who is connected through a registration', async ({ page }) => {- const creds = await login(page)- await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)-- const sheet = await connectKnownClient(page)- await expect(sheet.getByText(/ready to connect/i)).toBeVisible({ timeout: 15_000 })- await sheet.getByRole('button', { name: /^done$/i }).click()-- // Registered, but nobody has signed in through it yet.- await expect(page.getByText(/nobody is connected through this agent/i).first()).toBeVisible({- timeout: 15_000,- })- })-})--test.describe('connecting a client we do not know', () => {- // A merchant picking Claude never types a callback. Choosing "Something- // else" is the developer's path, and it is the only one where the field- // that must be right is filled in by hand.- test('asks for a callback only once Something else is chosen', async ({ page }) => {- const creds = await login(page)- await gotoIndex(page, AGENTS_PATH(creds.store_id), CONNECT_CTA)-- await page.getByRole('button', { name: CONNECT_CTA }).click()- const sheet = page.getByRole('dialog')-- await expect(sheet.getByText(/callback url/i)).toHaveCount(0)-- await sheet.getByText(/any other client that speaks mcp/i).click()- await expect(sheet.getByText(/callback url/i)).toBeVisible()-- const name = `CI custom ${Date.now()}`- await sheet.locator('#mcp-other-name').fill(name)- await sheet.locator('#mcp-other-uri').fill('https://custom.example.com/oauth/callback')- await sheet.getByRole('button', { name: /^continue$/i }).click()-- // Its own client id, and no CLI tab — we publish no command for a client- // we know nothing about.- await expect(sheet.getByText(/ready to connect/i)).toBeVisible({ timeout: 15_000 })- await expect(sheet.getByText(/client id/i)).toBeVisible()- await expect(sheet.getByRole('tab')).toHaveCount(0)-- await sheet.getByRole('button', { name: /^done$/i }).click()- await expect(page.getByText(name).first()).toBeVisible({ timeout: 15_000 })- })-})-+// The developer-facing screen: which OAuth clients this store has registered,+// before any merchant connects one. Connecting is the other screen's job —+// see ai-agents.spec.ts.test.describe('oauth clients', () => {test('registers a client from a name and a callback', async ({ page }) => {const creds = await login(page)@@ -178,3 +42,45 @@ test.describe('oauth clients', () => {).toBeVisible({ timeout: 15_000 })})})++test.describe('managing a registered client', () => {+ test('renames one, and removes it', async ({ page }) => {+ const creds = await login(page)+ await gotoIndex(page, CLIENTS_PATH(creds.store_id), REGISTER_CTA)++ const name = `CI editable ${Date.now()}`+ await page.getByRole('button', { name: REGISTER_CTA }).click()+ const sheet = page.getByRole('dialog')+ await sheet.locator('#oauth-application-name').fill(name)+ await sheet.locator('#oauth-application-redirect-uri').fill('https://ci.example.com/callback')+ await sheet.getByRole('button', { name: /^save$/i }).click()++ const row = page.getByRole('row', { name: new RegExp(name) })+ await expect(row).toBeVisible({ timeout: 15_000 })++ const renamed = `${name} renamed`+ await row.getByRole('button', { name: /open menu|actions/i }).click()+ await page.getByRole('menuitem', { name: /^edit$/i }).click()+ const editSheet = page.getByRole('dialog')+ await editSheet.locator('#oauth-application-name').fill(renamed)+ await editSheet.getByRole('button', { name: /^save$/i }).click()++ await expect(page.getByRole('row', { name: new RegExp(renamed) })).toBeVisible({+ timeout: 15_000,+ })++ await page+ .getByRole('row', { name: new RegExp(renamed) })+ .getByRole('button', { name: /open menu|actions/i })+ .click()+ await page.getByRole('menuitem', { name: /^delete$/i }).click()+ await page+ .getByRole('dialog')+ .getByRole('button', { name: /^delete$/i })+ .click()++ await expect(page.getByRole('row', { name: new RegExp(renamed) })).toHaveCount(0, {+ timeout: 15_000,+ })+ })+})diff --git a/packages/dashboard/e2e/oauth-helpers.ts b/packages/dashboard/e2e/oauth-helpers.tsnew file mode 100644index 0000000000..165ff27c01--- /dev/null+++ b/packages/dashboard/e2e/oauth-helpers.ts@@ -0,0 +1,108 @@+import { expect, type Page } from '@playwright/test'++export const AGENTS_PATH = (storeId: string) => `/${storeId}/settings/ai-agents`+export const CLIENTS_PATH = (storeId: string) => `/${storeId}/settings/oauth-clients`+export const CONNECT_CTA = /connect an agent/i+export const REGISTER_CTA = /register a client/i++/**+ * Registers a client through the connect sheet, the way a merchant does:+ * pick the assistant, continue, and the sheet hands back its address and id.+ */+export async function connectKnownClient(page: Page) {+ await page.getByRole('button', { name: CONNECT_CTA }).click()+ const sheet = page.getByRole('dialog')+ await expect(sheet.getByText(/choose the assistant/i)).toBeVisible()++ // The callback is filled in for a client we know, so a merchant never sees+ // a URL field — picking a name is the whole choice.+ await expect(sheet.getByText(/callback url/i)).toHaveCount(0)++ await sheet.getByText(/claude\.ai, claude desktop/i).click()+ await sheet.getByRole('button', { name: /^continue$/i }).click()++ return sheet+}++/**+ * Removes every registration, so the screens show the state a store is in+ * before anyone has connected anything. The suite runs serially and earlier+ * specs register clients, so this cannot be left to ordering.+ */+export async function clearRegistrations(page: Page, storeId: string, accessToken: string) {+ const headers = { 'X-Spree-Store-Id': storeId, Authorization: `Bearer ${accessToken}` }+ const res = await page.request.get('/api/v3/admin/oauth/applications?limit=100', { headers })+ if (!res.ok()) throw new Error(`Listing clients failed: ${res.status()} ${await res.text()}`)++ for (const client of (await res.json()).data ?? []) {+ const deleted = await page.request.delete(`/api/v3/admin/oauth/applications/${client.id}`, {+ headers,+ })+ if (!deleted.ok()) {+ throw new Error(`Deleting ${client.id} failed: ${deleted.status()} ${await deleted.text()}`)+ }+ }+}++/**+ * Connects an agent for real: registers a client, then walks consent and the+ * token exchange exactly as a client does.+ *+ * There is no endpoint that mints a token — by design, since a connection is+ * something a person approves. So a spec that needs one has to earn it the+ * way the product does.+ */+export async function seedConnection(+ page: Page,+ storeId: string,+ accessToken: string,+ { name, scope = 'read_products' }: { name: string; scope?: string },+) {+ const headers = { 'X-Spree-Store-Id': storeId, Authorization: `Bearer ${accessToken}` }++ const registered = await page.request.post('/api/v3/admin/oauth/applications', {+ headers,+ data: { name, redirect_uri: 'https://example.test/callback' },+ })+ if (!registered.ok()) {+ throw new Error(`Registering ${name} failed: ${registered.status()} ${await registered.text()}`)+ }+ const application = await registered.json()++ // A fixed verifier and its S256 challenge. The pair only has to be+ // self-consistent — the server checks one against the other.+ const verifier = 'v'.repeat(64)+ const challenge = 'w1TpKUdYE9hUAcNSeeSRFioHDxfUxuHho_JHAfZ_vDM'+ const query = new URLSearchParams({+ client_id: application.client_id,+ redirect_uri: 'https://example.test/callback',+ response_type: 'code',+ scope,+ code_challenge: challenge,+ code_challenge_method: 'S256',+ })++ const approved = await page.request.post(`/api/v3/admin/oauth/authorize?${query}`, { headers })+ if (!approved.ok()) {+ throw new Error(`Consent failed: ${approved.status()} ${await approved.text()}`)+ }+ const code = new URL((await approved.json()).redirect_uri).searchParams.get('code')++ // Straight to Rails: the token endpoint sits outside `/api`, which is the+ // only prefix the dashboard's dev proxy forwards.+ const railsPort = process.env.E2E_RAILS_PORT || '3010'+ const exchanged = await page.request.post(`http://localhost:${railsPort}/oauth/token`, {+ form: {+ grant_type: 'authorization_code',+ code: code ?? '',+ client_id: application.client_id,+ redirect_uri: 'https://example.test/callback',+ code_verifier: verifier,+ },+ })+ if (!exchanged.ok()) {+ throw new Error(`Token exchange failed: ${exchanged.status()} ${await exchanged.text()}`)+ }++ return application+}