Files
ocis/web/packages/web-app-admin-settings/tests/unit/components/General/RegistrationSection.spec.ts
T
sylph ce462dbe20 feat(admin-settings): 登録設定のトグルと承認待ち一覧を追加
- 一般設定に「登録」セクションを追加 (Settings.ReadWrite 権限が必要)
- 登録受付 / Web 登録 / Discord 登録 / 管理者承認を切り替えた時点で保存し、失敗したら元に戻す
- 承認待ちアカウントをユーザー名・メール・登録元・申請日時で一覧表示する
- 承認ボタンで有効化し、拒否は確認ダイアログの後にアカウントを削除する
2026-11-01 22:57:06 +09:00

176 lines
6.3 KiB
TypeScript

import RegistrationSection from '../../../../src/components/General/RegistrationSection.vue'
import {
defaultComponentMocks,
defaultPlugins,
mockAxiosResolve,
shallowMount
} from '@ownclouders/web-test-helpers'
import { flushPromises } from '@vue/test-utils'
import { useMessages, useModals } from '@ownclouders/web-pkg'
const SETTINGS_URL = '/oauth2/discord/admin/registration'
const PENDING_URL = '/oauth2/discord/admin/pending'
const stored = {
allow_registration: true,
allow_web: false,
allow_discord: true,
require_approval: true
}
const pendingUsers = [
{
user_id: 'u1',
username: 'alice',
mail: 'alice@example.com',
source: 'discord',
requested_at: '2026-11-01T10:00:00Z'
},
{
user_id: 'u2',
username: 'carol',
mail: 'carol@example.com',
source: 'web',
requested_at: '2026-11-01T11:00:00Z'
}
]
describe('RegistrationSection', () => {
it('is hidden and does not load without the settings permission', async () => {
const { wrapper, mocks } = getWrapper({ canManage: false })
await flushPromises()
expect(wrapper.find('#registration-section').exists()).toBeFalsy()
expect(mocks.$clientService.httpAuthenticated.get).not.toHaveBeenCalled()
})
it('loads the settings and the pending accounts', async () => {
const { wrapper, mocks } = getWrapper()
await flushPromises()
expect(mocks.$clientService.httpAuthenticated.get).toHaveBeenCalledWith(SETTINGS_URL)
expect(mocks.$clientService.httpAuthenticated.get).toHaveBeenCalledWith(PENDING_URL)
expect(wrapper.vm.settings).toEqual(stored)
expect(wrapper.findAll('.registration-pending-row').length).toBe(2)
const badges = wrapper.findAll('.registration-source').map((b) => b.text())
expect(badges).toEqual(['Discord', 'Web'])
})
it('shows a notice when no account is pending', async () => {
const { wrapper } = getWrapper({ pending: [] })
await flushPromises()
expect(wrapper.find('.registration-pending-empty').exists()).toBeTruthy()
expect(wrapper.find('.registration-pending-table').exists()).toBeFalsy()
})
it('saves all settings when a toggle changes', async () => {
const { wrapper, mocks } = getWrapper()
await flushPromises()
const next = { ...stored, allow_web: true }
mocks.$clientService.httpAuthenticated.put.mockResolvedValue(mockAxiosResolve(next))
await wrapper.vm.update('allow_web', true)
expect(mocks.$clientService.httpAuthenticated.put).toHaveBeenCalledWith(SETTINGS_URL, next)
expect(wrapper.vm.settings.allow_web).toBe(true)
const { showMessage } = useMessages()
expect(showMessage).toHaveBeenCalled()
})
it('reverts the toggle when saving fails', async () => {
vi.spyOn(console, 'error').mockImplementation(() => undefined)
const { wrapper, mocks } = getWrapper()
await flushPromises()
mocks.$clientService.httpAuthenticated.put.mockRejectedValue(new Error('500'))
await wrapper.vm.update('require_approval', false)
expect(wrapper.vm.settings.require_approval).toBe(true)
const { showErrorMessage } = useMessages()
expect(showErrorMessage).toHaveBeenCalled()
})
it('approves a pending account and removes it from the list', async () => {
const { wrapper, mocks } = getWrapper()
await flushPromises()
mocks.$clientService.httpAuthenticated.post.mockResolvedValue(mockAxiosResolve(undefined))
await wrapper.vm.approve(pendingUsers[0])
expect(mocks.$clientService.httpAuthenticated.post).toHaveBeenCalledWith(
`${PENDING_URL}/u1/approve`
)
expect(wrapper.vm.pending.map((p) => p.user_id)).toEqual(['u2'])
})
it('keeps the account in the list when approving fails', async () => {
vi.spyOn(console, 'error').mockImplementation(() => undefined)
const { wrapper, mocks } = getWrapper()
await flushPromises()
mocks.$clientService.httpAuthenticated.post.mockRejectedValue(new Error('502'))
await wrapper.vm.approve(pendingUsers[0])
expect(wrapper.vm.pending.length).toBe(2)
const { showErrorMessage } = useMessages()
expect(showErrorMessage).toHaveBeenCalled()
})
it('asks for confirmation before rejecting', async () => {
const { wrapper, mocks } = getWrapper()
await flushPromises()
wrapper.vm.confirmReject(pendingUsers[1])
const { dispatchModal } = useModals()
expect(dispatchModal).toHaveBeenCalledWith(expect.objectContaining({ variation: 'danger' }))
expect(mocks.$clientService.httpAuthenticated.post).not.toHaveBeenCalled()
})
it('rejects a pending account and removes it from the list', async () => {
const { wrapper, mocks } = getWrapper()
await flushPromises()
mocks.$clientService.httpAuthenticated.post.mockResolvedValue(mockAxiosResolve(undefined))
await wrapper.vm.reject(pendingUsers[1])
expect(mocks.$clientService.httpAuthenticated.post).toHaveBeenCalledWith(
`${PENDING_URL}/u2/reject`
)
expect(wrapper.vm.pending.map((p) => p.user_id)).toEqual(['u1'])
})
it('shows an error when the settings cannot be loaded', async () => {
vi.spyOn(console, 'error').mockImplementation(() => undefined)
const { wrapper } = getWrapper({ loadFails: true })
await flushPromises()
expect(wrapper.find('.registration-load-error').exists()).toBeTruthy()
expect(wrapper.find('.registration-settings').exists()).toBeFalsy()
})
})
function getWrapper({
canManage = true,
settings = stored,
pending = pendingUsers,
loadFails = false
}: {
canManage?: boolean
settings?: typeof stored
pending?: typeof pendingUsers
loadFails?: boolean
} = {}) {
const mocks = defaultComponentMocks()
if (loadFails) {
mocks.$clientService.httpAuthenticated.get.mockRejectedValue(new Error('500'))
} else {
mocks.$clientService.httpAuthenticated.get.mockImplementation((url: string) =>
Promise.resolve(
url === PENDING_URL ? mockAxiosResolve({ value: pending }) : mockAxiosResolve(settings)
)
)
}
return {
mocks,
wrapper: shallowMount(RegistrationSection, {
global: {
provide: mocks,
mocks,
// Render the table rows inside the stubbed design-system components.
renderStubDefaultSlot: true,
plugins: [
...defaultPlugins({
abilities: canManage ? [{ action: 'update-all', subject: 'Setting' }] : []
})
]
}
})
}
}