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.
89 lines
3.4 KiB
C#
89 lines
3.4 KiB
C#
using NSubstitute;
|
|
using SIGCM2.Application.Abstractions.Persistence;
|
|
using SIGCM2.Application.Audit;
|
|
using SIGCM2.Application.Medios.Deactivate;
|
|
using SIGCM2.Application.Medios.Reactivate;
|
|
using SIGCM2.Domain.Entities;
|
|
using SIGCM2.Domain.Exceptions;
|
|
|
|
namespace SIGCM2.Application.Tests.Medios.Reactivate;
|
|
|
|
public class ReactivateMedioCommandHandlerTests
|
|
{
|
|
private readonly IMedioRepository _repo = Substitute.For<IMedioRepository>();
|
|
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
|
|
private readonly ReactivateMedioCommandHandler _handler;
|
|
|
|
private static Medio MakeMedio(int id = 1, bool activo = false)
|
|
=> new(id, "COD" + id, "Nombre", TipoMedio.Diario, null, activo, DateTime.UtcNow, null);
|
|
|
|
public ReactivateMedioCommandHandlerTests()
|
|
{
|
|
_handler = new ReactivateMedioCommandHandler(_repo, _audit, TimeProvider.System);
|
|
}
|
|
|
|
// ── not found → throws ──────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task Handle_NotFound_ThrowsMedioNotFoundException()
|
|
{
|
|
_repo.GetByIdAsync(999, Arg.Any<CancellationToken>()).Returns((Medio?)null);
|
|
|
|
await Assert.ThrowsAsync<MedioNotFoundException>(
|
|
() => _handler.Handle(new ReactivateMedioCommand(999)));
|
|
}
|
|
|
|
// ── idempotent ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task Handle_AlreadyActive_IsIdempotentAndDoesNotCallUpdateAsync()
|
|
{
|
|
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeMedio(1, true));
|
|
|
|
await _handler.Handle(new ReactivateMedioCommand(1));
|
|
|
|
await _repo.DidNotReceive().UpdateAsync(Arg.Any<Medio>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_AlreadyActive_DoesNotWriteAuditEvent()
|
|
{
|
|
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeMedio(1, true));
|
|
|
|
await _handler.Handle(new ReactivateMedioCommand(1));
|
|
|
|
await _audit.DidNotReceive().LogAsync(
|
|
Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string>(),
|
|
Arg.Any<object?>(), Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
// ── happy path ───────────────────────────────────────────────────────────
|
|
|
|
[Fact]
|
|
public async Task Handle_InactiveMedio_CallsUpdateAsyncWithActiveEntity()
|
|
{
|
|
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeMedio(1, false));
|
|
|
|
await _handler.Handle(new ReactivateMedioCommand(1));
|
|
|
|
await _repo.Received(1).UpdateAsync(
|
|
Arg.Is<Medio>(m => m.Activo),
|
|
Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_InactiveMedio_WritesAuditWithReactivateAction()
|
|
{
|
|
_repo.GetByIdAsync(1, Arg.Any<CancellationToken>()).Returns(MakeMedio(1, false));
|
|
|
|
await _handler.Handle(new ReactivateMedioCommand(1));
|
|
|
|
await _audit.Received(1).LogAsync(
|
|
action: "medio.reactivate",
|
|
targetType: "Medio",
|
|
targetId: "1",
|
|
metadata: Arg.Any<object?>(),
|
|
ct: Arg.Any<CancellationToken>());
|
|
}
|
|
}
|