99 lines
3.9 KiB
C#
99 lines
3.9 KiB
C#
|
|
using NSubstitute;
|
||
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
||
|
|
using SIGCM2.Application.Audit;
|
||
|
|
using SIGCM2.Application.PuntosDeVenta.Deactivate;
|
||
|
|
using SIGCM2.Application.PuntosDeVenta.Reactivate;
|
||
|
|
using SIGCM2.Domain.Entities;
|
||
|
|
using SIGCM2.Domain.Exceptions;
|
||
|
|
|
||
|
|
namespace SIGCM2.Application.Tests.PuntosDeVenta.Reactivate;
|
||
|
|
|
||
|
|
public class ReactivatePuntoDeVentaCommandHandlerTests
|
||
|
|
{
|
||
|
|
private readonly IPuntoDeVentaRepository _repo = Substitute.For<IPuntoDeVentaRepository>();
|
||
|
|
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
|
||
|
|
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
||
|
|
private readonly ReactivatePuntoDeVentaCommandHandler _handler;
|
||
|
|
|
||
|
|
private static PuntoDeVenta MakePdv(int id = 10, int medioId = 5, bool activo = false)
|
||
|
|
=> new(id, medioId, 1, "PdV Test", null, activo, DateTime.UtcNow, null);
|
||
|
|
|
||
|
|
private static Medio MakeMedio(int id = 5, bool activo = true)
|
||
|
|
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
|
||
|
|
|
||
|
|
public ReactivatePuntoDeVentaCommandHandlerTests()
|
||
|
|
{
|
||
|
|
_handler = new ReactivatePuntoDeVentaCommandHandler(_repo, _medioRepo, _audit);
|
||
|
|
_medioRepo.GetByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(MakeMedio(5, true));
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Handle_NotFound_ThrowsPuntoDeVentaNotFoundException()
|
||
|
|
{
|
||
|
|
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((PuntoDeVenta?)null);
|
||
|
|
|
||
|
|
await Assert.ThrowsAsync<PuntoDeVentaNotFoundException>(
|
||
|
|
() => _handler.Handle(new ReactivatePuntoDeVentaCommand(999)));
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Handle_MedioInactivo_ThrowsMedioInactivoException()
|
||
|
|
{
|
||
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||
|
|
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
|
||
|
|
|
||
|
|
await Assert.ThrowsAsync<MedioInactivoException>(
|
||
|
|
() => _handler.Handle(new ReactivatePuntoDeVentaCommand(10)));
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Handle_AlreadyActive_IsIdempotentAndDoesNotCallUpdateAsync()
|
||
|
|
{
|
||
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: true));
|
||
|
|
|
||
|
|
await _handler.Handle(new ReactivatePuntoDeVentaCommand(10));
|
||
|
|
|
||
|
|
await _repo.DidNotReceive().UpdateAsync(Arg.Any<PuntoDeVenta>(), Arg.Any<CancellationToken>());
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Handle_InactivePdv_CallsUpdateAsyncWithActiveEntity()
|
||
|
|
{
|
||
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||
|
|
|
||
|
|
await _handler.Handle(new ReactivatePuntoDeVentaCommand(10));
|
||
|
|
|
||
|
|
await _repo.Received(1).UpdateAsync(
|
||
|
|
Arg.Is<PuntoDeVenta>(p => p.Activo),
|
||
|
|
Arg.Any<CancellationToken>());
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Handle_InactivePdv_WritesAuditWithReactivateAction()
|
||
|
|
{
|
||
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||
|
|
|
||
|
|
await _handler.Handle(new ReactivatePuntoDeVentaCommand(10));
|
||
|
|
|
||
|
|
await _audit.Received(1).LogAsync(
|
||
|
|
action: "punto_de_venta.reactivate",
|
||
|
|
targetType: "PuntoDeVenta",
|
||
|
|
targetId: "10",
|
||
|
|
metadata: Arg.Any<object?>(),
|
||
|
|
ct: Arg.Any<CancellationToken>());
|
||
|
|
}
|
||
|
|
|
||
|
|
[Fact]
|
||
|
|
public async Task Handle_MedioInactivo_NoAuditLogged()
|
||
|
|
{
|
||
|
|
_repo.GetByIdAsync(10, Arg.Any<CancellationToken>()).Returns(MakePdv(10, activo: false));
|
||
|
|
_medioRepo.GetByIdAsync(5, Arg.Any<CancellationToken>()).Returns(MakeMedio(5, false));
|
||
|
|
|
||
|
|
try { await _handler.Handle(new ReactivatePuntoDeVentaCommand(10)); } catch (MedioInactivoException) { }
|
||
|
|
|
||
|
|
await _audit.DidNotReceive().LogAsync(
|
||
|
|
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
|
||
|
|
Arg.Any<object?>(), Arg.Any<CancellationToken>());
|
||
|
|
}
|
||
|
|
}
|