Files
SIG-CM2.0/tests/SIGCM2.Application.Tests/Secciones/Update/UpdateSeccionCommandHandlerTests.cs
dmolinari 9bc191c3ae test(udt-011): T400.40 — update tests for TimeProvider injection and explicit now params
Fix all test compilation errors caused by T400.10/T400.20/T400.30:
- Handler constructors: add TimeProvider.System as last argument
- Domain mutator calls: add DateTime.UtcNow as explicit 'now' argument
- AuditLogger/SecurityEventLogger Build() helpers: add TimeProvider.System
- JwtService test constructors: add TimeProvider.System
Cat2 coverage already present in TimeProviderArgentinaExtensionsTests.cs:
FakeTimeProvider proves GetArgentinaToday() returns ART civil date, not UTC.
2026-04-18 10:12:32 -03:00

95 lines
3.5 KiB
C#

using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Secciones.Update;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.Secciones.Update;
public class UpdateSeccionCommandHandlerTests
{
private readonly ISeccionRepository _repo = Substitute.For<ISeccionRepository>();
private readonly IMedioRepository _medioRepo = Substitute.For<IMedioRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly UpdateSeccionCommandHandler _handler;
private static Seccion MakeSeccion(int id = 1, string nombre = "Original", string tipo = "clasificados")
=> new(id, 1, "COD" + id, nombre, tipo, true, DateTime.UtcNow, null);
private static Medio MakeMedio(int id = 1, bool activo = true)
=> new(id, "COD" + id, "Medio " + id, TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
private static UpdateSeccionCommand ValidCommand(int id = 1) => new(
Id: id,
Nombre: "Nuevo Nombre",
Tipo: "notables");
public UpdateSeccionCommandHandlerTests()
{
_handler = new UpdateSeccionCommandHandler(_repo, _medioRepo, _audit, TimeProvider.System);
// Default: medio is active
_medioRepo.GetByIdAsync(Arg.Any<int>(), Arg.Any<CancellationToken>()).Returns(MakeMedio(1, true));
}
[Fact]
public async Task Handle_NotFound_ThrowsSeccionNotFoundException()
{
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((Seccion?)null);
await Assert.ThrowsAsync<SeccionNotFoundException>(
() => _handler.Handle(new UpdateSeccionCommand(999, "X", "clasificados")));
}
[Fact]
public async Task Handle_HappyPath_CallsUpdateAsyncOnce()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1));
await _handler.Handle(ValidCommand(1));
await _repo.Received(1).UpdateAsync(Arg.Any<Seccion>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_HappyPath_ReturnsDtoWithUpdatedFields()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1, "Original", "clasificados"));
var result = await _handler.Handle(ValidCommand(1));
Assert.Equal(1, result.Id);
Assert.Equal("Nuevo Nombre", result.Nombre);
Assert.Equal("notables", result.Tipo);
}
[Fact]
public async Task Handle_HappyPath_CallsAuditWithUpdateAction()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1));
await _handler.Handle(ValidCommand(1));
await _audit.Received(1).LogAsync(
action: "seccion.update",
targetType: "Seccion",
targetId: "1",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_MedioInactivo_ThrowsMedioInactivoExceptionAndNoAuditLogged()
{
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeSeccion(1));
_medioRepo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeMedio(1, activo: false));
await Assert.ThrowsAsync<MedioInactivoException>(
() => _handler.Handle(ValidCommand(1)));
await _audit.DidNotReceive().LogAsync(
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
Arg.Any<object?>(), Arg.Any<CancellationToken>());
}
}