Files
summercms/admin/tests/smoke/connect.smoke.test.ts
Jakub Zych 6900bd1a72 feat(32.2-02): every consent card state, account switch and login hint
- Loading skeleton (aria-busy, no buttons), Expired (404 or no request,
  no call), Load error with Try again, No grantable scope (Deny only),
  Returning with the continue link for http(s) redirects
- 422 shows scopes_required under the scopes; 5xx, network, 400 and a
  javascript:/data:/vbscript: redirect show decide_failed; ticks kept
- 401 hands over to expireSession; focus follows the state heading
- Use another account: logout() without the router, then login with the
  /connect?request= redirect
- LoginView shows mcp_connect.login_hint when the redirect is /connect
2026-11-21 00:48:02 +01:00

118 lines
5.1 KiB
TypeScript

// Smoke tests of the MCP consent card (Phase 32.2 UI-SPEC C1): a signed-in
// administrator opens /connect?request=…, keeps the ticks and clicks Allow,
// and the browser leaves for the server-issued redirect_uri through
// window.location.assign, never through the router. Fixtures are neutral.
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
import { enableAutoUnmount, flushPromises } from '@vue/test-utils'
import type { MCPConsent } from '../../src/api/types'
import { API, mountApp, notFound, requestsTo, resetState, type Route } from '../helpers'
const REDIRECT = 'https://agent.example.test/callback?code=abc123&state=xyz'
function consentView(overrides: Partial<MCPConsent> = {}): MCPConsent {
return {
request_id: 'req1',
client: { id: 'client-1', name: 'Acme Agent', redirect_host: 'agent.example.test', logo_url: '' },
admin: { login: 'dev', name: 'Dana Dev', email: 'dev@example.test' },
scopes: [
{ name: 'admin:read', label: 'Read the admin', description: 'Read every admin list.', read_only: true, known: true },
{ name: 'acme', label: 'Acme', description: 'Use the acme tools.', read_only: false, known: true },
],
offline_access: true,
consent_required: true,
expires_at: '2026-11-21T12:00:00Z',
...overrides,
}
}
function consentRoutes(view: MCPConsent = consentView(), decide?: Route): Record<string, Route> {
return {
[`GET ${API}/mcp/consent/req1`]: { body: { data: view, meta: {} } },
[`POST ${API}/mcp/consent/req1`]: decide ?? { body: { data: { redirect_uri: REDIRECT }, meta: {} } },
}
}
let assign: ReturnType<typeof vi.fn>
beforeEach(() => {
resetState()
assign = vi.fn()
vi.spyOn(window.location, 'assign').mockImplementation(assign)
})
afterEach(() => {
vi.restoreAllMocks()
document.body.innerHTML = ''
})
enableAutoUnmount(afterEach)
describe('consent card (UI-SPEC C1)', () => {
it('Allow posts the ticked scopes and Stay connected, then leaves for redirect_uri without the router', async () => {
const { wrapper, calls, router } = await mountApp('/connect?request=req1', consentRoutes())
const push = vi.spyOn(router, 'push')
const replace = vi.spyOn(router, 'replace')
expect(router.currentRoute.value.name).toBe('connect')
const card = wrapper.find('[data-connect-state="ready"]')
expect(card.exists()).toBe(true)
// Standalone: no navigation shell around the card.
expect(wrapper.find('nav[aria-label="backend::lang.nav.plugins"]').exists()).toBe(false)
expect(card.find('[data-connect-host]').text()).toBe('agent.example.test')
const scopeBoxes = card.findAll('[data-scope] input[type="checkbox"]')
expect(scopeBoxes).toHaveLength(2)
expect(scopeBoxes.map((box) => (box.element as HTMLInputElement).checked)).toEqual([true, true])
const offline = card.find('[data-connect-offline] input[type="checkbox"]')
expect((offline.element as HTMLInputElement).checked).toBe(true)
// Nothing is posted before the click.
expect(requestsTo(calls, 'POST', `${API}/mcp/consent/req1`)).toHaveLength(0)
await card.trigger('submit')
await flushPromises()
const posts = requestsTo(calls, 'POST', `${API}/mcp/consent/req1`)
expect(posts).toHaveLength(1)
expect(posts[0]!.headers.get('X-Requested-With')).toBe('XMLHttpRequest')
expect(await posts[0]!.json()).toEqual({ decision: 'allow', scopes: ['admin:read', 'acme'], offline_access: true })
expect(assign).toHaveBeenCalledWith(REDIRECT)
for (const spy of [push, replace]) {
expect(spy).not.toHaveBeenCalledWith(REDIRECT)
for (const call of spy.mock.calls) {
expect(JSON.stringify(call)).not.toContain('agent.example.test')
}
}
})
it('shows the Expired state when the request is unknown (404 on load)', async () => {
const { wrapper, calls } = await mountApp('/connect?request=req1', {
[`GET ${API}/mcp/consent/req1`]: { status: 404, body: notFound },
})
const card = wrapper.find('[data-connect-state="expired"]')
expect(card.exists()).toBe(true)
expect(card.find('h1').text()).toBe('backend::lang.mcp_connect.expired_heading')
expect(card.find('[data-action="open-admin"]').attributes('href')).toBe('/')
expect(wrapper.find('[data-action="allow"]').exists()).toBe(false)
expect(requestsTo(calls, 'POST', `${API}/mcp/consent/req1`)).toHaveLength(0)
})
it('Use another account signs out and returns through login to the same request', async () => {
const { wrapper, calls, router } = await mountApp('/connect?request=req1', {
...consentRoutes(),
[`POST ${API}/auth/logout`]: { body: { data: {}, meta: {} } },
})
await wrapper.find('[data-action="switch-account"]').trigger('click')
await flushPromises()
expect(requestsTo(calls, 'POST', `${API}/auth/logout`)).toHaveLength(1)
expect(requestsTo(calls, 'POST', `${API}/mcp/consent/req1`)).toHaveLength(0)
expect(router.currentRoute.value.name).toBe('login')
expect(router.currentRoute.value.query.redirect).toBe('/connect?request=req1')
expect(wrapper.find('[data-login-hint]').text()).toBe('backend::lang.mcp_connect.login_hint')
expect(assign).not.toHaveBeenCalled()
})
})