mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 10:11:10 +00:00
Implements Authorization system
This commit is contained in:
1 parent
5bfb2ca212
commit
2c9c210e43
68 files changed
+2734
-336
No files matched your search
-23
@@ -1,23 +0,0 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.DataAccess.Core.Repositories.Common.Factory;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.IntegrationTests.Core.Repositories.Common.Factory;
|
||||
|
||||
/// <summary>
|
||||
/// Contains integration tests for the <see cref="RepositoryFactory"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class RepositoryFactoryTests
|
||||
{
|
||||
[Fact]
|
||||
public void RepositoryFactoryImplementsRepositoryFactoryInterface_InterfaceIsImplemented_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
RepositoryFactory repositoryFactory = new(null!);
|
||||
|
||||
// Assert
|
||||
Assert.True(repositoryFactory is IRepositoryFactory);
|
||||
}
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.DataAccess.Common.DependencyInjection;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.IntegrationTests.Core.UoW;
|
||||
|
||||
/// <summary>
|
||||
/// Contains integration tests for the <see cref="UnitOfWork"/> class.
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class UnitOfWorkTests
|
||||
{
|
||||
[Fact]
|
||||
public void UnitOfWorkImplementsUnitOfWorkInterface_InterfaceIsImplemented_ReturnsTrue()
|
||||
{
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
UnitOfWork unitOfWork = (UnitOfWork)serviceProvider.GetRequiredService<IUnitOfWork>();
|
||||
|
||||
// Assert
|
||||
Assert.True(unitOfWork is IUnitOfWork);
|
||||
}
|
||||
}
|
||||
-44
@@ -1,44 +0,0 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>disable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
|
||||
<IsPackable>false</IsPackable>
|
||||
<IsTestProject>true</IsTestProject>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<AssemblyAttribute Include="Xunit.CollectionBehaviorAttribute">
|
||||
<_Parameter1>DisableTestParallelization = true</_Parameter1>
|
||||
<_Parameter1_IsLiteral>true</_Parameter1_IsLiteral>
|
||||
</AssemblyAttribute>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoFixture.AutoNSubstitute" Version="5.0.0-preview0011" />
|
||||
<PackageReference Include="AutoFixture.Xunit2" Version="5.0.0-preview0011" />
|
||||
<PackageReference Include="coverlet.collector" Version="6.0.2">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="FluentAssertions" Version="7.0.0-alpha.5" />
|
||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.11.1" />
|
||||
<PackageReference Include="NSubstitute" Version="5.3.0" />
|
||||
<PackageReference Include="xunit" Version="2.9.2" />
|
||||
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.0-pre.49">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Using Include="Xunit" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\..\src\Lumina.DataAccess\Lumina.DataAccess.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
+4
-1
@@ -76,7 +76,10 @@ public class AuthenticatedLuminaApiFactory : LuminaApiFactory, IDisposable
|
||||
Username = TestUsername!,
|
||||
Password = _hashService.HashString("TestPass123!"),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
+6
-1
@@ -1,4 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.DataAccess.Common.Interceptors;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@@ -69,7 +70,11 @@ public class LuminaApiFactory : WebApplicationFactory<Program>, IDisposable
|
||||
if (descriptor is not null)
|
||||
services.Remove(descriptor);
|
||||
// add SQLite DbContext configuration
|
||||
services.AddDbContext<LuminaDbContext>(options => options.UseSqlite(_connection));
|
||||
services.AddDbContext<LuminaDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
options.UseSqlite(_connection);
|
||||
options.AddInterceptors(serviceProvider.GetRequiredService<UpdateAuditableEntitiesInterceptor>());
|
||||
});
|
||||
// configure JWT authentication for testing
|
||||
services.PostConfigure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
|
||||
{
|
||||
|
||||
+4
-1
@@ -119,7 +119,10 @@ public class CheckInitializationEndpointTests : IClassFixture<AuthenticatedLumin
|
||||
Username = _testUsername,
|
||||
Password = new HashService().HashString("TestPass123!"),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
+4
-1
@@ -173,7 +173,10 @@ public class SetupApplicationEndpointTests : IClassFixture<AuthenticatedLuminaAp
|
||||
Username = _testUsername,
|
||||
Password = new HashService().HashString("TestPass123!"),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [], // TODO: test default role and permissions?
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
+16
-4
@@ -245,7 +245,10 @@ public class LoginEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory>,
|
||||
Username = _testUsername,
|
||||
Password = _hashService.HashString("TestPass123!"),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
@@ -265,7 +268,10 @@ public class LoginEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory>,
|
||||
Password = _hashService.HashString("TestPass123!"),
|
||||
TotpSecret = _cryptographyService.Encrypt(Convert.ToBase64String(totpSecret)),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
@@ -285,7 +291,10 @@ public class LoginEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory>,
|
||||
TempPassword = Uri.EscapeDataString(_hashService.HashString("TempPass123!")),
|
||||
TempPasswordCreated = DateTime.UtcNow,
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
@@ -305,7 +314,10 @@ public class LoginEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory>,
|
||||
TempPassword = Uri.EscapeDataString(_hashService.HashString("TempPass123!")),
|
||||
TempPasswordCreated = DateTime.UtcNow.AddMinutes(-16), // expired (> 15 minutes old)
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
+12
-3
@@ -242,7 +242,10 @@ public class RecoverPasswordEndpointTests : IClassFixture<AuthenticatedLuminaApi
|
||||
Password = _hashService.HashString("OldPass123!"),
|
||||
TotpSecret = null, // No TOTP configured
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
dbContext.Users.Add(userWithoutTotp);
|
||||
await dbContext.SaveChangesAsync();
|
||||
@@ -306,7 +309,10 @@ public class RecoverPasswordEndpointTests : IClassFixture<AuthenticatedLuminaApi
|
||||
Password = _hashService.HashString("OldPass123!"),
|
||||
TotpSecret = _cryptographyService.Encrypt(Convert.ToBase64String(totpSecret)),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
@@ -325,7 +331,10 @@ public class RecoverPasswordEndpointTests : IClassFixture<AuthenticatedLuminaApi
|
||||
Password = _hashService.HashString("OldPass123!"),
|
||||
TotpSecret = null,
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
+4
-1
@@ -191,7 +191,10 @@ public class RegisterEndpointTests : IClassFixture<AuthenticatedLuminaApiFactory
|
||||
Username = _testUsername,
|
||||
Password = new HashService().HashString("TestPass123!"),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
+6
-1
@@ -1,4 +1,5 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.DataAccess.Common.Interceptors;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Microsoft.AspNetCore.Authentication.JwtBearer;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
@@ -64,7 +65,11 @@ public class LuminaApiFactory : WebApplicationFactory<Program>, IDisposable
|
||||
if (descriptor is not null)
|
||||
services.Remove(descriptor);
|
||||
// add SQLite DbContext configuration
|
||||
services.AddDbContext<LuminaDbContext>(options => options.UseSqlite(_connection));
|
||||
services.AddDbContext<LuminaDbContext>((serviceProvider, options) =>
|
||||
{
|
||||
options.UseSqlite(_connection);
|
||||
options.AddInterceptors(serviceProvider.GetRequiredService<UpdateAuditableEntitiesInterceptor>());
|
||||
});
|
||||
// configure JWT authentication for testing
|
||||
services.PostConfigure<JwtBearerOptions>(JwtBearerDefaults.AuthenticationScheme, options =>
|
||||
{
|
||||
|
||||
+4
-1
@@ -113,7 +113,10 @@ public class AuthChangePasswordTests : IClassFixture<LuminaApiFactory>, IDisposa
|
||||
Username = _testUsername,
|
||||
Password = _hashService.HashString("TestPass123!"),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserRoles = [],
|
||||
UserPermissions = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
+10
-44
@@ -79,7 +79,7 @@ public class AuthLoginTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
// Assert
|
||||
JwtSecurityTokenHandler handler = new();
|
||||
JwtSecurityToken token = handler.ReadJwtToken(result!.Token);
|
||||
|
||||
|
||||
string expValue = token.Claims.First(c => c.Type == "exp").Value; // get token claims
|
||||
DateTimeOffset tokenExpiration = DateTimeOffset.FromUnixTimeSeconds(long.Parse(expValue));
|
||||
DateTimeOffset expectedExpiration = beforeRequest.AddMinutes(15);
|
||||
@@ -142,7 +142,7 @@ public class AuthLoginTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
// Assert
|
||||
// check for timing consistency to prevent timing attacks
|
||||
double stdDev = CalculateStandardDeviation(timings);
|
||||
stdDev.Should().BeLessThan(200);
|
||||
stdDev.Should().BeLessThan(200);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -267,7 +267,10 @@ public class AuthLoginTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
Username = _testUsername,
|
||||
Password = _hashService.HashString("TestPass123!"),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
@@ -287,47 +290,10 @@ public class AuthLoginTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
Password = _hashService.HashString("TestPass123!"),
|
||||
TotpSecret = _cryptographyService.Encrypt(Convert.ToBase64String(totpSecret)),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
return user;
|
||||
}
|
||||
|
||||
private async Task<UserEntity> CreateTestUserWithTempPassword()
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
|
||||
UserEntity user = new()
|
||||
{
|
||||
Username = _testUsername,
|
||||
Password = _hashService.HashString("TestPass123!"),
|
||||
TempPassword = Uri.EscapeDataString(_hashService.HashString("TempPass123!")),
|
||||
TempPasswordCreated = DateTime.UtcNow,
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
await dbContext.SaveChangesAsync();
|
||||
return user;
|
||||
}
|
||||
|
||||
private async Task<UserEntity> CreateTestUserWithExpiredTempPassword()
|
||||
{
|
||||
using IServiceScope scope = _apiFactory.Services.CreateScope();
|
||||
LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService<LuminaDbContext>();
|
||||
|
||||
UserEntity user = new()
|
||||
{
|
||||
Username = _testUsername,
|
||||
Password = _hashService.HashString("TestPass123!"),
|
||||
TempPassword = Uri.EscapeDataString(_hashService.HashString("TempPass123!")),
|
||||
TempPasswordCreated = DateTime.UtcNow.AddMinutes(-16), // expired (> 15 minutes old)
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
+4
-1
@@ -223,7 +223,10 @@ public class AuthRegisterTests : IClassFixture<LuminaApiFactory>, IDisposable
|
||||
Username = _testUsername,
|
||||
Password = _hashService.HashString("TestPass123!"),
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
dbContext.Users.Add(user);
|
||||
|
||||
+6
-3
@@ -44,13 +44,16 @@ public class UserEntityFixture
|
||||
Id = userId,
|
||||
Username = default!,
|
||||
Password = default!,
|
||||
Created = default,
|
||||
CreatedOnUtc = default,
|
||||
TotpSecret = default,
|
||||
Libraries = libraries
|
||||
Libraries = libraries,
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = userId
|
||||
})
|
||||
.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.CreatedOnUtc, f => f.Date.Past())
|
||||
.RuleFor(x => x.TotpSecret, f => f.Random.Bool() ? f.Random.String2(32) : null)
|
||||
.Generate();
|
||||
}
|
||||
|
||||
+6
-6
@@ -42,8 +42,8 @@ public class UserEntityMappingTests
|
||||
result.Should().NotBeNull();
|
||||
result.Id.Should().Be(entity.Id);
|
||||
result.Username.Should().Be(entity.Username);
|
||||
result.Created.Should().Be(entity.Created);
|
||||
result.Updated.Should().Be(entity.Updated);
|
||||
result.Created.Should().Be(entity.CreatedOnUtc);
|
||||
result.Updated.Should().Be(entity.UpdatedOnUtc);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -70,14 +70,14 @@ public class UserEntityMappingTests
|
||||
{
|
||||
// Arrange
|
||||
UserEntity entity = UserEntityFixture.CreateUserEntity();
|
||||
entity.Updated = DateTime.UtcNow.AddDays(-1);
|
||||
entity.UpdatedOnUtc = DateTime.UtcNow.AddDays(-1);
|
||||
|
||||
// Act
|
||||
UserResponse result = entity.ToResponse();
|
||||
|
||||
// Assert
|
||||
result.Should().NotBeNull();
|
||||
result.Updated.Should().Be(entity.Updated);
|
||||
result.Updated.Should().Be(entity.UpdatedOnUtc);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
@@ -93,7 +93,7 @@ public class UserEntityMappingTests
|
||||
result.Should().NotBeNull();
|
||||
result.Id.Should().Be(entity.Id);
|
||||
result.Username.Should().Be(entity.Username);
|
||||
result.Created.Should().Be(entity.Created);
|
||||
result.Updated.Should().Be(entity.Updated);
|
||||
result.Created.Should().Be(entity.CreatedOnUtc);
|
||||
result.Updated.Should().Be(entity.UpdatedOnUtc);
|
||||
}
|
||||
}
|
||||
+21
-12
@@ -36,11 +36,12 @@ public class UserMappingTests
|
||||
Username = "oldusername",
|
||||
Password = "hashedpassword",
|
||||
TotpSecret = "totpsecret",
|
||||
VerificationToken = "verificationtoken",
|
||||
VerificationTokenCreated = DateTime.UtcNow,
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow,
|
||||
Updated = null
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
UpdatedOnUtc = null
|
||||
};
|
||||
|
||||
// Act
|
||||
@@ -55,11 +56,13 @@ public class UserMappingTests
|
||||
result.Id.Should().Be(repositoryEntity.Id);
|
||||
result.Password.Should().Be(repositoryEntity.Password);
|
||||
result.TotpSecret.Should().Be(repositoryEntity.TotpSecret);
|
||||
result.VerificationToken.Should().Be(repositoryEntity.VerificationToken);
|
||||
result.VerificationTokenCreated.Should().Be(repositoryEntity.VerificationTokenCreated);
|
||||
result.Libraries.Should().BeSameAs(repositoryEntity.Libraries);
|
||||
result.Created.Should().Be(repositoryEntity.Created);
|
||||
result.Updated.Should().Be(repositoryEntity.Updated);
|
||||
result.UserPermissions.Should().BeSameAs(repositoryEntity.UserPermissions);
|
||||
result.UserRoles.Should().BeSameAs(repositoryEntity.UserRoles);
|
||||
result.CreatedOnUtc.Should().Be(repositoryEntity.CreatedOnUtc);
|
||||
result.CreatedBy.Should().Be(repositoryEntity.CreatedBy);
|
||||
result.UpdatedOnUtc.Should().Be(repositoryEntity.UpdatedOnUtc);
|
||||
result.UpdatedBy.Should().Be(repositoryEntity.UpdatedBy);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
@@ -81,8 +84,11 @@ public class UserMappingTests
|
||||
Username = "oldusername",
|
||||
Password = "hashedpassword",
|
||||
Libraries = [],
|
||||
Created = DateTime.UtcNow,
|
||||
Updated = null
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
UpdatedOnUtc = null
|
||||
};
|
||||
|
||||
// Act
|
||||
@@ -132,8 +138,11 @@ public class UserMappingTests
|
||||
Username = "oldusername",
|
||||
Password = "hashedpassword",
|
||||
Libraries = libraries,
|
||||
Created = DateTime.UtcNow,
|
||||
Updated = null
|
||||
UserPermissions = [],
|
||||
UserRoles = [],
|
||||
CreatedBy = Guid.NewGuid(),
|
||||
CreatedOnUtc = DateTime.UtcNow,
|
||||
UpdatedOnUtc = null
|
||||
};
|
||||
|
||||
// Act
|
||||
|
||||
+6
-1
@@ -7,6 +7,7 @@ using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Security;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.Application.Core.Maintenance.ApplicationSetup.Commands.SetupApplication;
|
||||
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
|
||||
using Lumina.Application.UnitTests.Core.Maintenance.ApplicationSetup.Commands.SetupApplication.Fixtures;
|
||||
@@ -33,6 +34,7 @@ public class SetupApplicationCommandHandlerTests
|
||||
private readonly ITotpTokenGenerator _mockTotpTokenGenerator;
|
||||
private readonly IQRCodeGenerator _mockQRCodeGenerator;
|
||||
private readonly IUserRepository _mockUserRepository;
|
||||
private readonly IDateTimeProvider _mockDateTimeProvider;
|
||||
private readonly SetupApplicationCommandHandler _sut;
|
||||
private readonly SetupApplicationCommandFixture _setupApplicationCommandFixture;
|
||||
|
||||
@@ -47,16 +49,19 @@ public class SetupApplicationCommandHandlerTests
|
||||
_mockTotpTokenGenerator = Substitute.For<ITotpTokenGenerator>();
|
||||
_mockQRCodeGenerator = Substitute.For<IQRCodeGenerator>();
|
||||
_mockUserRepository = Substitute.For<IUserRepository>();
|
||||
_mockDateTimeProvider = Substitute.For<IDateTimeProvider>();
|
||||
_setupApplicationCommandFixture = new SetupApplicationCommandFixture();
|
||||
|
||||
_mockUnitOfWork.GetRepository<IUserRepository>().Returns(_mockUserRepository);
|
||||
_mockDateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
|
||||
|
||||
_sut = new SetupApplicationCommandHandler(
|
||||
_mockUnitOfWork,
|
||||
_mockHashService,
|
||||
_mockCryptographyService,
|
||||
_mockTotpTokenGenerator,
|
||||
_mockQRCodeGenerator);
|
||||
_mockQRCodeGenerator,
|
||||
_mockDateTimeProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+6
-1
@@ -7,6 +7,7 @@ using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Security;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.Application.Core.UsersManagement.Authentication.Commands.RegisterUser;
|
||||
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
|
||||
using Lumina.Application.UnitTests.Core.UsersManagement.Authentication.Commands.RegisterUser.Fixture;
|
||||
@@ -32,6 +33,7 @@ public class RegisterUserCommandHandlerTests
|
||||
private readonly ITotpTokenGenerator _mockTotpTokenGenerator;
|
||||
private readonly IQRCodeGenerator _mockQRCodeGenerator;
|
||||
private readonly IUserRepository _mockUserRepository;
|
||||
private readonly IDateTimeProvider _mockDateTimeProvider;
|
||||
private readonly RegisterUserCommandHandler _sut;
|
||||
|
||||
/// <summary>
|
||||
@@ -45,15 +47,18 @@ public class RegisterUserCommandHandlerTests
|
||||
_mockTotpTokenGenerator = Substitute.For<ITotpTokenGenerator>();
|
||||
_mockQRCodeGenerator = Substitute.For<IQRCodeGenerator>();
|
||||
_mockUserRepository = Substitute.For<IUserRepository>();
|
||||
_mockDateTimeProvider = Substitute.For<IDateTimeProvider>();
|
||||
|
||||
_mockUnitOfWork.GetRepository<IUserRepository>().Returns(_mockUserRepository);
|
||||
_mockDateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
|
||||
|
||||
_sut = new RegisterUserCommandHandler(
|
||||
_mockUnitOfWork,
|
||||
_mockHashService,
|
||||
_mockCryptographyService,
|
||||
_mockTotpTokenGenerator,
|
||||
_mockQRCodeGenerator);
|
||||
_mockQRCodeGenerator,
|
||||
_mockDateTimeProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+6
-1
@@ -7,6 +7,7 @@ using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Errors;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Security;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.Application.Core.UsersManagement.Authentication.Queries.LoginUser;
|
||||
using Lumina.Application.UnitTests.Common.Mapping.UserManagement.Users.Fixtures;
|
||||
using Lumina.Contracts.Responses.Authentication;
|
||||
@@ -31,6 +32,7 @@ public class LoginUserQueryHandlerTests
|
||||
private readonly ITotpTokenGenerator _mockTotpTokenGenerator;
|
||||
private readonly ICryptographyService _mockCryptographyService;
|
||||
private readonly IUserRepository _mockUserRepository;
|
||||
private readonly IDateTimeProvider _mockDateTimeProvider;
|
||||
private readonly LoginUserQueryHandler _sut;
|
||||
|
||||
/// <summary>
|
||||
@@ -44,15 +46,18 @@ public class LoginUserQueryHandlerTests
|
||||
_mockTotpTokenGenerator = Substitute.For<ITotpTokenGenerator>();
|
||||
_mockCryptographyService = Substitute.For<ICryptographyService>();
|
||||
_mockUserRepository = Substitute.For<IUserRepository>();
|
||||
_mockDateTimeProvider = Substitute.For<IDateTimeProvider>();
|
||||
|
||||
_mockUnitOfWork.GetRepository<IUserRepository>().Returns(_mockUserRepository);
|
||||
_mockDateTimeProvider.UtcNow.Returns(DateTime.UtcNow);
|
||||
|
||||
_sut = new LoginUserQueryHandler(
|
||||
_mockUnitOfWork,
|
||||
_mockHashService,
|
||||
_mockJwtTokenGenerator,
|
||||
_mockTotpTokenGenerator,
|
||||
_mockCryptographyService);
|
||||
_mockCryptographyService,
|
||||
_mockDateTimeProvider);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
|
||||
+9
@@ -2,10 +2,13 @@
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.DataAccess.Common.DependencyInjection;
|
||||
using Lumina.DataAccess.Core.Repositories.Books;
|
||||
using Lumina.DataAccess.Core.Repositories.Common.Factory;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.DataAccess.UnitTests.Common.Setup;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -46,6 +49,8 @@ public class DataAccessLayerServicesTests
|
||||
|
||||
// Act
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
services.AddTransient<ICurrentUserService, TestCurrentUserService>();
|
||||
services.AddTransient<IDateTimeProvider, TestDateTimeProvider>();
|
||||
|
||||
// Assert
|
||||
ServiceDescriptor? descriptor = services.SingleOrDefault(d => d.ServiceType == typeof(DbContextOptions<LuminaDbContext>));
|
||||
@@ -65,6 +70,10 @@ public class DataAccessLayerServicesTests
|
||||
|
||||
// Act
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
|
||||
services.AddTransient<ICurrentUserService, TestCurrentUserService>();
|
||||
services.AddTransient<IDateTimeProvider, TestDateTimeProvider>();
|
||||
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
|
||||
// Assert
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using System;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Fake service for retrieving the current user's information during tests.
|
||||
/// </summary>
|
||||
public class TestCurrentUserService : ICurrentUserService
|
||||
{
|
||||
private Guid userId;
|
||||
public Guid? UserId => userId;
|
||||
|
||||
/// <summary>
|
||||
/// Sets the test value of the user id.
|
||||
/// </summary>
|
||||
/// <param name="userId">The user id to use during tests.</param>
|
||||
public void SetUserId(Guid userId)
|
||||
{
|
||||
this.userId = userId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
#endregion
|
||||
|
||||
namespace Lumina.DataAccess.UnitTests.Common.Setup;
|
||||
|
||||
/// <summary>
|
||||
/// Service for time related concerns
|
||||
/// </summary>
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class TestDateTimeProvider : IDateTimeProvider
|
||||
{
|
||||
/// <summary>
|
||||
/// The current UTC time.
|
||||
/// </summary>
|
||||
public DateTime UtcNow => DateTime.UtcNow;
|
||||
}
|
||||
+8
-2
@@ -1,8 +1,11 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.DataAccess.Common.DependencyInjection;
|
||||
using Lumina.DataAccess.Core.Repositories.Common.Factory;
|
||||
using Lumina.DataAccess.UnitTests.Common.Setup;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
@@ -22,6 +25,9 @@ public class RepositoryFactoryTests
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
|
||||
services.AddTransient<ICurrentUserService, TestCurrentUserService>();
|
||||
services.AddTransient<IDateTimeProvider, TestDateTimeProvider>();
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
RepositoryFactory repositoryFactory = new(serviceProvider);
|
||||
|
||||
@@ -48,4 +54,4 @@ public class RepositoryFactoryTests
|
||||
}
|
||||
}
|
||||
|
||||
public interface IUnregisteredRepository { }
|
||||
public interface IUnregisteredRepository { }
|
||||
+4
-4
@@ -44,12 +44,12 @@ public class UserEntityFixture
|
||||
.RuleFor(x => x.Password, f => f.Internet.Password(12))
|
||||
.RuleFor(x => x.TempPassword, f => f.Random.Bool() ? f.Internet.Password(8) : null)
|
||||
.RuleFor(x => x.TotpSecret, f => f.Random.Bool() ? f.Random.String2(32, "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567") : null)
|
||||
.RuleFor(x => x.VerificationToken, f => f.Random.Bool() ? f.Random.Guid().ToString() : null)
|
||||
.RuleFor(x => x.VerificationTokenCreated, (f, u) => u.VerificationToken != null ? f.Date.Past() : null)
|
||||
.RuleFor(x => x.TempPasswordCreated, (f, u) => u.TempPassword != null ? f.Date.Past() : null)
|
||||
.RuleFor(x => x.Libraries, _ => CreateLibraries(_faker.Random.Number(1, 3), userId))
|
||||
.RuleFor(x => x.Created, f => f.Date.Past())
|
||||
.RuleFor(x => x.Updated, f => f.Random.Bool() ? f.Date.Recent() : null)
|
||||
.RuleFor(x => x.CreatedOnUtc, f => f.Date.Past())
|
||||
.RuleFor(x => x.CreatedBy, _ => userId)
|
||||
.RuleFor(x => x.UpdatedOnUtc, f => f.Random.Bool() ? f.Date.Recent() : null)
|
||||
.RuleFor(x => x.UpdatedBy, _ => userId)
|
||||
.Generate();
|
||||
}
|
||||
|
||||
|
||||
+5
-2
@@ -155,8 +155,11 @@ public class UserRepositoryTests
|
||||
TempPassword = "TempPass456",
|
||||
TotpSecret = "NewSecret",
|
||||
Libraries = [],
|
||||
Created = existingUser.Created,
|
||||
Updated = DateTime.UtcNow
|
||||
UserRoles = [],
|
||||
UserPermissions = [],
|
||||
CreatedBy = existingUser.Id,
|
||||
CreatedOnUtc = existingUser.CreatedOnUtc,
|
||||
UpdatedOnUtc = DateTime.UtcNow
|
||||
};
|
||||
|
||||
// Act
|
||||
|
||||
@@ -1,12 +1,15 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using AutoFixture;
|
||||
using AutoFixture.AutoNSubstitute;
|
||||
using FluentAssertions;
|
||||
using Lumina.Application.Common.DataAccess.Repositories.Books;
|
||||
using Lumina.Application.Common.DataAccess.UoW;
|
||||
using Lumina.Application.Common.Infrastructure.Authentication;
|
||||
using Lumina.Application.Common.Infrastructure.Time;
|
||||
using Lumina.DataAccess.Common.DependencyInjection;
|
||||
using Lumina.DataAccess.Core.Repositories.Common.Factory;
|
||||
using Lumina.DataAccess.Core.UoW;
|
||||
using Lumina.DataAccess.UnitTests.Common.Setup;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NSubstitute;
|
||||
@@ -47,6 +50,8 @@ public class UnitOfWorkTests
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
services.AddTransient<ICurrentUserService, TestCurrentUserService>();
|
||||
services.AddTransient<IDateTimeProvider, TestDateTimeProvider>();
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
UnitOfWork unitOfWork = (UnitOfWork)serviceProvider.GetRequiredService<IUnitOfWork>();
|
||||
|
||||
@@ -63,6 +68,8 @@ public class UnitOfWorkTests
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
services.AddTransient<ICurrentUserService, TestCurrentUserService>();
|
||||
services.AddTransient<IDateTimeProvider, TestDateTimeProvider>();
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
UnitOfWork unitOfWork = (UnitOfWork)serviceProvider.GetRequiredService<IUnitOfWork>();
|
||||
int initialCount = unitOfWork.Repositories.Count;
|
||||
@@ -96,6 +103,8 @@ public class UnitOfWorkTests
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
services.AddTransient<ICurrentUserService, TestCurrentUserService>();
|
||||
services.AddTransient<IDateTimeProvider, TestDateTimeProvider>();
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
UnitOfWork unitOfWork = (UnitOfWork)serviceProvider.GetRequiredService<IUnitOfWork>();
|
||||
unitOfWork.ResetRepositories();
|
||||
@@ -113,6 +122,8 @@ public class UnitOfWorkTests
|
||||
// Arrange
|
||||
ServiceCollection services = new();
|
||||
DataAccessLayerServices.AddDataAccessLayerServices(services);
|
||||
services.AddTransient<ICurrentUserService, TestCurrentUserService>();
|
||||
services.AddTransient<IDateTimeProvider, TestDateTimeProvider>();
|
||||
ServiceProvider serviceProvider = services.BuildServiceProvider();
|
||||
UnitOfWork unitOfWork = (UnitOfWork)serviceProvider.GetRequiredService<IUnitOfWork>();
|
||||
|
||||
@@ -135,4 +146,4 @@ public class UnitOfWorkTests
|
||||
// Assert
|
||||
await dbContext.Received(1).SaveChangesAsync(cancellationToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user