Centralizes and standardizes test fixtures.

This commit is contained in:
BinaryAlley committed 2026-08-15 02:58:17 -07:00
1 parent dfc513da4f
commit b648e2f054
294 files changed
+3807 -4437

No files matched your search

@@ -1,35 +0,0 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.Maintenance.ApplicationSetup.Commands.SetupApplication.Fixtures;
/// <summary>
/// Fixture class for the <see cref="SetupApplicationCommand"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class SetupApplicationCommandFixture
{
/// <summary>
/// Creates a random valid command for the application setup.
/// </summary>
/// <returns>The created command.</returns>
public SetupApplicationCommand CreateSetupApplicationCommand()
{
string password = "Abcd123$";
return new Faker<SetupApplicationCommand>()
.CustomInstantiator(f => new SetupApplicationCommand(
default!,
default!,
default!,
true
))
.RuleFor(x => x.Username, f => f.Internet.UserName())
.RuleFor(x => x.Password, password)
.RuleFor(x => x.PasswordConfirm, password)
.RuleFor(x => x.Use2fa, f => f.Random.Bool())
.Generate();
}
}
@@ -9,8 +9,8 @@ using Lumina.Application.Common.Infrastructure.Security;
using Lumina.Application.Common.Infrastructure.Time;
using Lumina.Application.Common.Infrastructure.Validation;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
using Lumina.Application.UnitTests.Core.Maintenance.ApplicationSetup.Commands.SetupApplication.Fixtures;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Fixtures.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
using Lumina.Contracts.Responses.Authentication;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
@@ -38,6 +38,7 @@ public class SetupApplicationCommandHandlerTests
private readonly IDateTimeProvider _mockDateTimeProvider;
private readonly IDataSeedService _mockDataSeedService;
private readonly SetupApplicationCommandHandler _sut;
private readonly UserEntityFixture _userEntityFixture = new();
private readonly SetupApplicationCommandFixture _setupApplicationCommandFixture;
/// <summary>
@@ -76,7 +77,7 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenNoExistingUsers_ShouldCreateAdminUserWithout2FA()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand() with { Use2fa = false };
SetupApplicationCommand command = _setupApplicationCommandFixture.Create() with { Use2fa = false };
string hashedPassword = Uri.EscapeDataString("hashedPassword");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
@@ -112,7 +113,7 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenNoExistingUsersAndWith2FA_ShouldCreateAdminUserWithTOTP()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand() with { Use2fa = true };
SetupApplicationCommand command = _setupApplicationCommandFixture.Create() with { Use2fa = true };
string hashedPassword = Uri.EscapeDataString("hashedPassword");
byte[] totpSecret = [1, 2, 3];
string qrCodeUri = "data:image/png;base64,test";
@@ -157,8 +158,8 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenUserAlreadyExists_ShouldReturnError()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand();
UserEntity existingUser = UserEntityFixture.CreateUserEntity();
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
UserEntity existingUser = _userEntityFixture.Create();
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(new[] { existingUser });
@@ -179,7 +180,7 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenGetAllUsersReturnsError_ShouldReturnError()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to retrieve users");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
@@ -201,7 +202,7 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenInsertUserReturnsError_ShouldReturnError()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to insert user");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
@@ -225,7 +226,7 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenSetDefaultAuthorizationPermissionsFails_ShouldReturnError()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to set default permissions");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
@@ -250,7 +251,7 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenSetDefaultAuthorizationRolesFails_ShouldReturnError()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to set default roles");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
@@ -278,7 +279,7 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenSetAdminRolePermissionsFails_ShouldReturnError()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to set admin role permissions");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
@@ -309,7 +310,7 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenSetAdminRoleToAdministratorAccountFails_ShouldReturnError()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
Error error = Error.Failure("Database.Error", "Failed to set admin role to administrator account");
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
@@ -342,7 +343,7 @@ public class SetupApplicationCommandHandlerTests
public async Task HandleAsync_WhenAllOperationsSucceed_ShouldReturnSuccessResponse()
{
// Arrange
SetupApplicationCommand command = _setupApplicationCommandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _setupApplicationCommandFixture.Create();
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(Enumerable.Empty<UserEntity>()));
@@ -1,8 +1,8 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Common.Errors;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
using Lumina.Application.Fixtures.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
using Lumina.Application.UnitTests.Common.Setup;
using Lumina.Application.UnitTests.Core.Maintenance.ApplicationSetup.Commands.SetupApplication.Fixtures;
using Lumina.Domain.Common.Primitives;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
@@ -32,7 +32,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenUsernameIsNull_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { Username = null };
// Act
@@ -46,7 +46,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenUsernameIsEmpty_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { Username = string.Empty };
// Act
@@ -60,7 +60,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenUsernameIsWhiteSpace_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { Username = " " };
// Act
@@ -74,7 +74,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenPasswordIsNull_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { Password = null! };
// Act
@@ -88,7 +88,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenPasswordIsEmpty_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { Password = string.Empty };
// Act
@@ -102,7 +102,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenPasswordIsWhiteSpace_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { Password = " " };
// Act
@@ -116,7 +116,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenPasswordConfirmIsNull_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { PasswordConfirm = null! };
// Act
@@ -130,7 +130,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenPasswordConfirmIsEmpty_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { PasswordConfirm = string.Empty };
// Act
@@ -144,7 +144,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenPasswordConfirmIsWhiteSpace_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { PasswordConfirm = " " };
// Act
@@ -158,7 +158,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenPasswordAndPasswordConfirmDontMatch_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { PasswordConfirm = "$321Bcda" };
// Act
@@ -178,7 +178,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenPasswordDoesNotMatchPattern_ShouldHaveValidationError(string password)
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { Password = password, PasswordConfirm = password };
// Act
@@ -196,7 +196,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenPasswordMatchesPattern_ShouldNotHaveValidationError(string password)
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { Password = password, PasswordConfirm = password };
// Act
@@ -210,7 +210,7 @@ public class SetupApplicationCommandValidatorTests
public void Validate_WhenCommandIsValid_ShouldNotHaveValidationError()
{
// Arrange
SetupApplicationCommand command = _commandFixture.CreateSetupApplicationCommand();
SetupApplicationCommand command = _commandFixture.Create();
command = command with { Password = "$321Bcda", PasswordConfirm = "$321Bcda" };
// Act
@@ -3,7 +3,7 @@ using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.DataAccess.Repositories.Users;
using Lumina.Application.Common.DataAccess.UoW;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Queries.CheckInitialization;
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
using Lumina.Contracts.Responses.UsersManagement;
using Lumina.Domain.Common.Primitives;
using NSubstitute;
@@ -25,6 +25,7 @@ public class CheckInitializationQueryHandlerTests
private readonly IUnitOfWork _mockUnitOfWork;
private readonly IUserRepository _mockUserRepository;
private readonly CheckInitializationQueryHandler _sut;
private readonly UserEntityFixture _userEntityFixture = new();
/// <summary>
/// Initializes a new instance of the <see cref="CheckInitializationQueryHandlerTests"/> class.
@@ -43,7 +44,7 @@ public class CheckInitializationQueryHandlerTests
public async Task HandleAsync_WhenUsersExist_ShouldReturnInitialized()
{
// Arrange
List<UserEntity> users = UserEntityFixture.CreateMany();
List<UserEntity> users = _userEntityFixture.CreateMany();
_mockUserRepository.GetAllAsync(Arg.Any<CancellationToken>())
.Returns(Result.From(users.AsEnumerable()));