using System.Security.Claims; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection.Extensions; using ZB.MOM.WW.OtOpcUa.AdminUI.Components.Pages.Clusters; using ZB.MOM.WW.OtOpcUa.AdminUI.Tests.Uns; using ZB.MOM.WW.OtOpcUa.AdminUI.Acls; using ZB.MOM.WW.OtOpcUa.AdminUI.Uns; using ZB.MOM.WW.OtOpcUa.Configuration.Entities; using ZB.MOM.WW.OtOpcUa.Configuration.Enums; 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; /// Real rail destinations retain their route-policy visibility. [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 { AdminUiPolicies.AuthenticatedRead }; if (editor) policies.Add(AdminUiPolicies.ConfigEditor); if (administrator) policies.AddRange([AdminUiPolicies.FleetAdmin, SecretsAuthorization.ManagePolicy]); auth.SetPolicies(policies.ToArray()); var cut = Render(); 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(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(); 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(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(); cut.Find("a[href='/admin/secrets']").ShouldNotBeNull(); cut.FindAll("a[href='/role-grants'],a[href='/apikeys']").ShouldBeEmpty(); } [Theory] [InlineData("Viewer", false)] [InlineData("Operator", false)] [InlineData("Designer", true)] [InlineData("Administrator", true)] public void ClusterListAndAclActions_FollowDestinationPolicy(string role, bool allowed) { Services.RemoveAll(); Services.RemoveAll(); Services.AddAuthorizationCore( options => options.AddPolicy(AdminUiPolicies.ConfigEditor, policy => policy.RequireRole("Administrator", "Designer"))); var principal = new ClaimsPrincipal(new ClaimsIdentity([ new(ClaimTypes.Name, "fixture"), new(ClaimTypes.Role, role)], "fixture")); var state = Task.FromResult(new AuthenticationState(principal)); var name = Guid.NewGuid().ToString("N"); using (var db = RawTreeTestDb.CreateNamed(name)) { db.ServerClusters.Add(new ServerCluster { ClusterId="C1", Name="Readable cluster", Enterprise="zb", Site="local", RedundancyMode=RedundancyMode.None, CreatedBy="fixture" }); db.NodeAcls.Add(new NodeAcl { NodeAclId="A1", ClusterId="C1", Principal="fixture-reader", ScopeKind=NodeAclScopeKind.Cluster, PermissionFlags=NodePermissions.Browse }); db.SaveChanges(); } var factory = RawTreeTestDb.Factory(name); Services.AddSingleton(factory); Services.AddSingleton( new AclScopeService(factory, new RawTreeService(factory), new UnsTreeService(factory))); var list = Render>>(p => p.Add(x => x.Value, state) .AddChildContent()); list.WaitForAssertion(() => list.Markup.ShouldContain("Readable cluster")); list.FindAll("a[href='/clusters/new']").Count.ShouldBe(allowed ? 1 : 0); var acls = Render>>(p => p.Add(x => x.Value, state) .AddChildContent(child => child.Add(x => x.ClusterId, "C1"))); acls.WaitForAssertion(() => acls.Markup.ShouldContain("fixture-reader")); acls.Markup.Contains("re-pick the scope", StringComparison.Ordinal).ShouldBe(allowed); acls.FindAll("thead th").Count.ShouldBe(allowed ? 7 : 6); acls.FindAll("tbody tr").ShouldAllBe(row => row.QuerySelectorAll("td").Length == (allowed ? 7 : 6)); foreach (var path in new[] { "/clusters/C1/acls/new", "/clusters/C1/acls/A1" }) acls.FindAll($"a[href='{path}']").Count.ShouldBe(allowed ? 1 : 0, path); foreach (var type in new[] { typeof(NewCluster), typeof(AclEdit) }) type.GetCustomAttributes(typeof(AuthorizeAttribute), true) .Cast().ShouldContain(x => x.Policy == AdminUiPolicies.ConfigEditor); } }