fix(36): guard stale scope catalog responses in ApiTokenCreateDialog

This commit is contained in:
Jakub Zych
2026-11-24 23:45:36 +01:00
parent 0b7e139d18
commit 2f2c33e68e
6 changed files with 129 additions and 11 deletions

View File

@@ -61,6 +61,7 @@ const copyFailed = ref(false)
const copyFlash = ref(false) const copyFlash = ref(false)
const announce = ref('') const announce = ref('')
let copyTimer: ReturnType<typeof setTimeout> | null = null let copyTimer: ReturnType<typeof setTimeout> | null = null
let loadSeq = 0
const nameInput = ref<HTMLInputElement | null>(null) const nameInput = ref<HTMLInputElement | null>(null)
const plaintextInput = ref<HTMLInputElement | null>(null) const plaintextInput = ref<HTMLInputElement | null>(null)
@@ -139,12 +140,16 @@ function setOpen(open: boolean): void {
} }
async function loadScopes(): Promise<void> { async function loadScopes(): Promise<void> {
const seq = ++loadSeq
scopesLoading.value = true scopesLoading.value = true
scopesFailed.value = false scopesFailed.value = false
scopes.value = [] scopes.value = []
resetScopeSelection([]) resetScopeSelection([])
try { try {
const result = await api.GET('/account/tokens/scopes') const result = await api.GET('/account/tokens/scopes')
if (seq !== loadSeq || !props.open) {
return
}
if (result.data) { if (result.data) {
const list = result.data.data ?? [] const list = result.data.data ?? []
scopes.value = list scopes.value = list
@@ -154,10 +159,15 @@ async function loadScopes(): Promise<void> {
} }
scopesFailed.value = true scopesFailed.value = true
} catch { } catch {
if (seq !== loadSeq || !props.open) {
return
}
scopesFailed.value = true scopesFailed.value = true
} finally { } finally {
if (seq === loadSeq) {
scopesLoading.value = false scopesLoading.value = false
} }
}
} }
watch( watch(
@@ -167,6 +177,10 @@ watch(
resetForm() resetForm()
void loadScopes() void loadScopes()
} else { } else {
loadSeq += 1
scopesLoading.value = false
scopesFailed.value = false
scopes.value = []
clearPlaintext() clearPlaintext()
resetScopeSelection([]) resetScopeSelection([])
step.value = 1 step.value = 1
@@ -392,6 +406,7 @@ function onHide(): void {
} }
onBeforeUnmount(() => { onBeforeUnmount(() => {
loadSeq += 1
clearPlaintext() clearPlaintext()
}) })
</script> </script>

View File

@@ -595,4 +595,107 @@ describe('ApiTokenCreateDialog mounted directly', () => {
await click('[data-action="hide-token"]', dialog()!) await click('[data-action="hide-token"]', dialog()!)
expect(wrapper.emitted('update:open')).toEqual([[false]]) expect(wrapper.emitted('update:open')).toEqual([[false]])
}) })
it('ignores stale scope catalog responses after rapid close/reopen without resetting Custom picker (WR-01)', async () => {
const first = deferred()
const second = deferred()
let callCount = 0
mockApi(
tokenRoutes({
[`GET ${SCOPES}`]: () => {
callCount += 1
return callCount === 1 ? first.route() : second.route()
},
}),
)
const wrapper = mount(ApiTokenCreateDialog, {
props: { open: false, confirm: useConfirm() },
attachTo: document.body,
})
// First open kicks off request #1 (stays pending), close cancels it, reopen starts request #2.
await wrapper.setProps({ open: true })
await flushPromises()
await wrapper.setProps({ open: false })
await flushPromises()
await wrapper.setProps({ open: true })
await flushPromises()
expect(callCount).toBe(2)
expect(dialog()!.querySelector('[data-scope-loading]')).toBeTruthy()
// Request #2 resolves first with plugin scopes; user switches to Custom and ticks journal in ScopePicker.
second.release(
envelope([
...scopes,
{
name: 'journal',
label: 'Journal',
description: 'Create and edit posts.',
read_only: false,
known: true,
plugin_id: 'acme.journal',
group: 'Journal',
},
]),
)
await flushPromises()
expect(dialog()!.querySelector('[data-scope-loading]')).toBeNull()
await click('[data-access-tier="custom"]', dialog()!)
expect(dialog()!.querySelector('[data-scope-picker]')).toBeTruthy()
const journalBox = dialog()!.querySelector<HTMLInputElement>('[data-scope="journal"] input[type="checkbox"]')!
journalBox.checked = true
journalBox.dispatchEvent(new Event('change', { bubbles: true }))
await flushPromises()
// Stale request #1 now settles late — must NOT close ScopePicker or reset the draft/tier.
first.release(envelope(scopes))
await flushPromises()
expect(dialog()!.querySelector('[data-scope-picker]')).toBeTruthy()
expect(dialog()!.querySelector<HTMLInputElement>('[data-scope="journal"] input[type="checkbox"]')!.checked).toBe(
true,
)
expect(dialog()!.querySelector('[role="alert"]')).toBeNull()
})
it('keeps loading skeleton active if stale first scope request settles while second is still pending (WR-01)', async () => {
const first = deferred()
const second = deferred()
let callCount = 0
mockApi(
tokenRoutes({
[`GET ${SCOPES}`]: () => {
callCount += 1
return callCount === 1 ? first.route() : second.route()
},
}),
)
const wrapper = mount(ApiTokenCreateDialog, {
props: { open: false, confirm: useConfirm() },
attachTo: document.body,
})
await wrapper.setProps({ open: true })
await flushPromises()
await wrapper.setProps({ open: false })
await flushPromises()
await wrapper.setProps({ open: true })
await flushPromises()
expect(dialog()!.querySelector('[data-scope-loading]')).toBeTruthy()
// Stale request #1 resolves early while request #2 is still in flight -> skeleton must remain visible.
first.release({ status: 500, body: { error: { code: 'err', message: 'boom', details: {} } } })
await flushPromises()
expect(dialog()!.querySelector('[data-scope-loading]')).toBeTruthy()
expect(dialog()!.querySelector('[role="alert"]')).toBeNull()
expect(dialog()!.querySelector<HTMLButtonElement>('[data-action="create-token-submit"]')!.disabled).toBe(true)
// Request #2 resolves -> skeleton clears and tiers render normally.
second.release(envelope(scopes))
await flushPromises()
expect(dialog()!.querySelector('[data-scope-loading]')).toBeNull()
expect(dialog()!.querySelector('[data-access-tier="read_only"]')!.getAttribute('data-state')).toBe('checked')
})
}) })

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@@ -1,2 +1,2 @@
const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./editor-uDgt9VKj.js","./index-Vcq4gwWv.js"])))=>i.map(i=>d[i]); const __vite__mapDeps=(i,m=__vite__mapDeps,d=(m.f||(m.f=["./editor-uDgt9VKj.js","./index-Vcq4gwWv.js"])))=>i.map(i=>d[i]);
import{_ as r}from"./index-D3o9qWRZ.js";function t(){return r(()=>import("./editor-uDgt9VKj.js"),__vite__mapDeps([0,1]),import.meta.url)}export{t as importCodeEditor}; import{_ as r}from"./index-fVcBkpQa.js";function t(){return r(()=>import("./editor-uDgt9VKj.js"),__vite__mapDeps([0,1]),import.meta.url)}export{t as importCodeEditor};

View File

@@ -8,7 +8,7 @@
<meta name="summer-admin-build" content="__SUMMER_ADMIN_BUILD__" /> <meta name="summer-admin-build" content="__SUMMER_ADMIN_BUILD__" />
<link rel="icon" href="./favicon.ico" sizes="48x48" /> <link rel="icon" href="./favicon.ico" sizes="48x48" />
<title>SummerCMS</title> <title>SummerCMS</title>
<script type="module" crossorigin src="./assets/index-D3o9qWRZ.js"></script> <script type="module" crossorigin src="./assets/index-fVcBkpQa.js"></script>
<link rel="stylesheet" crossorigin href="./assets/index-DB7OYKHr.css"> <link rel="stylesheet" crossorigin href="./assets/index-DB7OYKHr.css">
</head> </head>
<body> <body>