test(36-04): add Vitest unit and component tests covering all 7 scope picker backstops

This commit is contained in:
Jakub Zych
2026-11-24 20:08:24 +01:00
parent 782f4dc19d
commit 2661e0f363
5 changed files with 1072 additions and 0 deletions

View File

@@ -243,6 +243,162 @@ describe('the 3-tier access control & Customize modal (UI backstop)', () => {
{ decision: 'allow', scopes: ['admin:read', 'acme'], offline_access: true },
])
})
it('opens narrow plugin-only subset in Custom with subset selected and hides Custom when no plugin scopes exist (backstop #1, D-03)', async () => {
const scopeSeo: AdminAPITokenScope = {
name: 'seo',
label: 'SEO',
description: 'Manage SEO.',
read_only: false,
known: true,
plugin_id: 'acme.seo',
group: 'SEO',
}
const narrow = await open(
'/connect?request=req1',
routes(consentView({ scopes: [SCOPE_ACME, scopeSeo] })),
)
expect(narrow.wrapper.find('[data-access-tiers]').exists()).toBe(false)
expect(narrow.wrapper.findAll('[data-summary-chip]').map((c) => c.attributes('data-summary-chip'))).toEqual([
'acme',
'seo',
])
await narrow.wrapper.find('[data-connect-state="ready"]').trigger('submit')
await flushPromises()
expect(await postedBodies(narrow.calls)).toEqual([
{ decision: 'allow', scopes: ['acme', 'seo'], offline_access: true },
])
narrow.wrapper.unmount()
const noPlugins = await open(
'/connect?request=req1',
routes(consentView({ scopes: [SCOPE_ALL, SCOPE_READ] })),
)
expect(
noPlugins.wrapper.findAll('[data-access-tier]').map((item) => item.attributes('data-access-tier')),
).toEqual(['full', 'read_only'])
expect(noPlugins.wrapper.find('[data-access-tier="custom"]').exists()).toBe(false)
expect(noPlugins.wrapper.find('[data-access-tier="read_only"]').attributes('data-state')).toBe('checked')
})
it('reverts auto-opened empty Custom on Discard/Escape and remembers applied Custom across tier switches (backstop #3, D-09, D-11)', async () => {
const { wrapper } = await open(
'/connect?request=req1',
routes(consentView({ scopes: [SCOPE_ALL, SCOPE_READ, SCOPE_ACME] })),
)
expect(wrapper.find('[data-access-tier="read_only"]').attributes('data-state')).toBe('checked')
// Auto-open empty Custom -> Discard reverts tier back to Read-only.
await clickTier('custom')
expect(pickerDialog()).toBeTruthy()
await tickPickerScope('acme', true)
pickerDialog()!.querySelector<HTMLButtonElement>('[data-action="scope-picker-discard"]')!.click()
await flushPromises()
expect(pickerDialog()).toBeNull()
expect(wrapper.find('[data-access-tier="read_only"]').attributes('data-state')).toBe('checked')
// Auto-open empty Custom -> Escape reverts tier back to Read-only.
await clickTier('custom')
expect(pickerDialog()).toBeTruthy()
pickerDialog()!.dispatchEvent(new KeyboardEvent('keydown', { key: 'Escape', bubbles: true }))
await flushPromises()
expect(pickerDialog()).toBeNull()
expect(wrapper.find('[data-access-tier="read_only"]').attributes('data-state')).toBe('checked')
// Apply Custom selection -> switch to Read-only -> switch back to Custom restores saved selection without reopening modal.
await clickTier('custom')
expect(pickerDialog()).toBeTruthy()
await tickPickerScope('acme', true)
pickerDialog()!.querySelector<HTMLButtonElement>('[data-action="scope-picker-apply"]')!.click()
await flushPromises()
expect(pickerDialog()).toBeNull()
expect(wrapper.find('[data-summary-chip="acme"]').exists()).toBe(true)
await clickTier('read_only')
expect(wrapper.find('[data-access-tier="read_only"]').attributes('data-state')).toBe('checked')
await clickTier('custom')
expect(pickerDialog()).toBeNull()
expect(wrapper.find('[data-access-tier="custom"]').attributes('data-state')).toBe('checked')
expect(wrapper.find('[data-summary-chip="acme"]').exists()).toBe(true)
})
it('omits admin:read by default in Custom (PII footgun fix) and renders admin:read chip first when opted in (backstop #2, D-04, T-36-04)', async () => {
const scopeJournal: AdminAPITokenScope = {
name: 'journal',
label: 'Journal',
description: 'Manage posts.',
read_only: false,
known: true,
plugin_id: 'acme.journal',
group: 'Journal',
}
const scopeJournalRead: AdminAPITokenScope = {
name: 'journal:read',
label: 'Journal, read-only',
description: 'Read posts.',
read_only: true,
known: true,
plugin_id: 'acme.journal',
group: 'Journal',
}
// Without admin:read opt-in -> submits ['journal'] only.
const first = await open(
'/connect?request=req1',
routes(consentView({ scopes: [SCOPE_ALL, SCOPE_READ, scopeJournal, scopeJournalRead] })),
)
await clickTier('custom')
expect(pickerDialog()!.querySelector<HTMLInputElement>('[data-admin-read-checkbox]')!.checked).toBe(false)
await tickPickerScope('journal', true)
pickerDialog()!.querySelector<HTMLButtonElement>('[data-action="scope-picker-apply"]')!.click()
await flushPromises()
expect(first.wrapper.findAll('[data-summary-chip]').map((c) => c.attributes('data-summary-chip'))).toEqual([
'journal',
])
await first.wrapper.find('[data-connect-state="ready"]').trigger('submit')
await flushPromises()
expect(await postedBodies(first.calls)).toEqual([
{ decision: 'allow', scopes: ['journal'], offline_access: true },
])
first.wrapper.unmount()
// With admin:read opt-in -> renders admin:read chip first and submits ['admin:read', 'journal'].
const second = await open(
'/connect?request=req1',
routes(consentView({ scopes: [SCOPE_ALL, SCOPE_READ, scopeJournal, scopeJournalRead] })),
)
await clickTier('custom')
const adminRead = pickerDialog()!.querySelector<HTMLInputElement>('[data-admin-read-checkbox]')!
adminRead.checked = true
adminRead.dispatchEvent(new Event('change', { bubbles: true }))
await flushPromises()
await tickPickerScope('journal', true)
pickerDialog()!.querySelector<HTMLButtonElement>('[data-action="scope-picker-apply"]')!.click()
await flushPromises()
expect(second.wrapper.findAll('[data-summary-chip]').map((c) => c.attributes('data-summary-chip'))).toEqual([
'admin:read',
'journal',
])
await second.wrapper.find('[data-connect-state="ready"]').trigger('submit')
await flushPromises()
expect(await postedBodies(second.calls)).toEqual([
{ decision: 'allow', scopes: ['admin:read', 'journal'], offline_access: true },
])
})
it('applies connect-light-surface, mobile 100dvh sheet, and desktop 640px/85dvh classes to portaled Customize modal (backstop #6, D-10, D-12)', async () => {
await open('/connect?request=req1', routes(consentView({ scopes: [SCOPE_ALL, SCOPE_READ, SCOPE_ACME] })))
await clickTier('custom')
const overlay = document.body.querySelector<HTMLElement>('[data-dialog-overlay]')!
const content = pickerDialog()!
expect(overlay).toBeTruthy()
expect(content).toBeTruthy()
expect(overlay.classList.contains('connect-light-surface')).toBe(true)
expect(content.classList.contains('connect-light-surface')).toBe(true)
expect(content.className).toContain('h-[100dvh]')
expect(content.className).toContain('sm:max-w-[640px]')
expect(content.className).toContain('sm:max-h-[85dvh]')
})
})
describe('no auto-allow and the decision navigation (UI backstop)', () => {