mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 11: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
+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