Files
social-media/tests/Application.FunctionalTests/Testing.cs

156 lines
4.2 KiB
C#

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 Guid _userId;
[OneTimeSetUp]
public async Task RunBeforeAnyTests()
{
_database = await TestDatabaseFactory.CreateAsync();
_factory = new CustomWebApplicationFactory(_database.GetConnection());
_scopeFactory = _factory.Services.GetRequiredService<IServiceScopeFactory>();
}
public static async Task<TResponse> SendAsync<TResponse>(IRequest<TResponse> request)
{
using var scope = _scopeFactory.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<ISender>();
return await mediator.Send(request);
}
public static async Task SendAsync(IBaseRequest request)
{
using var scope = _scopeFactory.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<ISender>();
await mediator.Send(request);
}
public static Guid GetUserId()
{
return _userId;
}
public static async Task<Guid> RunAsDefaultUserAsync()
{
return await RunAsUserAsync(
"test@local",
"Testing1234!",
[]);
}
public static async Task<Guid> RunAsAdministratorAsync()
{
return await RunAsUserAsync(
"administrator@local",
"Administrator1234!",
[Roles.Administrator]);
}
public static async Task<Guid> RunAsUserAsync(string userName, string password, string[] roles)
{
using var scope = _scopeFactory.CreateScope();
var userManager = scope.ServiceProvider.GetRequiredService<ApplicationUserManager>();
var user = new ApplicationUser
{
UserName = userName, Email = userName, Firstname = "FirstName", Lastname = "LastName"
};
var result = await userManager.CreateAsync(user, password);
if (roles.Any())
{
var roleManager = scope.ServiceProvider.GetRequiredService<RoleManager<IdentityRole>>();
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 = Guid.Empty;
}
public static async Task<TEntity?> FindAsync<TEntity>(params object[] keyValues)
where TEntity : class
{
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
return await context.FindAsync<TEntity>(keyValues);
}
public static async Task AddAsync<TEntity>(TEntity entity)
where TEntity : class
{
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
context.Add(entity);
await context.SaveChangesAsync();
}
public static async Task<int> CountAsync<TEntity>() where TEntity : class
{
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
return await context.Set<TEntity>().CountAsync();
}
[OneTimeTearDown]
public async Task RunAfterAnyTests()
{
await _database.DisposeAsync();
await _factory.DisposeAsync();
}
}