test(integration): give the live GLAuth auth tests a seeded group mapper (#144)

The integration factory's in-memory configuration database is never seeded, so
it holds no LdapGroupMappings and the live GLAuth users mapped to zero roles.
LoginEndpoint_WithValidLdapCredentials_SetsCookieAndRedirects and
ActiveSession_GroupsAreReReadFromTheDirectory_AndAnUnknownUserIsEnded were
passing only because zero-role sign-in and zero-role sessions were allowed.
They now run against a factory variant whose IGroupRoleMapper mirrors the
seeded mappings (SecurityConfiguration.HasData).

Claude-Session: https://claude.ai/code/session_01GT67AFKC45MBz1wPZnVbX8
This commit is contained in:
Joseph Doherty
2026-11-17 11:27:23 -05:00
parent 2d8d5a3d6f
commit 764e75c32f
@@ -1,6 +1,9 @@
using System.Net;
using System.Text.RegularExpressions;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AspNetCore.Mvc.Testing;
using ZB.MOM.WW.ScadaBridge.CentralUI.Auth;
using ZB.MOM.WW.ScadaBridge.Security;
using ZB.MOM.WW.Auth.Abstractions.Roles;
@@ -14,12 +17,39 @@ namespace ZB.MOM.WW.ScadaBridge.IntegrationTests;
public class AuthFlowTests : IClassFixture<ScadaBridgeWebApplicationFactory>
{
private readonly ScadaBridgeWebApplicationFactory _factory;
private readonly WebApplicationFactory<Program> _mapped;
/// <summary>Initializes the test with the shared web application factory.</summary>
/// <param name="factory">The web application factory hosting the app under test.</param>
public AuthFlowTests(ScadaBridgeWebApplicationFactory factory)
{
_factory = factory;
// The factory's in-memory configuration database is never seeded, so it holds no LDAP group
// mappings and every live GLAuth user would map to zero roles — which /auth/login and the
// session refresh now refuse (ScadaBridge#144). The live tests run against this variant,
// whose mapper mirrors the seeded LdapGroupMappings (SecurityConfiguration.HasData).
_mapped = factory.WithWebHostBuilder(b => b.ConfigureTestServices(services =>
services.Replace(ServiceDescriptor.Scoped<IGroupRoleMapper<string>, SeededGroupRoleMapper>())));
}
/// <summary>Maps groups exactly as the seeded <c>LdapGroupMappings</c> rows do (system-wide, no scope rules).</summary>
private sealed class SeededGroupRoleMapper : IGroupRoleMapper<string>
{
private static readonly Dictionary<string, string> Seed = new(StringComparer.OrdinalIgnoreCase)
{
["SCADA-Admins"] = Roles.Administrator,
["SCADA-Designers"] = Roles.Designer,
["SCADA-Deploy-All"] = Roles.Deployer,
["SCADA-Deploy-SiteA"] = Roles.Deployer,
["SCADA-Viewers"] = Roles.Viewer,
};
public Task<GroupRoleMapping<string>> MapAsync(IReadOnlyList<string> groups, CancellationToken ct)
{
var roles = groups.Where(Seed.ContainsKey).Select(g => Seed[g]).Distinct().ToList();
var result = new RoleMappingResult(roles, [], IsSystemWideDeployment: true);
return Task.FromResult(new GroupRoleMapping<string>(roles, Scope: result));
}
}
/// <summary>Verifies that the login endpoint with empty credentials redirects to the login page with an error.</summary>
@@ -124,7 +154,7 @@ public class AuthFlowTests : IClassFixture<ScadaBridgeWebApplicationFactory>
return;
}
var client = _factory.CreateClient(new Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactoryClientOptions
var client = _mapped.CreateClient(new Microsoft.AspNetCore.Mvc.Testing.WebApplicationFactoryClientOptions
{
AllowAutoRedirect = false
});
@@ -197,7 +227,7 @@ public class AuthFlowTests : IClassFixture<ScadaBridgeWebApplicationFactory>
return; // GLAuth not running locally — see the login test above.
}
var refresher = _factory.Services.GetRequiredService<DirectoryGroupRefresher>();
var refresher = _mapped.Services.GetRequiredService<DirectoryGroupRefresher>();
Assert.True(refresher.IsActive);
refresher.Clear();
@@ -206,7 +236,7 @@ public class AuthFlowTests : IClassFixture<ScadaBridgeWebApplicationFactory>
var stale = SessionClaimBuilder.Build("designer", "designer", ["SCADA-Admins"],
new RoleMappingResult([Roles.Administrator], [], true), issued);
using (var scope = _factory.Services.CreateScope())
using (var scope = _mapped.Services.CreateScope())
{
var validator = scope.ServiceProvider.GetRequiredService<CookieSessionValidator>();
var result = await validator.ValidateAsync(stale);
@@ -219,7 +249,7 @@ public class AuthFlowTests : IClassFixture<ScadaBridgeWebApplicationFactory>
var gone = SessionClaimBuilder.Build("no-such-user-46", "x", ["SCADA-Admins"],
new RoleMappingResult([Roles.Administrator], [], true), issued);
using (var scope = _factory.Services.CreateScope())
using (var scope = _mapped.Services.CreateScope())
{
var validator = scope.ServiceProvider.GetRequiredService<CookieSessionValidator>();
Assert.Equal(SessionValidationAction.Reject, (await validator.ValidateAsync(gone)).Action);