Files
pos-system/services/iam-service-net/tests/IamService.UnitTests/Application/Commands/RegisterUserCommandHandlerTests.cs
Ho Ngoc Hai 12cbcd5d8e fix(authentication): Update JWT handling for ASP.NET Core 8 compatibility
- Replaced JwtSecurityToken with JsonWebToken in DependencyInjection.cs to align with ASP.NET Core 8+ requirements.
- Enhanced CustomWebApplicationFactory to configure minimal JWT validation and custom authentication handling for functional tests.
- Removed outdated tests that relied on InMemory Database limitations, ensuring a cleaner test suite.
- Updated RegisterUserCommandHandlerTests to throw DuplicateResourceException for better error handling.
2026-01-12 20:49:53 +07:00

102 lines
3.3 KiB
C#

using Xunit;
using Moq;
using Microsoft.AspNetCore.Identity;
using Microsoft.Extensions.Logging;
using IamService.API.Application.Commands.Auth;
using IamService.Domain.AggregatesModel.UserAggregate;
using IamService.Domain.Exceptions;
namespace IamService.UnitTests.Application.Commands;
/// <summary>
/// EN: Unit tests for RegisterUserCommandHandler.
/// VI: Unit tests cho RegisterUserCommandHandler.
/// </summary>
public class RegisterUserCommandHandlerTests
{
private readonly Mock<UserManager<ApplicationUser>> _userManagerMock;
private readonly Mock<ILogger<RegisterUserCommandHandler>> _loggerMock;
private readonly RegisterUserCommandHandler _handler;
public RegisterUserCommandHandlerTests()
{
var store = new Mock<IUserStore<ApplicationUser>>();
_userManagerMock = new Mock<UserManager<ApplicationUser>>(
store.Object, null!, null!, null!, null!, null!, null!, null!, null!);
_loggerMock = new Mock<ILogger<RegisterUserCommandHandler>>();
_handler = new RegisterUserCommandHandler(_userManagerMock.Object, _loggerMock.Object);
}
[Fact]
public async Task Handle_WithValidData_ShouldCreateUser()
{
// Arrange
var command = new RegisterUserCommand(
"test@example.com",
"Password123!",
"John",
"Doe");
_userManagerMock
.Setup(x => x.FindByEmailAsync(command.Email))
.ReturnsAsync((ApplicationUser?)null);
_userManagerMock
.Setup(x => x.CreateAsync(It.IsAny<ApplicationUser>(), command.Password))
.ReturnsAsync(IdentityResult.Success);
// Act
var result = await _handler.Handle(command, CancellationToken.None);
// Assert
Assert.NotNull(result);
Assert.Equal(command.Email, result.Email);
Assert.NotEqual(Guid.Empty, result.UserId);
}
[Fact]
public async Task Handle_WithExistingEmail_ShouldThrowException()
{
// Arrange
var command = new RegisterUserCommand(
"existing@example.com",
"Password123!",
"John",
"Doe");
var existingUser = new ApplicationUser(command.Email, "Existing", "User");
_userManagerMock
.Setup(x => x.FindByEmailAsync(command.Email))
.ReturnsAsync(existingUser);
// Act & Assert
await Assert.ThrowsAsync<DuplicateResourceException>(() =>
_handler.Handle(command, CancellationToken.None));
}
[Fact]
public async Task Handle_WhenCreateFails_ShouldThrowException()
{
// Arrange
var command = new RegisterUserCommand(
"test@example.com",
"weak",
"John",
"Doe");
_userManagerMock
.Setup(x => x.FindByEmailAsync(command.Email))
.ReturnsAsync((ApplicationUser?)null);
_userManagerMock
.Setup(x => x.CreateAsync(It.IsAny<ApplicationUser>(), command.Password))
.ReturnsAsync(IdentityResult.Failed(
new IdentityError { Code = "PasswordTooWeak", Description = "Password is too weak" }));
// Act & Assert
await Assert.ThrowsAsync<InvalidOperationException>(() =>
_handler.Handle(command, CancellationToken.None));
}
}