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

@@ -0,0 +1,85 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.ChangePassword;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Commands.ChangePassword.Fixtures;
using Lumina.Contracts.Requests.Authentication;
using System.Diagnostics.CodeAnalysis;
using Lumina.Application.Common.Mapping.Authentication;
#endregion
namespace Lumina.Application.UnitTests.Common.Mapping.Authentication;
/// <summary>
/// Contains unit tests for the <see cref="ChangePasswordRequestMapping"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class ChangePasswordRequestMappingTests
{
private readonly ChangePasswordRequestFixture _fixture;
/// <summary>
/// Initializes a new instance of the <see cref="ChangePasswordRequestMappingTests"/> class.
/// </summary>
public ChangePasswordRequestMappingTests()
{
_fixture = new();
}
[Fact]
public void ToCommand_WhenMappingRequest_ShouldMapCorrectly()
{
// Arrange
ChangePasswordRequest request = _fixture.CreateChangePasswordRequest();
// Act
ChangePasswordCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(request.Username);
result.CurrentPassword.Should().Be(request.CurrentPassword);
result.NewPassword.Should().Be(request.NewPassword);
result.NewPasswordConfirm.Should().Be(request.NewPasswordConfirm);
}
[Fact]
public void ToCommand_WhenMappingRequestWithNullValues_ShouldMapCorrectly()
{
// Arrange
ChangePasswordRequest request = new(null, null, null, null);
// Act
ChangePasswordCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().BeNull();
result.CurrentPassword.Should().BeNull();
result.NewPassword.Should().BeNull();
result.NewPasswordConfirm.Should().BeNull();
}
[Theory]
[InlineData("user1", "oldPass", "newPass", "newPass")]
[InlineData("", "", "", "")]
[InlineData("testUser", null, "pass123", "pass123")]
public void ToCommand_WhenMappingRequestWithSpecificValues_ShouldMapCorrectly(
string? username,
string? currentPassword,
string? newPassword,
string? newPasswordConfirm)
{
// Arrange
ChangePasswordRequest request = new(username, currentPassword, newPassword, newPasswordConfirm);
// Act
ChangePasswordCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(username);
result.CurrentPassword.Should().Be(currentPassword);
result.NewPassword.Should().Be(newPassword);
result.NewPasswordConfirm.Should().Be(newPasswordConfirm);
}
}
@@ -0,0 +1,82 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Common.Mapping.Authentication;
using Lumina.Application.Core.UsersManagement.Authentication.Queries.LoginUser;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Queries.LoginUser.Fixtures;
using Lumina.Contracts.Requests.Authentication;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Common.Mapping.Authentication;
/// <summary>
/// Contains unit tests for the <see cref="LoginRequestMapping"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class LoginRequestMappingTests
{
private readonly LoginRequestFixture _fixture;
/// <summary>
/// Initializes a new instance of the <see cref="LoginRequestMappingTests"/> class.
/// </summary>
public LoginRequestMappingTests()
{
_fixture = new();
}
[Fact]
public void ToQuery_WhenMappingRequest_ShouldMapCorrectly()
{
// Arrange
LoginRequest request = _fixture.CreateLoginRequest();
// Act
LoginUserQuery result = request.ToQuery();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(request.Username);
result.Password.Should().Be(request.Password);
result.TotpCode.Should().Be(request.TotpCode);
}
[Fact]
public void ToQuery_WhenMappingRequestWithNullValues_ShouldMapCorrectly()
{
// Arrange
LoginRequest request = new(null, null);
// Act
LoginUserQuery result = request.ToQuery();
// Assert
result.Should().NotBeNull();
result.Username.Should().BeNull();
result.Password.Should().BeNull();
result.TotpCode.Should().BeNull();
}
[Theory]
[InlineData("user1", "pass123", "123456")]
[InlineData("", "", "")]
[InlineData("testUser", null, null)]
[InlineData("admin", "admin123", null)]
public void ToQuery_WhenMappingRequestWithSpecificValues_ShouldMapCorrectly(
string? username,
string? password,
string? totpCode)
{
// Arrange
LoginRequest request = new(username, password, totpCode);
// Act
LoginUserQuery result = request.ToQuery();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(username);
result.Password.Should().Be(password);
result.TotpCode.Should().Be(totpCode);
}
}
@@ -0,0 +1,78 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Common.Mapping.Authentication;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RecoverPassword;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Commands.RecoverPassword.Fixtures;
using Lumina.Contracts.Requests.Authentication;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Common.Mapping.Authentication;
/// <summary>
/// Contains unit tests for the <see cref="RecoverPasswordRequestMapping"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class RecoverPasswordRequestMappingTests
{
private readonly RecoverPasswordRequestFixture _fixture;
/// <summary>
/// Initializes a new instance of the <see cref="RecoverPasswordRequestMappingTests"/> class.
/// </summary>
public RecoverPasswordRequestMappingTests()
{
_fixture = new();
}
[Fact]
public void ToCommand_WhenMappingRequest_ShouldMapCorrectly()
{
// Arrange
RecoverPasswordRequest request = _fixture.CreateRecoverPasswordRequest();
// Act
RecoverPasswordCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(request.Username);
result.TotpCode.Should().Be(request.TotpCode);
}
[Fact]
public void ToCommand_WhenMappingRequestWithNullValues_ShouldMapCorrectly()
{
// Arrange
RecoverPasswordRequest request = new(null, null);
// Act
RecoverPasswordCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().BeNull();
result.TotpCode.Should().BeNull();
}
[Theory]
[InlineData("user1", "123456")]
[InlineData("", "")]
[InlineData("testUser", null)]
[InlineData(null, "654321")]
public void ToCommand_WhenMappingRequestWithSpecificValues_ShouldMapCorrectly(
string? username,
string? totpCode)
{
// Arrange
RecoverPasswordRequest request = new(username, totpCode);
// Act
RecoverPasswordCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(username);
result.TotpCode.Should().Be(totpCode);
}
}
@@ -0,0 +1,146 @@
#region ========================================================================= USING =====================================================================================
using FluentAssertions;
using Lumina.Application.Common.Mapping.Authentication;
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RegisterUser;
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Commands.RegisterUser.Fixture;
using Lumina.Contracts.Requests.Authentication;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Application.UnitTests.Common.Mapping.Authentication;
/// <summary>
/// Contains unit tests for the <see cref="RegistrationRequestMapping"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class RegistrationRequestMappingTests
{
private readonly RegistrationRequestFixture _fixture;
/// <summary>
/// Initializes a new instance of the <see cref="RegistrationRequestMappingTests"/> class.
/// </summary>
public RegistrationRequestMappingTests()
{
_fixture = new();
}
[Fact]
public void ToSetupCommand_WhenMappingRequest_ShouldMapCorrectly()
{
// Arrange
RegistrationRequest request = _fixture.CreateRegistrationRequest();
// Act
SetupApplicationCommand result = request.ToSetupCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(request.Username);
result.Password.Should().Be(request.Password);
result.PasswordConfirm.Should().Be(request.PasswordConfirm);
result.Use2fa.Should().Be(request.Use2fa);
}
[Fact]
public void ToCommand_WhenMappingRequest_ShouldMapCorrectly()
{
// Arrange
RegistrationRequest request = _fixture.CreateRegistrationRequest();
// Act
RegisterUserCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(request.Username);
result.Password.Should().Be(request.Password);
result.PasswordConfirm.Should().Be(request.PasswordConfirm);
result.Use2fa.Should().Be(request.Use2fa);
}
[Fact]
public void ToSetupCommand_WhenMappingRequestWithNullValues_ShouldMapCorrectly()
{
// Arrange
RegistrationRequest request = new();
// Act
SetupApplicationCommand result = request.ToSetupCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().BeNull();
result.Password.Should().BeNull();
result.PasswordConfirm.Should().BeNull();
result.Use2fa.Should().BeTrue();
}
[Fact]
public void ToCommand_WhenMappingRequestWithNullValues_ShouldMapCorrectly()
{
// Arrange
RegistrationRequest request = new();
// Act
RegisterUserCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().BeNull();
result.Password.Should().BeNull();
result.PasswordConfirm.Should().BeNull();
result.Use2fa.Should().BeTrue();
}
[Theory]
[InlineData("user1", "pass123", "pass123", true)]
[InlineData("", "", "", false)]
[InlineData("testUser", null, null, true)]
[InlineData(null, "pass123", "pass123", false)]
public void ToSetupCommand_WhenMappingRequestWithSpecificValues_ShouldMapCorrectly(
string? username,
string? password,
string? passwordConfirm,
bool use2fa)
{
// Arrange
RegistrationRequest request = new(username, password, passwordConfirm, use2fa);
// Act
SetupApplicationCommand result = request.ToSetupCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(username);
result.Password.Should().Be(password);
result.PasswordConfirm.Should().Be(passwordConfirm);
result.Use2fa.Should().Be(use2fa);
}
[Theory]
[InlineData("user1", "pass123", "pass123", true)]
[InlineData("", "", "", false)]
[InlineData("testUser", null, null, true)]
[InlineData(null, "pass123", "pass123", false)]
public void ToCommand_WhenMappingRequestWithSpecificValues_ShouldMapCorrectly(
string? username,
string? password,
string? passwordConfirm,
bool use2fa)
{
// Arrange
RegistrationRequest request = new(username, password, passwordConfirm, use2fa);
// Act
RegisterUserCommand result = request.ToCommand();
// Assert
result.Should().NotBeNull();
result.Username.Should().Be(username);
result.Password.Should().Be(password);
result.PasswordConfirm.Should().Be(passwordConfirm);
result.Use2fa.Should().Be(use2fa);
}
}
@@ -0,0 +1,69 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management;
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Domain.Common.Enums.MediaLibrary;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
#endregion
namespace Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
/// <summary>
/// Fixture class for the <see cref="UserEntity"/> class.
/// </summary>
[ExcludeFromCodeCoverage]
public class UserEntityFixture
{
/// <summary>
/// Creates a random valid user entity.
/// </summary>
/// <param name="libraryCount">Number of libraries to generate. Default is 0.</param>
/// <returns>The created user entity.</returns>
public static UserEntity CreateUserEntity(int libraryCount = 0)
{
Guid userId = Guid.NewGuid();
List<LibraryEntity> libraries = [.. new Faker<LibraryEntity>()
.CustomInstantiator(f => new LibraryEntity
{
Id = Guid.NewGuid(),
UserId = userId,
Title = f.Commerce.ProductName(),
LibraryType = f.PickRandom<LibraryType>(),
ContentLocations = [],
Created = f.Date.Past(),
Updated = null
})
.Generate(libraryCount)];
return new Faker<UserEntity>()
.CustomInstantiator(f => new UserEntity
{
Id = userId,
Username = default!,
Password = default!,
Created = default,
TotpSecret = default,
Libraries = libraries
})
.RuleFor(x => x.Username, f => f.Internet.UserName())
.RuleFor(x => x.Password, f => Uri.EscapeDataString(f.Internet.Password()))
.RuleFor(x => x.Created, f => f.Date.Past())
.RuleFor(x => x.TotpSecret, f => f.Random.Bool() ? f.Random.String2(32) : null)
.Generate();
}
/// <summary>
/// Creates a list of user entities.
/// </summary>
/// <param name="count">The number of entities to create.</param>
/// <returns>The created list.</returns>
public static List<UserEntity> CreateMany(int count = 3)
{
return Enumerable.Range(0, count)
.Select(_ => CreateUserEntity())
.ToList();
}
}
@@ -4,6 +4,7 @@ using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
using Lumina.Application.Common.Mapping.Common.Metadata;
using Lumina.Application.Common.Mapping.MediaLibrary.WrittenContentLibrary.BookLibrary.Common;
using Lumina.Application.Common.Mapping.UsersManagement.Users;
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
using Lumina.Contracts.Responses.UsersManagement.Users;
using Lumina.Domain.Common.Enums.MediaLibrary;
using System;
@@ -18,19 +19,21 @@ namespace Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users;
[ExcludeFromCodeCoverage]
public class UserEntityMappingTests
{
private readonly UserEntityFixture _fixture;
/// <summary>
/// Initializes a new instance of the <see cref="UserEntityMappingTests"/> class.
/// </summary>
public UserEntityMappingTests()
{
_fixture = new UserEntityFixture();
}
[Fact]
public void ToResponse_WhenMappingValidUserEntity_ShouldMapCorrectly()
{
// Arrange
UserEntity entity = new()
{
Id = Guid.NewGuid(),
Username = "testuser",
Password = "hashedpassword",
Libraries = [],
Created = DateTime.UtcNow,
Updated = null
};
UserEntity entity = UserEntityFixture.CreateUserEntity();
// Act
UserResponse result = entity.ToResponse();
@@ -51,15 +54,8 @@ public class UserEntityMappingTests
public void ToResponse_WhenMappingDifferentUsernames_ShouldMapCorrectly(string username)
{
// Arrange
UserEntity entity = new()
{
Id = Guid.NewGuid(),
Username = username,
Password = "hashedpassword",
Libraries = [],
Created = DateTime.UtcNow,
Updated = null
};
UserEntity entity = UserEntityFixture.CreateUserEntity();
entity.Username = username;
// Act
UserResponse result = entity.ToResponse();
@@ -73,60 +69,22 @@ public class UserEntityMappingTests
public void ToResponse_WhenMappingWithUpdatedDateTime_ShouldMapCorrectly()
{
// Arrange
DateTime updated = DateTime.UtcNow.AddDays(-1);
UserEntity entity = new()
{
Id = Guid.NewGuid(),
Username = "testuser",
Password = "hashedpassword",
Libraries = [],
Created = DateTime.UtcNow,
Updated = updated
};
UserEntity entity = UserEntityFixture.CreateUserEntity();
entity.Updated = DateTime.UtcNow.AddDays(-1);
// Act
UserResponse result = entity.ToResponse();
// Assert
result.Should().NotBeNull();
result.Updated.Should().Be(updated);
result.Updated.Should().Be(entity.Updated);
}
[Fact]
public void ToResponse_WhenMappingWithLibraries_ShouldNotAffectMapping()
{
// Arrange
UserEntity entity = new()
{
Id = Guid.NewGuid(),
Username = "testuser",
Password = "hashedpassword",
Libraries =
[
new()
{
Id = Guid.NewGuid(),
UserId = Guid.NewGuid(),
Title = "Library 1",
LibraryType = LibraryType.Book,
ContentLocations = [],
Created = DateTime.UtcNow,
Updated = null
},
new()
{
Id = Guid.NewGuid(),
UserId = Guid.NewGuid(),
Title = "Library 2",
LibraryType = LibraryType.Movie,
ContentLocations = [],
Created = DateTime.UtcNow,
Updated = null
}
],
Created = DateTime.UtcNow,
Updated = null
};
UserEntity entity = UserEntityFixture.CreateUserEntity(2);
// Act
UserResponse result = entity.ToResponse();