Replaces ErrorOr library with a custom implementation.

This commit is contained in:
BinaryAlley committed 2026-08-14 03:04:28 -07:00
1 parent 8fe90a4dde
commit 3b772d68ca
436 files changed
+4234 -3922

No files matched your search

@@ -1,5 +1,4 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
@@ -9,6 +8,7 @@ using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.ChangePassword;
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
using Lumina.Contracts.Responses.Authentication;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
@@ -68,7 +68,7 @@ public class ChangePasswordCommandHandlerTests
newPassword);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(currentPassword, "hashedCurrentPassword")
.Returns(true);
_mockHashService.HashString(newPassword)
@@ -77,10 +77,10 @@ public class ChangePasswordCommandHandlerTests
.Returns(Result.Updated);
// Act
ErrorOr<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsError);
Assert.False(result.IsFailure);
Assert.True(result.Value.IsPasswordChanged);
await _mockUserRepository.Received(1).GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>());
@@ -97,13 +97,13 @@ public class ChangePasswordCommandHandlerTests
ChangePasswordCommand command = new("nonexistentUser", "oldPass", "newPass", "newPass");
_mockUserRepository.GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(null));
.Returns(Result.From<UserEntity?>(null));
// Act
ErrorOr<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.UsernameDoesNotExist, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>());
@@ -125,15 +125,15 @@ public class ChangePasswordCommandHandlerTests
"newPass");
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(incorrectPassword, Uri.UnescapeDataString(user.Password))
.Returns(false);
// Act
ErrorOr<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.InvalidCurrentPassword, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>());
@@ -161,17 +161,17 @@ public class ChangePasswordCommandHandlerTests
newPassword);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(currentPassword, "hashedCurrentPassword")
.Returns(true);
_mockUserRepository.UpdateAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(error);
// Act
ErrorOr<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>());
@@ -198,10 +198,10 @@ public class ChangePasswordCommandHandlerTests
.Returns(error);
// Act
ErrorOr<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<ChangePasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>());
@@ -1,9 +1,9 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.Errors;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.ChangePassword;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Commands.ChangePassword.Fixtures;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -1,5 +1,4 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
@@ -10,6 +9,7 @@ using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RecoverPassword;
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
using Lumina.Contracts.Responses.Authentication;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
@@ -72,7 +72,7 @@ public class RecoverPasswordCommandHandlerTests
RecoverPasswordCommand command = new(user.Username, totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockCryptographyService.Decrypt(encryptedTotpSecret)
.Returns(decryptedTotpSecret);
_mockTotpTokenGenerator.ValidateToken(Arg.Any<byte[]>(), totpCode)
@@ -83,10 +83,10 @@ public class RecoverPasswordCommandHandlerTests
.Returns(Result.Updated);
// Act
ErrorOr<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsError);
Assert.False(result.IsFailure);
Assert.True(result.Value.IsPasswordReset);
await _mockUserRepository.Received(1).GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>());
@@ -103,13 +103,13 @@ public class RecoverPasswordCommandHandlerTests
RecoverPasswordCommand command = new("nonexistentUser", "123456");
_mockUserRepository.GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(null));
.Returns(Result.From<UserEntity?>(null));
// Act
ErrorOr<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.UsernameDoesNotExist, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>());
@@ -128,13 +128,13 @@ public class RecoverPasswordCommandHandlerTests
RecoverPasswordCommand command = new(user.Username, "123456");
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
// Act
ErrorOr<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.PasswordResetAlreadyRequested, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>());
@@ -152,13 +152,13 @@ public class RecoverPasswordCommandHandlerTests
RecoverPasswordCommand command = new(user.Username, "123456");
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
// Act
ErrorOr<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.InvalidTotpCode, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>());
@@ -180,17 +180,17 @@ public class RecoverPasswordCommandHandlerTests
RecoverPasswordCommand command = new(user.Username, totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockCryptographyService.Decrypt(encryptedTotpSecret)
.Returns(decryptedTotpSecret);
_mockTotpTokenGenerator.ValidateToken(Arg.Any<byte[]>(), totpCode)
.Returns(false);
// Act
ErrorOr<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.InvalidTotpCode, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>());
@@ -214,7 +214,7 @@ public class RecoverPasswordCommandHandlerTests
RecoverPasswordCommand command = new(user.Username, totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockCryptographyService.Decrypt(encryptedTotpSecret)
.Returns(decryptedTotpSecret);
_mockTotpTokenGenerator.ValidateToken(Arg.Any<byte[]>(), totpCode)
@@ -223,10 +223,10 @@ public class RecoverPasswordCommandHandlerTests
.Returns(error);
// Act
ErrorOr<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>());
@@ -252,10 +252,10 @@ public class RecoverPasswordCommandHandlerTests
.Returns(error);
// Act
ErrorOr<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>());
@@ -275,13 +275,13 @@ public class RecoverPasswordCommandHandlerTests
RecoverPasswordCommand command = new(user.Username, null);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
// Act
ErrorOr<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RecoverPasswordResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.InvalidTotpCode, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>());
@@ -1,9 +1,9 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.Errors;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RecoverPassword;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Commands.RecoverPassword.Fixtures;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -1,5 +1,4 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
@@ -12,6 +11,7 @@ using Lumina.Application.Core.UsersManagement.Authentication.Commands.RegisterUs
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Commands.RegisterUser.Fixture;
using Lumina.Contracts.Responses.Authentication;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
@@ -76,7 +76,7 @@ public class RegisterUserCommandHandlerTests
string hashedPassword = "hashedPassword";
_mockUserRepository.GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(null));
.Returns(Result.From<UserEntity?>(null));
_mockHashService.HashString(command.Password!)
.Returns(hashedPassword);
_mockTotpTokenGenerator.GenerateSecret()
@@ -89,10 +89,10 @@ public class RegisterUserCommandHandlerTests
.Returns(Result.Created);
// Act
ErrorOr<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsError);
Assert.False(result.IsFailure);
Assert.Equal(command.Username, result.Value.Username);
Assert.Equal(qrCodeUri, result.Value.TotpSecret);
@@ -113,17 +113,17 @@ public class RegisterUserCommandHandlerTests
string hashedPassword = "hashedPassword";
_mockUserRepository.GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(null));
.Returns(Result.From<UserEntity?>(null));
_mockHashService.HashString(command.Password!)
.Returns(hashedPassword);
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(Result.Created);
// Act
ErrorOr<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.False(result.IsError);
Assert.False(result.IsFailure);
Assert.Equal(command.Username, result.Value.Username);
Assert.Null(result.Value.TotpSecret);
@@ -144,13 +144,13 @@ public class RegisterUserCommandHandlerTests
UserEntity existingUser = UserEntityFixture.CreateUserEntity();
_mockUserRepository.GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(existingUser));
.Returns(Result.From<UserEntity?>(existingUser));
// Act
ErrorOr<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.UsernameAlreadyExists, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>());
@@ -166,15 +166,15 @@ public class RegisterUserCommandHandlerTests
Error error = Error.Failure("Database.Error", "Failed to insert user");
_mockUserRepository.GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(null));
.Returns(Result.From<UserEntity?>(null));
_mockUserRepository.InsertAsync(Arg.Any<UserEntity>(), Arg.Any<CancellationToken>())
.Returns(error);
// Act
ErrorOr<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>());
@@ -193,10 +193,10 @@ public class RegisterUserCommandHandlerTests
.Returns(error);
// Act
ErrorOr<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
Result<RegistrationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
await _mockUserRepository.Received(1).GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>());
@@ -1,9 +1,9 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.Errors;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RegisterUser;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Commands.RegisterUser.Fixture;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -1,5 +1,4 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
@@ -10,6 +9,7 @@ using Lumina.Application.Core.UsersManagement.Authentication.Queries.GetUsers;
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Queries.GetUsers.Fixtures;
using Lumina.Contracts.Responses.UsersManagement.Users;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
using System;
using System.Collections.Generic;
@@ -65,10 +65,10 @@ public class GetUsersQueryHandlerTests
.Returns(false);
// Act
ErrorOr<IEnumerable<UserResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<IEnumerable<UserResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authorization.NotAuthorized, result.FirstError);
await _mockUserRepository.DidNotReceive().GetAllAsync(Arg.Any<CancellationToken>());
}
@@ -86,10 +86,10 @@ public class GetUsersQueryHandlerTests
.Returns(error);
// Act
ErrorOr<IEnumerable<UserResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<IEnumerable<UserResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
}
@@ -104,13 +104,13 @@ public class GetUsersQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From(users));
.Returns(Result.From(users));
// Act
ErrorOr<IEnumerable<UserResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<IEnumerable<UserResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsError);
Assert.False(result.IsFailure);
Assert.Equal(2, result.Value.Count());
}
}
@@ -1,5 +1,4 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
@@ -11,6 +10,7 @@ using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.UsersManagement.Authentication.Queries.LoginUser;
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
using Lumina.Contracts.Responses.Authentication;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
using System;
using System.Diagnostics.CodeAnalysis;
@@ -79,17 +79,17 @@ public class LoginUserQueryHandlerTests
LoginUserQuery query = new(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(password, hashedPassword)
.Returns(true);
_mockJwtTokenGenerator.GenerateToken(user.Id.ToString(), user.Username)
.Returns(jwtToken);
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsError);
Assert.False(result.IsFailure);
Assert.Equal(user.Id, result.Value.Id);
Assert.Equal(user.Username, result.Value.Username);
Assert.Equal(jwtToken, result.Value.Token);
@@ -114,7 +114,7 @@ public class LoginUserQueryHandlerTests
LoginUserQuery query = new(user.Username, password, totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(password, hashedPassword)
.Returns(true);
_mockJwtTokenGenerator.GenerateToken(user.Id.ToString(), user.Username)
@@ -125,10 +125,10 @@ public class LoginUserQueryHandlerTests
.Returns(true);
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsError);
Assert.False(result.IsFailure);
Assert.Equal(user.Id, result.Value.Id);
Assert.Equal(user.Username, result.Value.Username);
Assert.Equal(jwtToken, result.Value.Token);
@@ -142,13 +142,13 @@ public class LoginUserQueryHandlerTests
LoginUserQuery query = new("nonexistentUser", "password");
_mockUserRepository.GetByUsernameAsync(query.Username!, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(null));
.Returns(Result.From<UserEntity?>(null));
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.InvalidUsernameOrPassword, result.FirstError);
}
@@ -166,15 +166,15 @@ public class LoginUserQueryHandlerTests
LoginUserQuery query = new(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(password, hashedPassword)
.Returns(false);
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.InvalidUsernameOrPassword, result.FirstError);
}
@@ -194,17 +194,17 @@ public class LoginUserQueryHandlerTests
LoginUserQuery query = new(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(password, hashedPassword)
.Returns(false);
_mockHashService.CheckStringAgainstHash(password, hashedTempPassword)
.Returns(true);
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.TempPasswordExpired, result.FirstError);
await _mockUserRepository.Received(1).UpdateAsync(Arg.Is<UserEntity>(u =>
@@ -232,7 +232,7 @@ public class LoginUserQueryHandlerTests
LoginUserQuery query = new(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(password, hashedPassword)
.Returns(false); // Regular password check fails
_mockHashService.CheckStringAgainstHash(password, hashedTempPassword)
@@ -241,10 +241,10 @@ public class LoginUserQueryHandlerTests
.Returns(jwtToken);
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.False(result.IsError);
Assert.False(result.IsFailure);
Assert.Equal(user.Id, result.Value.Id);
Assert.Equal(user.Username, result.Value.Username);
Assert.Equal(jwtToken, result.Value.Token);
@@ -266,15 +266,15 @@ public class LoginUserQueryHandlerTests
LoginUserQuery query = new(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(password, hashedPassword)
.Returns(true);
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.InvalidTotpCode, result.FirstError);
}
@@ -295,7 +295,7 @@ public class LoginUserQueryHandlerTests
LoginUserQuery query = new(user.Username, password, totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(password, hashedPassword)
.Returns(true);
_mockCryptographyService.Decrypt(encryptedTotpSecret)
@@ -304,10 +304,10 @@ public class LoginUserQueryHandlerTests
.Returns(false);
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.InvalidTotpCode, result.FirstError);
}
@@ -322,10 +322,10 @@ public class LoginUserQueryHandlerTests
.Returns(error);
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(error, result.FirstError);
}
@@ -345,17 +345,17 @@ public class LoginUserQueryHandlerTests
LoginUserQuery query = new(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(ErrorOrFactory.From<UserEntity?>(user));
.Returns(Result.From<UserEntity?>(user));
_mockHashService.CheckStringAgainstHash(password, hashedPassword)
.Returns(false); // Regular password check fails
_mockHashService.CheckStringAgainstHash(password, hashedTempPassword)
.Returns(false); // Temp password check also fails
// Act
ErrorOr<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
Result<LoginResponse> result = await _sut.HandleAsync(query, CancellationToken.None);
// Assert
Assert.True(result.IsError);
Assert.True(result.IsFailure);
Assert.Equal(Errors.Authentication.InvalidUsernameOrPassword, result.FirstError);
_mockHashService.Received(1).CheckStringAgainstHash(password, hashedPassword);
@@ -1,9 +1,9 @@
#region ========================================================================= USING =====================================================================================
using ErrorOr;
using Lumina.Application.Common.Errors;
using Lumina.Application.Core.UsersManagement.Authentication.Queries.LoginUser;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Queries.LoginUser.Fixtures;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
#endregion