Refactors the test fixtures and their consumers to compose other fixtures instead of instantiating models inline

This commit is contained in:
BinaryAlley committed 2026-10-02 14:37:23 +03:00
1 parent 0abda472c6
commit 3aadd6de84
157 files changed
+1312 -597

No files matched your search

@@ -7,6 +7,7 @@ using Lumina.Application.Common.Infrastructure.Security;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.ChangePassword;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Core.UsersManagement.Authentication.Commands.ChangePassword;
using Lumina.Contracts.Responses.Authentication;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
@@ -29,6 +30,7 @@ public class ChangePasswordCommandHandlerTests
private readonly IUserRepository _mockUserRepository;
private readonly ChangePasswordCommandHandler _sut;
private readonly UserEntityFixture _userEntityFixture = new();
private readonly ChangePasswordCommandFixture _changePasswordCommandFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="ChangePasswordCommandHandlerTests"/> class.
@@ -63,7 +65,7 @@ public class ChangePasswordCommandHandlerTests
UserEntity user = _userEntityFixture.Create();
user.Password = escapedHashedCurrentPassword;
ChangePasswordCommand command = new(
ChangePasswordCommand command = _changePasswordCommandFixture.Create(
user.Username,
currentPassword,
newPassword,
@@ -96,7 +98,7 @@ public class ChangePasswordCommandHandlerTests
public async Task HandleAsync_WhenUserDoesNotExist_ShouldReturnError()
{
// Arrange
ChangePasswordCommand command = new("nonexistentUser", "oldPass", "newPass", "newPass");
ChangePasswordCommand command = _changePasswordCommandFixture.Create("nonexistentUser", "oldPass", "newPass", "newPass");
_mockUserRepository.GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(null));
@@ -120,7 +122,7 @@ public class ChangePasswordCommandHandlerTests
UserEntity user = _userEntityFixture.Create();
string incorrectPassword = "wrongPassword";
ChangePasswordCommand command = new(
ChangePasswordCommand command = _changePasswordCommandFixture.Create(
user.Username,
incorrectPassword,
"newPass",
@@ -156,7 +158,7 @@ public class ChangePasswordCommandHandlerTests
UserEntity user = _userEntityFixture.Create();
user.Password = escapedHashedCurrentPassword;
ChangePasswordCommand command = new(
ChangePasswordCommand command = _changePasswordCommandFixture.Create(
user.Username,
currentPassword,
newPassword,
@@ -189,7 +191,7 @@ public class ChangePasswordCommandHandlerTests
string currentPassword = "currentPassword";
string newPassword = "newPassword";
UserEntity user = _userEntityFixture.Create();
ChangePasswordCommand command = new(
ChangePasswordCommand command = _changePasswordCommandFixture.Create(
user.Username,
currentPassword,
newPassword,
@@ -8,6 +8,7 @@ using Lumina.Application.Common.Infrastructure.Security;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RecoverPassword;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Core.UsersManagement.Authentication.Commands.RecoverPassword;
using Lumina.Contracts.Responses.Authentication;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
@@ -32,6 +33,7 @@ public class RecoverPasswordCommandHandlerTests
private readonly IUserRepository _mockUserRepository;
private readonly RecoverPasswordCommandHandler _sut;
private readonly UserEntityFixture _userEntityFixture = new();
private readonly RecoverPasswordCommandFixture _recoverPasswordCommandFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="CheckInitializationQueryHandlerTests"/> class.
@@ -71,7 +73,7 @@ public class RecoverPasswordCommandHandlerTests
user.TotpSecret = encryptedTotpSecret;
user.TempPassword = null;
RecoverPasswordCommand command = new(user.Username, totpCode);
RecoverPasswordCommand command = _recoverPasswordCommandFixture.Create(user.Username, totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -102,7 +104,7 @@ public class RecoverPasswordCommandHandlerTests
public async Task HandleAsync_WhenUserDoesNotExist_ShouldReturnError()
{
// Arrange
RecoverPasswordCommand command = new("nonexistentUser", "123456");
RecoverPasswordCommand command = _recoverPasswordCommandFixture.Create("nonexistentUser", "123456");
_mockUserRepository.GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(null));
@@ -127,7 +129,7 @@ public class RecoverPasswordCommandHandlerTests
user.TempPassword = "existingTempPassword";
user.TempPasswordCreated = DateTime.UtcNow;
RecoverPasswordCommand command = new(user.Username, "123456");
RecoverPasswordCommand command = _recoverPasswordCommandFixture.Create(user.Username, "123456");
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -151,7 +153,7 @@ public class RecoverPasswordCommandHandlerTests
UserEntity user = _userEntityFixture.Create();
user.TotpSecret = null;
RecoverPasswordCommand command = new(user.Username, "123456");
RecoverPasswordCommand command = _recoverPasswordCommandFixture.Create(user.Username, "123456");
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -179,7 +181,7 @@ public class RecoverPasswordCommandHandlerTests
UserEntity user = _userEntityFixture.Create();
user.TotpSecret = encryptedTotpSecret;
RecoverPasswordCommand command = new(user.Username, totpCode);
RecoverPasswordCommand command = _recoverPasswordCommandFixture.Create(user.Username, totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -213,7 +215,7 @@ public class RecoverPasswordCommandHandlerTests
UserEntity user = _userEntityFixture.Create();
user.TotpSecret = encryptedTotpSecret;
RecoverPasswordCommand command = new(user.Username, totpCode);
RecoverPasswordCommand command = _recoverPasswordCommandFixture.Create(user.Username, totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -247,7 +249,7 @@ public class RecoverPasswordCommandHandlerTests
UserEntity user = _userEntityFixture.Create();
user.TotpSecret = encryptedTotpSecret;
RecoverPasswordCommand command = new(user.Username, totpCode);
RecoverPasswordCommand command = _recoverPasswordCommandFixture.Create(user.Username, totpCode);
Error error = Error.Failure("Database.Error", "Failed to check username");
_mockUserRepository.GetByUsernameAsync(command.Username!, Arg.Any<CancellationToken>())
@@ -274,7 +276,7 @@ public class RecoverPasswordCommandHandlerTests
UserEntity user = _userEntityFixture.Create();
user.TotpSecret = encryptedTotpSecret;
RecoverPasswordCommand command = new(user.Username, null);
RecoverPasswordCommand command = _recoverPasswordCommandFixture.Create(user.Username, includeTotpCode: false);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -9,6 +9,7 @@ using Lumina.Application.Common.Infrastructure.Authentication;
using Lumina.Application.Common.Infrastructure.Authorization;
using Lumina.Application.Core.UsersManagement.Authentication.Queries.GetUsers;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Common.DTO.Pagination;
using Lumina.Application.Fixtures.Core.UsersManagement.Authentication.Queries.GetUsers;
using Lumina.Contracts.Responses.UsersManagement.Users;
using Lumina.Domain.Common.Primitives;
@@ -35,6 +36,7 @@ public class GetUsersQueryHandlerTests
private readonly IUserRepository _mockUserRepository;
private readonly GetUsersQueryHandler _sut;
private readonly UserEntityFixture _userEntityFixture = new();
private readonly PaginatedResultDtoFixture<UserEntity> _paginatedResultDtoFixture = new();
private readonly GetUsersQueryFixture _getUsersQueryFixture = new();
private readonly Guid _userId;
@@ -124,7 +126,7 @@ public class GetUsersQueryHandlerTests
_mockAuthorizationService.IsInRoleAsync(_userId, "Admin", Arg.Any<CancellationToken>())
.Returns(true);
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = users, CurrentPage = 1, PerPage = users.Count, Count = users.Count, NumberOfPages = 1 }));
.Returns(Result.From(_paginatedResultDtoFixture.Create(data: users, currentPage: 1, perPage: users.Count, count: users.Count, numberOfPages: 1)));
// Act
Result<IEnumerable<UserResponse>> result = await _sut.HandleAsync(query, CancellationToken.None);
@@ -9,6 +9,7 @@ using Lumina.Application.Common.Infrastructure.Time;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.UsersManagement.Authentication.Queries.LoginUser;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Core.UsersManagement.Authentication.Queries.LoginUser;
using Lumina.Contracts.Responses.Authentication;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
@@ -35,6 +36,7 @@ public class LoginUserQueryHandlerTests
private readonly IDateTimeProvider _mockDateTimeProvider;
private readonly LoginUserQueryHandler _sut;
private readonly UserEntityFixture _userEntityFixture = new();
private readonly LoginUserQueryFixture _loginUserQueryFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="LoginUserQueryHandlerTests"/> class.
@@ -78,7 +80,7 @@ public class LoginUserQueryHandlerTests
user.Password = Uri.EscapeDataString(hashedPassword);
user.TotpSecret = null;
LoginUserQuery query = new(user.Username, password);
LoginUserQuery query = _loginUserQueryFixture.Create(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -113,7 +115,7 @@ public class LoginUserQueryHandlerTests
user.Password = Uri.EscapeDataString(hashedPassword);
user.TotpSecret = encryptedTotpSecret;
LoginUserQuery query = new(user.Username, password, totpCode);
LoginUserQuery query = _loginUserQueryFixture.Create(user.Username, password, totpCode: totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -141,7 +143,7 @@ public class LoginUserQueryHandlerTests
public async Task HandleAsync_WhenUserDoesNotExist_ShouldReturnError()
{
// Arrange
LoginUserQuery query = new("nonexistentUser", "password");
LoginUserQuery query = _loginUserQueryFixture.Create("nonexistentUser", "password");
_mockUserRepository.GetByUsernameAsync(query.Username!, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(null));
@@ -165,7 +167,7 @@ public class LoginUserQueryHandlerTests
user.Password = Uri.EscapeDataString(hashedPassword);
user.TempPassword = null;
LoginUserQuery query = new(user.Username, password);
LoginUserQuery query = _loginUserQueryFixture.Create(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -193,7 +195,7 @@ public class LoginUserQueryHandlerTests
user.TempPassword = Uri.EscapeDataString(hashedTempPassword);
user.TempPasswordCreated = DateTime.UtcNow.AddMinutes(-16); // Expired (more than 15 minutes old)
LoginUserQuery query = new(user.Username, password);
LoginUserQuery query = _loginUserQueryFixture.Create(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -231,7 +233,7 @@ public class LoginUserQueryHandlerTests
user.TempPasswordCreated = DateTime.UtcNow.AddMinutes(-10);
user.TotpSecret = null; // Ensure no TOTP validation is needed
LoginUserQuery query = new(user.Username, password);
LoginUserQuery query = _loginUserQueryFixture.Create(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -265,7 +267,7 @@ public class LoginUserQueryHandlerTests
user.Password = Uri.EscapeDataString(hashedPassword);
user.TotpSecret = encryptedTotpSecret;
LoginUserQuery query = new(user.Username, password);
LoginUserQuery query = _loginUserQueryFixture.Create(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -294,7 +296,7 @@ public class LoginUserQueryHandlerTests
user.Password = Uri.EscapeDataString(hashedPassword);
user.TotpSecret = encryptedTotpSecret;
LoginUserQuery query = new(user.Username, password, totpCode);
LoginUserQuery query = _loginUserQueryFixture.Create(user.Username, password, totpCode: totpCode);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -317,7 +319,7 @@ public class LoginUserQueryHandlerTests
public async Task HandleAsync_WhenGetByUsernameReturnsError_ShouldReturnError()
{
// Arrange
LoginUserQuery query = new("username", "password");
LoginUserQuery query = _loginUserQueryFixture.Create("username", "password");
Error error = Error.Failure("Database.Error", "Failed to retrieve user");
_mockUserRepository.GetByUsernameAsync(query.Username!, Arg.Any<CancellationToken>())
@@ -344,7 +346,7 @@ public class LoginUserQueryHandlerTests
user.TempPassword = Uri.EscapeDataString(hashedTempPassword);
user.TempPasswordCreated = DateTime.UtcNow.AddMinutes(-10); // Valid timeframe
LoginUserQuery query = new(user.Username, password);
LoginUserQuery query = _loginUserQueryFixture.Create(user.Username, password);
_mockUserRepository.GetByUsernameAsync(user.Username, Arg.Any<CancellationToken>())
.Returns(Result.From<UserEntity?>(user));
@@ -12,6 +12,7 @@ using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.UsersManagement.Authorization.Commands.UpdateUserRoleAndPermissions;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Authorization;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Common.DTO.Pagination;
using Lumina.Application.Fixtures.Core.UsersManagement.Authorization.Commands.UpdateUserRoleAndPermissions;
using Lumina.Contracts.Responses.Authorization;
using Lumina.Domain.Common.Primitives;
@@ -49,6 +50,7 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
private readonly UserPermissionEntityFixture _userPermissionEntityFixture = new();
private readonly UpdateUserRoleAndPermissionsCommandFixture _updateUserRoleAndPermissionsCommandFixture = new();
private readonly PermissionEntityFixture _permissionEntityFixture = new();
private readonly PaginatedResultDtoFixture<UserEntity> _paginatedResultDtoFixture = new();
private readonly Guid _userId;
/// <summary>
@@ -180,7 +182,7 @@ public class UpdateUserRoleAndPermissionsCommandHandlerTests
_mockRoleRepository.GetByIdAsync(command.RoleId!.Value, cancellationToken: Arg.Any<CancellationToken>())
.Returns(newRole);
_mockUserRepository.GetAllAsync<BaseFilterDto>(cancellationToken: Arg.Any<CancellationToken>())
.Returns(Result.From(new PaginatedResultDto<UserEntity> { Data = users, CurrentPage = 1, PerPage = users.Count, Count = users.Count, NumberOfPages = 1 }));
.Returns(Result.From(_paginatedResultDtoFixture.Create(data: users, currentPage: 1, perPage: users.Count, count: users.Count, numberOfPages: 1)));
// Act
Result<AuthorizationResponse> result = await _sut.HandleAsync(command, CancellationToken.None);
@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.UsersManagement.Authorization.Commands.UpdateUserRoleAndPermissions;
using Lumina.Application.Fixtures.Core.UsersManagement.Authorization.Commands.UpdateUserRoleAndPermissions;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Primitives;
using System;
@@ -18,12 +19,13 @@ namespace Lumina.Application.UnitTests.Core.UsersManagement.Authorization.Comman
public class UpdateUserRoleAndPermissionsCommandValidatorTests
{
private readonly UpdateUserRoleAndPermissionsCommandValidator _validator = new();
private readonly UpdateUserRoleAndPermissionsCommandFixture _updateUserRoleAndPermissionsCommandFixture = new();
[Fact]
public void Validate_WhenUserIdIsEmpty_ShouldHaveValidationError()
{
// Arrange
UpdateUserRoleAndPermissionsCommand command = new(
UpdateUserRoleAndPermissionsCommand command = _updateUserRoleAndPermissionsCommandFixture.Create(
Guid.Empty,
Guid.NewGuid(),
[Guid.NewGuid()]);
@@ -39,7 +41,7 @@ public class UpdateUserRoleAndPermissionsCommandValidatorTests
public void Validate_WhenPermissionsContainEmptyGuid_ShouldHaveValidationError()
{
// Arrange
UpdateUserRoleAndPermissionsCommand command = new(
UpdateUserRoleAndPermissionsCommand command = _updateUserRoleAndPermissionsCommandFixture.Create(
Guid.NewGuid(),
Guid.NewGuid(),
[Guid.Empty, Guid.NewGuid()]);
@@ -55,10 +57,10 @@ public class UpdateUserRoleAndPermissionsCommandValidatorTests
public void Validate_WhenPermissionsIsNull_ShouldNotHaveValidationError()
{
// Arrange
UpdateUserRoleAndPermissionsCommand command = new(
UpdateUserRoleAndPermissionsCommand command = _updateUserRoleAndPermissionsCommandFixture.Create(
Guid.NewGuid(),
Guid.NewGuid(),
null!);
includePermissions: false);
// Act
List<Error> result = _validator.TestValidate(command);
@@ -71,7 +73,7 @@ public class UpdateUserRoleAndPermissionsCommandValidatorTests
public void Validate_WhenPermissionsIsEmpty_ShouldNotHaveValidationError()
{
// Arrange
UpdateUserRoleAndPermissionsCommand command = new(
UpdateUserRoleAndPermissionsCommand command = _updateUserRoleAndPermissionsCommandFixture.Create(
Guid.NewGuid(),
Guid.NewGuid(),
[]);
@@ -87,7 +89,7 @@ public class UpdateUserRoleAndPermissionsCommandValidatorTests
public void Validate_WhenCommandIsValid_ShouldNotHaveValidationError()
{
// Arrange
UpdateUserRoleAndPermissionsCommand command = new(
UpdateUserRoleAndPermissionsCommand command = _updateUserRoleAndPermissionsCommandFixture.Create(
Guid.NewGuid(),
Guid.NewGuid(),
[Guid.NewGuid()]);
@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.UsersManagement.Authorization.Queries.GetUserPermissions;
using Lumina.Application.Fixtures.Core.UsersManagement.Authorization.Queries.GetUserPermissions;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
@@ -17,12 +18,13 @@ namespace Lumina.Application.UnitTests.Core.UsersManagement.Authorization.Querie
public class GetUserPermissionsQueryValidatorTests
{
private readonly GetUserPermissionsQueryValidator _validator = new();
private readonly GetUserPermissionsQueryFixture _getUserPermissionsQueryFixture = new();
[Fact]
public void Validate_WhenUserIdIsNull_ShouldHaveValidationError()
{
// Arrange
GetUserPermissionsQuery query = new(null);
GetUserPermissionsQuery query = _getUserPermissionsQueryFixture.Create(includeUserId: false);
// Act
List<Error> result = _validator.TestValidate(query);
@@ -35,7 +37,7 @@ public class GetUserPermissionsQueryValidatorTests
public void Validate_WhenUserIdIsEmpty_ShouldHaveValidationError()
{
// Arrange
GetUserPermissionsQuery query = new(Guid.Empty);
GetUserPermissionsQuery query = _getUserPermissionsQueryFixture.Create(Guid.Empty);
// Act
List<Error> result = _validator.TestValidate(query);
@@ -48,7 +50,7 @@ public class GetUserPermissionsQueryValidatorTests
public void Validate_WhenUserIdIsValid_ShouldNotHaveValidationError()
{
// Arrange
GetUserPermissionsQuery query = new(Guid.NewGuid());
GetUserPermissionsQuery query = _getUserPermissionsQueryFixture.Create(Guid.NewGuid());
// Act
List<Error> result = _validator.TestValidate(query);
@@ -1,5 +1,6 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.UsersManagement.Authorization.Queries.GetUserRole;
using Lumina.Application.Fixtures.Core.UsersManagement.Authorization.Queries.GetUserRole;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Domain.Common.Errors;
using Lumina.Domain.Common.Primitives;
@@ -17,12 +18,13 @@ namespace Lumina.Application.UnitTests.Core.UsersManagement.Authorization.Querie
public class GetUserRoleQueryValidatorTests
{
private readonly GetUserRoleQueryValidator _validator = new();
private readonly GetUserRoleQueryFixture _getUserRoleQueryFixture = new();
[Fact]
public void Validate_WhenUserIdIsNull_ShouldHaveValidationError()
{
// Arrange
GetUserRoleQuery query = new(null);
GetUserRoleQuery query = _getUserRoleQueryFixture.Create(includeUserId: false);
// Act
List<Error> result = _validator.TestValidate(query);
@@ -35,7 +37,7 @@ public class GetUserRoleQueryValidatorTests
public void Validate_WhenUserIdIsEmpty_ShouldHaveValidationError()
{
// Arrange
GetUserRoleQuery query = new(Guid.Empty);
GetUserRoleQuery query = _getUserRoleQueryFixture.Create(Guid.Empty);
// Act
List<Error> result = _validator.TestValidate(query);
@@ -48,7 +50,7 @@ public class GetUserRoleQueryValidatorTests
public void Validate_WhenUserIdIsValid_ShouldNotHaveValidationError()
{
// Arrange
GetUserRoleQuery query = new(Guid.NewGuid());
GetUserRoleQuery query = _getUserRoleQueryFixture.Create(Guid.NewGuid());
// Act
List<Error> result = _validator.TestValidate(query);