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,41 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using Lumina.Infrastructure.Common.Models.Configuration;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Infrastructure.UnitTests.Core.Authentication.Fixtures;
/// <summary>
/// Fixture class for JWT token generation tests.
/// </summary>
[ExcludeFromCodeCoverage]
public class JwtTokenGeneratorFixture
{
/// <summary>
/// Creates a valid JWT settings model for testing.
/// </summary>
/// <returns>The created settings model.</returns>
public static JwtSettingsModel CreateJwtSettings()
{
return new Faker<JwtSettingsModel>()
.CustomInstantiator(f => new JwtSettingsModel
{
SecretKey = "this-is-a-very-long-secret-key-for-testing-jwt-tokens",
ExpiryMinutes = 30,
Issuer = "test-issuer",
Audience = "test-audience"
})
.Generate();
}
/// <summary>
/// Creates test user credentials.
/// </summary>
/// <returns>A tuple containing the user ID and username.</returns>
public static (string Id, string Username) CreateUserCredentials()
{
return (Guid.NewGuid().ToString(), new Faker().Person.UserName);
}
}
@@ -0,0 +1,37 @@
#region ========================================================================= USING =====================================================================================
using Bogus;
using System;
using System.Diagnostics.CodeAnalysis;
#endregion
namespace Lumina.Infrastructure.UnitTests.Core.Authentication.Fixtures;
/// <summary>
/// Fixture class for QR code generation tests.
/// </summary>
[ExcludeFromCodeCoverage]
public class QRCodeGeneratorFixture
{
/// <summary>
/// Creates test data for QR code generation.
/// </summary>
/// <returns>A tuple containing username and secret bytes.</returns>
public static (string Username, byte[] Secret) CreateQrCodeTestData()
{
string username = new Faker().Person.UserName;
byte[] secret = new byte[20]; // Standard TOTP secret length
new Random().NextBytes(secret);
return (username, secret);
}
/// <summary>
/// Gets the expected otpauth URI format for validation.
/// </summary>
/// <param name="username">The username to use in the URI.</param>
/// <param name="base32Secret">The Base32 encoded secret.</param>
/// <returns>The formatted otpauth URI.</returns>
public static string GetExpectedOtpauthUri(string username, string base32Secret)
{
return $"otpauth://totp/Lumina:{Uri.EscapeDataString(username)}?secret={base32Secret}&issuer=Lumina";
}
}