- Added debug middleware for /connect/* endpoints to log request and response details for better troubleshooting. - Updated OAuth2 configuration to include "offline_access" scope and disabled access token encryption for development. - Improved DbContext registration in tests by removing all related registrations and ensuring in-memory database setup for testing purposes. - Addressed issues with the /connect/token endpoint not responding, outlining next steps for debugging and fixing the OpenIddict configuration.
101 lines
3.2 KiB
C#
101 lines
3.2 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;
|
|
|
|
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<InvalidOperationException>(() =>
|
|
_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));
|
|
}
|
|
}
|