mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 16:07:33 +00:00
Merge pull request #9027
fix(frontend): theme the MCP consent dropdowns with the common Select
This commit is contained in:
2 files changed
+67
-18
No files matched your search
@@ -41,18 +41,12 @@
|
||||
<p v-if="authStore.user?.email" class="break-all text-sm text-gray-600 dark:text-gray-300">{{ t('mcp.signedInAs', { email: authStore.user.email }) }}</p>
|
||||
<div>
|
||||
<label for="mcp-consent-scope" class="input-label">{{ t('mcp.scope') }}</label>
|
||||
<select id="mcp-consent-scope" v-model="scope" class="input" :disabled="submitting" aria-describedby="mcp-scope-hint">
|
||||
<option value="user">{{ t('mcp.userScope') }}</option>
|
||||
<option v-if="authStore.isAdmin" value="admin">{{ t('mcp.adminScope') }}</option>
|
||||
</select>
|
||||
<Select id="mcp-consent-scope" :model-value="scope" :options="scopeOptions" :disabled="submitting" aria-describedby="mcp-scope-hint" @update:model-value="setScope" />
|
||||
<p id="mcp-scope-hint" class="mt-2 text-xs text-gray-500 dark:text-gray-400">{{ scope === 'admin' ? t('mcp.adminScopeHint') : t('mcp.userScopeHint') }}</p>
|
||||
</div>
|
||||
<div>
|
||||
<label for="mcp-consent-permissions" class="input-label">{{ t('mcp.permissions') }}</label>
|
||||
<select id="mcp-consent-permissions" v-model="readOnly" class="input" :disabled="submitting" aria-describedby="mcp-permissions-hint">
|
||||
<option :value="true">{{ t('mcp.readOnly') }}</option>
|
||||
<option :value="false">{{ t('mcp.readWrite') }}</option>
|
||||
</select>
|
||||
<Select id="mcp-consent-permissions" :model-value="readOnly" :options="permissionOptions" :disabled="submitting" aria-describedby="mcp-permissions-hint" @update:model-value="setReadOnly" />
|
||||
<p id="mcp-permissions-hint" class="mt-2 text-xs" :class="readOnly ? 'text-gray-500 dark:text-gray-400' : 'text-amber-700 dark:text-amber-400'">{{ readOnly ? t('mcp.readOnlyHint') : t('mcp.readWriteHint') }}</p>
|
||||
<p v-if="scope === 'admin' && !readOnly" data-testid="admin-write-hint" class="mt-1 text-xs text-amber-700 dark:text-amber-400">{{ t('mcp.adminWriteHint') }}</p>
|
||||
</div>
|
||||
@@ -73,6 +67,7 @@ import { useRoute } from 'vue-router'
|
||||
import { useI18n } from 'vue-i18n'
|
||||
import { mcpAPI, type MCPAccessScope, type MCPAuthorization, type MCPConsent } from '@/api/mcp'
|
||||
import TotpStepUpDialog from '@/components/auth/TotpStepUpDialog.vue'
|
||||
import Select from '@/components/common/Select.vue'
|
||||
import { Icon } from '@/components/icons'
|
||||
import { isStepUpBlocked, isStepUpCancelled, stepUpBlockReason, useStepUp } from '@/composables/useStepUp'
|
||||
import { useAuthStore } from '@/stores/auth'
|
||||
@@ -92,6 +87,24 @@ let authorization: MCPAuthorization | null = null
|
||||
let generation = 0
|
||||
const redirectOrigin = computed(() => consent.value ? new URL(consent.value.redirect_uri).origin : '')
|
||||
|
||||
const scopeOptions = computed(() => [
|
||||
{ value: 'user', label: t('mcp.userScope') },
|
||||
...(authStore.isAdmin ? [{ value: 'admin', label: t('mcp.adminScope') }] : [])
|
||||
])
|
||||
|
||||
const permissionOptions = computed(() => [
|
||||
{ value: true, label: t('mcp.readOnly') },
|
||||
{ value: false, label: t('mcp.readWrite') }
|
||||
])
|
||||
|
||||
function setScope(value: string | number | boolean | null): void {
|
||||
scope.value = value === 'admin' ? 'admin' : 'user'
|
||||
}
|
||||
|
||||
function setReadOnly(value: string | number | boolean | null): void {
|
||||
readOnly.value = value !== false
|
||||
}
|
||||
|
||||
function errorMessage(error: unknown, fallback: string): string {
|
||||
const message = (error as { message?: unknown } | null)?.message
|
||||
return typeof message === 'string' && message ? message : fallback
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { flushPromises, mount } from '@vue/test-utils'
|
||||
import { reactive } from 'vue'
|
||||
import { flushPromises, mount, type VueWrapper } from '@vue/test-utils'
|
||||
import { nextTick, reactive } from 'vue'
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import MCPOAuthAuthorizeView from '../MCPOAuthAuthorizeView.vue'
|
||||
import type { MCPAuthorization, MCPConsent } from '@/api/mcp'
|
||||
@@ -36,6 +36,29 @@ function render() {
|
||||
mounted.push(wrapper)
|
||||
return wrapper
|
||||
}
|
||||
// The scope/permission pickers are common Select instances; their panels teleport to body.
|
||||
// Closed panels linger in the DOM until their leave transition ends (two rAF frames in jsdom),
|
||||
// so close any open panel first, wait it out, and expect exactly one portal afterwards.
|
||||
async function openDropdown(wrapper: VueWrapper, id: string) {
|
||||
for (const trigger of wrapper.findAll('button[aria-haspopup="true"]')) {
|
||||
if (trigger.attributes('aria-expanded') === 'true') await trigger.trigger('click')
|
||||
}
|
||||
await nextTick()
|
||||
await new Promise(resolve => setTimeout(resolve, 50))
|
||||
await wrapper.get(`#${id}`).trigger('click')
|
||||
await nextTick()
|
||||
const portals = document.body.querySelectorAll<HTMLElement>('.select-dropdown-portal')
|
||||
expect(portals.length, `expected exactly one open dropdown portal for #${id}, got ${portals.length}`).toBe(1)
|
||||
return portals[0]
|
||||
}
|
||||
|
||||
async function pickOption(wrapper: VueWrapper, id: string, label: string) {
|
||||
const dropdown = await openDropdown(wrapper, id)
|
||||
const option = [...dropdown.querySelectorAll('.select-option')].find((el) => el.textContent?.includes(label))
|
||||
expect(option, `option containing "${label}" not found`).toBeDefined()
|
||||
;(option as HTMLElement).click()
|
||||
await nextTick()
|
||||
}
|
||||
beforeEach(() => {
|
||||
vi.clearAllMocks()
|
||||
auth.isAdmin = false
|
||||
@@ -46,6 +69,7 @@ beforeEach(() => {
|
||||
})
|
||||
afterEach(() => {
|
||||
mounted.splice(0).forEach(wrapper => wrapper.unmount())
|
||||
document.body.innerHTML = ''
|
||||
Object.defineProperty(window, 'location', { configurable: true, value: originalLocation })
|
||||
})
|
||||
|
||||
@@ -60,13 +84,25 @@ describe('MCP OAuth consent', () => {
|
||||
expect(wrapper.text()).toContain('mcp.untrustedApplicationHint')
|
||||
expect(authorize).not.toHaveBeenCalled()
|
||||
expect(assign).not.toHaveBeenCalled()
|
||||
expect(wrapper.find('#mcp-consent-scope option[value="admin"]').exists()).toBe(false)
|
||||
const scopeLabels = [...(await openDropdown(wrapper, 'mcp-consent-scope')).querySelectorAll('.select-option-label')].map((el) => el.textContent?.trim())
|
||||
expect(scopeLabels).toEqual(['mcp.userScope'])
|
||||
await wrapper.get('#mcp-consent-scope').trigger('click')
|
||||
await nextTick()
|
||||
await wrapper.get('form').trigger('submit')
|
||||
await flushPromises()
|
||||
expect(authorize).toHaveBeenCalledWith({ authorization: request, approved: true, scope: 'user', read_only: true })
|
||||
expect(assign).toHaveBeenCalledWith(redirectURL)
|
||||
})
|
||||
|
||||
it('renders the scope and permission pickers as themed custom dropdown panels', async () => {
|
||||
const wrapper = render()
|
||||
await flushPromises()
|
||||
const permissionsLabels = [...(await openDropdown(wrapper, 'mcp-consent-permissions')).querySelectorAll('.select-option-label')].map((el) => el.textContent?.trim())
|
||||
expect(permissionsLabels).toEqual(['mcp.readOnly', 'mcp.readWrite'])
|
||||
const scopeLabels = [...(await openDropdown(wrapper, 'mcp-consent-scope')).querySelectorAll('.select-option-label')].map((el) => el.textContent?.trim())
|
||||
expect(scopeLabels).toEqual(['mcp.userScope'])
|
||||
})
|
||||
|
||||
it('allows an omitted OAuth scope without adding or changing request parameters', async () => {
|
||||
delete routeState.current.query.scope
|
||||
const wrapper = render()
|
||||
@@ -140,9 +176,9 @@ describe('MCP OAuth consent', () => {
|
||||
authorize.mockRejectedValueOnce({ reason: 'STEP_UP_REQUIRED' }).mockResolvedValueOnce({ redirect_url: redirectURL })
|
||||
const wrapper = render()
|
||||
await flushPromises()
|
||||
expect((wrapper.get('#mcp-consent-scope').element as HTMLSelectElement).value).toBe('user')
|
||||
await wrapper.get('#mcp-consent-scope').setValue('admin')
|
||||
await wrapper.get('#mcp-consent-permissions').setValue('false')
|
||||
expect(wrapper.get('#mcp-consent-scope').text()).toContain('mcp.userScope')
|
||||
await pickOption(wrapper, 'mcp-consent-scope', 'mcp.adminScope')
|
||||
await pickOption(wrapper, 'mcp-consent-permissions', 'mcp.readWrite')
|
||||
await wrapper.get('form').trigger('submit')
|
||||
await flushPromises()
|
||||
expect(authorize).toHaveBeenCalledTimes(1)
|
||||
@@ -159,11 +195,11 @@ describe('MCP OAuth consent', () => {
|
||||
const wrapper = render()
|
||||
await flushPromises()
|
||||
expect(wrapper.find('[data-testid="admin-write-hint"]').exists()).toBe(false)
|
||||
await wrapper.get('#mcp-consent-permissions').setValue('false')
|
||||
await pickOption(wrapper, 'mcp-consent-permissions', 'mcp.readWrite')
|
||||
expect(wrapper.find('[data-testid="admin-write-hint"]').exists()).toBe(false)
|
||||
await wrapper.get('#mcp-consent-scope').setValue('admin')
|
||||
await pickOption(wrapper, 'mcp-consent-scope', 'mcp.adminScope')
|
||||
expect(wrapper.get('[data-testid="admin-write-hint"]').text()).toBe('mcp.adminWriteHint')
|
||||
await wrapper.get('#mcp-consent-permissions').setValue('true')
|
||||
await pickOption(wrapper, 'mcp-consent-permissions', 'mcp.readOnly')
|
||||
expect(wrapper.find('[data-testid="admin-write-hint"]').exists()).toBe(false)
|
||||
})
|
||||
|
||||
@@ -172,7 +208,7 @@ describe('MCP OAuth consent', () => {
|
||||
authorize.mockRejectedValue({ reason: 'STEP_UP_REQUIRED' })
|
||||
const wrapper = render()
|
||||
await flushPromises()
|
||||
await wrapper.get('#mcp-consent-scope').setValue('admin')
|
||||
await pickOption(wrapper, 'mcp-consent-scope', 'mcp.adminScope')
|
||||
await wrapper.get('form').trigger('submit')
|
||||
await flushPromises()
|
||||
await wrapper.get('[data-testid="step-up-cancel"]').trigger('click')
|
||||
|
||||
Reference in new issue
Block a user