mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Refactors the test fixtures and their consumers to compose other fixtures instead of instantiating models inline
This commit is contained in:
1 parent
0abda472c6
commit
3aadd6de84
157 files changed
+1312
-597
No files matched your search
+7
-5
@@ -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,
|
||||
|
||||
+10
-8
@@ -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));
|
||||
|
||||
+3
-1
@@ -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);
|
||||
|
||||
+12
-10
@@ -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));
|
||||
|
||||
Reference in new issue
Block a user