test: exercise team member rollback through DI

This commit is contained in:
Alexandre Brandizzi 2026-09-23 03:44:44 -03:00
parent 3b626cca58
commit 5aa3a6f820

View file

@ -6,12 +6,17 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using SeaHaven.DataServices.Dto;
using SeaHaven.DataServices.Implementation;
using SeaHaven.DataServices.DependencyInjection;
using SeaHaven.DataServices.Interfaces;
using SeaHaven.Services.DTOs;
using SeaHaven.Services.DependencyInjection;
using SeaHaven.Services.Implementation;
using SeaHaven.Services.Interfaces;
namespace SeaHavenIndustries.Tests;
@ -30,18 +35,27 @@ public sealed class TeamMemberCreateTransactionTests
await using (var setup = new SqliteTeamMemberTestDbContext(options))
await setup.Database.EnsureCreatedAsync();
await using (var createScope = new SqliteTeamMemberTestDbContext(options))
{
var permissionData = new ThrowingAfterPersistPermissionDataService(
new TeamPermissionOverrideDataService(createScope));
var service = new TeamMemberService(
CreateUserManager(createScope),
CreateRoleManager(createScope),
new UserServiceAreaDataService(createScope),
permissionData,
new UserDataService(createScope),
permissionService: null!);
var configuration = new ConfigurationBuilder().Build();
var services = new ServiceCollection();
services.AddLogging();
services.AddDbContext<ApplicationDbContext>(builder => builder.UseSqlite(connection));
services.Replace(ServiceDescriptor.Scoped<ApplicationDbContext>(provider =>
new SqliteTeamMemberTestDbContext(
provider.GetRequiredService<DbContextOptions<ApplicationDbContext>>())));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddDataServices();
services.AddBusinessServices(configuration);
services.AddScoped<TeamPermissionOverrideDataService>();
services.AddScoped<ITeamPermissionOverrideDataService>(provider =>
new ThrowingAfterPersistPermissionDataService(
provider.GetRequiredService<TeamPermissionOverrideDataService>()));
await using var serviceProvider = services.BuildServiceProvider();
await using (var createScope = serviceProvider.CreateAsyncScope())
{
var service = createScope.ServiceProvider.GetRequiredService<ITeamMemberService>();
var failure = await Record.ExceptionAsync(() => service.CreateAsync(
ValidRequest(),
Admin(),
@ -51,7 +65,8 @@ public sealed class TeamMemberCreateTransactionTests
Assert.Equal("injected mid-sequence failure", failure!.Message);
}
await using var verify = new SqliteTeamMemberTestDbContext(options);
await using var verifyScope = serviceProvider.CreateAsyncScope();
var verify = verifyScope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
Assert.Empty(await verify.Users.AsNoTracking().ToListAsync());
Assert.Empty(await verify.Roles.AsNoTracking().ToListAsync());
Assert.Empty(await verify.UserRoles.AsNoTracking().ToListAsync());
@ -59,26 +74,6 @@ public sealed class TeamMemberCreateTransactionTests
Assert.Empty(await verify.UserPermissionOverrides.AsNoTracking().ToListAsync());
}
private static UserManager<ApplicationUser> CreateUserManager(ApplicationDbContext context) =>
new(
new UserStore<ApplicationUser>(context),
Microsoft.Extensions.Options.Options.Create(new IdentityOptions()),
new PasswordHasher<ApplicationUser>(),
Array.Empty<IUserValidator<ApplicationUser>>(),
Array.Empty<IPasswordValidator<ApplicationUser>>(),
new UpperInvariantLookupNormalizer(),
new IdentityErrorDescriber(),
new EmptyServiceProvider(),
NullLogger<UserManager<ApplicationUser>>.Instance);
private static RoleManager<IdentityRole> CreateRoleManager(ApplicationDbContext context) =>
new(
new RoleStore<IdentityRole>(context),
Array.Empty<IRoleValidator<IdentityRole>>(),
new UpperInvariantLookupNormalizer(),
new IdentityErrorDescriber(),
NullLogger<RoleManager<IdentityRole>>.Instance);
private static CreateTeamMemberRequestDTO ValidRequest() => new()
{
Name = "Taylor Dispatcher",
@ -155,8 +150,4 @@ public sealed class TeamMemberCreateTransactionTests
_inner.ClearOverridesAsync(userId, cancellationToken);
}
private sealed class EmptyServiceProvider : IServiceProvider
{
public object? GetService(Type serviceType) => null;
}
}