- 文件上传验证: ModelUploader组件增强、上传API错误处理 - GLTF解析器: 增强节点提取和元数据 - 板材编辑: PanelEditor客户端验证 + panels API服务端验证 - 装配步骤编辑: 修复未使用代码,添加预览按钮 - 装配动画播放: AssemblyPlayer步骤导航边界检查优化 - 分享链接: 完整CRUD API(创建、获取、删除),含验证和过期检查 跳过可选测试任务以加快MVP开发
528 lines
16 KiB
TypeScript
528 lines
16 KiB
TypeScript
/**
|
|
* 权限系统单元测试
|
|
* 测试所有权限检查函数
|
|
*/
|
|
|
|
import {
|
|
canEditProduct,
|
|
canDeleteProduct,
|
|
canEditSKU,
|
|
canTransferSKU,
|
|
canManageUser,
|
|
canHandleFeedback,
|
|
canManageCollaborators,
|
|
canViewProduct,
|
|
canCreateProduct,
|
|
canManageDepartment,
|
|
canManageTeam,
|
|
} from '@/lib/permissions';
|
|
|
|
describe('权限系统测试', () => {
|
|
describe('canEditProduct - 产品编辑权限', () => {
|
|
it('超级管理员可以编辑任何产品', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN', id: 'user1' };
|
|
const product = { createdBy: 'user2', collaborators: [] };
|
|
|
|
expect(canEditProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以编辑任何产品', () => {
|
|
const userInfo = { role: 'ADMIN', id: 'user1' };
|
|
const product = { createdBy: 'user2', collaborators: [] };
|
|
|
|
expect(canEditProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('创建者可以编辑自己的产品', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = { createdBy: 'user1', collaborators: [] };
|
|
|
|
expect(canEditProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('编辑协作者可以编辑产品', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = {
|
|
createdBy: 'user2',
|
|
collaborators: [{ userId: 'user1', role: 'editor' }],
|
|
};
|
|
|
|
expect(canEditProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('查看协作者不能编辑产品', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = {
|
|
createdBy: 'user2',
|
|
collaborators: [{ userId: 'user1', role: 'viewer' }],
|
|
};
|
|
|
|
expect(canEditProduct(userInfo, product)).toBe(false);
|
|
});
|
|
|
|
it('查看者角色不能编辑任何产品', () => {
|
|
const userInfo = { role: 'VIEWER', id: 'user1' };
|
|
const product = { createdBy: 'user1', collaborators: [] };
|
|
|
|
expect(canEditProduct(userInfo, product)).toBe(false);
|
|
});
|
|
|
|
it('非相关用户不能编辑产品', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = { createdBy: 'user2', collaborators: [] };
|
|
|
|
expect(canEditProduct(userInfo, product)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canDeleteProduct - 产品删除权限', () => {
|
|
it('超级管理员可以删除任何产品', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN', id: 'user1' };
|
|
const product = { createdBy: 'user2' };
|
|
|
|
expect(canDeleteProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以删除任何产品', () => {
|
|
const userInfo = { role: 'ADMIN', id: 'user1' };
|
|
const product = { createdBy: 'user2' };
|
|
|
|
expect(canDeleteProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('创建者可以删除自己的产品', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = { createdBy: 'user1' };
|
|
|
|
expect(canDeleteProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('协作者不能删除产品', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = { createdBy: 'user2' };
|
|
|
|
expect(canDeleteProduct(userInfo, product)).toBe(false);
|
|
});
|
|
|
|
it('查看者不能删除产品', () => {
|
|
const userInfo = { role: 'VIEWER', id: 'user1' };
|
|
const product = { createdBy: 'user1' };
|
|
|
|
expect(canDeleteProduct(userInfo, product)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canEditSKU - SKU 编辑权限', () => {
|
|
it('超级管理员可以编辑任何 SKU', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN', id: 'user1' };
|
|
const sku = { ownerId: 'user2' };
|
|
const product = { createdBy: 'user2', collaborators: [] };
|
|
|
|
expect(canEditSKU(userInfo, sku, product)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以编辑任何 SKU', () => {
|
|
const userInfo = { role: 'ADMIN', id: 'user1' };
|
|
const sku = { ownerId: 'user2' };
|
|
const product = { createdBy: 'user2', collaborators: [] };
|
|
|
|
expect(canEditSKU(userInfo, sku, product)).toBe(true);
|
|
});
|
|
|
|
it('SKU 负责人可以编辑自己的 SKU', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const sku = { ownerId: 'user1' };
|
|
const product = { createdBy: 'user2', collaborators: [] };
|
|
|
|
expect(canEditSKU(userInfo, sku, product)).toBe(true);
|
|
});
|
|
|
|
it('产品创建者可以编辑产品下的 SKU', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const sku = { ownerId: 'user2' };
|
|
const product = { createdBy: 'user1', collaborators: [] };
|
|
|
|
expect(canEditSKU(userInfo, sku, product)).toBe(true);
|
|
});
|
|
|
|
it('产品编辑协作者可以编辑 SKU', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const sku = { ownerId: 'user2' };
|
|
const product = {
|
|
createdBy: 'user3',
|
|
collaborators: [{ userId: 'user1', role: 'editor' }],
|
|
};
|
|
|
|
expect(canEditSKU(userInfo, sku, product)).toBe(true);
|
|
});
|
|
|
|
it('产品查看协作者不能编辑 SKU', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const sku = { ownerId: 'user2' };
|
|
const product = {
|
|
createdBy: 'user3',
|
|
collaborators: [{ userId: 'user1', role: 'viewer' }],
|
|
};
|
|
|
|
expect(canEditSKU(userInfo, sku, product)).toBe(false);
|
|
});
|
|
|
|
it('查看者不能编辑 SKU', () => {
|
|
const userInfo = { role: 'VIEWER', id: 'user1' };
|
|
const sku = { ownerId: 'user1' };
|
|
const product = { createdBy: 'user1', collaborators: [] };
|
|
|
|
expect(canEditSKU(userInfo, sku, product)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canTransferSKU - SKU 交接权限', () => {
|
|
it('超级管理员可以交接任何 SKU', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN', id: 'user1' };
|
|
const sku = { ownerId: 'user2' };
|
|
|
|
expect(canTransferSKU(userInfo, sku)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以交接任何 SKU', () => {
|
|
const userInfo = { role: 'ADMIN', id: 'user1' };
|
|
const sku = { ownerId: 'user2' };
|
|
|
|
expect(canTransferSKU(userInfo, sku)).toBe(true);
|
|
});
|
|
|
|
it('部门管理员可以交接本部门的 SKU', () => {
|
|
const userInfo = {
|
|
role: 'DEPARTMENT_MANAGER',
|
|
id: 'user1',
|
|
departmentId: 'dept1',
|
|
};
|
|
const sku = { ownerId: 'user2' };
|
|
const owner = { departmentId: 'dept1' };
|
|
|
|
expect(canTransferSKU(userInfo, sku, owner)).toBe(true);
|
|
});
|
|
|
|
it('部门管理员不能交接其他部门的 SKU', () => {
|
|
const userInfo = {
|
|
role: 'DEPARTMENT_MANAGER',
|
|
id: 'user1',
|
|
departmentId: 'dept1',
|
|
};
|
|
const sku = { ownerId: 'user2' };
|
|
const owner = { departmentId: 'dept2' };
|
|
|
|
expect(canTransferSKU(userInfo, sku, owner)).toBe(false);
|
|
});
|
|
|
|
it('SKU 负责人可以交接自己的 SKU', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const sku = { ownerId: 'user1' };
|
|
|
|
expect(canTransferSKU(userInfo, sku)).toBe(true);
|
|
});
|
|
|
|
it('非负责人不能交接 SKU', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const sku = { ownerId: 'user2' };
|
|
|
|
expect(canTransferSKU(userInfo, sku)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canManageUser - 用户管理权限', () => {
|
|
it('超级管理员可以管理任何用户', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN', id: 'user1' };
|
|
const targetUser = { role: 'ADMIN', id: 'user2' };
|
|
|
|
expect(canManageUser(userInfo, targetUser)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以管理非超级管理员用户', () => {
|
|
const userInfo = { role: 'ADMIN', id: 'user1' };
|
|
const targetUser = { role: 'DESIGNER', id: 'user2' };
|
|
|
|
expect(canManageUser(userInfo, targetUser)).toBe(true);
|
|
});
|
|
|
|
it('管理员不能管理超级管理员', () => {
|
|
const userInfo = { role: 'ADMIN', id: 'user1' };
|
|
const targetUser = { role: 'SUPER_ADMIN', id: 'user2' };
|
|
|
|
expect(canManageUser(userInfo, targetUser)).toBe(false);
|
|
});
|
|
|
|
it('部门管理员可以管理本部门用户', () => {
|
|
const userInfo = {
|
|
role: 'DEPARTMENT_MANAGER',
|
|
id: 'user1',
|
|
departmentId: 'dept1',
|
|
};
|
|
const targetUser = {
|
|
role: 'DESIGNER',
|
|
id: 'user2',
|
|
departmentId: 'dept1',
|
|
};
|
|
|
|
expect(canManageUser(userInfo, targetUser)).toBe(true);
|
|
});
|
|
|
|
it('部门管理员不能管理其他部门用户', () => {
|
|
const userInfo = {
|
|
role: 'DEPARTMENT_MANAGER',
|
|
id: 'user1',
|
|
departmentId: 'dept1',
|
|
};
|
|
const targetUser = {
|
|
role: 'DESIGNER',
|
|
id: 'user2',
|
|
departmentId: 'dept2',
|
|
};
|
|
|
|
expect(canManageUser(userInfo, targetUser)).toBe(false);
|
|
});
|
|
|
|
it('普通用户不能管理其他用户', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const targetUser = { role: 'DESIGNER', id: 'user2' };
|
|
|
|
expect(canManageUser(userInfo, targetUser)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canHandleFeedback - 反馈处理权限', () => {
|
|
it('超级管理员可以处理任何反馈', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN', id: 'user1' };
|
|
const feedback = { assignedTo: 'user2' };
|
|
|
|
expect(canHandleFeedback(userInfo, feedback)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以处理任何反馈', () => {
|
|
const userInfo = { role: 'ADMIN', id: 'user1' };
|
|
const feedback = { assignedTo: 'user2' };
|
|
|
|
expect(canHandleFeedback(userInfo, feedback)).toBe(true);
|
|
});
|
|
|
|
it('被分配者可以处理反馈', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const feedback = { assignedTo: 'user1' };
|
|
|
|
expect(canHandleFeedback(userInfo, feedback)).toBe(true);
|
|
});
|
|
|
|
it('非被分配者不能处理反馈', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const feedback = { assignedTo: 'user2' };
|
|
|
|
expect(canHandleFeedback(userInfo, feedback)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canManageCollaborators - 协作者管理权限', () => {
|
|
it('超级管理员可以管理任何产品的协作者', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN', id: 'user1' };
|
|
const product = { createdBy: 'user2' };
|
|
|
|
expect(canManageCollaborators(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以管理任何产品的协作者', () => {
|
|
const userInfo = { role: 'ADMIN', id: 'user1' };
|
|
const product = { createdBy: 'user2' };
|
|
|
|
expect(canManageCollaborators(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('产品创建者可以管理协作者', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = { createdBy: 'user1' };
|
|
|
|
expect(canManageCollaborators(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('协作者不能管理协作者', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = { createdBy: 'user2' };
|
|
|
|
expect(canManageCollaborators(userInfo, product)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canViewProduct - 产品查看权限', () => {
|
|
it('超级管理员可以查看任何产品', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN', id: 'user1' };
|
|
const product = {
|
|
createdBy: 'user2',
|
|
visibility: 'PRIVATE',
|
|
collaborators: [],
|
|
};
|
|
|
|
expect(canViewProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以查看任何产品', () => {
|
|
const userInfo = { role: 'ADMIN', id: 'user1' };
|
|
const product = {
|
|
createdBy: 'user2',
|
|
visibility: 'PRIVATE',
|
|
collaborators: [],
|
|
};
|
|
|
|
expect(canViewProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('任何人都可以查看公开产品', () => {
|
|
const userInfo = { role: 'VIEWER', id: 'user1' };
|
|
const product = {
|
|
createdBy: 'user2',
|
|
visibility: 'PUBLIC',
|
|
collaborators: [],
|
|
};
|
|
|
|
expect(canViewProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('创建者可以查看自己的私有产品', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = {
|
|
createdBy: 'user1',
|
|
visibility: 'PRIVATE',
|
|
collaborators: [],
|
|
};
|
|
|
|
expect(canViewProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('协作者可以查看产品', () => {
|
|
const userInfo = { role: 'DESIGNER', id: 'user1' };
|
|
const product = {
|
|
createdBy: 'user2',
|
|
visibility: 'PRIVATE',
|
|
collaborators: [{ userId: 'user1', role: 'viewer' }],
|
|
};
|
|
|
|
expect(canViewProduct(userInfo, product)).toBe(true);
|
|
});
|
|
|
|
it('同部门用户可以查看部门可见产品', () => {
|
|
const userInfo = {
|
|
role: 'DESIGNER',
|
|
id: 'user1',
|
|
departmentId: 'dept1',
|
|
};
|
|
const product = {
|
|
createdBy: 'user2',
|
|
visibility: 'DEPARTMENT',
|
|
collaborators: [],
|
|
};
|
|
const creator = { departmentId: 'dept1' };
|
|
|
|
expect(canViewProduct(userInfo, product, creator)).toBe(true);
|
|
});
|
|
|
|
it('其他部门用户不能查看部门可见产品', () => {
|
|
const userInfo = {
|
|
role: 'DESIGNER',
|
|
id: 'user1',
|
|
departmentId: 'dept1',
|
|
};
|
|
const product = {
|
|
createdBy: 'user2',
|
|
visibility: 'DEPARTMENT',
|
|
collaborators: [],
|
|
};
|
|
const creator = { departmentId: 'dept2' };
|
|
|
|
expect(canViewProduct(userInfo, product, creator)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canCreateProduct - 产品创建权限', () => {
|
|
it('超级管理员可以创建产品', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN' };
|
|
expect(canCreateProduct(userInfo)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以创建产品', () => {
|
|
const userInfo = { role: 'ADMIN' };
|
|
expect(canCreateProduct(userInfo)).toBe(true);
|
|
});
|
|
|
|
it('部门管理员可以创建产品', () => {
|
|
const userInfo = { role: 'DEPARTMENT_MANAGER' };
|
|
expect(canCreateProduct(userInfo)).toBe(true);
|
|
});
|
|
|
|
it('组长可以创建产品', () => {
|
|
const userInfo = { role: 'TEAM_LEADER' };
|
|
expect(canCreateProduct(userInfo)).toBe(true);
|
|
});
|
|
|
|
it('设计师可以创建产品', () => {
|
|
const userInfo = { role: 'DESIGNER' };
|
|
expect(canCreateProduct(userInfo)).toBe(true);
|
|
});
|
|
|
|
it('查看者不能创建产品', () => {
|
|
const userInfo = { role: 'VIEWER' };
|
|
expect(canCreateProduct(userInfo)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canManageDepartment - 部门管理权限', () => {
|
|
it('超级管理员可以管理部门', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN' };
|
|
expect(canManageDepartment(userInfo)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以管理部门', () => {
|
|
const userInfo = { role: 'ADMIN' };
|
|
expect(canManageDepartment(userInfo)).toBe(true);
|
|
});
|
|
|
|
it('其他角色不能管理部门', () => {
|
|
const userInfo = { role: 'DEPARTMENT_MANAGER' };
|
|
expect(canManageDepartment(userInfo)).toBe(false);
|
|
});
|
|
});
|
|
|
|
describe('canManageTeam - 小组管理权限', () => {
|
|
it('超级管理员可以管理小组', () => {
|
|
const userInfo = { role: 'SUPER_ADMIN' };
|
|
expect(canManageTeam(userInfo)).toBe(true);
|
|
});
|
|
|
|
it('管理员可以管理小组', () => {
|
|
const userInfo = { role: 'ADMIN' };
|
|
expect(canManageTeam(userInfo)).toBe(true);
|
|
});
|
|
|
|
it('部门管理员可以管理本部门的小组', () => {
|
|
const userInfo = {
|
|
role: 'DEPARTMENT_MANAGER',
|
|
departmentId: 'dept1',
|
|
};
|
|
const team = { departmentId: 'dept1' };
|
|
|
|
expect(canManageTeam(userInfo, team)).toBe(true);
|
|
});
|
|
|
|
it('部门管理员不能管理其他部门的小组', () => {
|
|
const userInfo = {
|
|
role: 'DEPARTMENT_MANAGER',
|
|
departmentId: 'dept1',
|
|
};
|
|
const team = { departmentId: 'dept2' };
|
|
|
|
expect(canManageTeam(userInfo, team)).toBe(false);
|
|
});
|
|
|
|
it('其他角色不能管理小组', () => {
|
|
const userInfo = { role: 'DESIGNER' };
|
|
expect(canManageTeam(userInfo)).toBe(false);
|
|
});
|
|
});
|
|
});
|