fix(admin-ui): align task orientation and navigation with page policies

This commit is contained in:
Joseph Doherty
2026-11-13 21:42:30 -05:00
parent 28add408c4
commit cb3400c13c
38 changed files with 1996 additions and 58 deletions
@@ -0,0 +1,77 @@
using Bunit;
using Bunit.TestDoubles;
using Microsoft.AspNetCore.Components;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Layout;
using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Shared;
using ZB.MOM.WW.OtOpcUa.Security.Auth;
using ZB.MOM.WW.Secrets.Ui;
namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Pages;
/// <summary>Real rail destinations retain their route-policy visibility.</summary>
[Trait("Category", "Unit")]
public sealed class NavigationPolicyRenderTests : BunitContext
{
[Theory]
[InlineData(false, false)]
[InlineData(true, false)]
[InlineData(true, true)]
public void Navigation_UsesDestinationPolicies(bool editor, bool administrator)
{
var auth = this.AddAuthorization();
auth.SetAuthorized("fixture");
var policies = new List<string> { AdminUiPolicies.AuthenticatedRead };
if (editor) policies.Add(AdminUiPolicies.ConfigEditor);
if (administrator) policies.AddRange([AdminUiPolicies.FleetAdmin, SecretsAuthorization.ManagePolicy]);
auth.SetPolicies(policies.ToArray());
var cut = Render<MainLayout>();
foreach (var path in new[] { "/fleet", "/hosts", "/clusters", "/certificates", "/script-log", "/alerts" })
cut.FindAll($"a[href='{path}']").Count.ShouldBe(1, path);
foreach (var path in new[] { "/raw", "/uns", "/scripts", "/deployments" })
cut.FindAll($"a[href='{path}']").Count.ShouldBe(editor ? 1 : 0, path);
foreach (var path in new[] { "/role-grants", "/apikeys", "/admin/secrets" })
cut.FindAll($"a[href='{path}']").Count.ShouldBe(administrator ? 1 : 0, path);
}
[Fact]
public void RetiredNamespaces_AreAbsentFromOrdinaryClusterNavigation()
{
var cut = Render<ClusterNav>(p => p.Add(x => x.ClusterId,"C1").Add(x => x.ActiveTab,"overview"));
cut.FindAll("a[href$='/namespaces']").ShouldBeEmpty();
cut.Find("a[href='/clusters/C1']").GetAttribute("aria-current").ShouldBe("page");
}
[Theory]
[InlineData(false)]
[InlineData(true)]
public void CompatibilityAndHome_OfferOnlyAuthorizedAuthoring(bool editor)
{
var auth = this.AddAuthorization();
auth.SetAuthorized("fixture");
auth.SetPolicies(editor
? [AdminUiPolicies.AuthenticatedRead, AdminUiPolicies.ConfigEditor]
: [AdminUiPolicies.AuthenticatedRead]);
var home = Render<ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Home>();
home.FindAll("h1").Count.ShouldBe(1);
home.Find("a[href='/fleet']").ShouldNotBeNull();
home.FindAll("a[href='/raw']").Count.ShouldBe(editor ? 1 : 0);
home.FindAll("a[href='/uns']").Count.ShouldBe(editor ? 1 : 0);
var compatibility = Render<ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters.ClusterNamespaces>(p => p.Add(x => x.ClusterId, "C1"));
compatibility.FindAll("h1").Count.ShouldBe(1);
compatibility.FindAll("a[href='/raw']").Count.ShouldBe(editor ? 1 : 0);
compatibility.FindAll("a[href='/uns']").Count.ShouldBe(editor ? 1 : 0);
}
[Fact]
public void SecretsPolicy_IsIndependentOfFleetAdministration()
{
var auth = this.AddAuthorization();
auth.SetAuthorized("fixture");
auth.SetPolicies(AdminUiPolicies.AuthenticatedRead, SecretsAuthorization.ManagePolicy);
var cut = Render<MainLayout>();
cut.Find("a[href='/admin/secrets']").ShouldNotBeNull();
cut.FindAll("a[href='/role-grants'],a[href='/apikeys']").ShouldBeEmpty();
}
}
@@ -15,24 +15,20 @@ namespace ZB.MOM.WW.OtOpcUa.AdminUI.Tests;
/// user can get there.
/// </summary>
/// <remarks>
/// Source scan rather than a rendering assertion: the repo has no bUnit (the central
/// <c>PackageVersion</c> for it is referenced by no project), and razor layout markup — unlike
/// a page's <c>@page</c>/<c>@attribute</c> directives — does not survive into class metadata
/// any form a reflection guard can read. So this proves the link and its gate are *written*;
/// that the gate actually admits an Administrator and hides the item from a Designer is
/// live-verified on the docker-dev rig, per this repo's Razor convention.
/// This source guard preserves the exact package policy expression. Rendered policy visibility
/// is independently covered by NavigationPolicyRenderTests; neither replaces route authorization.
/// </remarks>
[Trait("Category", "Unit")]
public sealed class SecretsNavLinkTests
{
private const string NavItem = "<NavRailItem Href=\"/admin/secrets\"";
private static string ReadMainLayoutSource()
private static string ReadMainLayoutSource([System.Runtime.CompilerServices.CallerFilePath] string sourceFile = "")
{
var dir = new DirectoryInfo(AppContext.BaseDirectory);
var dir = new DirectoryInfo(Path.GetDirectoryName(sourceFile)!);
while (dir is not null && !File.Exists(Path.Combine(dir.FullName, "ZB.MOM.WW.OtOpcUa.slnx")))
dir = dir.Parent;
dir.ShouldNotBeNull("could not locate the repository root (no ZB.MOM.WW.OtOpcUa.slnx above the test binary)");
dir.ShouldNotBeNull("could not locate the repository root (no ZB.MOM.WW.OtOpcUa.slnx above the test source)");
return File.ReadAllText(Path.Combine(
dir!.FullName, "src", "Server", "ZB.MOM.WW.OtOpcUa.AdminUI",
@@ -62,7 +58,7 @@ public sealed class SecretsNavLinkTests
{
var source = ReadMainLayoutSource();
var open = source.IndexOf("<AuthorizeView Policy=", StringComparison.Ordinal);
var open = source.IndexOf("<AuthorizeView Policy=\"@ZB.MOM.WW.Secrets.Ui.SecretsAuthorization.ManagePolicy\"", StringComparison.Ordinal);
var item = source.IndexOf(NavItem, StringComparison.Ordinal);
var close = source.IndexOf("</AuthorizeView>", open + 1, StringComparison.Ordinal);