Files
Heimdall/tests/Heimdall.Application.Tests/Handlers/Users/UnlockUserHandlerTests.cs
T
arnaud.dartois 2bf3dbc95b
Heimdall Gitea / Detect CI scope (pull_request) Successful in 5s
Heimdall Gitea / Build and tests (pull_request) Successful in 20m8s
Heimdall Gitea / Publish versioned release (pull_request) Skipped
Heimdall Gitea / Build and scan agent image (pull_request) Failing after 6m10s
Heimdall Gitea / Build and scan server image (pull_request) Failing after 8m2s
Heimdall Gitea / Publish Docker Hub latest tags (pull_request) Skipped
Heimdall Gitea / CI policy (pull_request) Failing after 2s
fix(identity): harden user lifecycle and permissions
2026-11-04 10:26:07 +01:00

78 lines
2.9 KiB
C#

using Heimdall.Application.Common;
using Heimdall.Application.Common.Persistence;
using Heimdall.Application.Users.Commands.UnlockUser;
using Heimdall.Domain.Entities;
using Microsoft.AspNetCore.Identity;
using NSubstitute;
namespace Heimdall.Application.Tests.Handlers.Users;
public class UnlockUserHandlerTests
{
private readonly AppUser _user = new()
{
Id = Guid.NewGuid(),
UserName = "locked@test.com",
Email = "locked@test.com",
LockoutEnd = DateTimeOffset.UtcNow.AddHours(1)
};
private readonly UserManager<AppUser> _userManager;
private readonly IUserRepository _repository;
private readonly UnlockUserHandler _handler;
public UnlockUserHandlerTests()
{
var store = Substitute.For<IUserStore<AppUser>>();
_userManager = Substitute.For<UserManager<AppUser>>(store, null!, null!, null!, null!, null!, null!, null!, null!);
_repository = Substitute.For<IUserRepository>();
_handler = new UnlockUserHandler(_userManager, _repository);
}
[Fact]
public async Task Unlock_user_should_return_success()
{
_userManager.FindByIdAsync(_user.Id.ToString()).Returns(_user);
_userManager.SetLockoutEndDateAsync(_user, null).Returns(IdentityResult.Success);
_userManager.ResetAccessFailedCountAsync(_user).Returns(IdentityResult.Success);
_userManager.UpdateSecurityStampAsync(_user).Returns(IdentityResult.Success);
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()
{
_userManager.FindByIdAsync(Arg.Any<string>()).Returns((AppUser?)null);
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()
{
var unlockedUser = new AppUser
{
Id = Guid.NewGuid(),
UserName = "unlocked@test.com",
Email = "unlocked@test.com",
LockoutEnd = null
};
_userManager.FindByIdAsync(unlockedUser.Id.ToString()).Returns(unlockedUser);
_userManager.SetLockoutEndDateAsync(unlockedUser, null).Returns(IdentityResult.Success);
_userManager.ResetAccessFailedCountAsync(unlockedUser).Returns(IdentityResult.Success);
_userManager.UpdateSecurityStampAsync(unlockedUser).Returns(IdentityResult.Success);
var result = await _handler.Handle(new UnlockUserCommand(unlockedUser.Id), CancellationToken.None);
result.IsSuccess.ShouldBeTrue();
}
}