Files
SIG-CM2.0/tests/SIGCM2.Application.Tests/Auth/Logout/LogoutCommandHandlerTests.cs
dmolinari b619c05762 feat(audit): security events en Auth + authorization handlers (UDT-010 B9)
Instruments auth pipeline with ISecurityEventLogger per #REQ-AUTH-SEC:

LoginCommandHandler:
- login success → action=login result=success actorUserId=user.Id
- login failure disaggregated internally (client still sees 401 unified):
  user_not_found / user_inactive / invalid_password
  — attempts captured with attemptedUsername + FailureReason

LogoutCommandHandler:
- action=logout result=success actorUserId=cmd.UsuarioId

RefreshCommandHandler:
- refresh.issue success on successful rotation
- refresh.reuse_detected failure when revoked token is presented (chain
  revoke already happens; we add the security event with metadata.familyId)
- refresh.issue failure for: token_expired / sub_mismatch / user_not_found /
  user_inactive

PermissionAuthorizationHandler:
- permission.denied failure on require-permission rejection, with metadata
  { permissionRequired, endpoint, method }. ActorUserId from JWT sub.

DI: ISecurityEventLogger was already registered by B6 (AddInfrastructure).

Test updates: 4 test classes now inject ISecurityEventLogger mock:
- LoginCommandHandlerTests, LogoutCommandHandlerTests, RefreshCommandHandlerTests
- PermissionAuthorizationHandlerTests (Api.Tests)

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

Refs: sdd/udt-010-auditoria-trazabilidad/{spec#REQ-SEC-2/3/4/5 #REQ-AUTH-SEC,
design, tasks#B9}
2026-04-16 13:59:27 -03:00

42 lines
1.3 KiB
C#

using NSubstitute;
using SIGCM2.Application.Abstractions.Persistence;
using SIGCM2.Application.Audit;
using SIGCM2.Application.Auth.Logout;
namespace SIGCM2.Application.Tests.Auth.Logout;
public class LogoutCommandHandlerTests
{
private readonly IRefreshTokenRepository _refreshRepo = Substitute.For<IRefreshTokenRepository>();
private readonly ISecurityEventLogger _security = Substitute.For<ISecurityEventLogger>();
private readonly LogoutCommandHandler _handler;
public LogoutCommandHandlerTests()
{
_handler = new LogoutCommandHandler(_refreshRepo, _security);
}
[Fact]
public async Task Handle_RevokesAllActiveForUser()
{
_refreshRepo.RevokeAllActiveForUserAsync(42, Arg.Any<DateTime>()).Returns(3);
var result = await _handler.Handle(new LogoutCommand(42));
Assert.True(result.Success);
Assert.False(string.IsNullOrWhiteSpace(result.Mensaje));
await _refreshRepo.Received(1).RevokeAllActiveForUserAsync(42, Arg.Any<DateTime>());
}
[Fact]
public async Task Handle_NoActiveTokens_StillReturnsSuccess()
{
// 0 rows affected = idempotent logout
_refreshRepo.RevokeAllActiveForUserAsync(Arg.Any<int>(), Arg.Any<DateTime>()).Returns(0);
var result = await _handler.Handle(new LogoutCommand(99));
Assert.True(result.Success);
}
}