diff --git a/tests/ZB.MOM.WW.ScadaBridge.IntegrationTests/AuthFlowTests.cs b/tests/ZB.MOM.WW.ScadaBridge.IntegrationTests/AuthFlowTests.cs index 5e7f53c3..4f3d695f 100644 --- a/tests/ZB.MOM.WW.ScadaBridge.IntegrationTests/AuthFlowTests.cs +++ b/tests/ZB.MOM.WW.ScadaBridge.IntegrationTests/AuthFlowTests.cs @@ -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 { private readonly ScadaBridgeWebApplicationFactory _factory; + private readonly WebApplicationFactory _mapped; /// Initializes the test with the shared web application factory. /// The web application factory hosting the app under test. 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, SeededGroupRoleMapper>()))); + } + + /// Maps groups exactly as the seeded LdapGroupMappings rows do (system-wide, no scope rules). + private sealed class SeededGroupRoleMapper : IGroupRoleMapper + { + private static readonly Dictionary 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> MapAsync(IReadOnlyList 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(roles, Scope: result)); + } } /// Verifies that the login endpoint with empty credentials redirects to the login page with an error. @@ -124,7 +154,7 @@ public class AuthFlowTests : IClassFixture 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 return; // GLAuth not running locally — see the login test above. } - var refresher = _factory.Services.GetRequiredService(); + var refresher = _mapped.Services.GetRequiredService(); Assert.True(refresher.IsActive); refresher.Clear(); @@ -206,7 +236,7 @@ public class AuthFlowTests : IClassFixture 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(); var result = await validator.ValidateAsync(stale); @@ -219,7 +249,7 @@ public class AuthFlowTests : IClassFixture 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(); Assert.Equal(SessionValidationAction.Reject, (await validator.ValidateAsync(gone)).Action);