feat: roles SPA Users, Profile, API keys, Activity
This commit is contained in:
1 parent
3db490a615
commit
34dffe222f
98 files changed
+4957
-434
No files matched your search
@@ -0,0 +1,290 @@
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'
|
||||
import { flushPromises, type VueWrapper } from '@vue/test-utils'
|
||||
import { createPinia, setActivePinia } from 'pinia'
|
||||
import { ApiError } from '@/composables/useApi'
|
||||
import type { UserListItem } from '@/composables/useUsers'
|
||||
import { useSessionStore } from '@/stores/session'
|
||||
import { ownerAccess, roleRefFor } from '@/test/access'
|
||||
import { renderInApp } from './roleKit'
|
||||
import TransferOwnershipDialog from '../TransferOwnershipDialog.vue'
|
||||
|
||||
const transferOwnership = vi.fn()
|
||||
const refresh = vi.fn()
|
||||
let toastAdd: ReturnType<typeof vi.fn>
|
||||
|
||||
vi.mock('@/composables/useUsers', () => ({ useUsers: () => ({ transferOwnership }) }))
|
||||
vi.mock('@nuxt/ui/composables', async (importOriginal) => {
|
||||
const actual = await importOriginal<typeof import('@nuxt/ui/composables')>()
|
||||
return { ...actual, useToast: () => ({ add: toastAdd }) }
|
||||
})
|
||||
|
||||
const JONAS_EMAIL = 'jonas@renewbuild.example'
|
||||
|
||||
function accepted(id: number, name: string, email: string, role: string): UserListItem {
|
||||
return {
|
||||
kind: 'user',
|
||||
id,
|
||||
username: name.toLowerCase().split(' ')[0]!,
|
||||
display_name: name,
|
||||
email,
|
||||
role: roleRefFor(role),
|
||||
status: 'accepted',
|
||||
created_at: '2026-06-18T09:00:00Z',
|
||||
}
|
||||
}
|
||||
|
||||
const USERS: UserListItem[] = [
|
||||
{
|
||||
kind: 'invite',
|
||||
id: 9,
|
||||
email: 'pending@renewbuild.example',
|
||||
status: 'pending',
|
||||
created_at: '2026-09-27T09:00:00Z',
|
||||
},
|
||||
accepted(1, 'Marta Alves', 'marta@renewbuild.example', 'owner'),
|
||||
accepted(2, 'Jonas Weber', JONAS_EMAIL, 'admin'),
|
||||
accepted(3, 'Lea Martin', 'lea@renewbuild.example', 'admin'),
|
||||
]
|
||||
|
||||
let wrapper: VueWrapper | null = null
|
||||
|
||||
async function mountDialog() {
|
||||
setActivePinia(createPinia())
|
||||
const session = useSessionStore()
|
||||
session.session = { id: 1, verified: true, displayName: '', access: ownerAccess() }
|
||||
session.refresh = refresh
|
||||
wrapper = renderInApp(TransferOwnershipDialog, { users: USERS, open: true })
|
||||
await flushPromises()
|
||||
}
|
||||
|
||||
function dialog() {
|
||||
return document.body.querySelector('[role="alertdialog"]') as HTMLElement
|
||||
}
|
||||
|
||||
function button(label: string) {
|
||||
return [...dialog().querySelectorAll('button')].find(
|
||||
(candidate) => candidate.textContent?.trim() === label,
|
||||
) as HTMLButtonElement | undefined
|
||||
}
|
||||
|
||||
function radio(name: string) {
|
||||
return [...dialog().querySelectorAll('label')]
|
||||
.find((candidate) => candidate.textContent?.includes(name))
|
||||
?.querySelector<HTMLButtonElement>('[role="radio"]')
|
||||
}
|
||||
|
||||
function field() {
|
||||
return dialog().querySelector('input') as HTMLInputElement
|
||||
}
|
||||
|
||||
async function pickJonas() {
|
||||
radio('Jonas Weber')!.click()
|
||||
await flushPromises()
|
||||
button('Continue')!.click()
|
||||
await flushPromises()
|
||||
}
|
||||
|
||||
async function type(value: string) {
|
||||
field().value = value
|
||||
field().dispatchEvent(new Event('input', { bubbles: true }))
|
||||
await flushPromises()
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
await pickJonas()
|
||||
await type(JONAS_EMAIL)
|
||||
button('Transfer ownership')!.click()
|
||||
await flushPromises()
|
||||
}
|
||||
|
||||
function emitted(name: string) {
|
||||
return wrapper!.findComponent(TransferOwnershipDialog).emitted(name)
|
||||
}
|
||||
|
||||
beforeEach(() => {
|
||||
toastAdd = vi.fn()
|
||||
refresh.mockResolvedValue(undefined)
|
||||
transferOwnership.mockResolvedValue({
|
||||
owner: { id: 2, name: 'Jonas Weber' },
|
||||
former_owner: { id: 1, name: 'Marta Alves', role: roleRefFor('admin') },
|
||||
})
|
||||
})
|
||||
|
||||
afterEach(() => {
|
||||
wrapper?.unmount()
|
||||
wrapper = null
|
||||
document.body.innerHTML = ''
|
||||
transferOwnership.mockReset()
|
||||
refresh.mockReset()
|
||||
})
|
||||
|
||||
describe('Pick a member', () => {
|
||||
it('reads the copy and offers accepted users other than the owner, never invites', async () => {
|
||||
await mountDialog()
|
||||
|
||||
expect(dialog().textContent).toContain('Transfer ownership')
|
||||
expect(dialog().textContent).toContain(
|
||||
'Choose who becomes the owner. Only members can be chosen.',
|
||||
)
|
||||
expect(radio('Jonas Weber')).toBeDefined()
|
||||
expect(radio('Lea Martin')).toBeDefined()
|
||||
expect(radio('Marta Alves')).toBeUndefined()
|
||||
expect(radio('pending@renewbuild.example')).toBeUndefined()
|
||||
expect(button('Cancel')).toBeDefined()
|
||||
})
|
||||
|
||||
it('keeps Continue disabled until a member is chosen', async () => {
|
||||
await mountDialog()
|
||||
|
||||
expect(button('Continue')!.disabled).toBe(true)
|
||||
radio('Lea Martin')!.click()
|
||||
await flushPromises()
|
||||
expect(button('Continue')!.disabled).toBe(false)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Confirm', () => {
|
||||
it('names the member, lists the consequences and asks for the email', async () => {
|
||||
await mountDialog()
|
||||
await pickJonas()
|
||||
|
||||
const text = dialog().textContent!
|
||||
expect(text).toContain('Transfer ownership to Jonas Weber')
|
||||
expect(text).toContain('This takes effect straight away.')
|
||||
expect(text).toContain('Jonas Weber becomes the owner.')
|
||||
expect(text).toContain(
|
||||
"You become an Admin. You can't take ownership back unless they transfer it.",
|
||||
)
|
||||
expect(text).toContain(
|
||||
'Only the owner can apply updates, delete a site and change activity retention.',
|
||||
)
|
||||
expect(text).toContain('Your API keys follow your new role.')
|
||||
expect(text).toContain(`Type ${JONAS_EMAIL} to confirm`)
|
||||
expect(text).toContain('The email must match exactly.')
|
||||
})
|
||||
|
||||
it.each([
|
||||
['', true],
|
||||
['jonas@renewbuild', true],
|
||||
['Jonas@renewbuild.example', true],
|
||||
[` ${JONAS_EMAIL}`, true],
|
||||
[`${JONAS_EMAIL} `, true],
|
||||
[JONAS_EMAIL, false],
|
||||
])('gates Transfer ownership on the exact email %j (disabled: %s)', async (typed, disabled) => {
|
||||
await mountDialog()
|
||||
await pickJonas()
|
||||
await type(typed)
|
||||
|
||||
expect(button('Transfer ownership')!.disabled).toBe(disabled)
|
||||
})
|
||||
|
||||
it('goes Back to the pick step with the member still chosen', async () => {
|
||||
await mountDialog()
|
||||
await pickJonas()
|
||||
button('Back')!.click()
|
||||
await flushPromises()
|
||||
|
||||
expect(dialog()).not.toBeNull()
|
||||
expect(dialog().textContent).toContain('Choose who becomes the owner.')
|
||||
expect(radio('Jonas Weber')!.getAttribute('aria-checked')).toBe('true')
|
||||
expect(emitted('update:open')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
|
||||
describe('Transferring', () => {
|
||||
it('sends the transfer, locks the dialog in flight, refreshes the session then the list', async () => {
|
||||
let release!: (value: unknown) => void
|
||||
transferOwnership.mockReturnValue(new Promise((resolve) => (release = resolve)))
|
||||
await mountDialog()
|
||||
await pickJonas()
|
||||
await type(JONAS_EMAIL)
|
||||
button('Transfer ownership')!.click()
|
||||
await flushPromises()
|
||||
|
||||
expect(transferOwnership).toHaveBeenCalledWith(2, JONAS_EMAIL)
|
||||
expect(button('Transferring…')!.disabled).toBe(true)
|
||||
expect(button('Back')!.disabled).toBe(true)
|
||||
|
||||
release({ owner: { id: 2, name: 'Jonas Weber' }, former_owner: { id: 1 } })
|
||||
await flushPromises()
|
||||
|
||||
expect(refresh).toHaveBeenCalledTimes(1)
|
||||
expect(emitted('transferred')).toHaveLength(1)
|
||||
expect(emitted('update:open')?.at(-1)).toEqual([false])
|
||||
expect(toastAdd).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it('400 invalid_transfer returns to the pick step with an inline error and asks for a re-read', async () => {
|
||||
transferOwnership.mockRejectedValue(
|
||||
new ApiError(400, undefined, 'invalid_transfer', { code: 'invalid_transfer' }),
|
||||
)
|
||||
await mountDialog()
|
||||
await submit()
|
||||
|
||||
expect(dialog().textContent).toContain('Choose who becomes the owner.')
|
||||
expect(dialog().querySelector('[role="alert"]')).not.toBeNull()
|
||||
expect(emitted('stale')).toHaveLength(1)
|
||||
expect(emitted('update:open')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('400 confirmation_mismatch shows an error under the field and stays on the step', async () => {
|
||||
transferOwnership.mockRejectedValue(
|
||||
new ApiError(400, undefined, 'confirmation_mismatch', {
|
||||
code: 'confirmation_mismatch',
|
||||
}),
|
||||
)
|
||||
await mountDialog()
|
||||
await submit()
|
||||
|
||||
expect(dialog().textContent).toContain('Transfer ownership to Jonas Weber')
|
||||
expect(field().getAttribute('aria-invalid')).toBe('true')
|
||||
expect(emitted('update:open')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('409 user_role_changed re-reads the session and the list, toasts and closes', async () => {
|
||||
transferOwnership.mockRejectedValue(
|
||||
new ApiError(409, undefined, 'user_role_changed', { code: 'user_role_changed' }),
|
||||
)
|
||||
await mountDialog()
|
||||
await submit()
|
||||
|
||||
expect(refresh).toHaveBeenCalledTimes(1)
|
||||
expect(emitted('stale')).toHaveLength(1)
|
||||
expect(toastAdd).toHaveBeenCalledTimes(1)
|
||||
expect(emitted('update:open')?.at(-1)).toEqual([false])
|
||||
})
|
||||
|
||||
it('403 missing_permission closes and leaves the toast to the API client', async () => {
|
||||
transferOwnership.mockRejectedValue(
|
||||
new ApiError(403, undefined, 'missing_permission', { code: 'missing_permission' }),
|
||||
)
|
||||
await mountDialog()
|
||||
await submit()
|
||||
|
||||
expect(emitted('update:open')?.at(-1)).toEqual([false])
|
||||
expect(toastAdd).not.toHaveBeenCalled()
|
||||
})
|
||||
|
||||
it('403 session_required shows nothing and changes nothing', async () => {
|
||||
transferOwnership.mockRejectedValue(
|
||||
new ApiError(403, undefined, 'This action needs a real session.', {
|
||||
code: 'session_required',
|
||||
}),
|
||||
)
|
||||
await mountDialog()
|
||||
await submit()
|
||||
|
||||
expect(dialog().querySelector('[role="alert"]')).toBeNull()
|
||||
expect(toastAdd).not.toHaveBeenCalled()
|
||||
expect(emitted('update:open')).toBeUndefined()
|
||||
})
|
||||
|
||||
it('500 stays open with an inline error', async () => {
|
||||
transferOwnership.mockRejectedValue(new ApiError(500))
|
||||
await mountDialog()
|
||||
await submit()
|
||||
|
||||
expect(dialog().querySelector('[role="alert"]')).not.toBeNull()
|
||||
expect(emitted('update:open')).toBeUndefined()
|
||||
})
|
||||
})
|
||||
Reference in new issue
Block a user