using Guestbooky.Application.Interfaces; using Guestbooky.Application.UseCases.AuthenticateUser; using Guestbooky.Domain.Abstractions.Infrastructure; using Guestbooky.Domain.Entities.User; using Moq; namespace Guestbooky.UnitTests.Application.UseCases; public class AuthenticateUserCommandHandlerTests { private readonly Mock _passwordHasherMock; private readonly Mock _userCredentialsProviderMock; private readonly Mock _jwtTokenServiceMock; private readonly Mock _refreshTokenServiceMock; private readonly AuthenticateUserCommandHandler _handler; public AuthenticateUserCommandHandlerTests() { _passwordHasherMock = new Mock(); _userCredentialsProviderMock = new Mock(); _jwtTokenServiceMock = new Mock(); _refreshTokenServiceMock = new Mock(); _handler = new AuthenticateUserCommandHandler( _passwordHasherMock.Object, _jwtTokenServiceMock.Object, _userCredentialsProviderMock.Object, _refreshTokenServiceMock.Object ); } [Fact] public async Task Handle_WithValidCredentials_ReturnsSuccessfulAuthentication() { // Arrange var command = new AuthenticateUserCommand("testuser", "testpass"); var userCredentials = new ApplicationUser("testuser","hashpass"); var expectedToken = "token"; var expectedRefreshToken = "refresh"; _userCredentialsProviderMock.Setup(x => x.GetCredentials()).Returns(userCredentials); _passwordHasherMock.Setup(x => x.VerifyPassword(It.IsAny(), It.IsAny())).Returns(true); _jwtTokenServiceMock.Setup(x => x.GenerateToken(It.IsAny())).Returns(expectedToken); _refreshTokenServiceMock.Setup(x => x.GenerateRefreshToken()).Returns(expectedRefreshToken); // Act var result = await _handler.Handle(command, CancellationToken.None); // Assert Assert.True(result.IsAuthenticated); Assert.Equal(expectedToken, result.Token); Assert.Equal(expectedRefreshToken, result.RefreshToken); _refreshTokenServiceMock.Verify(x => x.SaveRefreshToken(It.IsAny(), It.IsAny()), Times.Once); } [Fact] public async Task Handle_WithInvalidUsername_ReturnsFailedAuthentication() { // Arrange var command = new AuthenticateUserCommand("wronguser", "userpass"); var userCredentials = new ApplicationUser("testuser", "hashpass"); _userCredentialsProviderMock.Setup(x => x.GetCredentials()).Returns(userCredentials); // Act var result = await _handler.Handle(command, CancellationToken.None); // Assert Assert.False(result.IsAuthenticated); Assert.Equal(string.Empty, result.Token); Assert.Equal(string.Empty, result.RefreshToken); _jwtTokenServiceMock.Verify(x => x.GenerateToken(It.IsAny()), Times.Never); _refreshTokenServiceMock.Verify(x => x.GenerateRefreshToken(), Times.Never); _refreshTokenServiceMock.Verify(x => x.SaveRefreshToken(It.IsAny(), It.IsAny()), Times.Never); } [Fact] public async Task Handle_WithInvalidPassword_ReturnsFailedAuthentication() { // Arrange var command = new AuthenticateUserCommand("testuser", "wrongpass"); var userCredentials = new ApplicationUser("testuser", "hashpass"); _userCredentialsProviderMock.Setup(x => x.GetCredentials()).Returns(userCredentials); _passwordHasherMock.Setup(x => x.VerifyPassword(It.IsAny(), It.IsAny())).Returns(false); // Act var result = await _handler.Handle(command, CancellationToken.None); // Assert Assert.False(result.IsAuthenticated); Assert.Equal(string.Empty, result.Token); Assert.Equal(string.Empty, result.RefreshToken); _jwtTokenServiceMock.Verify(x => x.GenerateToken(It.IsAny()), Times.Never); _refreshTokenServiceMock.Verify(x => x.GenerateRefreshToken(), Times.Never); _refreshTokenServiceMock.Verify(x => x.SaveRefreshToken(It.IsAny(), It.IsAny()), Times.Never); } }