Adds authentication unit tests

This commit is contained in:
BinaryAlley committed 2024-11-12 15:45:26 +02:00
1 parent a6dcf65d1c
commit 4f91f29b62
101 files changed
+5549 -135

No files matched your search

@@ -3,6 +3,7 @@ using FluentValidation;
using Lumina.Application.Common.Behaviors;
using Mediator;
using Microsoft.Extensions.DependencyInjection;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.Common.DependencyInjection;
@@ -10,6 +11,7 @@ namespace Lumina.Application.Common.DependencyInjection;
/// <summary>
/// Contains all services of the Application layer.
/// </summary>
[ExcludeFromCodeCoverage]
public static class ApplicationLayerServices
{
/// <summary>
@@ -1,5 +1,5 @@
#region ========================================================================= USING =====================================================================================
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RegisterUser;
using Lumina.Contracts.Requests.Authentication;
#endregion
@@ -4,7 +4,7 @@ using Lumina.Contracts.Responses.Authentication;
using Mediator;
#endregion
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Commands;
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
/// <summary>
/// Command for the initial application setup.
@@ -14,8 +14,8 @@ namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Commands;
/// <param name="PasswordConfirm">Confirmation of the password.</param>
/// <param name="Use2fa">Indicates whether to enable two-factor authentication for the Admin account.</param>
public record SetupApplicationCommand(
string? Username,
string? Password,
string? PasswordConfirm,
string? Username,
string? Password,
string? PasswordConfirm,
bool Use2fa
) : IRequest<ErrorOr<RegistrationResponse>>;
@@ -15,7 +15,7 @@ using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Commands;
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
/// <summary>
/// Handler for the command to perform the initial application setup.
@@ -37,9 +37,9 @@ public class SetupApplicationCommandHandler : IRequestHandler<SetupApplicationCo
/// <param name="totpTokenGenerator">Injected service for generating and validating TOTP tokens.</param>
/// <param name="qRCodeGenerator">Injected service for generating QR codes.</param>
public SetupApplicationCommandHandler(
IUnitOfWork unitOfWork,
IHashService hashService,
ICryptographyService cryptographyService,
IUnitOfWork unitOfWork,
IHashService hashService,
ICryptographyService cryptographyService,
ITotpTokenGenerator totpTokenGenerator,
IQRCodeGenerator qRCodeGenerator)
{
@@ -3,7 +3,7 @@ using FluentValidation;
using Lumina.Application.Common.Errors;
#endregion
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Commands;
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
/// <summary>
/// Validates the needed validation rules for <see cref="SetupApplicationCommand"/>.
@@ -3,7 +3,7 @@ using Lumina.Contracts.Responses.UsersManagement;
using Mediator;
#endregion
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Queries;
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Queries.CheckInitialization;
/// <summary>
/// Query for checking the initialization of the application.
@@ -13,7 +13,7 @@ using System.Threading;
using System.Threading.Tasks;
#endregion
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Queries;
namespace Lumina.Application.Core.Maintenance.ApplicationSetup.Queries.CheckInitialization;
/// <summary>
/// Handler for the query to check the initialization of the application.
@@ -22,6 +22,9 @@ public class CheckInitializationQueryHandler : IRequestHandler<CheckInitializati
{
private readonly IUnitOfWork _unitOfWork;
/// <summary>
/// Initializes a new instance of the <see cref="CheckInitializationQueryHandler"/> class.
/// </summary>
public CheckInitializationQueryHandler(IUnitOfWork unitOfWork)
{
_unitOfWork = unitOfWork;
@@ -48,6 +48,14 @@ public class RegisterUserCommandHandler : IRequestHandler<RegisterUserCommand, E
_qRCodeGenerator = qRCodeGenerator;
}
/// <summary>
/// Handles the command to register an account.
/// </summary>
/// <param name="request">The request to be handled.</param>
/// <param name="cancellationToken">Cancellation token that can be used to stop the execution.</param>
/// <returns>
/// An <see cref="ErrorOr{TValue}"/> containing either a successfully created <see cref="RegistrationResponse"/>, or an error message.
/// </returns>
public async ValueTask<ErrorOr<RegistrationResponse>> Handle(RegisterUserCommand request, CancellationToken cancellationToken)
{
// check if any users already exists (admin account is only set once!)
@@ -72,7 +72,7 @@ public class LoginUserQueryHandler : IRequestHandler<LoginUserQuery, ErrorOr<Log
if (resultSelectUser.Value.TempPassword is not null)
{
// the temporary password should only be valid for 15 minutes, if its obsolete, remove it and return error
if (resultSelectUser.Value.TempPasswordCreated!.Value.AddMinutes(15) > DateTime.UtcNow)
if (resultSelectUser.Value.TempPasswordCreated!.Value.AddMinutes(15) < DateTime.UtcNow)
{
resultSelectUser.Value.TempPassword = null;
resultSelectUser.Value.TempPasswordCreated = null;