Implements authentication system

This commit is contained in:
BinaryAlley committed 2024-11-11 22:57:33 +02:00
1 parent 38829d64ff
commit a6dcf65d1c
103 files changed
+2983 -454

No files matched your search

@@ -0,0 +1,33 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.Maintenance.ApplicationSetup.Commands.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 static SetupApplicationCommand CreateSetupApplicationCommand()
{
string password = "Abcd123$";
return new Faker<SetupApplicationCommand>()
.CustomInstantiator(f => new SetupApplicationCommand(
default!,
default!,
default!,
true
))
.RuleFor(x => x.Username, f => f.Person.UserName)
.RuleFor(x => x.Password, password)
.RuleFor(x => x.PasswordConfirm, password);
}
}
@@ -0,0 +1,233 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation.TestHelper;
using Lumina.Application.Core.FileSystemManagement.Paths.Commands.CombinePath;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands;
using Lumina.Application.UnitTests.Core.FileSystemManagement.Pahs.Commands.CombinePath.Fixtures;
using Lumina.Application.UnitTests.Core.Maintenance.ApplicationSetup.Commands.Fixtures;
using Lumina.Application.Common.Errors;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.Maintenance.ApplicationSetup.Commands;
/// <summary>
/// Contains unit tests for the <see cref="SetupApplicationCommandValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class SetupApplicationCommandValidatorTests
{
private readonly SetupApplicationCommandValidator _validator;
/// <summary>
/// Initializes a new instance of the <see cref="SetupApplicationCommandValidatorTests"/> class.
/// </summary>
public SetupApplicationCommandValidatorTests()
{
_validator = new SetupApplicationCommandValidator();
}
[Fact]
public void Validate_WhenUsernameIsNull_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { Username = null! };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Username)
.WithErrorMessage(Errors.Authentication.UsernameCannotBeEmpty.Description);
}
[Fact]
public void Validate_WhenUsernameIsEmpty_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { Username = string.Empty };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Username)
.WithErrorMessage(Errors.Authentication.UsernameCannotBeEmpty.Description);
}
[Fact]
public void Validate_WhenUsernameIsWhiteSpace_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { Username = " " };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Username)
.WithErrorMessage(Errors.Authentication.UsernameCannotBeEmpty.Description);
}
[Fact]
public void Validate_WhenPasswordIsNull_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { Password = null! };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
.WithErrorMessage(Errors.Authentication.PasswordCannotBeEmpty.Description);
}
[Fact]
public void Validate_WhenPasswordIsEmpty_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { Password = string.Empty };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
.WithErrorMessage(Errors.Authentication.PasswordCannotBeEmpty.Description);
}
[Fact]
public void Validate_WhenPasswordIsWhiteSpace_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { Password = " " };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
.WithErrorMessage(Errors.Authentication.PasswordCannotBeEmpty.Description);
}
[Fact]
public void Validate_WhenPasswordConfirmIsNull_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { PasswordConfirm = null! };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.PasswordConfirm)
.WithErrorMessage(Errors.Authentication.PasswordConfirmCannotBeEmpty.Description);
}
[Fact]
public void Validate_WhenPasswordConfirmIsEmpty_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { PasswordConfirm = string.Empty };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.PasswordConfirm)
.WithErrorMessage(Errors.Authentication.PasswordConfirmCannotBeEmpty.Description);
}
[Fact]
public void Validate_WhenPasswordConfirmIsWhiteSpace_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { PasswordConfirm = " " };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.PasswordConfirm)
.WithErrorMessage(Errors.Authentication.PasswordConfirmCannotBeEmpty.Description);
}
[Fact]
public void Validate_WhenPasswordAndPasswordConfirmDontMatch_ShouldHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { PasswordConfirm = "$321Bcda" };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
.WithErrorMessage(Errors.Authentication.PasswordsNotMatch.Description);
}
[Theory]
[InlineData("password123")] // Missing uppercase and special character
[InlineData("PASSWORD123")] // Missing special character
[InlineData("Password123")] // Missing special character
[InlineData("Password!@#")] // Missing number
[InlineData("Pass1!")] // Too short
[InlineData("password")] // Missing uppercase, number, and special character
public void Validate_WhenPasswordDoesNotMatchPattern_ShouldHaveValidationError(string password)
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { Password = password, PasswordConfirm = password };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
.WithErrorMessage(Errors.Authentication.InvalidPassword.Description);
}
[Theory]
[InlineData("Password1!")] // Valid password
[InlineData("Complex1@Password")] // Valid password
[InlineData("MyP@ssw0rd")] // Valid password
[InlineData("Abcd123!@#")] // Valid password
public void Validate_WhenPasswordMatchesPattern_ShouldNotHaveValidationError(string password)
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { Password = password, PasswordConfirm = password };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Password);
}
[Fact]
public void Validate_WhenCommandIsValid_ShouldNotHaveValidationError()
{
// Arrange
SetupApplicationCommand command = SetupApplicationCommandFixture.CreateSetupApplicationCommand();
command = command with { Password = "$321Bcda", PasswordConfirm = "$321Bcda" };
// Act
TestValidationResult<SetupApplicationCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Username);
result.ShouldNotHaveValidationErrorFor(x => x.Password);
result.ShouldNotHaveValidationErrorFor(x => x.PasswordConfirm);
}
}
@@ -1,13 +1,13 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Core.UsersManagement.Register;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RegisterUser;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Core.UsersManagement.Register.Fixture;
/// <summary>
/// Fixture class for the <see cref="RegisterCommand"/> class.
/// Fixture class for the <see cref="RegisterUserCommand"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class RegisterCommandFixture
@@ -16,11 +16,11 @@ public class RegisterCommandFixture
/// Creates a random valid command for a new account registration.
/// </summary>
/// <returns>The created command.</returns>
public static RegisterCommand CreateRegisterCommand()
public static RegisterUserCommand CreateRegisterCommand()
{
string password = "Abcd123$";
return new Faker<RegisterCommand>()
.CustomInstantiator(f => new RegisterCommand(
return new Faker<RegisterUserCommand>()
.CustomInstantiator(f => new RegisterUserCommand(
default!,
default!,
default!,
@@ -1,7 +1,7 @@
#region ========================================================================= USING =====================================================================================
using FluentValidation.TestHelper;
using Lumina.Application.Common.Errors;
using Lumina.Application.Core.UsersManagement.Register;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RegisterUser;
using Lumina.Application.UnitTests.Core.UsersManagement.Register.Fixture;
using System.Diagnostics.CodeAnalysis;
#endregion
@@ -9,30 +9,30 @@ using System.Diagnostics.CodeAnalysis;
namespace Lumina.Application.UnitTests.Core.UsersManagement.Register;
/// <summary>
/// Contains unit tests for the <see cref="RegisterCommandValidator"/> class.
/// Contains unit tests for the <see cref="RegisterUserCommandValidator"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class RegisterCommandValidatorTests
{
private readonly RegisterCommandValidator _validator;
private readonly RegisterUserCommandValidator _validator;
/// <summary>
/// Initializes a new instance of the <see cref="RegisterCommandValidatorTests"/> class.
/// </summary>
public RegisterCommandValidatorTests()
{
_validator = new RegisterCommandValidator();
_validator = new RegisterUserCommandValidator();
}
[Fact]
public void Validate_WhenUsernameIsNull_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { Username = null! };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Username)
@@ -43,11 +43,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenUsernameIsEmpty_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { Username = string.Empty };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Username)
@@ -58,11 +58,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenUsernameIsWhiteSpace_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { Username = " " };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Username)
@@ -73,11 +73,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenPasswordIsNull_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { Password = null! };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
@@ -88,11 +88,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenPasswordIsEmpty_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { Password = string.Empty };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
@@ -103,11 +103,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenPasswordIsWhiteSpace_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { Password = " " };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
@@ -118,11 +118,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenPasswordConfirmIsNull_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { PasswordConfirm = null! };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.PasswordConfirm)
@@ -133,11 +133,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenPasswordConfirmIsEmpty_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { PasswordConfirm = string.Empty };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.PasswordConfirm)
@@ -148,11 +148,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenPasswordConfirmIsWhiteSpace_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { PasswordConfirm = " " };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.PasswordConfirm)
@@ -163,11 +163,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenPasswordAndPasswordConfirmDontMatch_ShouldHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { PasswordConfirm = "$321Bcda" };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
@@ -184,11 +184,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenPasswordDoesNotMatchPattern_ShouldHaveValidationError(string password)
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { Password = password, PasswordConfirm = password };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldHaveValidationErrorFor(x => x.Password)
@@ -203,11 +203,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenPasswordMatchesPattern_ShouldNotHaveValidationError(string password)
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { Password = password, PasswordConfirm = password };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Password);
@@ -217,11 +217,11 @@ public class RegisterCommandValidatorTests
public void Validate_WhenCommandIsValid_ShouldNotHaveValidationError()
{
// Arrange
RegisterCommand command = RegisterCommandFixture.CreateRegisterCommand();
RegisterUserCommand command = RegisterCommandFixture.CreateRegisterCommand();
command = command with { Password = "$321Bcda", PasswordConfirm = "$321Bcda" };
// Act
TestValidationResult<RegisterCommand> result = _validator.TestValidate(command);
TestValidationResult<RegisterUserCommand> result = _validator.TestValidate(command);
// Assert
result.ShouldNotHaveValidationErrorFor(x => x.Username);
@@ -4,10 +4,10 @@ using ErrorOr;
using FluentAssertions;
using Lumina.Application.Common.DataAccess.Entities.Common;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary;
using Lumina.Domain.Common.Enums.BookLibrary;
using Lumina.DataAccess.Core.Repositories.Books;
using Lumina.DataAccess.Core.UoW;
using Lumina.DataAccess.UnitTests.Core.Repositories.Books.Fixtures;
using Lumina.Domain.Common.Enums.BookLibrary;
using Lumina.Domain.Common.Errors;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
@@ -16,7 +16,6 @@ using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Lumina.Contracts.DTO.Common;
#endregion
namespace Lumina.DataAccess.UnitTests.Core.Repositories.Books;