using Hutopy.Domain.Constants; using Hutopy.Infrastructure.Data; using Hutopy.Infrastructure.Identity; using MediatR; using Microsoft.AspNetCore.Identity; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.DependencyInjection; namespace Hutopy.Application.FunctionalTests; [SetUpFixture] public partial class Testing { private static ITestDatabase _database; private static CustomWebApplicationFactory _factory = null!; private static IServiceScopeFactory _scopeFactory = null!; private static string? _userId; [OneTimeSetUp] public async Task RunBeforeAnyTests() { _database = await TestDatabaseFactory.CreateAsync(); _factory = new CustomWebApplicationFactory(_database.GetConnection()); _scopeFactory = _factory.Services.GetRequiredService(); } public static async Task SendAsync(IRequest request) { using var scope = _scopeFactory.CreateScope(); var mediator = scope.ServiceProvider.GetRequiredService(); return await mediator.Send(request); } public static async Task SendAsync(IBaseRequest request) { using var scope = _scopeFactory.CreateScope(); var mediator = scope.ServiceProvider.GetRequiredService(); await mediator.Send(request); } public static string? GetUserId() { return _userId; } public static async Task RunAsDefaultUserAsync() { return await RunAsUserAsync("test@local", "Testing1234!", Array.Empty()); } public static async Task RunAsAdministratorAsync() { return await RunAsUserAsync("administrator@local", "Administrator1234!", new[] { Roles.Administrator }); } public static async Task RunAsUserAsync(string userName, string password, string[] roles) { using var scope = _scopeFactory.CreateScope(); var userManager = scope.ServiceProvider.GetRequiredService(); var user = new ApplicationUser { UserName = userName, Email = userName }; var result = await userManager.CreateAsync(user, password); if (roles.Any()) { var roleManager = scope.ServiceProvider.GetRequiredService>(); foreach (var role in roles) { await roleManager.CreateAsync(new IdentityRole(role)); } await userManager.AddToRolesAsync(user, roles); } if (result.Succeeded) { _userId = user.Id; return _userId; } var errors = string.Join(Environment.NewLine, result.ToApplicationResult().Errors); throw new Exception($"Unable to create {userName}.{Environment.NewLine}{errors}"); } public static async Task ResetState() { try { await _database.ResetAsync(); } catch (Exception) { } _userId = null; } public static async Task FindAsync(params object[] keyValues) where TEntity : class { using var scope = _scopeFactory.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); return await context.FindAsync(keyValues); } public static async Task AddAsync(TEntity entity) where TEntity : class { using var scope = _scopeFactory.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); context.Add(entity); await context.SaveChangesAsync(); } public static async Task CountAsync() where TEntity : class { using var scope = _scopeFactory.CreateScope(); var context = scope.ServiceProvider.GetRequiredService(); return await context.Set().CountAsync(); } [OneTimeTearDown] public async Task RunAfterAnyTests() { await _database.DisposeAsync(); await _factory.DisposeAsync(); } }