- ConnectView.test.ts: every C1 state, the * rule, no auto-allow, Allow and Deny bodies through window.location.assign, forbidden schemes, switch account, 401/404 handling, focus and the profile fallback - router, LoginView and AccountContributions tests extended for /connect, the login hint and item rows (null items unchanged, empty block, :title confirm, 404 item_gone, 409 action_stale)
397 lines
20 KiB
TypeScript
397 lines
20 KiB
TypeScript
// My account sections plugins contribute (phase 19.7, D-04 part 1).
|
|
// Fixtures are neutral acme.* data; no application names appear in
|
|
// framework tests.
|
|
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
|
import { enableAutoUnmount, flushPromises } from '@vue/test-utils'
|
|
import { setBundle } from '../../src/app/i18n'
|
|
import { toasts } from '../../src/state/useToasts'
|
|
import { langFixture } from '../fixtures/typed'
|
|
import { API, mountApp, profile, requestsTo, resetState, type Route } from '../helpers'
|
|
|
|
const CONTRIBUTIONS = `${API}/account/contributions`
|
|
const OPEN = `${CONTRIBUTIONS}/site_link/actions/open`
|
|
|
|
const strings = {
|
|
'backend::lang.myaccount.action_stale': { other: 'This action no longer applies.' },
|
|
'backend::lang.form.action_done': { other: 'Action completed.' },
|
|
}
|
|
|
|
const envelope = (data: unknown) => ({ body: { data, meta: {} } })
|
|
|
|
const sections = [
|
|
{
|
|
name: 'site_link',
|
|
label: 'Site account',
|
|
description: 'Link the site account you sign in with.',
|
|
status: 'Not linked',
|
|
actions: [{ name: 'open', label: 'Link account', confirm: '', danger: false }],
|
|
},
|
|
{
|
|
name: 'newsletter',
|
|
label: 'Newsletter digest',
|
|
description: '',
|
|
status: 'Weekly',
|
|
actions: [{ name: 'stop', label: 'Stop the digest', confirm: 'Stop the weekly digest?', danger: true }],
|
|
},
|
|
]
|
|
|
|
async function openAccount(routes: Record<string, Route> = {}) {
|
|
return mountApp(
|
|
'/account',
|
|
{
|
|
[`GET ${API}/auth/me`]: envelope(profile.data),
|
|
[`GET ${API}/account/tokens`]: envelope([]),
|
|
[`GET ${CONTRIBUTIONS}`]: envelope(sections),
|
|
...routes,
|
|
},
|
|
{ attach: true },
|
|
)
|
|
}
|
|
|
|
beforeEach(() => {
|
|
resetState()
|
|
setBundle({ ...langFixture.data, ...strings }, 'en')
|
|
})
|
|
|
|
afterEach(() => {
|
|
document.body.innerHTML = ''
|
|
vi.restoreAllMocks()
|
|
})
|
|
|
|
enableAutoUnmount(afterEach)
|
|
|
|
describe('My account contributions', () => {
|
|
it('renders one card per section with its label, description, status and actions', async () => {
|
|
const { wrapper } = await openAccount()
|
|
const cards = wrapper.findAll('[data-account-contribution]')
|
|
expect(cards.map((card) => card.attributes('data-account-contribution'))).toEqual(['site_link', 'newsletter'])
|
|
expect(cards[0]!.find('h2').text()).toBe('Site account')
|
|
expect(cards[0]!.text()).toContain('Link the site account you sign in with.')
|
|
expect(wrapper.findAll('[data-account-contribution-status]').map((status) => status.text())).toEqual(['Not linked', 'Weekly'])
|
|
expect(cards[1]!.find('[data-account-contribution-action="stop"]').text()).toBe('Stop the digest')
|
|
// Text, never HTML.
|
|
expect(wrapper.find('[data-account-contribution]').html()).not.toContain('v-html')
|
|
})
|
|
|
|
it('renders no card when no plugin contributes', async () => {
|
|
const { wrapper } = await openAccount({ [`GET ${CONTRIBUTIONS}`]: envelope([]) })
|
|
expect(wrapper.findAll('[data-account-contribution]')).toHaveLength(0)
|
|
expect(wrapper.find('[data-api-tokens]').exists()).toBe(true)
|
|
})
|
|
|
|
it('an action without a confirmation posts at once, shows the toast, opens and renders the URL and reloads the list', async () => {
|
|
const opened = vi.spyOn(window, 'open').mockImplementation(() => null)
|
|
const { wrapper, calls } = await openAccount({
|
|
[`POST ${OPEN}`]: envelope({ message: 'Open the site to finish linking.', url: 'https://blog.example.com/link-admin?code=abc' }),
|
|
})
|
|
await wrapper.find('[data-account-contribution-action="open"]').trigger('click')
|
|
await flushPromises()
|
|
expect(requestsTo(calls, 'POST', OPEN)).toHaveLength(1)
|
|
expect(requestsTo(calls, 'POST', OPEN)[0]!.headers.get('X-Requested-With')).toBe('XMLHttpRequest')
|
|
expect(toasts.value.map((toast) => [toast.text, toast.tone])).toEqual([['Open the site to finish linking.', 'success']])
|
|
expect(opened).toHaveBeenCalledWith('https://blog.example.com/link-admin?code=abc', '_blank', 'noopener')
|
|
const link = wrapper.find('[data-account-contribution-url]')
|
|
expect(link.attributes('href')).toBe('https://blog.example.com/link-admin?code=abc')
|
|
expect(link.attributes('target')).toBe('_blank')
|
|
expect(link.attributes('rel')).toBe('noopener noreferrer')
|
|
expect(requestsTo(calls, 'GET', CONTRIBUTIONS)).toHaveLength(2)
|
|
expect(document.body.querySelector('[data-confirm-dialog]')).toBeNull()
|
|
})
|
|
|
|
it('a danger action asks first and posts on confirm', async () => {
|
|
const STOP = `${CONTRIBUTIONS}/newsletter/actions/stop`
|
|
const { wrapper, calls } = await openAccount({ [`POST ${STOP}`]: envelope({ message: 'Stopped.', url: '' }) })
|
|
await wrapper.find('[data-account-contribution-action="stop"]').trigger('click')
|
|
await flushPromises()
|
|
const dialog = document.body.querySelector<HTMLElement>('[data-confirm-dialog]')
|
|
expect(dialog?.textContent).toContain('Stop the weekly digest?')
|
|
expect(requestsTo(calls, 'POST', STOP)).toHaveLength(0)
|
|
dialog!.querySelector<HTMLButtonElement>('[data-action="confirm"]')!.click()
|
|
await flushPromises()
|
|
expect(requestsTo(calls, 'POST', STOP)).toHaveLength(1)
|
|
expect(toasts.value.map((toast) => toast.text)).toEqual(['Stopped.'])
|
|
expect(wrapper.find('[data-account-contribution-url]').exists()).toBe(false)
|
|
})
|
|
|
|
it('409 shows the stale toast and refetches the list', async () => {
|
|
const { wrapper, calls } = await openAccount({
|
|
[`POST ${OPEN}`]: { status: 409, body: { error: { code: 'conflict', message: 'Conflict', details: {} } } },
|
|
})
|
|
await wrapper.find('[data-account-contribution-action="open"]').trigger('click')
|
|
await flushPromises()
|
|
expect(toasts.value.map((toast) => [toast.text, toast.tone])).toEqual([['This action no longer applies.', 'danger']])
|
|
expect(requestsTo(calls, 'GET', CONTRIBUTIONS)).toHaveLength(2)
|
|
})
|
|
|
|
it("403 shows the server's message as a danger toast and keeps the list", async () => {
|
|
const { wrapper, calls } = await openAccount({
|
|
[`POST ${OPEN}`]: { status: 403, body: { error: { code: 'forbidden', message: 'You may not link this account.', details: {} } } },
|
|
})
|
|
await wrapper.find('[data-account-contribution-action="open"]').trigger('click')
|
|
await flushPromises()
|
|
expect(toasts.value.map((toast) => [toast.text, toast.tone])).toEqual([['You may not link this account.', 'danger']])
|
|
expect(requestsTo(calls, 'GET', CONTRIBUTIONS)).toHaveLength(1)
|
|
expect(wrapper.find('[data-account-contribution-url]').exists()).toBe(false)
|
|
})
|
|
|
|
it('a failed list load renders no card and leaves the rest of My account', async () => {
|
|
const { wrapper } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: { status: 500, body: { error: { code: 'server_error', message: 'Server error', details: {} } } },
|
|
})
|
|
expect(wrapper.findAll('[data-account-contribution]')).toHaveLength(0)
|
|
expect(wrapper.find('[data-api-tokens]').exists()).toBe(true)
|
|
})
|
|
|
|
// Plan 07 hardening (B-WR-03, B-WR-06, B-IN-01).
|
|
|
|
it('a javascript: URL answer renders no link and never opens a window', async () => {
|
|
const opened = vi.spyOn(window, 'open').mockImplementation(() => null)
|
|
const { wrapper } = await openAccount({
|
|
[`POST ${OPEN}`]: envelope({ message: 'Done.', url: 'javascript:alert(1)' }),
|
|
})
|
|
await wrapper.find('[data-account-contribution-action="open"]').trigger('click')
|
|
await flushPromises()
|
|
expect(opened).not.toHaveBeenCalled()
|
|
expect(wrapper.find('[data-account-contribution-url]').exists()).toBe(false)
|
|
expect(wrapper.html()).not.toContain('javascript:')
|
|
expect(toasts.value.map((toast) => toast.text)).toEqual(['Done.'])
|
|
})
|
|
|
|
// RB-IN-01: browsers strip tab and newline, so these resolve off-site.
|
|
for (const url of ['/\t/evil.example/x', '/\n/evil.example', '/\r\n/evil.example', '/x\u0000', 'https://']) {
|
|
it(`a ${JSON.stringify(url)} URL answer renders no link and never opens a window`, async () => {
|
|
const opened = vi.spyOn(window, 'open').mockImplementation(() => null)
|
|
const { wrapper } = await openAccount({
|
|
[`POST ${OPEN}`]: envelope({ message: 'Done.', url }),
|
|
})
|
|
await wrapper.find('[data-account-contribution-action="open"]').trigger('click')
|
|
await flushPromises()
|
|
expect(opened).not.toHaveBeenCalled()
|
|
expect(wrapper.find('[data-account-contribution-url]').exists()).toBe(false)
|
|
expect(wrapper.html()).not.toContain('evil.example')
|
|
})
|
|
}
|
|
|
|
it('a section with error: true shows the section error text', async () => {
|
|
setBundle({ ...langFixture.data, ...strings, 'backend::lang.myaccount.section_error': { other: 'This section could not be loaded.' } }, 'en')
|
|
const { wrapper } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: envelope([{ ...sections[0], status: '', error: true }, sections[1]]),
|
|
})
|
|
const cards = wrapper.findAll('[data-account-contribution]')
|
|
expect(cards[0]!.find('[data-account-contribution-error]').text()).toBe('This section could not be loaded.')
|
|
expect(cards[0]!.find('[data-account-contribution-status]').exists()).toBe(false)
|
|
expect(cards[1]!.find('[data-account-contribution-error]').exists()).toBe(false)
|
|
expect(cards[1]!.find('[data-account-contribution-status]').text()).toBe('Weekly')
|
|
})
|
|
|
|
it('a 500 on the list reload shows the error toast and keeps the earlier sections', async () => {
|
|
setBundle({ ...langFixture.data, ...strings, 'backend::lang.form.error_generic': { other: 'Something went wrong.' } }, 'en')
|
|
let loads = 0
|
|
const { wrapper } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: () => {
|
|
loads++
|
|
return loads === 1
|
|
? envelope(sections)
|
|
: { status: 500, body: { error: { code: 'server_error', message: 'Server error', details: {} } } }
|
|
},
|
|
[`POST ${OPEN}`]: envelope({ message: 'Done.', url: '' }),
|
|
})
|
|
await wrapper.find('[data-account-contribution-action="open"]').trigger('click')
|
|
await flushPromises()
|
|
expect(loads).toBe(2)
|
|
expect(wrapper.findAll('[data-account-contribution]').map((card) => card.attributes('data-account-contribution'))).toEqual([
|
|
'site_link',
|
|
'newsletter',
|
|
])
|
|
// Newest toast first.
|
|
expect(toasts.value.map((toast) => [toast.text, toast.tone])).toEqual([
|
|
['Something went wrong.', 'danger'],
|
|
['Done.', 'success'],
|
|
])
|
|
})
|
|
|
|
it('the confirm and stale texts are the My account ones, not the record form ones', async () => {
|
|
setBundle(
|
|
{
|
|
...langFixture.data,
|
|
...strings,
|
|
'backend::lang.myaccount.action_confirm': { other: 'Run “:action”?' },
|
|
'backend::lang.form.action_confirm': { other: 'Run “:action” on this record?' },
|
|
'backend::lang.form.action_stale': { other: 'This action no longer applies to this record.' },
|
|
},
|
|
'en',
|
|
)
|
|
const STOP = `${CONTRIBUTIONS}/newsletter/actions/stop`
|
|
const { wrapper } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: envelope([sections[0], { ...sections[1], actions: [{ ...sections[1]!.actions[0], confirm: '' }] }]),
|
|
[`POST ${STOP}`]: { status: 409, body: { error: { code: 'conflict', message: 'Conflict', details: {} } } },
|
|
})
|
|
await wrapper.find('[data-account-contribution-action="stop"]').trigger('click')
|
|
await flushPromises()
|
|
const dialog = document.body.querySelector<HTMLElement>('[data-confirm-dialog]')
|
|
expect(dialog?.textContent).toContain('Run “Stop the digest”?')
|
|
expect(dialog?.textContent).not.toContain('on this record')
|
|
dialog!.querySelector<HTMLButtonElement>('[data-action="confirm"]')!.click()
|
|
await flushPromises()
|
|
expect(toasts.value.map((toast) => toast.text)).toEqual(['This action no longer applies.'])
|
|
})
|
|
})
|
|
|
|
// Phase 32.2 UI-SPEC C3: per-item rows and item actions, additive to the
|
|
// sections above.
|
|
describe('My account item rows (32.2)', () => {
|
|
const itemStrings = {
|
|
...strings,
|
|
'backend::lang.myaccount.items_empty': { other: 'Nothing here yet.' },
|
|
'backend::lang.myaccount.item_action_named': { other: ':action: :title' },
|
|
'backend::lang.myaccount.item_gone': { other: 'This item is already gone.' },
|
|
}
|
|
const itemPath = (item: string, action: string) =>
|
|
`${API}/account/contribution-items/agents/${encodeURIComponent(item)}/actions/${action}`
|
|
|
|
const agents = {
|
|
name: 'agents',
|
|
label: 'Connected agents',
|
|
description: 'Agents you connected.',
|
|
status: 'Connector address: https://example.test/mcp',
|
|
actions: [],
|
|
items: [
|
|
{ id: 'client:1', title: 'Acme Agent', subtitle: 'Read the admin, Acme', meta: ['Connected 2026-11-01', 'Never used'] },
|
|
{ id: 'client:2', title: '<b>Bold</b> Agent', subtitle: '', meta: [] },
|
|
],
|
|
item_actions: [
|
|
{ name: 'rename', label: 'Rename', confirm: '', danger: false },
|
|
{ name: 'revoke', label: 'Revoke', confirm: 'Revoke the connection “:title”?', danger: true },
|
|
],
|
|
items_empty: '',
|
|
}
|
|
|
|
beforeEach(() => {
|
|
setBundle({ ...langFixture.data, ...itemStrings }, 'en')
|
|
})
|
|
|
|
it('a section with items null renders exactly as before', async () => {
|
|
const legacy = { ...sections[0], items: null, item_actions: null, items_empty: '' }
|
|
const before = await openAccount({ [`GET ${CONTRIBUTIONS}`]: envelope([sections[0]]) })
|
|
const beforeHtml = before.wrapper.find('[data-account-contribution="site_link"]').html()
|
|
before.wrapper.unmount()
|
|
const after = await openAccount({ [`GET ${CONTRIBUTIONS}`]: envelope([legacy]) })
|
|
const card = after.wrapper.find('[data-account-contribution="site_link"]')
|
|
expect(card.html()).toBe(beforeHtml)
|
|
expect(card.find('[data-account-contribution-status]').text()).toBe('Not linked')
|
|
expect(card.find('[data-account-contribution-action="open"]').exists()).toBe(true)
|
|
expect(card.find('[role="list"]').exists()).toBe(false)
|
|
expect(card.find('[data-account-items-empty]').exists()).toBe(false)
|
|
})
|
|
|
|
it('items [] renders the empty block with the section text or the default', async () => {
|
|
const { wrapper } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: envelope([
|
|
{ ...agents, items: [] },
|
|
{ ...agents, name: 'other', items: [], items_empty: 'No agents connected yet.' },
|
|
]),
|
|
})
|
|
const [first, second] = wrapper.findAll('[data-account-items-empty]')
|
|
expect(first!.text()).toBe('Nothing here yet.')
|
|
expect(second!.text()).toBe('No agents connected yet.')
|
|
expect(wrapper.find('[data-account-item]').exists()).toBe(false)
|
|
expect(wrapper.find('[data-account-contribution="agents"] [data-account-contribution-status]').exists()).toBe(true)
|
|
})
|
|
|
|
it('renders one row per item with its text and one button per item action', async () => {
|
|
const { wrapper } = await openAccount({ [`GET ${CONTRIBUTIONS}`]: envelope([agents]) })
|
|
const rows = wrapper.findAll('[data-account-item]')
|
|
expect(rows.map((row) => row.attributes('data-account-item'))).toEqual(['client:1', 'client:2'])
|
|
expect(rows.every((row) => row.attributes('role') === 'listitem')).toBe(true)
|
|
expect(rows[0]!.text()).toContain('Read the admin, Acme')
|
|
expect(rows[0]!.text()).toContain('Connected 2026-11-01 · Never used')
|
|
// Text, never HTML.
|
|
expect(rows[1]!.find('b').exists()).toBe(false)
|
|
expect(rows[1]!.text()).toContain('<b>Bold</b> Agent')
|
|
const buttons = rows[0]!.findAll('[data-account-item-action]')
|
|
expect(buttons.map((button) => button.attributes('data-account-item-action'))).toEqual(['rename', 'revoke'])
|
|
expect(buttons.map((button) => button.attributes('aria-label'))).toEqual(['Rename: Acme Agent', 'Revoke: Acme Agent'])
|
|
expect(buttons[0]!.find('svg').exists()).toBe(false)
|
|
expect(buttons[1]!.find('svg').exists()).toBe(true)
|
|
})
|
|
|
|
it('a section with error: true shows the error line instead of rows or the empty block', async () => {
|
|
setBundle({ ...langFixture.data, ...itemStrings, 'backend::lang.myaccount.section_error': { other: 'This section could not be loaded.' } }, 'en')
|
|
const { wrapper } = await openAccount({ [`GET ${CONTRIBUTIONS}`]: envelope([{ ...agents, items: null, error: true }]) })
|
|
const card = wrapper.find('[data-account-contribution="agents"]')
|
|
expect(card.find('[data-account-contribution-error]').text()).toBe('This section could not be loaded.')
|
|
expect(card.find('[data-account-item]').exists()).toBe(false)
|
|
expect(card.find('[data-account-items-empty]').exists()).toBe(false)
|
|
})
|
|
|
|
it('a danger item action confirms with :title filled as text and posts the item route', async () => {
|
|
const REVOKE = itemPath('client:2', 'revoke')
|
|
const { wrapper, calls } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: envelope([agents]),
|
|
[`POST ${REVOKE}`]: envelope({ message: 'Connection revoked', url: '' }),
|
|
})
|
|
await wrapper.find('[data-account-item="client:2"] [data-account-item-action="revoke"]').trigger('click')
|
|
await flushPromises()
|
|
const dialog = document.body.querySelector<HTMLElement>('[data-confirm-dialog]')
|
|
expect(dialog?.textContent).toContain('Revoke the connection “<b>Bold</b> Agent”?')
|
|
expect(dialog?.querySelector('b')).toBeNull()
|
|
expect(requestsTo(calls, 'POST', REVOKE)).toHaveLength(0)
|
|
dialog!.querySelector<HTMLButtonElement>('[data-action="confirm"]')!.click()
|
|
await flushPromises()
|
|
const posts = requestsTo(calls, 'POST', REVOKE)
|
|
expect(posts).toHaveLength(1)
|
|
expect(posts[0]!.headers.get('X-Requested-With')).toBe('XMLHttpRequest')
|
|
expect(toasts.value.map((toast) => [toast.text, toast.tone])).toEqual([['Connection revoked', 'success']])
|
|
expect(requestsTo(calls, 'GET', CONTRIBUTIONS)).toHaveLength(2)
|
|
})
|
|
|
|
it('an item action without confirm or danger posts at once', async () => {
|
|
const RENAME = itemPath('client:1', 'rename')
|
|
const { wrapper, calls } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: envelope([agents]),
|
|
[`POST ${RENAME}`]: envelope({ message: '', url: '' }),
|
|
})
|
|
await wrapper.find('[data-account-item="client:1"] [data-account-item-action="rename"]').trigger('click')
|
|
await flushPromises()
|
|
expect(document.body.querySelector('[data-confirm-dialog]')).toBeNull()
|
|
expect(requestsTo(calls, 'POST', RENAME)).toHaveLength(1)
|
|
expect(toasts.value.map((toast) => toast.text)).toEqual(['Action completed.'])
|
|
})
|
|
|
|
it('404 shows the item_gone toast and reloads the list', async () => {
|
|
const RENAME = itemPath('client:1', 'rename')
|
|
const { wrapper, calls } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: envelope([agents]),
|
|
[`POST ${RENAME}`]: { status: 404, body: { error: { code: 'not_found', message: 'Not found', details: {} } } },
|
|
})
|
|
await wrapper.find('[data-account-item="client:1"] [data-account-item-action="rename"]').trigger('click')
|
|
await flushPromises()
|
|
expect(toasts.value.map((toast) => [toast.text, toast.tone])).toEqual([['This item is already gone.', 'danger']])
|
|
expect(requestsTo(calls, 'GET', CONTRIBUTIONS)).toHaveLength(2)
|
|
})
|
|
|
|
it('409 shows the action_stale toast and reloads the list', async () => {
|
|
const RENAME = itemPath('client:1', 'rename')
|
|
const { wrapper, calls } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: envelope([agents]),
|
|
[`POST ${RENAME}`]: { status: 409, body: { error: { code: 'conflict', message: 'Conflict', details: {} } } },
|
|
})
|
|
await wrapper.find('[data-account-item="client:1"] [data-account-item-action="rename"]').trigger('click')
|
|
await flushPromises()
|
|
expect(toasts.value.map((toast) => [toast.text, toast.tone])).toEqual([['This action no longer applies.', 'danger']])
|
|
expect(requestsTo(calls, 'GET', CONTRIBUTIONS)).toHaveLength(2)
|
|
})
|
|
|
|
it("403 shows the server's message and keeps the list", async () => {
|
|
const RENAME = itemPath('client:1', 'rename')
|
|
const { wrapper, calls } = await openAccount({
|
|
[`GET ${CONTRIBUTIONS}`]: envelope([agents]),
|
|
[`POST ${RENAME}`]: { status: 403, body: { error: { code: 'forbidden', message: 'You may not rename this.', details: {} } } },
|
|
})
|
|
await wrapper.find('[data-account-item="client:1"] [data-account-item-action="rename"]').trigger('click')
|
|
await flushPromises()
|
|
expect(toasts.value.map((toast) => [toast.text, toast.tone])).toEqual([['You may not rename this.', 'danger']])
|
|
expect(requestsTo(calls, 'GET', CONTRIBUTIONS)).toHaveLength(1)
|
|
})
|
|
})
|