"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const testing_1 = require("@nestjs/testing"); const core_1 = require("@nestjs/core"); const common_1 = require("@nestjs/common"); const tenants_controller_1 = require("../tenants.controller"); const tenants_service_1 = require("../tenants.service"); const rbac_service_1 = require("../../rbac/services/rbac.service"); describe('TenantsController', () => { let controller; let service; const mockTenant = { id: 'tenant-123', name: 'Test Company', slug: 'test-company', domain: 'test.example.com', logo_url: 'https://example.com/logo.png', status: 'active', plan_id: 'plan-123', trial_ends_at: new Date('2026-02-01'), settings: { theme: 'dark' }, metadata: {}, created_at: new Date('2026-01-01'), updated_at: new Date('2026-01-01'), }; const mockUser = { id: 'user-123', email: 'user@example.com', tenant_id: 'tenant-123', }; beforeEach(async () => { const module = await testing_1.Test.createTestingModule({ controllers: [tenants_controller_1.TenantsController], providers: [ { provide: tenants_service_1.TenantsService, useValue: { findOne: jest.fn(), findBySlug: jest.fn(), create: jest.fn(), update: jest.fn(), slugExists: jest.fn(), }, }, { provide: rbac_service_1.RbacService, useValue: { userHasPermission: jest.fn().mockResolvedValue(true), userHasAnyPermission: jest.fn().mockResolvedValue(true), userHasAllPermissions: jest.fn().mockResolvedValue(true), userHasRole: jest.fn().mockResolvedValue(true), }, }, core_1.Reflector, ], }).compile(); controller = module.get(tenants_controller_1.TenantsController); service = module.get(tenants_service_1.TenantsService); }); afterEach(() => { jest.clearAllMocks(); }); describe('findOne', () => { it('should return a tenant by id', async () => { service.findOne.mockResolvedValue(mockTenant); const result = await controller.findOne('tenant-123'); expect(result).toEqual(mockTenant); expect(service.findOne).toHaveBeenCalledWith('tenant-123'); }); it('should propagate NotFoundException from service', async () => { service.findOne.mockRejectedValue(new common_1.NotFoundException('Tenant no encontrado')); await expect(controller.findOne('non-existent')).rejects.toThrow(common_1.NotFoundException); }); }); describe('create', () => { const createDto = { name: 'New Company', slug: 'new-company', domain: 'new.example.com', logo_url: 'https://example.com/logo.png', settings: { timezone: 'America/Mexico_City', industry: 'Technology', }, }; it('should create a new tenant successfully', async () => { const createdTenant = { ...mockTenant, id: 'new-tenant-id', name: createDto.name, slug: createDto.slug, domain: createDto.domain, logo_url: createDto.logo_url, status: 'trial', }; service.create.mockResolvedValue(createdTenant); const result = await controller.create(createDto); expect(result).toEqual(createdTenant); expect(service.create).toHaveBeenCalledWith(createDto); }); it('should propagate ConflictException for duplicate slug', async () => { service.create.mockRejectedValue(new common_1.ConflictException('Ya existe un tenant con este slug')); await expect(controller.create(createDto)).rejects.toThrow(common_1.ConflictException); }); }); describe('getCurrent', () => { it('should return current user tenant', async () => { service.findOne.mockResolvedValue(mockTenant); const result = await controller.getCurrent(mockUser); expect(result).toEqual(mockTenant); expect(service.findOne).toHaveBeenCalledWith(mockUser.tenant_id); }); it('should propagate NotFoundException if tenant not found', async () => { service.findOne.mockRejectedValue(new common_1.NotFoundException('Tenant no encontrado')); await expect(controller.getCurrent(mockUser)).rejects.toThrow(common_1.NotFoundException); }); }); describe('updateCurrent', () => { const updateDto = { name: 'Updated Company', logo_url: 'https://example.com/updated-logo.png', settings: { timezone: 'America/New_York', }, }; it('should update current tenant successfully', async () => { const updatedTenant = { ...mockTenant, name: updateDto.name, logo_url: updateDto.logo_url, settings: { ...mockTenant.settings, ...updateDto.settings }, }; service.update.mockResolvedValue(updatedTenant); const result = await controller.updateCurrent(mockUser, updateDto); expect(result).toEqual(updatedTenant); expect(service.update).toHaveBeenCalledWith(mockUser.tenant_id, updateDto); }); it('should propagate NotFoundException if tenant not found', async () => { service.update.mockRejectedValue(new common_1.NotFoundException('Tenant no encontrado')); await expect(controller.updateCurrent(mockUser, updateDto)).rejects.toThrow(common_1.NotFoundException); }); it('should allow partial updates', async () => { const partialUpdateDto = { name: 'Only Name Updated', }; const updatedTenant = { ...mockTenant, name: partialUpdateDto.name, }; service.update.mockResolvedValue(updatedTenant); const result = await controller.updateCurrent(mockUser, partialUpdateDto); expect(result.name).toBe('Only Name Updated'); expect(service.update).toHaveBeenCalledWith(mockUser.tenant_id, partialUpdateDto); }); }); }); //# sourceMappingURL=tenants.controller.spec.js.map