"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const testing_1 = require("@nestjs/testing");
const core_1 = require("@nestjs/core");
const rbac_controller_1 = require("../rbac.controller");
const rbac_service_1 = require("../services/rbac.service");
describe('RbacController', () => {
    let controller;
    let service;
    const mockRequestUser = {
        id: 'user-123',
        sub: 'user-123',
        tenant_id: 'tenant-123',
        email: 'test@example.com',
        role: 'admin',
    };
    const mockRole = {
        id: 'role-123',
        tenant_id: 'tenant-123',
        name: 'Admin',
        description: 'Administrator role',
        is_system: false,
        created_at: new Date('2026-01-01'),
    };
    const mockPermission = {
        id: 'perm-123',
        key: 'users:read',
        name: 'Read Users',
        description: 'Can read users',
        category: 'users',
    };
    const mockUserRole = {
        id: 'ur-123',
        user_id: 'user-123',
        role_id: 'role-123',
        tenant_id: 'tenant-123',
        role: mockRole,
    };
    beforeEach(async () => {
        const module = await testing_1.Test.createTestingModule({
            controllers: [rbac_controller_1.RbacController],
            providers: [
                {
                    provide: rbac_service_1.RbacService,
                    useValue: {
                        findAllRoles: jest.fn(),
                        getRoleWithPermissions: jest.fn(),
                        createRole: jest.fn(),
                        updateRole: jest.fn(),
                        deleteRole: jest.fn(),
                        findAllPermissions: jest.fn(),
                        findPermissionsByCategory: jest.fn(),
                        getUserRoles: jest.fn(),
                        getUserPermissions: jest.fn(),
                        assignRoleToUser: jest.fn(),
                        removeRoleFromUser: jest.fn(),
                        userHasPermission: jest.fn().mockResolvedValue(true),
                        userHasAnyPermission: jest.fn().mockResolvedValue(true),
                    },
                },
                core_1.Reflector,
            ],
        }).compile();
        controller = module.get(rbac_controller_1.RbacController);
        service = module.get(rbac_service_1.RbacService);
    });
    afterEach(() => {
        jest.clearAllMocks();
    });
    describe('findAllRoles', () => {
        it('should return all roles for tenant', async () => {
            service.findAllRoles.mockResolvedValue([mockRole]);
            const result = await controller.findAllRoles(mockRequestUser);
            expect(result).toEqual([mockRole]);
            expect(service.findAllRoles).toHaveBeenCalledWith('tenant-123');
        });
    });
    describe('findRoleById', () => {
        it('should return role with permissions', async () => {
            const roleWithPerms = { ...mockRole, permissions: [mockPermission] };
            service.getRoleWithPermissions.mockResolvedValue(roleWithPerms);
            const result = await controller.findRoleById('role-123', mockRequestUser);
            expect(result).toEqual(roleWithPerms);
            expect(service.getRoleWithPermissions).toHaveBeenCalledWith('role-123', 'tenant-123');
        });
    });
    describe('createRole', () => {
        it('should create a role', async () => {
            const dto = { name: 'New Role', description: 'A new role' };
            service.createRole.mockResolvedValue(mockRole);
            const result = await controller.createRole(dto, mockRequestUser);
            expect(result).toEqual(mockRole);
            expect(service.createRole).toHaveBeenCalledWith(dto, 'tenant-123');
        });
    });
    describe('updateRole', () => {
        it('should update a role', async () => {
            const dto = { name: 'Updated Role' };
            const updated = { ...mockRole, name: 'Updated Role' };
            service.updateRole.mockResolvedValue(updated);
            const result = await controller.updateRole('role-123', dto, mockRequestUser);
            expect(result.name).toBe('Updated Role');
            expect(service.updateRole).toHaveBeenCalledWith('role-123', dto, 'tenant-123');
        });
    });
    describe('deleteRole', () => {
        it('should delete a role', async () => {
            service.deleteRole.mockResolvedValue(undefined);
            const result = await controller.deleteRole('role-123', mockRequestUser);
            expect(result.message).toBe('Role eliminado correctamente');
            expect(service.deleteRole).toHaveBeenCalledWith('role-123', 'tenant-123');
        });
    });
    describe('findAllPermissions', () => {
        it('should return all permissions', async () => {
            service.findAllPermissions.mockResolvedValue([mockPermission]);
            const result = await controller.findAllPermissions();
            expect(result).toEqual([mockPermission]);
            expect(service.findAllPermissions).toHaveBeenCalled();
        });
    });
    describe('findPermissionsByCategory', () => {
        it('should return permissions by category', async () => {
            service.findPermissionsByCategory.mockResolvedValue([mockPermission]);
            const result = await controller.findPermissionsByCategory('users');
            expect(result).toEqual([mockPermission]);
            expect(service.findPermissionsByCategory).toHaveBeenCalledWith('users');
        });
    });
    describe('getUserRoles', () => {
        it('should return user roles', async () => {
            service.getUserRoles.mockResolvedValue([mockUserRole]);
            const result = await controller.getUserRoles('user-456', mockRequestUser);
            expect(result).toEqual([mockUserRole]);
            expect(service.getUserRoles).toHaveBeenCalledWith('user-456', 'tenant-123');
        });
    });
    describe('getUserPermissions', () => {
        it('should return user permissions', async () => {
            const permissions = [mockPermission, { ...mockPermission, id: 'perm-456', key: 'users:write' }];
            service.getUserPermissions.mockResolvedValue(permissions);
            const result = await controller.getUserPermissions('user-456', mockRequestUser);
            expect(result).toEqual(permissions);
            expect(service.getUserPermissions).toHaveBeenCalledWith('user-456', 'tenant-123');
        });
    });
    describe('assignRoleToUser', () => {
        it('should assign role to user', async () => {
            const dto = { user_id: 'user-456', role_id: 'role-123' };
            service.assignRoleToUser.mockResolvedValue(mockUserRole);
            const result = await controller.assignRoleToUser(dto, mockRequestUser);
            expect(result).toEqual(mockUserRole);
            expect(service.assignRoleToUser).toHaveBeenCalledWith(dto, 'tenant-123', 'user-123');
        });
    });
    describe('removeRoleFromUser', () => {
        it('should remove role from user', async () => {
            service.removeRoleFromUser.mockResolvedValue(undefined);
            const result = await controller.removeRoleFromUser('user-456', 'role-123', mockRequestUser);
            expect(result.message).toBe('Role removido correctamente');
            expect(service.removeRoleFromUser).toHaveBeenCalledWith('user-456', 'role-123', 'tenant-123');
        });
    });
    describe('checkPermission', () => {
        it('should return true when user has permission', async () => {
            service.userHasPermission.mockResolvedValue(true);
            const result = await controller.checkPermission('users:read', mockRequestUser);
            expect(result).toEqual({ hasPermission: true });
            expect(service.userHasPermission).toHaveBeenCalledWith('user-123', 'tenant-123', 'users:read');
        });
        it('should return false when user lacks permission', async () => {
            service.userHasPermission.mockResolvedValue(false);
            const result = await controller.checkPermission('admin:delete', mockRequestUser);
            expect(result).toEqual({ hasPermission: false });
        });
    });
    describe('getMyRoles', () => {
        it('should return current user roles', async () => {
            service.getUserRoles.mockResolvedValue([mockUserRole]);
            const result = await controller.getMyRoles(mockRequestUser);
            expect(result).toEqual([mockUserRole]);
            expect(service.getUserRoles).toHaveBeenCalledWith('user-123', 'tenant-123');
        });
    });
    describe('getMyPermissions', () => {
        it('should return current user permissions', async () => {
            const permissions = [mockPermission, { ...mockPermission, id: 'perm-456', key: 'roles:read' }];
            service.getUserPermissions.mockResolvedValue(permissions);
            const result = await controller.getMyPermissions(mockRequestUser);
            expect(result).toEqual(permissions);
            expect(service.getUserPermissions).toHaveBeenCalledWith('user-123', 'tenant-123');
        });
    });
});
//# sourceMappingURL=rbac.controller.spec.js.map