Files
SIG-CM2.0/tests/SIGCM2.Application.Tests/IngresosBrutos/Create/CreateIngresosBrutosCommandHandlerTests.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

87 lines
3.0 KiB
C#

using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.IngresosBrutos.Create;
using SIGCM2.Domain.Fiscal;
using IibbEntity = SIGCM2.Domain.Entities.IngresosBrutos;
namespace SIGCM2.Application.Tests.IngresosBrutos.Create;
public class CreateIngresosBrutosCommandHandlerTests
{
private readonly IIngresosBrutosRepository _repo = Substitute.For<IIngresosBrutosRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly CreateIngresosBrutosCommandHandler _handler;
private static CreateIngresosBrutosCommand ValidCommand() => new(
Provincia: ProvinciaArgentina.BuenosAires,
Descripcion: "IIBB Buenos Aires",
Alicuota: 3.5m,
VigenciaDesde: new DateOnly(2024, 1, 1));
public CreateIngresosBrutosCommandHandlerTests()
{
_handler = new CreateIngresosBrutosCommandHandler(_repo, _audit, TimeProvider.System);
_repo.InsertAsync(Arg.Any<IibbEntity>(), Arg.Any<CancellationToken>()).Returns(55);
}
[Fact]
public async Task Handle_HappyPath_ReturnsDtoWithIdFromRepository()
{
var result = await _handler.Handle(ValidCommand());
Assert.Equal(55, result.Id);
}
[Fact]
public async Task Handle_HappyPath_DtoContainsCorrectFields()
{
var result = await _handler.Handle(ValidCommand());
Assert.Equal(ProvinciaArgentina.BuenosAires, result.Provincia);
Assert.Equal(3.5m, result.Alicuota);
Assert.True(result.Activo);
}
[Fact]
public async Task Handle_HappyPath_CallsAuditWithCreateAction()
{
await _handler.Handle(ValidCommand());
await _audit.Received(1).LogAsync(
action: "ingresos_brutos.create",
targetType: "IngresosBrutos",
targetId: "55",
metadata: Arg.Any<object?>(),
ct: Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_AuditLoggerThrows_ExceptionBubblesUp()
{
_audit.LogAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
Arg.Any<object?>(), Arg.Any<CancellationToken>())
.Returns(Task.FromException(new InvalidOperationException("audit fail")));
await Assert.ThrowsAsync<InvalidOperationException>(
() => _handler.Handle(ValidCommand()));
}
// ── triangulation: zero alicuota ─────────────────────────────────────────
[Fact]
public async Task Handle_WithZeroAlicuota_ReturnsDtoWithCorrectAlicuota()
{
var cmd = new CreateIngresosBrutosCommand(
Provincia: ProvinciaArgentina.CiudadAutonomaDeBuenosAires,
Descripcion: "IIBB CABA",
Alicuota: 0m,
VigenciaDesde: new DateOnly(2024, 1, 1));
var result = await _handler.Handle(cmd);
Assert.Equal(0m, result.Alicuota);
Assert.Equal(ProvinciaArgentina.CiudadAutonomaDeBuenosAires, result.Provincia);
}
}