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.Data.Sqlite;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata; 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.Dto;
using SeaHaven.DataServices.Implementation; using SeaHaven.DataServices.Implementation;
using SeaHaven.DataServices.DependencyInjection;
using SeaHaven.DataServices.Interfaces; using SeaHaven.DataServices.Interfaces;
using SeaHaven.Services.DTOs; using SeaHaven.Services.DTOs;
using SeaHaven.Services.DependencyInjection;
using SeaHaven.Services.Implementation; using SeaHaven.Services.Implementation;
using SeaHaven.Services.Interfaces;
namespace SeaHavenIndustries.Tests; namespace SeaHavenIndustries.Tests;
@ -30,18 +35,27 @@ public sealed class TeamMemberCreateTransactionTests
await using (var setup = new SqliteTeamMemberTestDbContext(options)) await using (var setup = new SqliteTeamMemberTestDbContext(options))
await setup.Database.EnsureCreatedAsync(); await setup.Database.EnsureCreatedAsync();
await using (var createScope = new SqliteTeamMemberTestDbContext(options)) var configuration = new ConfigurationBuilder().Build();
{ var services = new ServiceCollection();
var permissionData = new ThrowingAfterPersistPermissionDataService( services.AddLogging();
new TeamPermissionOverrideDataService(createScope)); services.AddDbContext<ApplicationDbContext>(builder => builder.UseSqlite(connection));
var service = new TeamMemberService( services.Replace(ServiceDescriptor.Scoped<ApplicationDbContext>(provider =>
CreateUserManager(createScope), new SqliteTeamMemberTestDbContext(
CreateRoleManager(createScope), provider.GetRequiredService<DbContextOptions<ApplicationDbContext>>())));
new UserServiceAreaDataService(createScope), services.AddIdentity<ApplicationUser, IdentityRole>()
permissionData, .AddEntityFrameworkStores<ApplicationDbContext>()
new UserDataService(createScope), .AddDefaultTokenProviders();
permissionService: null!); 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( var failure = await Record.ExceptionAsync(() => service.CreateAsync(
ValidRequest(), ValidRequest(),
Admin(), Admin(),
@ -51,7 +65,8 @@ public sealed class TeamMemberCreateTransactionTests
Assert.Equal("injected mid-sequence failure", failure!.Message); 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.Users.AsNoTracking().ToListAsync());
Assert.Empty(await verify.Roles.AsNoTracking().ToListAsync()); Assert.Empty(await verify.Roles.AsNoTracking().ToListAsync());
Assert.Empty(await verify.UserRoles.AsNoTracking().ToListAsync()); Assert.Empty(await verify.UserRoles.AsNoTracking().ToListAsync());
@ -59,26 +74,6 @@ public sealed class TeamMemberCreateTransactionTests
Assert.Empty(await verify.UserPermissionOverrides.AsNoTracking().ToListAsync()); 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() private static CreateTeamMemberRequestDTO ValidRequest() => new()
{ {
Name = "Taylor Dispatcher", Name = "Taylor Dispatcher",
@ -155,8 +150,4 @@ public sealed class TeamMemberCreateTransactionTests
_inner.ClearOverridesAsync(userId, cancellationToken); _inner.ClearOverridesAsync(userId, cancellationToken);
} }
private sealed class EmptyServiceProvider : IServiceProvider
{
public object? GetService(Type serviceType) => null;
}
} }