forked from wangziqi/gongxue-base
58 lines
2.2 KiB
TypeScript
58 lines
2.2 KiB
TypeScript
import { RbacService } from './rbac.service';
|
|
|
|
describe('RbacService seedData', () => {
|
|
it('adds preset permissions to system roles without removing manually granted permissions', async () => {
|
|
const permissions = [
|
|
{ id: 1, code: 'profile:view', name: '查看个人资料', group: 'profile' },
|
|
{ id: 2, code: 'notification:view', name: '查看通知', group: 'notification' },
|
|
{ id: 3, code: 'student:view', name: '查看学生', group: 'student' },
|
|
{ id: 4, code: 'class:view', name: '查看班级', group: 'class' },
|
|
{ id: 5, code: 'schedule:view', name: '查看排课', group: 'schedule' },
|
|
{ id: 6, code: 'attendance:view', name: '查看考勤', group: 'attendance' },
|
|
{ id: 7, code: 'attendance:create', name: '新增考勤', group: 'attendance' },
|
|
{ id: 8, code: 'attendance:export', name: '导出考勤', group: 'attendance' },
|
|
{ id: 9, code: 'room:view', name: '查看宿舍', group: 'room' },
|
|
];
|
|
const teacherRole = {
|
|
id: 1,
|
|
name: '老师',
|
|
description: '查看和管理本班学生',
|
|
isSystem: true,
|
|
permissions: [permissions[8]],
|
|
};
|
|
|
|
const permRepo = {
|
|
findOne: jest.fn(
|
|
async ({ where }: any) => permissions.find((p) => p.code === where.code) ?? null,
|
|
),
|
|
create: jest.fn((value) => value),
|
|
save: jest.fn(async (value) => value),
|
|
find: jest.fn(async () => permissions),
|
|
};
|
|
const roleRepo = {
|
|
findOne: jest.fn(async ({ where }: any) => (where.name === '老师' ? teacherRole : null)),
|
|
create: jest.fn((value) => ({ ...value, permissions: [] })),
|
|
save: jest.fn(async (value) => value),
|
|
find: jest.fn(async () => [teacherRole]),
|
|
};
|
|
const userRepo = { count: jest.fn(async () => 1), create: jest.fn(), save: jest.fn() };
|
|
|
|
const service = new RbacService(
|
|
permRepo as never,
|
|
roleRepo as never,
|
|
userRepo as never,
|
|
{} as never,
|
|
{} as never,
|
|
{} as never,
|
|
{} as never,
|
|
{} as never,
|
|
);
|
|
|
|
await service.seedData();
|
|
|
|
expect(teacherRole.permissions.map((permission) => permission.code)).toEqual(
|
|
expect.arrayContaining(['room:view', 'profile:view', 'student:view', 'attendance:create']),
|
|
);
|
|
});
|
|
});
|