Files
admin-web/src/modules/iam/pages/RolesView.test.ts
T

141 lines
6.0 KiB
TypeScript
Raw Normal View History

2026-07-30 17:59:43 +08:00
import { createPinia, setActivePinia } from 'pinia'
2026-08-12 14:08:33 +08:00
import { readFileSync } from 'node:fs'
import { resolve } from 'node:path'
2026-07-30 17:59:43 +08:00
import { flushPromises, shallowMount } from '@vue/test-utils'
import { beforeEach, describe, expect, it, vi } from 'vitest'
2026-08-03 18:49:18 +08:00
import { createPlatformRole, listPlatformInternalUsers, listPlatformPermissions, listPlatformRoles, type PlatformRoleResource } from '@/modules/iam/api/platformIam'
import { useAuthStore } from '@/core/auth/auth'
2026-07-30 17:59:43 +08:00
import RolesView from './RolesView.vue'
2026-08-12 14:08:33 +08:00
const rolesSource = readFileSync(resolve(process.cwd(), 'src/modules/iam/pages/RolesView.vue'), 'utf8')
2026-08-03 18:49:18 +08:00
vi.mock('@/modules/iam/api/platformIam', () => ({
2026-07-30 17:59:43 +08:00
createPlatformRole: vi.fn(),
2026-07-31 15:34:35 +08:00
listPlatformInternalUsers: vi.fn(),
2026-07-31 13:43:16 +08:00
listPlatformPermissions: vi.fn(),
2026-07-30 17:59:43 +08:00
listPlatformRoles: vi.fn(),
2026-07-31 13:43:16 +08:00
updatePlatformRole: vi.fn(),
changePlatformRoleStatus: vi.fn(),
deletePlatformRole: vi.fn(),
replacePlatformRolePermissions: vi.fn(),
2026-07-30 17:59:43 +08:00
}))
const platformRole: PlatformRoleResource = {
id: 4000002,
tenantId: 0,
appCode: 'PLATFORM',
roleCode: 'CUSTOMER_REVIEWER',
roleName: '企业审核员',
roleType: 'SYSTEM',
status: 'ACTIVE',
description: '企业资料与入驻审核',
}
function mountView(pinia = createPinia()) {
setActivePinia(pinia)
2026-07-31 15:34:35 +08:00
const auth = useAuthStore()
if (auth.permissions.length === 0) auth.permissions = ['platform:role:list', 'platform:role:create', 'platform:role:grant']
2026-07-30 17:59:43 +08:00
return shallowMount(RolesView, {
global: {
plugins: [pinia],
directives: { loading: () => undefined },
stubs: {
2026-08-10 15:36:53 +08:00
AdminPage: { template: '<main><slot /></main>' },
ListPageLayout: { template: '<section><slot name="toolbar" /><slot name="state" /><slot /></section>' },
FormSection: { template: '<section><slot /></section>' },
2026-08-12 16:58:52 +08:00
AppDialog: { template: '<section><slot /><slot name="footer" /></section>' },
2026-08-10 15:36:53 +08:00
LoadErrorState: { props: ['message'], template: '<div role="alert">{{ message }}<slot name="details" /></div>' },
2026-08-10 16:24:54 +08:00
DataTable: { props: ['data'], template: '<section><slot name="toolbar-actions" /><span data-testid="row-count">{{ data.length }}</span></section>' },
2026-07-30 17:59:43 +08:00
'el-alert': { props: ['title'], template: '<div role="alert">{{ title }}<slot name="title" /></div>' },
'el-input': true,
'el-option': true,
'el-select': true,
'el-button': true,
'el-table': true,
'el-table-column': true,
'el-tag': true,
'el-radio-group': true,
'el-radio-button': true,
'el-drawer': {
props: ['modelValue'],
template: '<section><slot /><slot name="footer" /></section>',
},
'el-form': { template: '<form><slot /></form>' },
'el-form-item': { template: '<label><slot /></label>' },
'el-tree': {
props: { checkStrictly: Boolean },
template: '<div data-testid="permission-tree" :data-check-strictly="String(checkStrictly)" />',
},
},
},
})
}
describe('RolesView', () => {
beforeEach(() => {
setActivePinia(createPinia())
vi.mocked(listPlatformRoles).mockReset()
2026-07-31 13:43:16 +08:00
vi.mocked(listPlatformPermissions).mockReset()
2026-07-31 15:34:35 +08:00
vi.mocked(listPlatformInternalUsers).mockReset()
2026-07-31 13:43:16 +08:00
vi.mocked(listPlatformPermissions).mockResolvedValue([])
2026-07-31 15:34:35 +08:00
vi.mocked(listPlatformInternalUsers).mockResolvedValue([])
2026-07-30 17:59:43 +08:00
vi.mocked(createPlatformRole).mockReset()
})
it('keeps directory, menu, and action permissions independently checkable', () => {
const wrapper = mountView()
expect(wrapper.get('[data-testid="permission-tree"]').attributes('data-check-strictly')).toBe('true')
})
2026-08-12 14:08:33 +08:00
it('keeps the role code available but hidden from the default list layout', () => {
expect(rolesSource).toContain("{ key: 'roleCode', prop: 'code', label: '角色编码', minWidth: 180, showOverflowTooltip: true, defaultVisible: false }")
expect(rolesSource).toContain('<el-form-item label="角色编码"')
expect(rolesSource).not.toContain('<small>{{ (row as Role).code }}</small>')
expect(rolesSource).toContain('aria-label="平台角色列表"')
})
2026-08-12 16:58:52 +08:00
it('uses the prototype-sized centered role editor with responsive split panes', () => {
expect(rolesSource).toContain('<AppDialog v-model="editorOpen"')
expect(rolesSource).toContain('width="min(860px, calc(100vw - 28px))"')
expect(rolesSource).not.toContain('<AppDrawer v-model="editorOpen"')
expect(rolesSource).toContain('grid-template-columns: minmax(0, 0.9fr) minmax(0, 1.3fr)')
expect(rolesSource).toContain('.role-editor-layout { grid-template-columns: minmax(0, 1fr); }')
expect(rolesSource).toContain(':global(.role-editor-dialog .el-dialog__body) { min-height: 0; overflow-y: auto; }')
})
2026-07-30 17:59:43 +08:00
it('loads platform roles from IAM for a server session', async () => {
const pinia = createPinia()
setActivePinia(pinia)
const auth = useAuthStore()
auth.isServerSession = true
auth.permissions = ['platform:role:list', 'platform:role:create']
vi.mocked(listPlatformRoles).mockResolvedValue([platformRole])
const wrapper = mountView(pinia)
await flushPromises()
expect(listPlatformRoles).toHaveBeenCalledTimes(1)
2026-07-31 15:34:35 +08:00
expect(listPlatformInternalUsers).toHaveBeenCalledTimes(1)
2026-08-10 16:24:54 +08:00
expect(wrapper.get('[data-testid="row-count"]').text()).toBe('1')
expect(wrapper.text()).not.toContain('角色负责聚合菜单、操作和数据范围,内部用户可分配多个角色')
2026-07-30 17:59:43 +08:00
expect(wrapper.find('[data-testid="permission-tree"]').exists()).toBe(false)
})
it('keeps a failed IAM role list recoverable', async () => {
const pinia = createPinia()
setActivePinia(pinia)
const auth = useAuthStore()
auth.isServerSession = true
auth.permissions = ['platform:role:list']
vi.mocked(listPlatformRoles).mockRejectedValue(new Error('IAM unavailable'))
const wrapper = mountView(pinia)
await flushPromises()
2026-08-12 12:35:32 +08:00
const alertText = wrapper.findAll('[role="alert"]').map((item) => item.text()).join(' ')
expect(alertText).toContain('平台角色列表加载失败,请稍后重试')
expect(alertText).not.toContain('IAM unavailable')
2026-07-30 17:59:43 +08:00
})
})