66 lines
2.3 KiB
C#
66 lines
2.3 KiB
C#
using FluentAssertions;
|
|
using NSubstitute;
|
|
using Knot.Modules.Identity.Application.Abstractions;
|
|
using Knot.Modules.Identity.Application.Users.Register;
|
|
using Knot.Modules.Identity.Domain;
|
|
using Knot.Shared.Kernel;
|
|
using Xunit;
|
|
|
|
namespace Knot.Modules.Identity.UnitTests;
|
|
|
|
public class RegisterUserCommandHandlerTests
|
|
{
|
|
private readonly IUserRepository _userRepository;
|
|
private readonly IIdentityUnitOfWork _unitOfWork;
|
|
private readonly RegisterUserCommandHandler _handler;
|
|
|
|
public RegisterUserCommandHandlerTests()
|
|
{
|
|
_userRepository = Substitute.For<IUserRepository>();
|
|
_unitOfWork = Substitute.For<IIdentityUnitOfWork>();
|
|
_handler = new RegisterUserCommandHandler(_userRepository, _unitOfWork);
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_ShouldReturnSuccess_WhenRegistrationIsSuccessful()
|
|
{
|
|
// Arrange
|
|
var command = new RegisterUserCommand("testuser", "password123", "Test User", "test@example.com", "My bio");
|
|
_userRepository.IsUsernameUniqueAsync(command.Username, Arg.Any<CancellationToken>())
|
|
.Returns(true);
|
|
|
|
// Act
|
|
var result = await _handler.Handle(command, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.IsSuccess.Should().BeTrue();
|
|
result.Value.Should().NotBeEmpty();
|
|
|
|
_userRepository.Received(1).Add(Arg.Is<User>(u =>
|
|
u.Username == command.Username &&
|
|
u.DisplayName == command.DisplayName &&
|
|
u.Email == command.Email));
|
|
|
|
await _unitOfWork.Received(1).SaveChangesAsync(Arg.Any<CancellationToken>());
|
|
}
|
|
|
|
[Fact]
|
|
public async Task Handle_ShouldReturnFailure_WhenUsernameIsNotUnique()
|
|
{
|
|
// Arrange
|
|
var command = new RegisterUserCommand("duplicate", "password123", "Test User", null, null);
|
|
_userRepository.IsUsernameUniqueAsync(command.Username, Arg.Any<CancellationToken>())
|
|
.Returns(false);
|
|
|
|
// Act
|
|
var result = await _handler.Handle(command, CancellationToken.None);
|
|
|
|
// Assert
|
|
result.IsFailure.Should().BeTrue();
|
|
result.Error.Code.Should().Be("Identity.UsernameNotUnique");
|
|
|
|
_userRepository.DidNotReceive().Add(Arg.Any<User>());
|
|
await _unitOfWork.DidNotReceive().SaveChangesAsync(Arg.Any<CancellationToken>());
|
|
}
|
|
}
|