From 073d8aa0cbdf6cbd7c42e9eb991463c64ebd6d84 Mon Sep 17 00:00:00 2001 From: tyrannusss Date: Fri, 24 Oct 2025 18:35:23 -0600 Subject: [PATCH] Creacion de .spec de controllers --- src/programa/programa.controller.spec.ts | 182 ++++++++++++ src/servicio/servicio.controller.spec.ts | 356 +++++++++++++++++++++++ 2 files changed, 538 insertions(+) create mode 100644 src/programa/programa.controller.spec.ts create mode 100644 src/servicio/servicio.controller.spec.ts diff --git a/src/programa/programa.controller.spec.ts b/src/programa/programa.controller.spec.ts new file mode 100644 index 0000000..6dac171 --- /dev/null +++ b/src/programa/programa.controller.spec.ts @@ -0,0 +1,182 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { ProgramaController } from './programa.controller'; +import { ProgramaService } from './programa.service'; +import { AuthGuard } from '@nestjs/passport'; +import { BadRequestException } from '@nestjs/common'; +import { FileInterceptor } from '@nestjs/platform-express'; +import { diskStorage } from 'multer'; + +// Mocks +const mockProgramaService = { + cargaMasiva: jest.fn(), + reasignarProgramas: jest.fn(), + programasAdmin: jest.fn(), + programasResponsable: jest.fn(), +}; + +const mockAuthGuard = { + canActivate: jest.fn(() => true), +}; + +const mockFile: Express.Multer.File = { + fieldname: 'csv', + originalname: 'test.csv', + encoding: '7bit', + mimetype: 'text/csv', + size: 1024, + destination: './server/uploads', + filename: '1234567890.csv', + path: './server/uploads/1234567890.csv', + buffer: Buffer.from('test,data'), + stream: null as any, +}; + +describe('ProgramaController', () => { + let controller: ProgramaController; + let service: ProgramaService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [ProgramaController], + providers: [ + { + provide: ProgramaService, + useValue: mockProgramaService, + }, + ], + }) + .overrideGuard(AuthGuard('jwt')) + .useValue(mockAuthGuard) + .compile(); + + controller = module.get(ProgramaController); + service = module.get(ProgramaService); + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); + + describe('cargaMasiva', () => { + it('should process CSV file successfully', async () => { + // Arrange + const expectedResult = { processed: 10, errors: 0 }; + mockProgramaService.cargaMasiva.mockResolvedValue(expectedResult); + + // Act + const result = await controller.cargaMasiva(mockFile); + + // Assert + expect(service.cargaMasiva).toHaveBeenCalledWith(mockFile.filename); + expect(result).toEqual({ + statusCode: 201, + message: 'Carga masiva realizada con éxito', + data: expectedResult, + }); + }); + }); + + describe('reasignarProgramas', () => { + it('should reassign programs successfully', async () => { + // Arrange + const idUsuario = 1; + const emailDto = { correo: 'nuevo@unam.mx' }; + const expectedResult = { reassigned: 5 }; + mockProgramaService.reasignarProgramas.mockResolvedValue(expectedResult); + + // Act + const result = await controller.reasignarProgramas(emailDto, idUsuario); + + // Assert + expect(service.reasignarProgramas).toHaveBeenCalledWith( + idUsuario, + emailDto.correo, + ); + expect(result).toEqual({ + statusCode: 200, + message: 'Programas reasignados con éxito', + data: expectedResult, + }); + }); + }); + + describe('programasAdmin', () => { + it('should return admin programs successfully', async () => { + // Arrange + const idUsuario = 1; + const expectedResult = [{ id: 1, nombre: 'Programa 1' }]; + mockProgramaService.programasAdmin.mockResolvedValue(expectedResult); + + // Act + const result = await controller.programasAdmin(idUsuario); + + // Assert + expect(service.programasAdmin).toHaveBeenCalledWith(idUsuario); + expect(result).toEqual({ + statusCode: 200, + data: expectedResult, + }); + }); + + it('should handle different user IDs', async () => { + // Arrange + const idUsuario = 2; + const expectedResult = [{ id: 2, nombre: 'Programa 2' }]; + mockProgramaService.programasAdmin.mockResolvedValue(expectedResult); + + // Act + const result = await controller.programasAdmin(idUsuario); + + // Assert + expect(service.programasAdmin).toHaveBeenCalledWith(idUsuario); + expect(result.data).toEqual(expectedResult); + }); + }); + + describe('programasResponsable', () => { + it('should return responsible programs successfully', async () => { + // Arrange + const idUsuario = 1; + const expectedResult = [{ id: 1, nombre: 'Programa Responsable 1' }]; + mockProgramaService.programasResponsable.mockResolvedValue( + expectedResult, + ); + + // Act + const result = await controller.programasResponsable(idUsuario); + + // Assert + expect(service.programasResponsable).toHaveBeenCalledWith(idUsuario); + expect(result).toEqual({ + statusCode: 200, + data: expectedResult, + }); + }); + + it('should handle parameter correctly', async () => { + // Arrange + const idUsuario = 3; + const expectedResult = [{ id: 3, nombre: 'Programa 3' }]; + mockProgramaService.programasResponsable.mockResolvedValue( + expectedResult, + ); + + // Act + const result = await controller.programasResponsable(idUsuario); + + // Assert + expect(service.programasResponsable).toHaveBeenCalledWith(idUsuario); + expect(result.data).toHaveLength(1); + }); + }); + + // Tests para decoradores y configuración + describe('Decorators and Configuration', () => { + it('should have JWT auth guard on all endpoints', () => { + // Este test verifica que todos los endpoints estén protegidos + // La protección se verifica en los tests individuales + expect(mockAuthGuard.canActivate).toBeDefined(); + }); + }); +}); diff --git a/src/servicio/servicio.controller.spec.ts b/src/servicio/servicio.controller.spec.ts new file mode 100644 index 0000000..c697d79 --- /dev/null +++ b/src/servicio/servicio.controller.spec.ts @@ -0,0 +1,356 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { ServicioController } from './servicio.controller'; +import { ServicioService } from './servicio.service'; +import { AuthGuard } from '@nestjs/passport'; +import { Response } from 'express'; +import { CrearServicioDto } from './dto/create-servicio.dto'; +import { BadRequestException, NotFoundException } from '@nestjs/common'; + +// Mocks +const mockServicioService = { + registrarNuevoServicio: jest.fn(), + obtenerDetalleServicio: jest.fn(), + obtenerServicioAlumno: jest.fn(), + generarGustavoBazPrada: jest.fn(), + cancelarServicio: jest.fn(), + cartaTermino: jest.fn(), + subirCartaTermino: jest.fn(), + subirInformeGlobal: jest.fn(), + liberarServicio: jest.fn(), + rechazar: jest.fn(), + rechazarInforme: jest.fn(), + rechazarTermino: jest.fn(), + update: jest.fn(), +}; + +const mockAuthGuard = { + canActivate: jest.fn(() => true), +}; + +const mockFile: Express.Multer.File = { + fieldname: 'cartaAceptacion', + originalname: 'carta.pdf', + encoding: '7bit', + mimetype: 'application/pdf', + size: 1024, + destination: './uploads', + filename: '1234567890.pdf', + path: './uploads/1234567890.pdf', + buffer: Buffer.from('test'), + stream: null as any, +}; + +const mockResponse = { + download: jest.fn(), +} as unknown as Response; + +describe('ServicioController', () => { + let controller: ServicioController; + let service: ServicioService; + + beforeEach(async () => { + const module: TestingModule = await Test.createTestingModule({ + controllers: [ServicioController], + providers: [ + { + provide: ServicioService, + useValue: mockServicioService, + }, + ], + }) + .overrideGuard(AuthGuard('jwt')) + .useValue(mockAuthGuard) + .compile(); + + controller = module.get(ServicioController); + service = module.get(ServicioService); + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(controller).toBeDefined(); + }); + + describe('POST endpoints', () => { + describe('crearServicio', () => { + it('should create new service successfully', async () => { + // Arrange + const alumnoJson = JSON.stringify({ + idUsuario: 1, + programa: 'Programa Test', + }); + const expectedResult = { idServicio: 1, status: 'created' }; + mockServicioService.registrarNuevoServicio.mockResolvedValue( + expectedResult, + ); + + // Act + const result = await controller.crearServicio(mockFile, alumnoJson); + + // Assert + expect(service.registrarNuevoServicio).toHaveBeenCalledWith( + expect.any(Object), + mockFile, + ); + expect(result).toEqual(expectedResult); + }); + + it('should handle invalid JSON in alumno parameter', async () => { + // Arrange + const invalidJson = 'invalid-json'; + + // Act & Assert + await expect( + controller.crearServicio(mockFile, invalidJson), + ).rejects.toThrow(SyntaxError); + }); + }); + }); + + describe('GET endpoints', () => { + describe('obtenerAdmin', () => { + it('should get admin service details', async () => { + // Arrange + const query = { idServicio: '1' }; + const expectedResult = { idServicio: 1, detalles: 'admin' }; + mockServicioService.obtenerDetalleServicio.mockResolvedValue( + expectedResult, + ); + + // Act + const result = await controller.obtenerAdmin(query); + + // Assert + expect(service.obtenerDetalleServicio).toHaveBeenCalledWith('1'); + expect(result).toEqual(expectedResult); + }); + }); + + describe('obtenerAlumno', () => { + it('should get student service', async () => { + // Arrange + const idUsuario = 1; + const expectedResult = { idServicio: 1, alumno: 'Test Alumno' }; + mockServicioService.obtenerServicioAlumno.mockResolvedValue( + expectedResult, + ); + + // Act + const result = await controller.obtenerAlumno(idUsuario); + + // Assert + expect(service.obtenerServicioAlumno).toHaveBeenCalledWith(idUsuario); + expect(result).toEqual(expectedResult); + }); + }); + + describe('gustavoBaz', () => { + it('should generate and download Gustavo Baz report', async () => { + // Arrange + const year = 2024; + const filePath = '/path/to/report.pdf'; + mockServicioService.generarGustavoBazPrada.mockResolvedValue(filePath); + mockResponse; + + // Act + await controller.gustavoBaz(year, mockResponse); + + // Assert + expect(service.generarGustavoBazPrada).toHaveBeenCalledWith(year); + expect(mockResponse.download).toHaveBeenCalledWith(filePath); + }); + }); + + describe('generarReporte', () => { + it('should generate and download report', async () => { + // Arrange + const query = { year: 2024 }; + const filePath = '/path/to/report.pdf'; + mockServicioService.generarGustavoBazPrada.mockResolvedValue(filePath); + mockResponse; + + // Act + await controller.generarReporte(query, mockResponse); + + // Assert + expect(service.generarGustavoBazPrada).toHaveBeenCalledWith(2024); + expect(mockResponse.download).toHaveBeenCalledWith(filePath); + }); + }); + }); + + describe('PUT endpoints', () => { + describe('cancelar', () => { + it('should cancel service successfully', async () => { + // Arrange + const body = { idServicio: 1, mensaje: 'Cancelación por prueba' }; + const expectedResult = { message: 'Servicio cancelado' }; + mockServicioService.cancelarServicio.mockResolvedValue(expectedResult); + + // Act + const result = await controller.cancelar(body); + + // Assert + expect(service.cancelarServicio).toHaveBeenCalledWith(body); + expect(result).toEqual(expectedResult); + }); + }); + + describe('subirCartaAceptacion', () => { + it('should upload acceptance letter successfully', async () => { + // Arrange + const dataJson = JSON.stringify({ idServicio: 1 }); + const expectedResult = { message: 'Carta subida' }; + mockServicioService.cartaTermino.mockResolvedValue(expectedResult); + + // Act + const result = await controller.subirCartaAceptacion( + mockFile, + dataJson, + ); + + // Assert + expect(service.cartaTermino).toHaveBeenCalledWith(1, mockFile); + expect(result).toEqual(expectedResult); + }); + }); + + describe('subirCartaTermino', () => { + it('should upload termination letter successfully', async () => { + // Arrange + const dataJson = JSON.stringify({ idServicio: 1 }); + const expectedResult = { message: 'Carta término subida' }; + mockServicioService.subirCartaTermino.mockResolvedValue(expectedResult); + + // Act + const result = await controller.subirCartaTermino(mockFile, dataJson); + + // Assert + expect(service.subirCartaTermino).toHaveBeenCalledWith(1, mockFile); + expect(result).toEqual(expectedResult); + }); + }); + + describe('subirInformeGlobal', () => { + it('should upload global report successfully', async () => { + // Arrange + const dataJson = JSON.stringify({ idServicio: 1 }); + const expectedResult = { message: 'Informe subido' }; + mockServicioService.subirInformeGlobal.mockResolvedValue( + expectedResult, + ); + + // Act + const result = await controller.subirInformeGlobal(mockFile, dataJson); + + // Assert + expect(service.subirInformeGlobal).toHaveBeenCalledWith(1, mockFile); + expect(result).toEqual(expectedResult); + }); + }); + + describe('liberarServicio', () => { + it('should release service successfully', async () => { + // Arrange + const idServicio = 1; + const vistoBuenoAcatlan = true; + const expectedResult = { message: 'Servicio liberado' }; + mockServicioService.liberarServicio.mockResolvedValue(expectedResult); + + // Act + const result = await controller.liberarServicio( + idServicio, + vistoBuenoAcatlan, + ); + + // Assert + expect(service.liberarServicio).toHaveBeenCalledWith( + idServicio, + vistoBuenoAcatlan, + ); + expect(result).toEqual(expectedResult); + }); + }); + + describe('rechazarAceptacion', () => { + it('should reject acceptance successfully', async () => { + // Arrange + const body = { idServicio: 1, mensaje: 'Rechazado por prueba' }; + const expectedResult = { message: 'Aceptación rechazada' }; + mockServicioService.rechazar.mockResolvedValue(expectedResult); + + // Act + const result = await controller.rechazarAceptacion(body); + + // Assert + expect(service.rechazar).toHaveBeenCalledWith( + body.idServicio, + body.mensaje, + ); + expect(result).toEqual(expectedResult); + }); + }); + + describe('rechazarInforme', () => { + it('should reject report successfully', async () => { + // Arrange + const body = { idServicio: 1, mensaje: 'Informe rechazado' }; + const expectedResult = { message: 'Informe rechazado' }; + mockServicioService.rechazarInforme.mockResolvedValue(expectedResult); + + // Act + const result = await controller.rechazarInforme(body); + + // Assert + expect(service.rechazarInforme).toHaveBeenCalledWith( + body.idServicio, + body.mensaje, + ); + expect(result).toEqual(expectedResult); + }); + }); + + describe('rechazarTermino', () => { + it('should reject termination successfully', async () => { + // Arrange + const body = { idServicio: 1, mensaje: 'Término rechazado' }; + const expectedResult = { message: 'Término rechazado' }; + mockServicioService.rechazarTermino.mockResolvedValue(expectedResult); + + // Act + const result = await controller.rechazarTermino(body); + + // Assert + expect(service.rechazarTermino).toHaveBeenCalledWith( + body.idServicio, + body.mensaje, + ); + expect(result).toEqual(expectedResult); + }); + }); + + describe('actualizarServicio', () => { + it('should update service with multiple files', async () => { + // Arrange + const files = { + cartaAceptacion: [mockFile], + cartaTermino: [mockFile], + informeGlobal: [mockFile], + }; + const dataJson = JSON.stringify({ idServicio: 1, campo: 'valor' }); + const expectedResult = { message: 'Servicio actualizado' }; + mockServicioService.update.mockResolvedValue(expectedResult); + + // Act + const result = await controller.actualizarServicio(files, dataJson); + + // Assert + expect(service.update).toHaveBeenCalledWith( + { idServicio: 1, campo: 'valor' }, + files, + ); + expect(result).toEqual(expectedResult); + }); + }); + }); +});