Files
admin-web/src/api/platformIam.test.ts
T

148 lines
6.0 KiB
TypeScript
Raw Normal View History

2026-07-30 17:59:43 +08:00
import { beforeEach, describe, expect, it, vi } from 'vitest'
import { http } from '@/api/http'
import {
2026-07-31 13:43:16 +08:00
changePlatformRoleStatus,
2026-07-30 17:59:43 +08:00
createPlatformPermission,
createPlatformRole,
2026-07-31 13:43:16 +08:00
deletePlatformRole,
2026-07-30 17:59:43 +08:00
downloadTenantQualification,
listTenantQualifications,
2026-07-31 13:43:16 +08:00
listCurrentPlatformPermissionCodes,
2026-07-30 17:59:43 +08:00
listPlatformPermissions,
listPlatformRoles,
qualificationFileName,
2026-07-31 13:43:16 +08:00
replacePlatformRolePermissions,
updatePlatformRole,
2026-07-30 17:59:43 +08:00
type PlatformPermissionResource,
type PlatformRoleResource,
} from './platformIam'
vi.mock('@/api/http', () => ({
2026-07-31 13:43:16 +08:00
http: { get: vi.fn(), post: vi.fn(), put: vi.fn(), delete: vi.fn() },
2026-07-30 17:59:43 +08:00
unwrapApiResponse: <T>(response: { data: T }) => response.data,
}))
const role: PlatformRoleResource = {
id: 4000002,
tenantId: 0,
appCode: 'PLATFORM',
roleCode: 'CUSTOMER_REVIEWER',
roleName: '企业审核员',
roleType: 'SYSTEM',
status: 'ACTIVE',
description: '企业资料与入驻审核',
2026-07-31 13:43:16 +08:00
permissionIds: ['5100010'],
2026-07-30 17:59:43 +08:00
}
const permission: PlatformPermissionResource = {
id: 5100010,
tenantId: 0,
appCode: 'PLATFORM',
permissionType: 'MENU',
parentId: 5100000,
permissionCode: 'platform:customer:list',
permissionName: '企业客户',
routePath: '/customers',
httpMethod: null,
sortNo: 10,
status: 'ACTIVE',
}
describe('platform IAM roles', () => {
beforeEach(() => {
vi.mocked(http.get).mockReset()
vi.mocked(http.post).mockReset()
2026-07-31 13:43:16 +08:00
vi.mocked(http.put).mockReset()
vi.mocked(http.delete).mockReset()
2026-07-30 17:59:43 +08:00
})
it('loads roles from the platform IAM prefix', async () => {
vi.mocked(http.get).mockResolvedValue({ code: '0', message: 'success', data: [role] })
await expect(listPlatformRoles()).resolves.toEqual([role])
expect(http.get).toHaveBeenCalledWith('/platform/v1/iam/roles')
})
it('creates a platform role with the documented payload', async () => {
const payload = {
roleCode: 'CUSTOMER_REVIEWER',
roleName: '企业审核员',
description: '企业资料与入驻审核',
}
vi.mocked(http.post).mockResolvedValue({ code: '0', message: 'success', data: role })
await expect(createPlatformRole(payload)).resolves.toEqual(role)
expect(http.post).toHaveBeenCalledWith('/platform/v1/iam/roles', payload)
})
2026-07-31 13:43:16 +08:00
it('updates status, permissions, and deletes roles with the implemented IAM routes', async () => {
vi.mocked(http.put).mockResolvedValue({ code: '0', message: 'success', data: role })
vi.mocked(http.delete).mockResolvedValue({ code: '0', message: 'success', data: null })
await expect(updatePlatformRole(role.id, { roleName: '审核主管', description: '负责企业审核' })).resolves.toEqual(role)
await expect(changePlatformRoleStatus(role.id, 'DISABLED')).resolves.toEqual(role)
await expect(replacePlatformRolePermissions(role.id, ['5100010'])).resolves.toEqual(role)
await expect(deletePlatformRole(role.id)).resolves.toBeNull()
expect(http.put).toHaveBeenNthCalledWith(1, `/platform/v1/iam/roles/${role.id}`, { roleName: '审核主管', description: '负责企业审核' })
expect(http.put).toHaveBeenNthCalledWith(2, `/platform/v1/iam/roles/${role.id}/status`, { status: 'DISABLED' })
expect(http.put).toHaveBeenNthCalledWith(3, '/platform/v1/iam/roles/permissions', { roleId: role.id, permissionIds: ['5100010'] })
expect(http.delete).toHaveBeenCalledWith(`/platform/v1/iam/roles/${role.id}`)
})
2026-07-30 17:59:43 +08:00
it('loads and creates permission resources under the platform IAM prefix', async () => {
vi.mocked(http.get).mockResolvedValue({ code: '0', message: 'success', data: [permission] })
vi.mocked(http.post).mockResolvedValue({ code: '0', message: 'success', data: permission })
const payload = {
type: 'MENU' as const,
code: permission.permissionCode,
name: permission.permissionName,
parentId: permission.parentId,
routePath: permission.routePath,
httpMethod: null,
sortNo: permission.sortNo,
}
await expect(listPlatformPermissions()).resolves.toEqual([permission])
await expect(createPlatformPermission(payload)).resolves.toEqual(permission)
expect(http.get).toHaveBeenCalledWith('/platform/v1/iam/permissions')
expect(http.post).toHaveBeenCalledWith('/platform/v1/iam/permissions', payload)
})
2026-07-31 13:43:16 +08:00
it('loads the current users permission snapshot without calling the role-management endpoint', async () => {
vi.mocked(http.get).mockResolvedValue({ code: '0', message: 'success', data: ['platform:warehouse:list'] })
await expect(listCurrentPlatformPermissionCodes()).resolves.toEqual(['platform:warehouse:list'])
expect(http.get).toHaveBeenCalledWith('/platform/v1/iam/authorization/permissions')
})
2026-07-30 17:59:43 +08:00
it('loads qualifications for the selected tenant', async () => {
const qualification = {
id: 12,
tenantId: 8,
qualificationType: 'BUSINESS_LICENSE',
certificateNo: '91440300XXXXXXXXXX',
documentUrl: '/api/merchant/v1/files/qualifications/license.png',
status: 'PENDING' as const,
reviewComment: null,
}
vi.mocked(http.get).mockResolvedValue({ code: '0', message: 'success', data: [qualification] })
await expect(listTenantQualifications(8)).resolves.toEqual([qualification])
expect(http.get).toHaveBeenCalledWith('/platform/v1/iam/tenants/8/qualifications')
})
it('downloads a qualification with the platform file endpoint and a safe filename', async () => {
const blob = new Blob(['image'], { type: 'image/png' })
vi.mocked(http.get).mockResolvedValue(blob)
expect(qualificationFileName('/api/merchant/v1/files/qualifications/%E8%90%A5%E4%B8%9A%E6%89%A7%E7%85%A7.png?token=ignored')).toBe('营业执照.png')
await expect(downloadTenantQualification(8, '/api/merchant/v1/files/qualifications/%E8%90%A5%E4%B8%9A%E6%89%A7%E7%85%A7.png')).resolves.toBe(blob)
expect(http.get).toHaveBeenCalledWith(
'/platform/v1/files/qualifications/8/%E8%90%A5%E4%B8%9A%E6%89%A7%E7%85%A7.png',
{ responseType: 'blob' },
)
expect(() => qualificationFileName('/api/merchant/v1/files/qualifications/%2Fetc%2Fpasswd')).toThrow('资质文件地址无效')
})
})