mirror of
https://github.com/BinaryAlley/Nemera.git
synced 2026-12-03 18:21:10 +00:00
Refactors the test fixtures and their consumers to compose other fixtures instead of instantiating models inline
This commit is contained in:
1 parent
0abda472c6
commit
3aadd6de84
157 files changed
+1312
-597
No files matched your search
+49
-64
@@ -1,6 +1,8 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Lumina.Application.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.Authorization;
|
||||
using Lumina.Application.Fixtures.Common.DataAccess.Entities.UsersManagement;
|
||||
using Lumina.Domain.SharedKernel.Common.Enums.Authorization;
|
||||
using Lumina.Infrastructure.Core.Authorization;
|
||||
using System;
|
||||
@@ -16,6 +18,13 @@ namespace Lumina.Infrastructure.Fixtures.Core.Authorization;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class AuthorizationServiceFixture
|
||||
{
|
||||
private readonly UserEntityFixture _userEntityFixture = new();
|
||||
private readonly RoleEntityFixture _roleEntityFixture = new();
|
||||
private readonly PermissionEntityFixture _permissionEntityFixture = new();
|
||||
private readonly RolePermissionEntityFixture _rolePermissionEntityFixture = new();
|
||||
private readonly UserRoleEntityFixture _userRoleEntityFixture = new();
|
||||
private readonly UserPermissionEntityFixture _userPermissionEntityFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a user entity with specified permissions and roles.
|
||||
/// </summary>
|
||||
@@ -28,7 +37,6 @@ public class AuthorizationServiceFixture
|
||||
{
|
||||
Guid userId = Guid.NewGuid();
|
||||
DateTime utcNow = DateTime.UtcNow;
|
||||
List<UserPermissionEntity> userPermissions = [];
|
||||
UserRoleEntity? userRole = null;
|
||||
|
||||
if (rolePermissions is not null)
|
||||
@@ -36,86 +44,63 @@ public class AuthorizationServiceFixture
|
||||
foreach (KeyValuePair<string, IEnumerable<AuthorizationPermission>> rolePerm in rolePermissions)
|
||||
{
|
||||
Guid roleId = Guid.NewGuid();
|
||||
RoleEntity role = new()
|
||||
{
|
||||
Id = roleId,
|
||||
RoleName = rolePerm.Key,
|
||||
CreatedOnUtc = utcNow,
|
||||
CreatedBy = userId,
|
||||
RolePermissions = []
|
||||
};
|
||||
RoleEntity role = _roleEntityFixture.Create(
|
||||
id: roleId,
|
||||
roleName: rolePerm.Key,
|
||||
createdBy: userId,
|
||||
createdOnUtc: utcNow);
|
||||
|
||||
foreach (AuthorizationPermission permission in rolePerm.Value)
|
||||
{
|
||||
Guid permissionId = Guid.NewGuid();
|
||||
PermissionEntity permissionEntity = new()
|
||||
{
|
||||
Id = permissionId,
|
||||
PermissionName = permission,
|
||||
CreatedOnUtc = utcNow,
|
||||
CreatedBy = userId
|
||||
};
|
||||
PermissionEntity permissionEntity = _permissionEntityFixture.Create(
|
||||
id: permissionId,
|
||||
permissionName: permission,
|
||||
createdBy: userId,
|
||||
createdOnUtc: utcNow);
|
||||
|
||||
role.RolePermissions.Add(new RolePermissionEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
RoleId = roleId,
|
||||
Role = role,
|
||||
PermissionId = permissionId,
|
||||
Permission = permissionEntity,
|
||||
CreatedOnUtc = utcNow,
|
||||
CreatedBy = userId
|
||||
});
|
||||
role.RolePermissions.Add(_rolePermissionEntityFixture.Create(
|
||||
role: role,
|
||||
permission: permissionEntity,
|
||||
createdBy: userId,
|
||||
createdOnUtc: utcNow));
|
||||
}
|
||||
|
||||
userRole = new UserRoleEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserId = userId,
|
||||
User = null!,
|
||||
RoleId = roleId,
|
||||
Role = role,
|
||||
CreatedOnUtc = utcNow,
|
||||
CreatedBy = userId
|
||||
};
|
||||
userRole = _userRoleEntityFixture.Create(
|
||||
userId: userId,
|
||||
roleId: roleId,
|
||||
role: role,
|
||||
includeUser: false,
|
||||
createdBy: userId,
|
||||
createdOnUtc: utcNow);
|
||||
}
|
||||
}
|
||||
|
||||
UserEntity user = new()
|
||||
{
|
||||
Id = userId,
|
||||
Username = "test-user",
|
||||
Password = "hashed-password",
|
||||
CreatedOnUtc = utcNow,
|
||||
CreatedBy = userId,
|
||||
Libraries = [],
|
||||
UserPermissions = userPermissions,
|
||||
UserRole = userRole
|
||||
};
|
||||
UserEntity user = _userEntityFixture.Create(
|
||||
username: "test-user",
|
||||
password: "hashed-password",
|
||||
id: userId,
|
||||
userRole: userRole,
|
||||
includeUserRole: true,
|
||||
createdBy: userId,
|
||||
createdOnUtc: utcNow);
|
||||
|
||||
if (directPermissions is not null)
|
||||
{
|
||||
foreach (AuthorizationPermission permission in directPermissions)
|
||||
{
|
||||
Guid permissionId = Guid.NewGuid();
|
||||
PermissionEntity permissionEntity = new()
|
||||
{
|
||||
Id = permissionId,
|
||||
PermissionName = permission,
|
||||
CreatedOnUtc = utcNow,
|
||||
CreatedBy = userId
|
||||
};
|
||||
PermissionEntity permissionEntity = _permissionEntityFixture.Create(
|
||||
id: permissionId,
|
||||
permissionName: permission,
|
||||
createdBy: userId,
|
||||
createdOnUtc: utcNow);
|
||||
|
||||
userPermissions.Add(new UserPermissionEntity
|
||||
{
|
||||
Id = Guid.NewGuid(),
|
||||
UserId = userId,
|
||||
User = user,
|
||||
PermissionId = permissionId,
|
||||
Permission = permissionEntity,
|
||||
CreatedOnUtc = utcNow,
|
||||
CreatedBy = userId
|
||||
});
|
||||
user.UserPermissions.Add(_userPermissionEntityFixture.Create(
|
||||
user,
|
||||
permissionEntity,
|
||||
createdOnUtc: utcNow,
|
||||
createdBy: userId));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+6
-6
@@ -1,6 +1,7 @@
|
||||
#region ========================================================================= USING =====================================================================================
|
||||
using Bogus;
|
||||
using Lumina.Infrastructure.Common.Models.DTO.Configuration;
|
||||
using Lumina.Infrastructure.Fixtures.Common.Models.DTO.Configuration;
|
||||
using Microsoft.Extensions.Options;
|
||||
using NSubstitute;
|
||||
using System;
|
||||
@@ -16,21 +17,20 @@ namespace Lumina.Infrastructure.Fixtures.Core.Security;
|
||||
[ExcludeFromCodeCoverage]
|
||||
public class CryptographyServiceFixture
|
||||
{
|
||||
private readonly EncryptionSettingsDtoFixture _encryptionSettingsDtoFixture = new();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a valid encryption settings model for testing.
|
||||
/// </summary>
|
||||
/// <returns>The configured options.</returns>
|
||||
public static IOptions<EncryptionSettingsDto> CreateEncryptionSettings()
|
||||
public IOptions<EncryptionSettingsDto> CreateEncryptionSettings()
|
||||
{
|
||||
// Generate a valid 256-bit (32 byte) key for AES-256
|
||||
byte[] key = new byte[32];
|
||||
RandomNumberGenerator.Fill(key);
|
||||
string base64Key = Convert.ToBase64String(key);
|
||||
|
||||
EncryptionSettingsDto settings = new()
|
||||
{
|
||||
SecretKey = base64Key
|
||||
};
|
||||
EncryptionSettingsDto settings = _encryptionSettingsDtoFixture.Create(secretKey: base64Key);
|
||||
|
||||
IOptions<EncryptionSettingsDto> options = Substitute.For<IOptions<EncryptionSettingsDto>>();
|
||||
options.Value.Returns(settings);
|
||||
@@ -42,7 +42,7 @@ public class CryptographyServiceFixture
|
||||
/// Creates test data for encryption/decryption.
|
||||
/// </summary>
|
||||
/// <returns>A string to be encrypted/decrypted.</returns>
|
||||
public static string CreateTestData()
|
||||
public string CreateTestData()
|
||||
{
|
||||
return new Faker().Lorem.Sentence();
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user