Files
RayLab-Core/tests/authorization.service.spec.ts
2026-08-02 17:44:50 +07:00

35 lines
1.4 KiB
TypeScript

import { AuthorizationService, PERMISSION_CACHE } from '../src/modules/authorization/authorization.service';
describe('AuthorizationService', () => {
let service: AuthorizationService;
const mockPrisma: any = { $queryRaw: jest.fn(), $queryRawUnsafe: jest.fn() };
const mockCache: any = { get: jest.fn(), set: jest.fn(), invalidate: jest.fn() };
beforeEach(() => {
service = new AuthorizationService(mockPrisma as any, mockCache as any);
});
test('getUserPermissions uses cache when available', async () => {
mockCache.get.mockResolvedValue(['users.read']);
const perms = await service.getUserPermissions('uid');
expect(perms).toEqual(['users.read']);
expect(mockCache.get).toHaveBeenCalledWith('uid');
});
test('getUserPermissions queries DB and sets cache when missing', async () => {
mockCache.get.mockResolvedValue(null);
mockPrisma.$queryRaw = jest.fn().mockResolvedValue([{ code: 'users.read' }]);
const perms = await service.getUserPermissions('uid');
expect(perms).toEqual(['users.read']);
expect(mockCache.set).toHaveBeenCalledWith('uid', ['users.read']);
});
test('hasPermission returns correct boolean', async () => {
jest.spyOn(service, 'getUserPermissions' as any).mockResolvedValue(['users.read']);
const ok = await service.hasPermission('uid', 'users.read');
expect(ok).toBe(true);
const nok = await service.hasPermission('uid', 'users.delete');
expect(nok).toBe(false);
});
});