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

89 lines
3.2 KiB
C#

using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Roles.Create;
using SIGCM2.Domain.Entities;
using SIGCM2.Domain.Exceptions;
namespace SIGCM2.Application.Tests.Roles.Create;
public class CreateRolCommandHandlerTests
{
private readonly IRolRepository _repository = Substitute.For<IRolRepository>();
private readonly IAuditLogger _audit = Substitute.For<IAuditLogger>();
private readonly CreateRolCommandHandler _handler;
private static CreateRolCommand ValidCommand() => new("cajero_senior", "Cajero Senior", "Con más permisos");
public CreateRolCommandHandlerTests()
{
_handler = new CreateRolCommandHandler(_repository, _audit);
}
[Fact]
public async Task Handle_CodigoDuplicado_ThrowsRolAlreadyExistsException()
{
var now = new DateTime(2026, 4, 15, 10, 0, 0, DateTimeKind.Utc);
_repository.GetByCodigoAsync("cajero_senior")
.Returns(new Rol(99, "cajero_senior", "Cajero Senior", null, true, now, null));
var ex = await Assert.ThrowsAsync<RolAlreadyExistsException>(
() => _handler.Handle(ValidCommand()));
Assert.Equal("cajero_senior", ex.Codigo);
}
[Fact]
public async Task Handle_CodigoDuplicado_DoesNotCallAddAsync()
{
var now = new DateTime(2026, 4, 15, 10, 0, 0, DateTimeKind.Utc);
_repository.GetByCodigoAsync(Arg.Any<string>())
.Returns(new Rol(1, "cajero_senior", "X", null, true, now, null));
try { await _handler.Handle(ValidCommand()); } catch (RolAlreadyExistsException) { }
await _repository.DidNotReceive().AddAsync(Arg.Any<Rol>(), Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_Happy_AddsAndReturnsDtoWithId()
{
_repository.GetByCodigoAsync(Arg.Any<string>()).Returns((Rol?)null);
_repository.AddAsync(Arg.Any<Rol>(), Arg.Any<CancellationToken>()).Returns(42);
var result = await _handler.Handle(ValidCommand());
Assert.Equal(42, result.Id);
Assert.Equal("cajero_senior", result.Codigo);
Assert.Equal("Cajero Senior", result.Nombre);
Assert.Equal("Con más permisos", result.Descripcion);
Assert.True(result.Activo);
}
[Fact]
public async Task Handle_Happy_CallsAddAsyncOnce()
{
_repository.GetByCodigoAsync(Arg.Any<string>()).Returns((Rol?)null);
_repository.AddAsync(Arg.Any<Rol>(), Arg.Any<CancellationToken>()).Returns(5);
await _handler.Handle(ValidCommand());
await _repository.Received(1).AddAsync(
Arg.Is<Rol>(r => r.Codigo == "cajero_senior" && r.Nombre == "Cajero Senior" && r.Activo),
Arg.Any<CancellationToken>());
}
[Fact]
public async Task Handle_Happy_WithNullDescripcion_PassesNullToRepository()
{
_repository.GetByCodigoAsync(Arg.Any<string>()).Returns((Rol?)null);
_repository.AddAsync(Arg.Any<Rol>(), Arg.Any<CancellationToken>()).Returns(1);
await _handler.Handle(new CreateRolCommand("nuevo_rol", "Nuevo", null));
await _repository.Received(1).AddAsync(
Arg.Is<Rol>(r => r.Descripcion == null),
Arg.Any<CancellationToken>());
}
}