- IMedioRepository, ISeccionRepository interfaces - MediosQuery, SeccionesQuery common records - TipoSeccion static AllowedTipos helper - Medios: 6 use cases (Create/Update/Deactivate/Reactivate/List/GetById) with validators, handlers and DTOs - Secciones: 6 use cases mirroring Medios; Create validates MedioId active via IMedioRepository - 52 unit tests (xUnit + NSubstitute) all green; audit LogAsync asserted per mutating handler - DI registrations for all 12 handlers and validators auto-scanned via AddValidatorsFromAssemblyContaining
82 lines
2.8 KiB
C#
82 lines
2.8 KiB
C#
using NSubstitute;
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
|
using SIGCM2.Application.Audit;
|
|
using SIGCM2.Application.Secciones.Deactivate;
|
|
using SIGCM2.Domain.Entities;
|
|
using SIGCM2.Domain.Exceptions;
|
|
|
|
namespace SIGCM2.Application.Tests.Secciones.Deactivate;
|
|
|
|
public class DeactivateSeccionCommandHandlerTests
|
|
{
|
|
private readonly ISeccionRepository _repo = Substitute.For<ISeccionRepository>();
|
|
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
|
private readonly DeactivateSeccionCommandHandler _handler;
|
|
|
|
private static Seccion MakeSeccion(int id = 1, bool activo = true)
|
|
=> new(id, 1, "COD" + id, "Nombre", "clasificados", activo, DateTime.UtcNow, null);
|
|
|
|
public DeactivateSeccionCommandHandlerTests()
|
|
{
|
|
_handler = new DeactivateSeccionCommandHandler(_repo, _audit);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_NotFound_ThrowsSeccionNotFoundException()
|
|
{
|
|
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((Seccion?)null);
|
|
|
|
await Assert.ThrowsAsync<SeccionNotFoundException>(
|
|
() => _handler.Handle(new DeactivateSeccionCommand(999)));
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_AlreadyInactive_IsIdempotentAndDoesNotCallUpdateAsync()
|
|
{
|
|
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, false));
|
|
|
|
await _handler.Handle(new DeactivateSeccionCommand(1));
|
|
|
|
await _repo.DidNotReceive().UpdateAsync(Arg.Any<Seccion>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_AlreadyInactive_DoesNotWriteAuditEvent()
|
|
{
|
|
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, false));
|
|
|
|
await _handler.Handle(new DeactivateSeccionCommand(1));
|
|
|
|
await _audit.DidNotReceive().LogAsync(
|
|
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
|
|
Arg.Any<object?>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_ActiveSeccion_CallsUpdateAsyncWithInactiveEntity()
|
|
{
|
|
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, true));
|
|
|
|
await _handler.Handle(new DeactivateSeccionCommand(1));
|
|
|
|
await _repo.Received(1).UpdateAsync(
|
|
Arg.Is<Seccion>(s => !s.Activo),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_ActiveSeccion_WritesAuditWithDeactivateAction()
|
|
{
|
|
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, true));
|
|
|
|
await _handler.Handle(new DeactivateSeccionCommand(1));
|
|
|
|
await _audit.Received(1).LogAsync(
|
|
action: "seccion.deactivate",
|
|
targetType: "Seccion",
|
|
targetId: "1",
|
|
metadata: Arg.Any<object?>(),
|
|
ct: Arg.Any<CancellationToken>());
|
|
}
|
|
}
|