357 lines
11 KiB
TypeScript
357 lines
11 KiB
TypeScript
|
|
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>(ServicioController);
|
||
|
|
service = module.get<ServicioService>(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);
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|
||
|
|
});
|