2026-07-23 19:19:05 +00:00
|
|
|
using Heimdall.Application.Common;
|
|
|
|
|
using Heimdall.Application.Common.Persistence;
|
|
|
|
|
using Heimdall.Application.Users.Commands.UnlockUser;
|
|
|
|
|
using Heimdall.Domain.Entities;
|
|
|
|
|
using NSubstitute;
|
2026-11-04 13:33:30 +01:00
|
|
|
using Heimdall.Application.Services;
|
2026-07-23 19:19:05 +00:00
|
|
|
|
|
|
|
|
namespace Heimdall.Application.Tests.Handlers.Users;
|
|
|
|
|
|
|
|
|
|
public class UnlockUserHandlerTests
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
private readonly IdentityAccount _user = new()
|
2026-07-23 19:19:05 +00:00
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
UserName = "locked@test.com",
|
|
|
|
|
Email = "locked@test.com",
|
|
|
|
|
LockoutEnd = DateTimeOffset.UtcNow.AddHours(1)
|
|
|
|
|
};
|
|
|
|
|
|
2026-11-04 13:33:30 +01:00
|
|
|
private readonly IIdentityAccountService _userManager;
|
2026-07-23 19:19:05 +00:00
|
|
|
private readonly IUserRepository _repository;
|
|
|
|
|
private readonly UnlockUserHandler _handler;
|
|
|
|
|
|
|
|
|
|
public UnlockUserHandlerTests()
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
_userManager = Substitute.For<IIdentityAccountService>();
|
2026-07-23 19:19:05 +00:00
|
|
|
_repository = Substitute.For<IUserRepository>();
|
|
|
|
|
_handler = new UnlockUserHandler(_userManager, _repository);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Unlock_user_should_return_success()
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
_userManager.FindByIdAsync(_user.Id, Arg.Any<CancellationToken>()).Returns(_user);
|
|
|
|
|
_userManager.SetLockoutEndDateAsync(_user, null).Returns(IdentityOperationResult.Success);
|
|
|
|
|
_userManager.ResetAccessFailedCountAsync(_user).Returns(IdentityOperationResult.Success);
|
|
|
|
|
_userManager.UpdateSecurityStampAsync(_user).Returns(true);
|
2026-07-23 19:19:05 +00:00
|
|
|
|
|
|
|
|
var result = await _handler.Handle(new UnlockUserCommand(_user.Id), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
await _repository.Received(1).AddAuditLogAsync(null, "UserUnlocked", "User",
|
|
|
|
|
_user.Id.ToString(), null, null, Arg.Any<CancellationToken>());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Unlock_nonexistent_user_should_return_not_found()
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
_userManager.FindByIdAsync(Arg.Any<Guid>(), Arg.Any<CancellationToken>()).Returns((IdentityAccount?)null);
|
2026-07-23 19:19:05 +00:00
|
|
|
|
|
|
|
|
var result = await _handler.Handle(new UnlockUserCommand(Guid.NewGuid()), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeFalse();
|
|
|
|
|
result.StatusCode.ShouldBe(404);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[Fact]
|
|
|
|
|
public async Task Unlock_already_unlocked_user_should_return_success()
|
|
|
|
|
{
|
2026-11-04 13:33:30 +01:00
|
|
|
var unlockedUser = new IdentityAccount
|
2026-07-23 19:19:05 +00:00
|
|
|
{
|
|
|
|
|
Id = Guid.NewGuid(),
|
|
|
|
|
UserName = "unlocked@test.com",
|
|
|
|
|
Email = "unlocked@test.com",
|
|
|
|
|
LockoutEnd = null
|
|
|
|
|
};
|
2026-11-04 13:33:30 +01:00
|
|
|
_userManager.FindByIdAsync(unlockedUser.Id, Arg.Any<CancellationToken>()).Returns(unlockedUser);
|
|
|
|
|
_userManager.SetLockoutEndDateAsync(unlockedUser, null).Returns(IdentityOperationResult.Success);
|
|
|
|
|
_userManager.ResetAccessFailedCountAsync(unlockedUser).Returns(IdentityOperationResult.Success);
|
|
|
|
|
_userManager.UpdateSecurityStampAsync(unlockedUser).Returns(true);
|
2026-07-23 19:19:05 +00:00
|
|
|
|
|
|
|
|
var result = await _handler.Handle(new UnlockUserCommand(unlockedUser.Id), CancellationToken.None);
|
|
|
|
|
|
|
|
|
|
result.IsSuccess.ShouldBeTrue();
|
|
|
|
|
}
|
|
|
|
|
}
|