using System.Security.Claims; using Bunit; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.Rendering; using Microsoft.Extensions.DependencyInjection; using ZB.MOM.WW.ScadaBridge.Security; using NavMenu = ZB.MOM.WW.ScadaBridge.CentralUI.Components.Layout.NavMenu; namespace ZB.MOM.WW.ScadaBridge.CentralUI.Tests.Layout; /// /// bUnit rendering tests for the rail . After the /// ZB.MOM.WW.Theme adoption (Task 2.4) the nav is composed of the kit's /// NavRailSection (a native <details> disclosure, expanded /// by default — no JS interop, works in static SSR) and NavRailItem /// links. These tests verify the structure (sections render as /// details.rail-section, not the old button.nav-section-toggle) /// and — the load-bearing security behaviour — that each section's items are /// gated by the correct authorization policy. The AuthorizeView Policy=... /// blocks evaluate the real policies, which require a claim of type /// (the canonical /// ZbClaimTypes.Role framework URI), so the test principal carries /// claims of that exact type. /// public class NavMenuTests : BunitContext { /// /// Renders under a principal holding the given roles. /// 's top-level AuthorizeView requires the /// cascading , so it is rendered inside a /// ; the real policies are /// registered so the per-item AuthorizeView Policy=... blocks are /// genuinely evaluated. /// private IRenderedComponent RenderWithRoles(params string[] roles) { var claims = new List { new(JwtTokenService.UsernameClaimType, "tester") }; claims.AddRange(roles.Select(r => new Claim(JwtTokenService.RoleClaimType, r))); // "TestAuth" authentication type → IsAuthenticated == true. var user = new ClaimsPrincipal(new ClaimsIdentity(claims, "TestAuth")); return RenderWithPrincipal(user); } /// /// Renders with an anonymous (unauthenticated) /// principal — a bare with no authentication /// type, so IsAuthenticated == false and every AuthorizeView /// renders its (empty) NotAuthorized/fallthrough branch. /// private IRenderedComponent RenderAnonymous() => RenderWithPrincipal(new ClaimsPrincipal(new ClaimsIdentity())); private IRenderedComponent RenderWithPrincipal(ClaimsPrincipal user) { Services.AddSingleton(new TestAuthStateProvider(user)); Services.AddAuthorizationCore(); AuthorizationPolicies.AddScadaBridgeAuthorization(Services); // BunitContext pre-registers a placeholder IAuthorizationService that // throws when AuthorizeView evaluates a policy. Force the real service // so the per-item policy gating is genuinely exercised. Services.AddSingleton(); var host = Render(parameters => parameters .Add(p => p.ChildContent, (RenderFragment)(builder => { builder.OpenComponent(0); builder.CloseComponent(); }))); return host.FindComponent(); } [Fact] public void Sections_RenderAsKitDetailsDisclosures_ExpandedByDefault() { var cut = RenderWithRoles("Administrator", "Designer", "Deployer"); cut.WaitForAssertion(() => { // Kit nav: sections are
disclosures, // NOT the old interactive button.nav-section-toggle. Assert.Empty(cut.FindAll("button.nav-section-toggle")); Assert.NotEmpty(cut.FindAll("details.rail-section")); // Section eyebrows still render their titles. Assert.Contains(">Notifications<", cut.Markup); Assert.Contains(">Deployment<", cut.Markup); // The kit's NavRailSection defaults to open, so items are in the DOM // without any toggle/JS — this is the static-SSR-friendly behaviour. Assert.Contains("/notifications/smtp", cut.Markup); Assert.Contains("/deployment/topology", cut.Markup); }); } [Fact] public void DeploymentSection_RendersAllItems_ForDeployer() { var cut = RenderWithRoles("Deployer"); cut.WaitForAssertion(() => { Assert.Contains("/deployment/topology", cut.Markup); Assert.Contains("/deployment/deployments", cut.Markup); Assert.Contains("/deployment/debug-view", cut.Markup); }); } [Fact] public void NotificationsSection_ShowsAllItems_ForMultiRoleUser() { var cut = RenderWithRoles("Administrator", "Designer", "Deployer"); cut.WaitForAssertion(() => { Assert.Contains("Notifications", cut.Markup); Assert.Contains("/notifications/smtp", cut.Markup); Assert.Contains("/notifications/lists", cut.Markup); Assert.Contains("/notifications/report", cut.Markup); Assert.Contains("/notifications/kpis", cut.Markup); }); } [Fact] public void NotificationsSection_AdminOnlyUser_SeesOnlySmtp() { var cut = RenderWithRoles("Administrator"); cut.WaitForAssertion(() => { Assert.Contains("/notifications/smtp", cut.Markup); Assert.DoesNotContain("/notifications/report", cut.Markup); Assert.DoesNotContain("/notifications/lists", cut.Markup); Assert.DoesNotContain("/notifications/kpis", cut.Markup); }); } [Fact] public void OldRoutes_AreNoLongerLinked() { var cut = RenderWithRoles("Administrator", "Designer", "Deployer"); cut.WaitForAssertion(() => { Assert.DoesNotContain("/admin/smtp", cut.Markup); Assert.DoesNotContain("/monitoring/notification-outbox", cut.Markup); }); } // ── AuthorizeView-gated SECTION visibility (the GAPS open question) ────── // A whole NavRailSection wrapped in must SHOW // (heading + items) for an authorized principal and be entirely ABSENT for // an unauthorized/anonymous one. The Audit section (OperationalAudit policy) // is the witness — its heading lives inside the policy block, so a denied // user must not even see the eyebrow. [Fact] public void PolicyGatedSection_IsVisible_ForAuthorizedPrincipal() { // OperationalAudit is satisfied by the Administrator role. var cut = RenderWithRoles("Administrator"); cut.WaitForAssertion(() => { Assert.Contains(">Audit<", cut.Markup); // section heading present Assert.Contains("/audit/log", cut.Markup); // gated items present Assert.Contains("/audit/configuration", cut.Markup); }); } [Fact] public void PolicyGatedSection_IsAbsent_ForUnauthorizedPrincipal() { // Designer satisfies RequireDesign but NOT OperationalAudit. var cut = RenderWithRoles("Designer"); cut.WaitForAssertion(() => { Assert.DoesNotContain(">Audit<", cut.Markup); // heading hidden, not just items Assert.DoesNotContain("/audit/log", cut.Markup); Assert.DoesNotContain("/audit/configuration", cut.Markup); }); } [Fact] public void PolicyGatedSections_AllAbsent_ForAnonymousPrincipal() { var cut = RenderAnonymous(); cut.WaitForAssertion(() => { // The Dashboard link is always-visible (outside every AuthorizeView)... Assert.Contains("Dashboard", cut.Markup); // ...but no policy-gated section or its items leak to an anonymous user. Assert.DoesNotContain(">Admin<", cut.Markup); Assert.DoesNotContain(">Design<", cut.Markup); Assert.DoesNotContain(">Deployment<", cut.Markup); Assert.DoesNotContain(">Notifications<", cut.Markup); Assert.DoesNotContain(">Audit<", cut.Markup); Assert.DoesNotContain("/admin/sites", cut.Markup); Assert.DoesNotContain("/design/templates", cut.Markup); Assert.DoesNotContain("/audit/log", cut.Markup); }); } }