#region ========================================================================= USING ===================================================================================== using Lumina.Application.Common.DataAccess.Entities.Authorization; using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.Management; using Lumina.Application.Common.DataAccess.Entities.MediaLibrary.WrittenContentLibrary.BookLibrary; using Lumina.Application.Common.DataAccess.Entities.UsersManagement; using Lumina.Contracts.Requests.Authentication; using Lumina.Contracts.Responses.Authentication; using Lumina.DataAccess.Common.Interceptors; using Lumina.DataAccess.Core.UoW; using Lumina.Domain.SharedKernel.Common.Enums.MediaLibrary; using Lumina.Infrastructure.Core.Security; using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Mvc.Testing; using Microsoft.Data.Sqlite; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.IdentityModel.Tokens; using System.Diagnostics.CodeAnalysis; using System.Net.Http; using System.Net.Http.Headers; using System.Net.Http.Json; using System.Text; using System.Text.Json; #endregion namespace Lumina.Presentation.Api.SecurityTests.Common.Setup; /// /// Factory for creating a web application for security tests. /// [ExcludeFromCodeCoverage] public class LuminaApiFactory : WebApplicationFactory, IDisposable { private readonly SqliteConnection _connection; private readonly string _sharedInMemoryConnectionString; private readonly PasswordHashService _hashService = new(); private readonly JsonSerializerOptions _jsonOptions = new() { PropertyNameCaseInsensitive = true }; private const string TEST_ENCRYPTION_KEY = "MTIzNDU2Nzg5MDEyMzQ1Njc4OTAxMjM0NTY3ODkwMTI="; private const string SHARED_IN_MEMORY_CONNECTION_STRING_PREFIX = "Data Source=lumina-tests-{0};Mode=Memory;Cache=Shared"; /// /// Initializes a new instance of the class. /// public LuminaApiFactory() { // each factory gets its own uniquely named shared in-memory database, so that parallel test classes remain isolated from one another; // the shared cache allows each DbContext to open its own pooled connection to the same database, instead of sharing a single connection, // which would otherwise serialize every request on one connection and cause nested transaction errors between requests _sharedInMemoryConnectionString = string.Format(SHARED_IN_MEMORY_CONNECTION_STRING_PREFIX, Guid.NewGuid()); // keep a single anchor connection open, so that the shared in-memory database it refers to is not dropped _connection = new SqliteConnection(_sharedInMemoryConnectionString); _connection.Open(); } /// /// Configures the web host for the security tests. /// /// The web host builder to configure. protected override void ConfigureWebHost(IWebHostBuilder builder) { builder.ConfigureAppConfiguration((context, config) => { // clear all existing configuration sources config.Sources.Clear(); config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); config.AddJsonFile($"appsettings.test.json", optional: true, reloadOnChange: true); config.AddJsonFile("appsettings.shared.json", optional: true, reloadOnChange: true); config.AddJsonFile($"appsettings.shared.test.json", optional: true, reloadOnChange: true); // First add the test values directly config.AddInMemoryCollection(initialData: new Dictionary { ["JwtSettings:SecretKey"] = "test-key-thats-at-least-32-chars-long-for-jwt", ["JwtSettings:ExpiryMinutes"] = "15", // control the expiry time during tests ["JwtSettings:Issuer"] = "Lumina", ["JwtSettings:Audience"] = "Lumina", ["EncryptionSettings:SecretKey"] = TEST_ENCRYPTION_KEY // base64 encoded test key }); }); builder.ConfigureServices(services => { // remove existing DbContext configuration ServiceDescriptor? descriptor = services.SingleOrDefault(serviceDescriptor => serviceDescriptor.ServiceType == typeof(DbContextOptions)); if (descriptor is not null) services.Remove(descriptor); // add SQLite DbContext configuration services.AddDbContext((serviceProvider, options) => { options.UseSqlite(_sharedInMemoryConnectionString); options.AddInterceptors(serviceProvider.GetRequiredService()); }); // configure JWT authentication for testing services.PostConfigure(JwtBearerDefaults.AuthenticationScheme, options => { options.TokenValidationParameters = new TokenValidationParameters { ValidateIssuer = true, ValidateAudience = true, ValidateLifetime = true, ValidateIssuerSigningKey = true, ValidIssuer = "Lumina", ValidAudience = "Lumina", IssuerSigningKey = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("test-key-thats-at-least-32-chars-long-for-jwt")) }; }); // build service provider and ensure database is created ServiceProvider servicePreovider = services.BuildServiceProvider(); using (IServiceScope scope = servicePreovider.CreateScope()) { IServiceProvider scopedServices = scope.ServiceProvider; LuminaDbContext dbContext = scopedServices.GetRequiredService(); dbContext.Database.Migrate(); } }); } /// /// Gets a unique IP address derived from a fresh GUID, to isolate the rate-limiting partition of each test. /// /// A unique IP address in the 192.168.0.0/16 private range. public static string GetUniqueTestIp() { byte[] bytes = Guid.NewGuid().ToByteArray(); return $"192.168.{bytes[0]}.{bytes[1]}"; } /// /// Creates a test user with the Admin role, authenticates it on , and returns its Id. /// /// The HTTP client to configure with authentication headers. /// The Id of the created admin test user. public async Task CreateAndAuthenticateAdminUserAsync(HttpClient client) { // a unique X-Forwarded-For isolates rate limiting state per test client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Add("X-Forwarded-For", GetUniqueTestIp()); using IServiceScope scope = Services.CreateScope(); LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService(); Guid userId = Guid.NewGuid(); string username = $"testuser_{Guid.NewGuid()}"; UserEntity user = new() { Id = userId, Username = username, Password = _hashService.HashString("TestPass123!"), Libraries = [], UserPermissions = [], UserRole = null, CreatedBy = userId, CreatedOnUtc = DateTime.UtcNow }; dbContext.Users.Add(user); await dbContext.SaveChangesAsync(); // seed a dedicated Admin role for this test user, keeping each test isolated in the shared in-memory database Guid roleId = Guid.NewGuid(); RoleEntity role = new() { Id = roleId, RoleName = "Admin", CreatedBy = userId, CreatedOnUtc = DateTime.UtcNow }; dbContext.Roles.Add(role); dbContext.UserRoles.Add(new UserRoleEntity { Id = Guid.NewGuid(), UserId = userId, User = user, RoleId = roleId, Role = role, CreatedBy = userId, CreatedOnUtc = DateTime.UtcNow }); await dbContext.SaveChangesAsync(); HttpResponseMessage loginResponse = await client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest(username, "TestPass123!")); string content = await loginResponse.Content.ReadAsStringAsync(); LoginResponse? loginResult = JsonSerializer.Deserialize(content, _jsonOptions); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loginResult!.Token); return userId; } /// /// Creates a test user, authenticates it on , and returns its Id and username. /// /// The HTTP client to configure with authentication headers. /// The Id and username of the created test user. public async Task<(Guid UserId, string Username)> CreateAndAuthenticateUserAsync(HttpClient client) { // a unique X-Forwarded-For isolates rate limiting state per test client.DefaultRequestHeaders.Clear(); client.DefaultRequestHeaders.Add("X-Forwarded-For", GetUniqueTestIp()); using IServiceScope scope = Services.CreateScope(); LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService(); Guid userId = Guid.NewGuid(); string username = $"testuser_{Guid.NewGuid()}"; UserEntity user = new() { Id = userId, Username = username, Password = _hashService.HashString("TestPass123!"), Libraries = [], UserPermissions = [], UserRole = null, CreatedBy = userId, CreatedOnUtc = DateTime.UtcNow }; dbContext.Users.Add(user); await dbContext.SaveChangesAsync(); HttpResponseMessage loginResponse = await client.PostAsJsonAsync("/api/v1/auth/login", new LoginRequest(username, "TestPass123!")); string content = await loginResponse.Content.ReadAsStringAsync(); LoginResponse? loginResult = JsonSerializer.Deserialize(content, _jsonOptions); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", loginResult!.Token); return (userId, username); } /// /// Removes a test user and its owned libraries from the database. /// /// The username of the test user to remove. public async Task RemoveTestUserAsync(string username) { using IServiceScope scope = Services.CreateScope(); LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService(); UserEntity? user = await dbContext.Users.Include(candidate => candidate.Libraries).FirstOrDefaultAsync(candidate => candidate.Username == username).ConfigureAwait(false); if (user is not null) { dbContext.Libraries.RemoveRange(user.Libraries); dbContext.Users.Remove(user); await dbContext.SaveChangesAsync(); } } /// /// Removes a test admin user, its role, and its user-role link from the database. /// /// The Id of the admin test user to remove. public async Task RemoveAdminUserAsync(Guid userId) { using IServiceScope scope = Services.CreateScope(); LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService(); UserRoleEntity? userRole = await dbContext.UserRoles.FirstOrDefaultAsync(candidate => candidate.UserId == userId).ConfigureAwait(false); if (userRole is not null) { RoleEntity? role = await dbContext.Roles.FirstOrDefaultAsync(candidate => candidate.Id == userRole.RoleId).ConfigureAwait(false); if (role is not null) dbContext.Roles.Remove(role); dbContext.UserRoles.Remove(userRole); } UserEntity? user = await dbContext.Users.FirstOrDefaultAsync(candidate => candidate.Id == userId).ConfigureAwait(false); if (user is not null) dbContext.Users.Remove(user); await dbContext.SaveChangesAsync(); } /// /// Seeds a owned by . /// /// The Id of the library to seed. /// The Id of the user that owns the library. public async Task SeedLibraryAsync(Guid libraryId, Guid userId) { using IServiceScope scope = Services.CreateScope(); LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService(); dbContext.Libraries.Add(new LibraryEntity { Id = libraryId, UserId = userId, Title = "Test Library", LibraryType = LibraryType.EBook, ContentLocations = [], CreatedBy = userId, CreatedOnUtc = DateTime.UtcNow, UpdatedBy = null }); await dbContext.SaveChangesAsync(); } /// /// Seeds a belonging to the media library identified by . /// /// The Id of the media library the book belongs to. /// The title of the book. public async Task SeedBookAsync(Guid libraryId, string title) { using IServiceScope scope = Services.CreateScope(); LuminaDbContext dbContext = scope.ServiceProvider.GetRequiredService(); dbContext.Books.Add(new BookEntity { Id = Guid.NewGuid(), LibraryId = libraryId, Path = $"/books/{Guid.NewGuid()}.epub", Title = title, CreatedBy = Guid.NewGuid(), CreatedOnUtc = DateTime.UtcNow, UpdatedBy = null }); await dbContext.SaveChangesAsync(); } /// /// Disposes the connection to the database. /// public new void Dispose() { _connection.Close(); base.Dispose(); } }