Files
SIG-CM2.0/tests/SIGCM2.Application.Tests/Roles/Update/UpdateRolCommandHandlerTests.cs
dmolinari a3f01bc6c9 feat(audit): enchufar audit en handlers de Rol (UDT-010 B8)
4 command handlers del módulo Roles + Permisos ahora auditan:

| Handler                              | Action                 |
|--------------------------------------|------------------------|
| CreateRolCommandHandler              | rol.create             |
| UpdateRolCommandHandler              | rol.update             |
| DeactivateRolCommandHandler          | rol.deactivate         |
| AssignPermisosToRolCommandHandler    | rol.permisos_update    |

Mismo patrón que B7 (using block + post-commit reads outside scope).

Metadata:
- rol.create: after={Codigo, Nombre, Descripcion}
- rol.update: {before, after} diff
- rol.permisos_update: {before, after} con arrays de codigos ordenados

AssignPermisosToRolCommandHandler captura 'before' leyendo
GetByRolCodigoAsync antes del TransactionScope para poder emitir el diff.

4 test classes actualizados con mock de IAuditLogger.

Suite: 378/378 Application.Tests + 141/141 Api.Tests = 519/519 passing.

Refs: sdd/udt-010-auditoria-trazabilidad/{spec#REQ-RM-AUD, design, tasks#B8}
2026-04-16 13:54:47 -03:00

67 lines
2.6 KiB
C#

using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Roles.Update;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.Roles.Update;
public class UpdateRolCommandHandlerTests
{
private readonly IRolRepository _repository = Substitute.For<IRolRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly UpdateRolCommandHandler _handler;
public UpdateRolCommandHandlerTests()
{
_handler = new UpdateRolCommandHandler(_repository, _audit);
}
[Fact]
public async Task Handle_NonExistentCodigo_ThrowsRolNotFoundException()
{
_repository.UpdateAsync("missing", Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(false);
var ex = await Assert.ThrowsAsync<RolNotFoundException>(
() => _handler.Handle(new UpdateRolCommand("missing", "X", null, true)));
Assert.Equal("missing", ex.Codigo);
}
[Fact]
public async Task Handle_Happy_ReturnsDtoWithUpdatedFields()
{
var fechaCreacion = new DateTime(2026, 4, 10, 9, 0, 0, DateTimeKind.Utc);
var fechaModificacion = new DateTime(2026, 4, 15, 12, 0, 0, DateTimeKind.Utc);
_repository.UpdateAsync("cajero", "Cajero V2", "Desc V2", true, Arg.Any<CancellationToken>())
.Returns(true);
_repository.GetByCodigoAsync("cajero")
.Returns(new Rol(10, "cajero", "Cajero V2", "Desc V2", true, fechaCreacion, fechaModificacion));
var dto = await _handler.Handle(new UpdateRolCommand("cajero", "Cajero V2", "Desc V2", true));
Assert.Equal(10, dto.Id);
Assert.Equal("Cajero V2", dto.Nombre);
Assert.Equal("Desc V2", dto.Descripcion);
Assert.True(dto.Activo);
Assert.Equal(fechaModificacion, dto.FechaModificacion);
}
[Fact]
public async Task Handle_Happy_CallsUpdateAsyncWithExactFields()
{
var now = new DateTime(2026, 4, 15, 12, 0, 0, DateTimeKind.Utc);
_repository.UpdateAsync(Arg.Any<string>(), Arg.Any<string>(), Arg.Any<string?>(), Arg.Any<bool>(), Arg.Any<CancellationToken>())
.Returns(true);
_repository.GetByCodigoAsync("cajero")
.Returns(new Rol(1, "cajero", "X", null, false, now, now));
await _handler.Handle(new UpdateRolCommand("cajero", "X", null, false));
await _repository.Received(1).UpdateAsync("cajero", "X", null, false, Arg.Any<CancellationToken>());
}
}