2026-07-30 17:59:43 +08:00
|
|
|
import { beforeEach, describe, expect, it, vi } from 'vitest'
|
|
|
|
|
import { http } from '@/api/http'
|
|
|
|
|
import {
|
2026-08-01 12:17:33 +08:00
|
|
|
assignPlatformInternalUserRoles,
|
|
|
|
|
changePlatformInternalUserStatus,
|
|
|
|
|
changePlatformPermissionStatus,
|
2026-07-31 13:43:16 +08:00
|
|
|
changePlatformRoleStatus,
|
2026-07-30 17:59:43 +08:00
|
|
|
createPlatformPermission,
|
|
|
|
|
createPlatformRole,
|
2026-08-01 12:17:33 +08:00
|
|
|
deletePlatformPermission,
|
2026-07-31 13:43:16 +08:00
|
|
|
deletePlatformRole,
|
2026-07-30 17:59:43 +08:00
|
|
|
downloadTenantQualification,
|
2026-08-01 12:17:33 +08:00
|
|
|
listPlatformTenants,
|
2026-07-30 17:59:43 +08:00
|
|
|
listTenantQualifications,
|
2026-07-31 13:43:16 +08:00
|
|
|
listCurrentPlatformPermissionCodes,
|
2026-07-30 17:59:43 +08:00
|
|
|
listPlatformPermissions,
|
|
|
|
|
listPlatformRoles,
|
2026-08-01 12:17:33 +08:00
|
|
|
listPlatformInternalUsers,
|
2026-07-30 17:59:43 +08:00
|
|
|
qualificationFileName,
|
2026-07-31 13:43:16 +08:00
|
|
|
replacePlatformRolePermissions,
|
2026-08-01 12:17:33 +08:00
|
|
|
resetPlatformInternalUserPassword,
|
|
|
|
|
updatePlatformInternalUser,
|
|
|
|
|
updatePlatformPermission,
|
2026-07-31 13:43:16 +08:00
|
|
|
updatePlatformRole,
|
2026-07-30 17:59:43 +08:00
|
|
|
type PlatformPermissionResource,
|
2026-08-01 12:17:33 +08:00
|
|
|
type PlatformInternalUser,
|
2026-07-30 17:59:43 +08:00
|
|
|
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',
|
2026-08-01 12:17:33 +08:00
|
|
|
builtIn: false,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const internalUser: PlatformInternalUser = {
|
|
|
|
|
membershipId: 3000001,
|
|
|
|
|
userId: 2000001,
|
|
|
|
|
authUserId: 2000001,
|
|
|
|
|
username: 'platform_admin',
|
|
|
|
|
displayName: '平台管理员',
|
|
|
|
|
phone: '13800138000',
|
|
|
|
|
email: 'admin@example.com',
|
|
|
|
|
memberType: 'INTERNAL',
|
|
|
|
|
memberNo: 'PLATFORM-0001',
|
|
|
|
|
status: 'ACTIVE',
|
|
|
|
|
roleIds: [4000001],
|
2026-07-30 17:59:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 () => {
|
2026-08-01 12:17:33 +08:00
|
|
|
vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: [role] })
|
2026-07-30 17:59:43 +08:00
|
|
|
|
|
|
|
|
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: '企业资料与入驻审核',
|
|
|
|
|
}
|
2026-08-01 12:17:33 +08:00
|
|
|
vi.mocked(http.post).mockResolvedValue({ code: '200', message: 'success', data: role })
|
2026-07-30 17:59:43 +08:00
|
|
|
|
|
|
|
|
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 () => {
|
2026-08-01 12:17:33 +08:00
|
|
|
vi.mocked(http.put).mockResolvedValue({ code: '200', message: 'success', data: role })
|
|
|
|
|
vi.mocked(http.delete).mockResolvedValue({ code: '200', message: 'success', data: null })
|
2026-07-31 13:43:16 +08:00
|
|
|
|
|
|
|
|
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 () => {
|
2026-08-01 12:17:33 +08:00
|
|
|
vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: [permission] })
|
|
|
|
|
vi.mocked(http.post).mockResolvedValue({ code: '200', message: 'success', data: permission })
|
2026-07-30 17:59:43 +08:00
|
|
|
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-08-01 12:17:33 +08:00
|
|
|
it('updates, changes status, and deletes custom permission resources with IAM routes', async () => {
|
|
|
|
|
vi.mocked(http.put).mockResolvedValue({ code: '200', message: 'success', data: permission })
|
|
|
|
|
vi.mocked(http.delete).mockResolvedValue({ code: '200', message: 'success', data: null })
|
|
|
|
|
const payload = { name: '企业客户', parentId: permission.parentId, routePath: '/customers', httpMethod: null, sortNo: 10 }
|
|
|
|
|
|
|
|
|
|
await expect(updatePlatformPermission(permission.id, payload)).resolves.toEqual(permission)
|
|
|
|
|
await expect(changePlatformPermissionStatus(permission.id, 'DISABLED')).resolves.toEqual(permission)
|
|
|
|
|
await expect(deletePlatformPermission(permission.id)).resolves.toBeNull()
|
|
|
|
|
|
|
|
|
|
expect(http.put).toHaveBeenNthCalledWith(1, `/platform/v1/iam/permissions/${permission.id}`, payload)
|
|
|
|
|
expect(http.put).toHaveBeenNthCalledWith(2, `/platform/v1/iam/permissions/${permission.id}/status`, { status: 'DISABLED' })
|
|
|
|
|
expect(http.delete).toHaveBeenCalledWith(`/platform/v1/iam/permissions/${permission.id}`)
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
it('maintains internal users through their public membership ID', async () => {
|
|
|
|
|
vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: [internalUser] })
|
|
|
|
|
vi.mocked(http.put).mockResolvedValue({ code: '200', message: 'success', data: internalUser })
|
|
|
|
|
const update = {
|
|
|
|
|
displayName: '平台管理员',
|
|
|
|
|
phone: '13800138000',
|
|
|
|
|
email: 'admin@example.com',
|
|
|
|
|
memberNo: 'PLATFORM-0001',
|
|
|
|
|
roleIds: [4000001],
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await expect(listPlatformInternalUsers()).resolves.toEqual([internalUser])
|
|
|
|
|
await expect(updatePlatformInternalUser(internalUser.membershipId, update)).resolves.toEqual(internalUser)
|
|
|
|
|
await expect(changePlatformInternalUserStatus(internalUser.membershipId, 'DISABLED')).resolves.toEqual(internalUser)
|
|
|
|
|
await expect(resetPlatformInternalUserPassword(internalUser.membershipId, 'ChangeMe123')).resolves.toEqual(internalUser)
|
|
|
|
|
await expect(assignPlatformInternalUserRoles(internalUser.membershipId, [4000001])).resolves.toEqual(internalUser)
|
|
|
|
|
|
|
|
|
|
expect(http.get).toHaveBeenCalledWith('/platform/v1/iam/internal-users')
|
|
|
|
|
expect(http.put).toHaveBeenNthCalledWith(1, `/platform/v1/iam/internal-users/${internalUser.membershipId}`, update)
|
|
|
|
|
expect(http.put).toHaveBeenNthCalledWith(2, `/platform/v1/iam/internal-users/${internalUser.membershipId}/status`, { status: 'DISABLED' })
|
|
|
|
|
expect(http.put).toHaveBeenNthCalledWith(3, `/platform/v1/iam/internal-users/${internalUser.membershipId}/password`, { newPassword: 'ChangeMe123' })
|
|
|
|
|
expect(http.put).toHaveBeenNthCalledWith(4, `/platform/v1/iam/internal-users/${internalUser.membershipId}/roles`, { roleIds: [4000001] })
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-31 13:43:16 +08:00
|
|
|
it('loads the current users permission snapshot without calling the role-management endpoint', async () => {
|
2026-08-01 12:17:33 +08:00
|
|
|
vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: ['platform:warehouse:list'] })
|
2026-07-31 13:43:16 +08:00
|
|
|
|
|
|
|
|
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,
|
|
|
|
|
}
|
2026-08-01 12:17:33 +08:00
|
|
|
vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: [qualification] })
|
2026-07-30 17:59:43 +08:00
|
|
|
|
|
|
|
|
await expect(listTenantQualifications(8)).resolves.toEqual([qualification])
|
|
|
|
|
expect(http.get).toHaveBeenCalledWith('/platform/v1/iam/tenants/8/qualifications')
|
|
|
|
|
})
|
|
|
|
|
|
2026-08-01 12:17:33 +08:00
|
|
|
it('loads paginated tenants with the documented filters', async () => {
|
|
|
|
|
const page = { records: [], page: 2, size: 10, total: 11, pages: 2 }
|
|
|
|
|
vi.mocked(http.get).mockResolvedValue({ code: '200', message: 'success', data: page })
|
|
|
|
|
|
|
|
|
|
await expect(listPlatformTenants({ page: 2, size: 10, keyword: '柏悦', status: 'ACTIVE' })).resolves.toEqual(page)
|
|
|
|
|
expect(http.get).toHaveBeenCalledWith('/platform/v1/iam/tenants', {
|
|
|
|
|
params: { page: 2, size: 10, keyword: '柏悦', status: 'ACTIVE' },
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
|
2026-07-30 17:59:43 +08:00
|
|
|
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('资质文件地址无效')
|
|
|
|
|
})
|
|
|
|
|
})
|